$result = $app->get_animation( $data, $context, $request_ids ); $this->throw_on_error( $result ); return [ 'text' => $result['text'], 'response_id' => $result['responseId'], 'usage' => $result['usage'], ]; } /** * @param mixed $result */ private function throw_on_error( $result ): void { if ( is_wp_error( $result ) ) { wp_send_json_error( [ 'message' => esc_html( $result->get_error_message() ), 'extra_data' => $result->get_error_data(), ] ); } } /** * @return void */ public function add_wc_scripts(): void { wp_enqueue_script( 'elementor-ai-unify-product-images', $this->get_js_assets_url( 'ai-unify-product-images' ), [ 'jquery', 'elementor-v2-ui', 'elementor-v2-icons', 'wp-components', 'elementor-common', ], ELEMENTOR_VERSION, true ); wp_localize_script( 'elementor-ai-unify-product-images', 'UnifyProductImagesConfig', [ 'get_product_images_url' => admin_url( 'admin-ajax.php' ), 'set_product_images_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'elementor-ai-unify-product-images_nonce' ), 'placeholder' => ELEMENTOR_ASSETS_URL . 'images/app/ai/product-image-unification-example.gif?' . ELEMENTOR_VERSION, 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ), 'connect_url' => $this->get_ai_connect_url(), ] ); add_filter( 'bulk_actions-edit-product', function ( $data ) { return $this->add_products_bulk_action( $data ); }); wp_set_script_translations( 'elementor-ai-unify-product-images', 'elementor' ); } /** * @param $product * @param int|null $image_to_remove * @param int|null $image_to_add * @return void */ private function update_product_gallery( $product, ?int $image_to_remove, ?int $image_to_add ): void { $gallery_image_ids = $product->get_gallery_image_ids(); $index = array_search( $image_to_remove, $gallery_image_ids, true ); if ( false !== $index ) { unset( $gallery_image_ids[ $index ] ); } if ( ! in_array( $image_to_add, $gallery_image_ids, true ) ) { $gallery_image_ids[] = $image_to_add; } $product->set_gallery_image_ids( $gallery_image_ids ); $product->save(); } private function should_display_create_with_ai_banner() { $elementor_pages = new \WP_Query( [ 'post_type' => 'page', 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => self::MIN_PAGES_FOR_CREATE_WITH_AI_BANNER + 1, ] ); if ( $elementor_pages->post_count > self::MIN_PAGES_FOR_CREATE_WITH_AI_BANNER ) { return false; } if ( Utils::is_custom_kit_applied() ) { return false; } return true; } private function get_create_with_ai_banner_data() { return [ 'title' => 'Create and launch your site faster with AI', 'description' => 'Share your vision with our AI Chat and watch as it becomes a brief, sitemap, and wireframes in minutes:', 'input_placeholder' => 'Start describing the site you want to create...', 'button_title' => 'Create with AI', 'button_cta_url' => 'http://planner.elementor.com/chat.html', 'background_image' => ELEMENTOR_ASSETS_URL . 'images/app/ai/ai-site-creator-homepage-bg.svg', 'utm_source' => 'editor-home', 'utm_medium' => 'wp-dash', 'utm_campaign' => 'generate-with-ai', ]; } public function add_create_with_ai_banner_to_homescreen( $home_screen_data ) { if ( $this->should_display_create_with_ai_banner() ) { $home_screen_data['create_with_ai'] = $this->get_create_with_ai_banner_data(); } else { $home_screen_data['create_with_ai'] = null; } return $home_screen_data; } }