PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.5
Elementor Website Builder – more than just a page builder v3.27.5
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / ai / module.php

module.php in Elementor Website Builder – more than just a page builder 3.27.5, at modules/ai/module.php

1,540 lines 41.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\Ai;
3
4 use Elementor\Controls_Manager;
5 use Elementor\Core\Base\Module as BaseModule;
6 use Elementor\Core\Common\Modules\Connect\Module as ConnectModule;
7 use Elementor\Element_Base;
8 use Elementor\Modules\Ai\Feature_Intro\Product_Image_Unification_Intro;
9 use Elementor\Plugin;
10 use Elementor\Core\Utils\Collection;
11 use Elementor\Modules\Ai\Connect\Ai;
12 use Elementor\User;
13 use Elementor\Utils;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 class Module extends BaseModule {
20 const HISTORY_TYPE_ALL = 'all';
21 const HISTORY_TYPE_TEXT = 'text';
22 const HISTORY_TYPE_CODE = 'code';
23 const HISTORY_TYPE_IMAGE = 'images';
24 const HISTORY_TYPE_BLOCK = 'blocks';
25 const VALID_HISTORY_TYPES = [
26 self::HISTORY_TYPE_ALL,
27 self::HISTORY_TYPE_TEXT,
28 self::HISTORY_TYPE_CODE,
29 self::HISTORY_TYPE_IMAGE,
30 self::HISTORY_TYPE_BLOCK,
31 ];
32
33 public function get_name() {
34 return 'ai';
35 }
36
37 public function __construct() {
38 parent::__construct();
39
40 ( new SitePlannerConnect\Module() );
41
42 if ( is_admin() ) {
43 ( new Preferences() )->register();
44 add_action( 'elementor/import-export/import-kit/runner/after-run', [ $this, 'handle_kit_install' ] );
45 }
46
47 if ( ! $this->is_ai_enabled() ) {
48 return;
49 }
50
51 add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) {
52 $connect_module->register_app( 'ai', Ai::get_class_name() );
53 } );
54
55 add_action( 'elementor/ajax/register_actions', function( $ajax ) {
56 $handlers = [
57 'ai_get_user_information' => [ $this, 'ajax_ai_get_user_information' ],
58 'ai_get_remote_config' => [ $this, 'ajax_ai_get_remote_config' ],
59 'ai_get_remote_frontend_config' => [ $this, 'ajax_ai_get_remote_frontend_config' ],
60 'ai_get_completion_text' => [ $this, 'ajax_ai_get_completion_text' ],
61 'ai_get_excerpt' => [ $this, 'ajax_ai_get_excerpt' ],
62 'ai_get_featured_image' => [ $this, 'ajax_ai_get_featured_image' ],
63 'ai_get_edit_text' => [ $this, 'ajax_ai_get_edit_text' ],
64 'ai_get_custom_code' => [ $this, 'ajax_ai_get_custom_code' ],
65 'ai_get_custom_css' => [ $this, 'ajax_ai_get_custom_css' ],
66 'ai_set_get_started' => [ $this, 'ajax_ai_set_get_started' ],
67 'ai_set_status_feedback' => [ $this, 'ajax_ai_set_status_feedback' ],
68 'ai_get_image_prompt_enhancer' => [ $this, 'ajax_ai_get_image_prompt_enhancer' ],
69 'ai_get_text_to_image' => [ $this, 'ajax_ai_get_text_to_image' ],
70 'ai_get_image_to_image' => [ $this, 'ajax_ai_get_image_to_image' ],
71 'ai_get_image_to_image_mask' => [ $this, 'ajax_ai_get_image_to_image_mask' ],
72 'ai_get_image_to_image_mask_cleanup' => [ $this, 'ajax_ai_get_image_to_image_mask_cleanup' ],
73 'ai_get_image_to_image_outpainting' => [ $this, 'ajax_ai_get_image_to_image_outpainting' ],
74 'ai_get_image_to_image_upscale' => [ $this, 'ajax_ai_get_image_to_image_upscale' ],
75 'ai_get_image_to_image_remove_background' => [ $this, 'ajax_ai_get_image_to_image_remove_background' ],
76 'ai_get_image_to_image_replace_background' => [ $this, 'ajax_ai_get_image_to_image_replace_background' ],
77 'ai_upload_image' => [ $this, 'ajax_ai_upload_image' ],
78 'ai_generate_layout' => [ $this, 'ajax_ai_generate_layout' ],
79 'ai_get_layout_prompt_enhancer' => [ $this, 'ajax_ai_get_layout_prompt_enhancer' ],
80 'ai_get_history' => [ $this, 'ajax_ai_get_history' ],
81 'ai_delete_history_item' => [ $this, 'ajax_ai_delete_history_item' ],
82 'ai_toggle_favorite_history_item' => [ $this, 'ajax_ai_toggle_favorite_history_item' ],
83 'ai_get_product_image_unification' => [ $this, 'ajax_ai_get_product_image_unification' ],
84 'ai_get_animation' => [ $this, 'ajax_ai_get_animation' ],
85 ];
86
87 foreach ( $handlers as $tag => $callback ) {
88 $ajax->register_ajax_action( $tag, $callback );
89 }
90 } );
91
92 add_action( 'elementor/editor/before_enqueue_scripts', function() {
93 $this->enqueue_main_script();
94 $this->enqueue_layout_script();
95 } );
96
97 add_action( 'elementor/editor/after_enqueue_styles', function() {
98 wp_enqueue_style(
99 'elementor-ai-editor',
100 $this->get_css_assets_url( 'modules/ai/editor' ),
101 [],
102 ELEMENTOR_VERSION
103 );
104 } );
105
106 add_action( 'elementor/preview/enqueue_styles', function() {
107 wp_enqueue_style(
108 'elementor-ai-layout-preview',
109 $this->get_css_assets_url( 'modules/ai/layout-preview' ),
110 [],
111 ELEMENTOR_VERSION
112 );
113 } );
114
115 if ( is_admin() ) {
116 add_action( 'wp_enqueue_media', [ $this, 'enqueue_ai_media_library' ] );
117 add_action( 'admin_head', [ $this, 'enqueue_ai_media_library_upload_screen' ] );
118
119 if ( current_user_can( 'edit_products' ) || current_user_can( 'publish_products' ) ) {
120 add_action( 'admin_init', [ $this, 'enqueue_ai_products_page_scripts' ] );
121 add_action( 'current_screen', [ $this, 'enqueue_ai_single_product_page_scripts' ] );
122 add_action( 'wp_ajax_elementor-ai-get-product-images', [ $this, 'get_product_images_ajax' ] );
123 add_action( 'wp_ajax_elementor-ai-set-product-images', [ $this, 'set_product_images_ajax' ] );
124 Product_Image_Unification_Intro::add_hooks();
125 }
126 }
127
128 add_action( 'enqueue_block_editor_assets', function() {
129 wp_enqueue_script( 'elementor-ai-gutenberg',
130 $this->get_js_assets_url( 'ai-gutenberg' ),
131 [
132 'jquery',
133 'elementor-v2-ui',
134 'elementor-v2-icons',
135 'wp-blocks',
136 'wp-element',
137 'wp-editor',
138 'wp-data',
139 'wp-components',
140 'wp-compose',
141 'wp-i18n',
142 'wp-hooks',
143 'elementor-ai-media-library',
144 ],
145 ELEMENTOR_VERSION, true );
146
147 wp_localize_script(
148 'elementor-ai-gutenberg',
149 'ElementorAiConfig',
150 [
151 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
152 'connect_url' => $this->get_ai_connect_url(),
153 ]
154 );
155
156 wp_set_script_translations( 'elementor-ai-gutenberg', 'elementor' );
157 });
158
159 add_filter( 'elementor/document/save/data', function ( $data ) {
160 return $this->remove_temporary_containers( $data );
161 } );
162
163 add_action( 'elementor/element/common/section_effects/after_section_start', [ $this, 'register_ai_motion_effect_control' ], 10, 1 );
164 add_action( 'elementor/element/container/section_effects/after_section_start', [ $this, 'register_ai_motion_effect_control' ], 10, 1 );
165 add_action( 'elementor/element/common/_section_transform/after_section_end', [ $this, 'register_ai_hover_effect_control' ], 10, 1 );
166 add_action( 'elementor/element/container/_section_transform/after_section_end', [ $this, 'register_ai_hover_effect_control' ], 10, 1 );
167 }
168
169 public function is_ai_enabled() {
170 if ( ! Plugin::$instance->experiments->is_feature_active( 'container' ) ) {
171 return false;
172 }
173
174 return Preferences::is_ai_enabled( get_current_user_id() );
175 }
176
177 public function handle_kit_install( $imported_data ) {
178 if ( ! $this->is_ai_enabled() ) {
179 return;
180 }
181
182 if ( ! isset( $imported_data['status'] ) || 'success' !== $imported_data['status'] ) {
183 return;
184 }
185
186 if ( ! isset( $imported_data['runner'] ) || 'site-settings' !== $imported_data['runner'] ) {
187 return;
188 }
189
190 if ( ! isset( $imported_data['configData']['lastImportedSession']['instance_data']['site_settings']['settings']['ai'] ) ) {
191 return;
192 }
193
194 $is_connected = $this->get_ai_app()->is_connected() && User::get_introduction_meta( 'ai_get_started' );
195
196 if ( ! $is_connected ) {
197 return;
198 }
199
200 $last_imported_session = $imported_data['configData']['lastImportedSession'];
201 $imported_ai_data = $last_imported_session['instance_data']['site_settings']['settings']['ai'];
202
203 $this->get_ai_app()->send_event( [
204 'name' => 'kit_installed',
205 'data' => $imported_ai_data,
206 'client' => [
207 'name' => 'elementor',
208 'version' => ELEMENTOR_VERSION,
209 'session_id' => $last_imported_session['session_id'],
210 ],
211 ] );
212 }
213
214 public function register_ai_hover_effect_control( Element_Base $element ) {
215 if ( ! $element->get_controls( 'ai_hover_animation' ) ) {
216 $element->add_control(
217 'ai_hover_animation',
218 [
219 'tabs_wrapper' => '_tabs_positioning',
220 'inner_tab' => '_tab_positioning_hover',
221 'label' => esc_html__( 'Animate With AI', 'elementor' ),
222 'type' => Controls_Manager::RAW_HTML,
223 'raw' => '
224 <style>
225 .elementor-control-ai_hover_animation .elementor-control-content {
226 display: flex;
227 flex-direction: row;
228 justify-content: space-between;
229 align-items: center;
230 }
231 .elementor-control-ai_hover_animation .elementor-control-raw-html {
232 display: none;
233 }
234 </style>',
235 'render_type' => 'none',
236 'ai' => [
237 'active' => true,
238 'type' => 'hover_animation',
239 ],
240 ],
241 [
242 'position' => [
243 'of' => '_transform_rotate_popover_hover',
244 'type' => 'control',
245 'at' => 'before',
246 ],
247 ]
248 );
249 }
250 }
251 public function register_ai_motion_effect_control( $element ) {
252 if ( Utils::has_pro() && ! $element->get_controls( 'ai_animation' ) ) {
253 $element->add_control(
254 'ai_animation',
255 [
256 'label' => esc_html__( 'Animate With AI', 'elementor' ),
257 'type' => Controls_Manager::RAW_HTML,
258 'raw' => '
259 <style>
260 .elementor-control-ai_animation .elementor-control-content {
261 display: flex;
262 flex-direction: row;
263 justify-content: space-between;
264 align-items: center;
265 }
266 .elementor-control-ai_animation .elementor-control-raw-html {
267 display: none;
268 }
269 </style>',
270 'render_type' => 'none',
271 'ai' => [
272 'active' => true,
273 'type' => 'animation',
274 ],
275 ]
276 );
277 }
278 }
279
280 private function get_current_screen() {
281 $is_wc = class_exists( 'WooCommerce' ) && post_type_exists( 'product' );
282 if ( ! $is_wc ) {
283 return 'other';
284 }
285
286 $is_products_page = isset( $_GET['post_type'] ) && 'product' === $_GET['post_type'];
287 if ( $is_products_page ) {
288 return 'wc-products';
289 }
290
291 $screen = get_current_screen();
292 $is_single_product_page = isset( $screen->post_type ) && ( 'product' === $screen->post_type && 'post' === $screen->base );
293 if ( $is_single_product_page ) {
294 return 'wc-single-product';
295 }
296
297 return 'other';
298 }
299
300 public function enqueue_ai_products_page_scripts() {
301 if ( 'wc-products' !== $this->get_current_screen() ) {
302 return;
303 }
304
305 $this->add_wc_scripts();
306
307 }
308
309 public function enqueue_ai_single_product_page_scripts() {
310 if ( 'wc-single-product' !== $this->get_current_screen() ) {
311 return;
312 }
313
314 $this->add_wc_scripts();
315
316 }
317
318 private function add_products_bulk_action( $bulk_actions ) {
319 $bulk_actions['elementor-ai-unify-product-images'] = __( 'Unify with Elementor AI', 'elementor' );
320 return $bulk_actions;
321 }
322
323 public function get_product_images_ajax() {
324 check_ajax_referer( 'elementor-ai-unify-product-images_nonce', 'nonce' );
325
326 $post_ids = isset( $_POST['post_ids'] ) ? array_map( 'intval', $_POST['post_ids'] ) : [];
327 $is_galley_only = isset( $_POST['is_galley_only'] ) && sanitize_text_field( wp_unslash( $_POST['is_galley_only'] ) );
328
329 $image_ids = [];
330
331 foreach ( $post_ids as $post_id ) {
332 if ( $is_galley_only ) {
333 $product = wc_get_product( $post_id );
334 $gallery_image_ids = $product->get_gallery_image_ids();
335 foreach ( $gallery_image_ids as $image_id ) {
336 $image_ids[] = [
337 'productId' => $post_id,
338 'id' => $image_id,
339 'image_url' => wp_get_attachment_url( $image_id ),
340 ];
341 }
342 continue;
343 }
344
345 $image_id = get_post_thumbnail_id( $post_id );
346
347 if ( ! $image_id ) {
348 $product = wc_get_product( $post_id );
349 $gallery_image_ids = $product->get_gallery_image_ids();
350 if ( ! empty( $gallery_image_ids ) ) {
351 $image_id = $gallery_image_ids[0];
352 }
353 }
354
355 $image_ids[] = [
356 'productId' => $post_id,
357 'id' => $image_id ? $image_id : 'No Image',
358 'image_url' => $image_id ? wp_get_attachment_url( $image_id ) : 'No Image',
359 ];
360 }
361
362 wp_send_json_success( [ 'product_images' => array_slice( $image_ids, 0, 10 ) ] );
363
364 wp_die();
365 }
366
367 private function get_attachment_id_by_url( $url ) {
368 $attachments = get_posts( [
369 'post_type' => 'attachment',
370 'meta_query' => [
371 [
372 'key' => '_wp_attached_file',
373 'value' => basename( $url ),
374 'compare' => 'LIKE',
375 ],
376 ],
377 'fields' => 'ids',
378 'numberposts' => 1,
379 ] );
380
381 return ! empty( $attachments ) ? $attachments[0] : null;
382 }
383
384 public function set_product_images_ajax() {
385 check_ajax_referer( 'elementor-ai-unify-product-images_nonce', 'nonce' );
386
387 $product_id = isset( $_POST['productId'] ) ? sanitize_text_field( wp_unslash( $_POST['productId'] ) ) : '';
388 $image_url = isset( $_POST['image_url'] ) ? sanitize_text_field( wp_unslash( $_POST['image_url'] ) ) : '';
389 $image_to_add = isset( $_POST['image_to_add'] ) ? intval( wp_unslash( $_POST['image_to_add'] ) ) : null;
390 $image_to_remove = isset( $_POST['image_to_remove'] ) ? intval( wp_unslash( $_POST['image_to_remove'] ) ) : null;
391 $is_product_gallery = isset( $_POST['is_product_gallery'] ) && sanitize_text_field( wp_unslash( $_POST['is_product_gallery'] ) ) === 'true';
392
393 if ( ! $product_id || ! $image_url ) {
394 throw new \Exception( 'Product ID and Image URL are required' );
395 }
396
397 $product = wc_get_product( $product_id );
398 if ( ! $product ) {
399 throw new \Exception( 'Product not found' );
400 }
401
402 $attachment_id = $this->get_attachment_id_by_url( $image_url );
403 if ( is_wp_error( $attachment_id ) ) {
404 throw new \Exception( 'Image upload failed' );
405 }
406
407 if ( $is_product_gallery ) {
408 $this->update_product_gallery( $product, $image_to_remove, $image_to_add );
409 } else {
410 $product->set_image_id( $attachment_id );
411 $product->save();
412 }
413
414 wp_send_json_success( [
415 'message' => __( 'Image added successfully', 'elementor' ),
416 ] );
417 }
418
419 public function enqueue_ai_media_library_upload_screen() {
420 $screen = get_current_screen();
421 if ( ! $screen || 'upload' !== $screen->id ) {
422 return;
423 }
424
425 $this->enqueue_ai_media_library();
426 }
427
428 public function enqueue_ai_media_library() {
429 wp_enqueue_script( 'elementor-ai-media-library',
430 $this->get_js_assets_url( 'ai-media-library' ),
431 [
432 'jquery',
433 'elementor-v2-ui',
434 'elementor-v2-icons',
435 'media-grid',
436 ],
437 ELEMENTOR_VERSION,
438 true
439 );
440
441 wp_localize_script(
442 'elementor-ai-media-library',
443 'ElementorAiConfig',
444 [
445 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
446 'connect_url' => $this->get_ai_connect_url(),
447 ]
448 );
449
450 wp_set_script_translations( 'elementor-ai-media-library', 'elementor' );
451 }
452
453 private function enqueue_main_script() {
454 wp_enqueue_script(
455 'elementor-ai',
456 $this->get_js_assets_url( 'ai' ),
457 [
458 'react',
459 'react-dom',
460 'backbone-marionette',
461 'elementor-web-cli',
462 'wp-date',
463 'elementor-common',
464 'elementor-editor-modules',
465 'elementor-editor-document',
466 'elementor-v2-ui',
467 'elementor-v2-icons',
468 ],
469 ELEMENTOR_VERSION,
470 true
471 );
472
473 $config = [
474 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
475 'connect_url' => $this->get_ai_connect_url(),
476 ];
477
478 wp_localize_script(
479 'elementor-ai',
480 'ElementorAiConfig',
481 $config
482 );
483
484 wp_set_script_translations( 'elementor-ai', 'elementor' );
485 }
486
487 private function enqueue_layout_script() {
488 wp_enqueue_script(
489 'elementor-ai-layout',
490 $this->get_js_assets_url( 'ai-layout' ),
491 [
492 'react',
493 'react-dom',
494 'backbone-marionette',
495 'elementor-common',
496 'elementor-web-cli',
497 'elementor-editor-modules',
498 'elementor-ai',
499 'elementor-v2-ui',
500 'elementor-v2-icons',
501 ],
502 ELEMENTOR_VERSION,
503 true
504 );
505
506 wp_set_script_translations( 'elementor-ai-layout', 'elementor' );
507 }
508
509 private function remove_temporary_containers( $data ) {
510 if ( empty( $data['elements'] ) || ! is_array( $data['elements'] ) ) {
511 return $data;
512 }
513
514 // If for some reason the document has been saved during an AI Layout session,
515 // ensure that the temporary containers are removed from the data.
516 $data['elements'] = array_filter( $data['elements'], function( $element ) {
517 $is_preview_container = strpos( $element['id'], 'e-ai-preview-container' ) === 0;
518 $is_screenshot_container = strpos( $element['id'], 'e-ai-screenshot-container' ) === 0;
519
520 return ! $is_preview_container && ! $is_screenshot_container;
521 } );
522
523 return $data;
524 }
525
526 private function get_ai_connect_url() {
527 $app = $this->get_ai_app();
528
529 return $app->get_admin_url( 'authorize', [
530 'utm_source' => 'ai-popup',
531 'utm_campaign' => 'connect-account',
532 'utm_medium' => 'wp-dash',
533 'source' => 'generic',
534 ] );
535 }
536
537 public function ajax_ai_get_user_information( $data ) {
538 $app = $this->get_ai_app();
539
540 if ( ! $app->is_connected() ) {
541 return [
542 'is_connected' => false,
543 'connect_url' => $this->get_ai_connect_url(),
544 ];
545 }
546
547 $user_usage = wp_parse_args( $app->get_usage(), [
548 'hasAiSubscription' => false,
549 'usedQuota' => 0,
550 'quota' => 100,
551 ] );
552
553 return [
554 'is_connected' => true,
555 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
556 'usage' => $user_usage,
557 ];
558 }
559
560 public function ajax_ai_get_remote_config() {
561 $app = $this->get_ai_app();
562
563 if ( ! $app->is_connected() ) {
564 return [];
565 }
566
567 return $app->get_remote_config();
568 }
569
570 public function ajax_ai_get_remote_frontend_config( $data ) {
571 $callback = function () use ( $data ) {
572 return $this->get_ai_app()->get_remote_frontend_config( $data );
573 };
574
575 return Utils::get_cached_callback( $callback, 'ai_remote_frontend_config-' . get_current_user_id(), HOUR_IN_SECONDS );
576 }
577
578 public function verify_upload_permissions( $data ) {
579 $referer = wp_get_referer();
580
581 if ( str_contains( $referer, 'wp-admin/upload.php' ) && current_user_can( 'upload_files' ) ) {
582 return;
583 }
584 $this->verify_permissions( $data['editor_post_id'] );
585 }
586
587 private function verify_permissions( $editor_post_id ) {
588 $document = Plugin::$instance->documents->get( $editor_post_id );
589
590 if ( ! $document ) {
591 throw new \Exception( 'Document not found' );
592 }
593
594 if ( $document->is_built_with_elementor() ) {
595 if ( ! $document->is_editable_by_current_user() ) {
596 throw new \Exception( 'Access denied' );
597 }
598 } else {
599 if ( ! current_user_can( 'edit_post', $editor_post_id ) ) {
600 throw new \Exception( 'Access denied' );
601 }
602 }
603 }
604
605 public function ajax_ai_get_image_prompt_enhancer( $data ) {
606 $this->verify_upload_permissions( $data );
607
608 $app = $this->get_ai_app();
609
610 if ( empty( $data['prompt'] ) ) {
611 throw new \Exception( 'Missing prompt' );
612 }
613
614 if ( ! $app->is_connected() ) {
615 throw new \Exception( 'not_connected' );
616 }
617 $request_ids = $this->get_request_ids( $data['payload'] );
618
619 $result = $app->get_image_prompt_enhanced( $data['prompt'], [], $request_ids );
620 $this->throw_on_error( $result );
621
622 return [
623 'text' => $result['text'],
624 'response_id' => $result['responseId'],
625 'usage' => $result['usage'],
626 ];
627 }
628
629 public function ajax_ai_get_completion_text( $data ) {
630 $this->verify_permissions( $data['editor_post_id'] );
631
632 $app = $this->get_ai_app();
633
634 if ( empty( $data['payload']['prompt'] ) ) {
635 throw new \Exception( 'Missing prompt' );
636 }
637
638 if ( ! $app->is_connected() ) {
639 throw new \Exception( 'not_connected' );
640 }
641
642 $context = $this->get_request_context( $data );
643
644 $request_ids = $this->get_request_ids( $data['payload'] );
645
646 $result = $app->get_completion_text( $data['payload']['prompt'], $context, $request_ids );
647 $this->throw_on_error( $result );
648
649 return [
650 'text' => $result['text'],
651 'response_id' => $result['responseId'],
652 'usage' => $result['usage'],
653 ];
654 }
655
656
657 public function ajax_ai_get_excerpt( $data ): array {
658 $app = $this->get_ai_app();
659
660 if ( empty( $data['payload']['content'] ) ) {
661 throw new \Exception( 'Missing content' );
662 }
663
664 if ( ! $app->is_connected() ) {
665 throw new \Exception( 'Not connected' );
666 }
667
668 $context = $this->get_request_context( $data );
669
670 $request_ids = $this->get_request_ids( $data['payload'] );
671
672 $result = $app->get_excerpt( $data['payload']['content'], $context, $request_ids );
673 $this->throw_on_error( $result );
674
675 return [
676 'text' => $result['text'],
677 'response_id' => $result['responseId'],
678 'usage' => $result['usage'],
679 ];
680 }
681
682 public function ajax_ai_get_featured_image( $data ): array {
683 $this->verify_upload_permissions( $data );
684
685 if ( empty( $data['payload']['prompt'] ) ) {
686 throw new \Exception( 'Missing prompt' );
687 }
688
689 $app = $this->get_ai_app();
690
691 if ( ! $app->is_connected() ) {
692 throw new \Exception( 'not_connected' );
693 }
694
695 $context = $this->get_request_context( $data );
696 $request_ids = $this->get_request_ids( $data['payload'] );
697
698 $result = $app->get_featured_image( $data, $context, $request_ids );
699
700 $this->throw_on_error( $result );
701
702 return [
703 'images' => $result['images'],
704 'response_id' => $result['responseId'],
705 'usage' => $result['usage'],
706 ];
707 }
708
709 private function get_ai_app() : Ai {
710 return Plugin::$instance->common->get_component( 'connect' )->get_app( 'ai' );
711 }
712
713 private function get_request_context( $data ) {
714 if ( empty( $data['context'] ) ) {
715 return [];
716 }
717
718 return $data['context'];
719 }
720
721 private function get_request_ids( $data ) {
722 if ( empty( $data['requestIds'] ) ) {
723 return new \stdClass();
724 }
725
726 return $data['requestIds'];
727 }
728
729 public function ajax_ai_get_edit_text( $data ) {
730 $this->verify_permissions( $data['editor_post_id'] );
731
732 $app = $this->get_ai_app();
733
734 if ( empty( $data['payload']['input'] ) ) {
735 throw new \Exception( 'Missing input' );
736 }
737
738 if ( empty( $data['payload']['instruction'] ) ) {
739 throw new \Exception( 'Missing instruction' );
740 }
741
742 if ( ! $app->is_connected() ) {
743 throw new \Exception( 'not_connected' );
744 }
745
746 $context = $this->get_request_context( $data );
747
748 $request_ids = $this->get_request_ids( $data['payload'] );
749
750 $result = $app->get_edit_text( $data, $context, $request_ids );
751 $this->throw_on_error( $result );
752
753 return [
754 'text' => $result['text'],
755 'response_id' => $result['responseId'],
756 'usage' => $result['usage'],
757 ];
758 }
759
760 public function ajax_ai_get_custom_code( $data ) {
761 $app = $this->get_ai_app();
762
763 if ( empty( $data['payload']['prompt'] ) ) {
764 throw new \Exception( 'Missing prompt' );
765 }
766
767 if ( empty( $data['payload']['language'] ) ) {
768 throw new \Exception( 'Missing language' );
769 }
770
771 if ( ! $app->is_connected() ) {
772 throw new \Exception( 'not_connected' );
773 }
774
775 $context = $this->get_request_context( $data );
776
777 $request_ids = $this->get_request_ids( $data['payload'] );
778
779 $result = $app->get_custom_code( $data, $context, $request_ids );
780 $this->throw_on_error( $result );
781
782 return [
783 'text' => $result['text'],
784 'response_id' => $result['responseId'],
785 'usage' => $result['usage'],
786 ];
787 }
788
789 public function ajax_ai_get_custom_css( $data ) {
790 $this->verify_permissions( $data['editor_post_id'] );
791
792 $app = $this->get_ai_app();
793
794 if ( empty( $data['payload']['prompt'] ) ) {
795 throw new \Exception( 'Missing prompt' );
796 }
797
798 if ( empty( $data['payload']['html_markup'] ) ) {
799 $data['html_markup'] = '';
800 }
801
802 if ( empty( $data['payload']['element_id'] ) ) {
803 throw new \Exception( 'Missing element_id' );
804 }
805
806 if ( ! $app->is_connected() ) {
807 throw new \Exception( 'not_connected' );
808 }
809
810 $context = $this->get_request_context( $data );
811 $request_ids = $this->get_request_ids( $data['payload'] );
812
813 $result = $app->get_custom_css( $data, $context, $request_ids );
814 $this->throw_on_error( $result );
815
816 return [
817 'text' => $result['text'],
818 'response_id' => $result['responseId'],
819 'usage' => $result['usage'],
820 ];
821 }
822
823 public function ajax_ai_set_get_started( $data ) {
824 $app = $this->get_ai_app();
825
826 User::set_introduction_viewed( [
827 'introductionKey' => 'ai_get_started',
828 ] );
829
830 return $app->set_get_started();
831 }
832
833 public function ajax_ai_set_status_feedback( $data ) {
834 if ( empty( $data['response_id'] ) ) {
835 throw new \Exception( 'Missing response_id' );
836 }
837
838 $app = $this->get_ai_app();
839
840 if ( ! $app->is_connected() ) {
841 throw new \Exception( 'not_connected' );
842 }
843
844 $app->set_status_feedback( $data['response_id'] );
845
846 return [];
847 }
848
849 public function ajax_ai_get_text_to_image( $data ) {
850 $this->verify_upload_permissions( $data );
851
852 if ( empty( $data['payload']['prompt'] ) ) {
853 throw new \Exception( 'Missing prompt' );
854 }
855
856 $app = $this->get_ai_app();
857
858 if ( ! $app->is_connected() ) {
859 throw new \Exception( 'not_connected' );
860 }
861
862 $context = $this->get_request_context( $data );
863 $request_ids = $this->get_request_ids( $data['payload'] );
864
865 $result = $app->get_text_to_image( $data, $context, $request_ids );
866
867 $this->throw_on_error( $result );
868
869 return [
870 'images' => $result['images'],
871 'response_id' => $result['responseId'],
872 'usage' => $result['usage'],
873 ];
874 }
875
876 public function ajax_ai_get_image_to_image( $data ) {
877 $this->verify_upload_permissions( $data );
878
879 $app = $this->get_ai_app();
880
881 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
882 throw new \Exception( 'Missing Image' );
883 }
884
885 if ( empty( $data['payload']['settings'] ) ) {
886 throw new \Exception( 'Missing prompt settings' );
887 }
888
889 if ( ! $app->is_connected() ) {
890 throw new \Exception( 'not_connected' );
891 }
892
893 $context = $this->get_request_context( $data );
894 $request_ids = $this->get_request_ids( $data['payload'] );
895
896 $result = $app->get_image_to_image( [
897 'prompt' => $data['payload']['prompt'],
898 'promptSettings' => $data['payload']['settings'],
899 'attachment_id' => $data['payload']['image']['id'],
900 ], $context, $request_ids );
901
902 $this->throw_on_error( $result );
903
904 return [
905 'images' => $result['images'],
906 'response_id' => $result['responseId'],
907 'usage' => $result['usage'],
908 ];
909 }
910
911 public function ajax_ai_get_image_to_image_upscale( $data ) {
912 $this->verify_upload_permissions( $data );
913
914 $app = $this->get_ai_app();
915
916 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
917 throw new \Exception( 'Missing Image' );
918 }
919
920 if ( empty( $data['payload']['promptSettings'] ) ) {
921 throw new \Exception( 'Missing prompt settings' );
922 }
923
924 if ( ! $app->is_connected() ) {
925 throw new \Exception( 'not_connected' );
926 }
927
928 $context = $this->get_request_context( $data );
929 $request_ids = $this->get_request_ids( $data['payload'] );
930
931 $result = $app->get_image_to_image_upscale( [
932 'promptSettings' => $data['payload']['promptSettings'],
933 'attachment_id' => $data['payload']['image']['id'],
934 ], $context, $request_ids );
935
936 $this->throw_on_error( $result );
937
938 return [
939 'images' => $result['images'],
940 'response_id' => $result['responseId'],
941 'usage' => $result['usage'],
942 ];
943 }
944
945 public function ajax_ai_get_image_to_image_replace_background( $data ) {
946 $this->verify_upload_permissions( $data );
947
948 $app = $this->get_ai_app();
949
950 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
951 throw new \Exception( 'Missing Image' );
952 }
953
954 if ( empty( $data['payload']['prompt'] ) ) {
955 throw new \Exception( 'Prompt Missing' );
956 }
957
958 if ( ! $app->is_connected() ) {
959 throw new \Exception( 'not_connected' );
960 }
961
962 $context = $this->get_request_context( $data );
963 $request_ids = $this->get_request_ids( $data['payload'] );
964
965 $result = $app->get_image_to_image_replace_background( [
966 'attachment_id' => $data['payload']['image']['id'],
967 'prompt' => $data['payload']['prompt'],
968 ], $context, $request_ids );
969
970 $this->throw_on_error( $result );
971
972 return [
973 'images' => $result['images'],
974 'response_id' => $result['responseId'],
975 'usage' => $result['usage'],
976 ];
977 }
978
979 public function ajax_ai_get_image_to_image_remove_background( $data ) {
980 $this->verify_upload_permissions( $data );
981
982 $app = $this->get_ai_app();
983
984 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
985 throw new \Exception( 'Missing Image' );
986 }
987
988 if ( ! $app->is_connected() ) {
989 throw new \Exception( 'not_connected' );
990 }
991
992 $context = $this->get_request_context( $data );
993 $request_ids = $this->get_request_ids( $data['payload'] );
994 $result = $app->get_image_to_image_remove_background( [
995 'attachment_id' => $data['payload']['image']['id'],
996 ], $context, $request_ids );
997
998 $this->throw_on_error( $result );
999
1000 return [
1001 'images' => $result['images'],
1002 'response_id' => $result['responseId'],
1003 'usage' => $result['usage'],
1004 ];
1005 }
1006
1007 public function ajax_ai_get_image_to_image_mask( $data ) {
1008 $this->verify_upload_permissions( $data );
1009
1010 $app = $this->get_ai_app();
1011
1012 if ( empty( $data['payload']['prompt'] ) ) {
1013 throw new \Exception( 'Missing prompt' );
1014 }
1015
1016 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
1017 throw new \Exception( 'Missing Image' );
1018 }
1019
1020 if ( empty( $data['payload']['settings'] ) ) {
1021 throw new \Exception( 'Missing prompt settings' );
1022 }
1023
1024 if ( ! $app->is_connected() ) {
1025 throw new \Exception( 'not_connected' );
1026 }
1027
1028 if ( empty( $data['payload']['mask'] ) ) {
1029 throw new \Exception( 'Missing Mask' );
1030 }
1031
1032 $context = $this->get_request_context( $data );
1033 $request_ids = $this->get_request_ids( $data['payload'] );
1034
1035 $result = $app->get_image_to_image_mask( [
1036 'prompt' => $data['payload']['prompt'],
1037 'attachment_id' => $data['payload']['image']['id'],
1038 'mask' => $data['payload']['mask'],
1039 ], $context, $request_ids );
1040
1041 $this->throw_on_error( $result );
1042
1043 return [
1044 'images' => $result['images'],
1045 'response_id' => $result['responseId'],
1046 'usage' => $result['usage'],
1047 ];
1048 }
1049 public function ajax_ai_get_image_to_image_mask_cleanup( $data ) {
1050 $this->verify_upload_permissions( $data );
1051
1052 $app = $this->get_ai_app();
1053
1054 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
1055 throw new \Exception( 'Missing Image' );
1056 }
1057
1058 if ( empty( $data['payload']['settings'] ) ) {
1059 throw new \Exception( 'Missing prompt settings' );
1060 }
1061
1062 if ( ! $app->is_connected() ) {
1063 throw new \Exception( 'not_connected' );
1064 }
1065
1066 if ( empty( $data['payload']['mask'] ) ) {
1067 throw new \Exception( 'Missing Mask' );
1068 }
1069
1070 $context = $this->get_request_context( $data );
1071 $request_ids = $this->get_request_ids( $data['payload'] );
1072
1073 $result = $app->get_image_to_image_mask_cleanup( [
1074 'attachment_id' => $data['payload']['image']['id'],
1075 'mask' => $data['payload']['mask'],
1076 ], $context, $request_ids );
1077
1078 $this->throw_on_error( $result );
1079
1080 return [
1081 'images' => $result['images'],
1082 'response_id' => $result['responseId'],
1083 'usage' => $result['usage'],
1084 ];
1085 }
1086
1087 public function ajax_ai_get_image_to_image_outpainting( $data ) {
1088 $this->verify_upload_permissions( $data );
1089
1090 $app = $this->get_ai_app();
1091
1092 if ( ! $app->is_connected() ) {
1093 throw new \Exception( 'not_connected' );
1094 }
1095
1096 if ( empty( $data['payload']['mask'] ) ) {
1097 throw new \Exception( 'Missing Expended Image' );
1098 }
1099
1100 $context = $this->get_request_context( $data );
1101 $request_ids = $this->get_request_ids( $data['payload'] );
1102 $result = $app->get_image_to_image_out_painting( [
1103 'size' => $data['payload']['size'],
1104 'position' => $data['payload']['position'],
1105 'mask' => $data['payload']['mask'],
1106 'image_base64' => $data['payload']['image_base64'],
1107 ], $context, $request_ids );
1108
1109 $this->throw_on_error( $result );
1110
1111 return [
1112 'images' => $result['images'],
1113 'response_id' => $result['responseId'],
1114 'usage' => $result['usage'],
1115 ];
1116 }
1117
1118 public function ajax_ai_upload_image( $data ) {
1119 if ( empty( $data['image'] ) ) {
1120 throw new \Exception( 'Missing image data' );
1121 }
1122
1123 $image = $data['image'];
1124
1125 if ( empty( $image['image_url'] ) ) {
1126 throw new \Exception( 'Missing image_url' );
1127 }
1128
1129 $image_data = $this->upload_image( $image['image_url'], $data['prompt'], $data['editor_post_id'] );
1130
1131 if ( is_wp_error( $image_data ) ) {
1132 throw new \Exception( esc_html( $image_data->get_error_message() ) );
1133 }
1134
1135 if ( ! empty( $image['use_gallery_image'] ) && ! empty( $image['id'] ) ) {
1136 $app = $this->get_ai_app();
1137 $app->set_used_gallery_image( $image['id'] );
1138 }
1139
1140 return [
1141 'image' => array_merge( $image_data, $data ),
1142 ];
1143 }
1144
1145 public function ajax_ai_generate_layout( $data ) {
1146 $this->verify_permissions( $data['editor_post_id'] );
1147
1148 $app = $this->get_ai_app();
1149
1150 if ( empty( $data['prompt'] ) && empty( $data['attachments'] ) ) {
1151 throw new \Exception( 'Missing prompt / attachments' );
1152 }
1153
1154 if ( ! $app->is_connected() ) {
1155 throw new \Exception( 'not_connected' );
1156 }
1157
1158 $result = $app->generate_layout(
1159 $data,
1160 $this->prepare_generate_layout_context( $data )
1161 );
1162
1163 if ( is_wp_error( $result ) ) {
1164 $message = $result->get_error_message();
1165
1166 if ( is_array( $message ) ) {
1167 $message = implode( ', ', $message );
1168 throw new \Exception( esc_html( $message ) );
1169 }
1170
1171 $this->throw_on_error( $result );
1172 }
1173
1174 $elements = $result['text']['elements'] ?? [];
1175 $base_template_id = $result['baseTemplateId'] ?? null;
1176 $template_type = $result['templateType'] ?? null;
1177
1178 if ( empty( $elements ) || ! is_array( $elements ) ) {
1179 throw new \Exception( 'unknown_error' );
1180 }
1181
1182 if ( 1 === count( $elements ) ) {
1183 $template = $elements[0];
1184 } else {
1185 $template = [
1186 'elType' => 'container',
1187 'elements' => $elements,
1188 'settings' => [
1189 'content_width' => 'full',
1190 'flex_gap' => [
1191 'column' => '0',
1192 'row' => '0',
1193 'unit' => 'px',
1194 ],
1195 'padding' => [
1196 'unit' => 'px',
1197 'top' => '0',
1198 'right' => '0',
1199 'bottom' => '0',
1200 'left' => '0',
1201 'isLinked' => true,
1202 ],
1203 ],
1204 ];
1205 }
1206
1207 return [
1208 'all' => [],
1209 'text' => $template,
1210 'response_id' => $result['responseId'],
1211 'usage' => $result['usage'],
1212 'base_template_id' => $base_template_id,
1213 'template_type' => $template_type,
1214 ];
1215 }
1216
1217 public function ajax_ai_get_layout_prompt_enhancer( $data ) {
1218 $this->verify_permissions( $data['editor_post_id'] );
1219
1220 $app = $this->get_ai_app();
1221
1222 if ( empty( $data['prompt'] ) ) {
1223 throw new \Exception( 'Missing prompt' );
1224 }
1225
1226 if ( ! $app->is_connected() ) {
1227 throw new \Exception( 'not_connected' );
1228 }
1229
1230 $result = $app->get_layout_prompt_enhanced(
1231 $data['prompt'],
1232 $data['enhance_type'],
1233 $this->prepare_generate_layout_context( $data )
1234 );
1235
1236 $this->throw_on_error( $result );
1237
1238 return [
1239 'text' => $result['text'] ?? $data['prompt'],
1240 'response_id' => $result['responseId'] ?? '',
1241 'usage' => $result['usage'] ?? '',
1242 ];
1243 }
1244
1245 private function prepare_generate_layout_context( $data ) {
1246 $request_context = $this->get_request_context( $data );
1247 $kit = Plugin::$instance->kits_manager->get_active_kit();
1248
1249 if ( ! $kit ) {
1250 return $request_context;
1251 }
1252
1253 $kits_data = Collection::make( $kit->get_data()['settings'] ?? [] );
1254
1255 $colors = $kits_data
1256 ->filter( function ( $_, $key ) {
1257 return in_array( $key, [ 'system_colors', 'custom_colors' ], true );
1258 } )
1259 ->flatten()
1260 ->filter( function ( $val ) {
1261 return ! empty( $val['_id'] );
1262 } )
1263 ->map( function ( $val ) {
1264 return [
1265 'id' => $val['_id'],
1266 'label' => $val['title'] ?? null,
1267 'value' => $val['color'] ?? null,
1268 ];
1269 } );
1270
1271 $typography = $kits_data
1272 ->filter( function ( $_, $key ) {
1273 return in_array( $key, [ 'system_typography', 'custom_typography' ], true );
1274 } )
1275 ->flatten()
1276 ->filter( function ( $val ) {
1277 return ! empty( $val['_id'] );
1278 } )
1279 ->map( function ( $val ) {
1280 $font_size = null;
1281
1282 if ( isset(
1283 $val['typography_font_size']['unit'],
1284 $val['typography_font_size']['size']
1285 ) ) {
1286 $prop = $val['typography_font_size'];
1287
1288 $font_size = 'custom' === $prop['unit']
1289 ? $prop['size']
1290 : $prop['size'] . $prop['unit'];
1291 }
1292
1293 return [
1294 'id' => $val['_id'],
1295 'label' => $val['title'] ?? null,
1296 'value' => [
1297 'family' => $val['typography_font_family'] ?? null,
1298 'weight' => $val['typography_font_weight'] ?? null,
1299 'style' => $val['typography_font_style'] ?? null,
1300 'size' => $font_size,
1301 ],
1302 ];
1303 } );
1304
1305 $request_context['globals'] = [
1306 'colors' => $colors->all(),
1307 'typography' => $typography->all(),
1308 ];
1309
1310 return $request_context;
1311 }
1312
1313 private function upload_image( $image_url, $image_title, $parent_post_id = 0 ) {
1314 if ( ! current_user_can( 'upload_files' ) ) {
1315 throw new \Exception( 'Not Allowed to Upload images' );
1316 }
1317
1318 $attachment_id = media_sideload_image( $image_url, $parent_post_id, $image_title, 'id' );
1319
1320 if ( ! empty( $attachment_id['error'] ) ) {
1321 return new \WP_Error( 'upload_error', $attachment_id['error'] );
1322 }
1323
1324 return [
1325 'id' => $attachment_id,
1326 'url' => esc_url( wp_get_attachment_image_url( $attachment_id, 'full' ) ),
1327 'alt' => esc_attr( $image_title ),
1328 'source' => 'library',
1329 ];
1330 }
1331
1332 public function ajax_ai_get_history( $data ): array {
1333 $type = $data['type'] ?? self::HISTORY_TYPE_ALL;
1334
1335 if ( ! in_array( $type, self::VALID_HISTORY_TYPES, true ) ) {
1336 throw new \Exception( 'Invalid history type' );
1337 }
1338
1339 $page = sanitize_text_field( $data['page'] ?? 1 );
1340 $limit = sanitize_text_field( $data['limit'] ?? 10 );
1341
1342 $app = $this->get_ai_app();
1343
1344 if ( ! $app->is_connected() ) {
1345 throw new \Exception( 'not_connected' );
1346 }
1347
1348 $context = $this->get_request_context( $data );
1349
1350 $result = $app->get_history_by_type( $type, $page, $limit, $context );
1351
1352 if ( is_wp_error( $result ) ) {
1353 throw new \Exception( esc_html( $result->get_error_message() ) );
1354 }
1355
1356 return $result;
1357 }
1358
1359 public function ajax_ai_delete_history_item( $data ): array {
1360 if ( empty( $data['id'] ) || ! wp_is_uuid( $data['id'] ) ) {
1361 throw new \Exception( 'Missing id parameter' );
1362 }
1363
1364 $app = $this->get_ai_app();
1365
1366 if ( ! $app->is_connected() ) {
1367 throw new \Exception( 'not_connected' );
1368 }
1369
1370 $context = $this->get_request_context( $data );
1371
1372 $result = $app->delete_history_item( $data['id'], $context );
1373
1374 if ( is_wp_error( $result ) ) {
1375 throw new \Exception( esc_html( $result->get_error_message() ) );
1376 }
1377
1378 return [];
1379 }
1380
1381 public function ajax_ai_toggle_favorite_history_item( $data ): array {
1382 if ( empty( $data['id'] ) || ! wp_is_uuid( $data['id'] ) ) {
1383 throw new \Exception( 'Missing id parameter' );
1384 }
1385
1386 $app = $this->get_ai_app();
1387
1388 if ( ! $app->is_connected() ) {
1389 throw new \Exception( 'not_connected' );
1390 }
1391
1392 $context = $this->get_request_context( $data );
1393
1394 $result = $app->toggle_favorite_history_item( $data['id'], $context );
1395
1396 if ( is_wp_error( $result ) ) {
1397 throw new \Exception( esc_html( $result->get_error_message() ) );
1398 }
1399
1400 return [];
1401 }
1402
1403 public function ajax_ai_get_product_image_unification( $data ): array {
1404 $data['editor_post_id'] = $data['payload']['postId'];
1405 $this->verify_upload_permissions( $data );
1406
1407 $app = $this->get_ai_app();
1408
1409 if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
1410 throw new \Exception( esc_html__( 'Missing Image', 'elementor' ) );
1411 }
1412
1413 if ( empty( $data['payload']['settings'] ) ) {
1414 throw new \Exception( esc_html__( 'Missing prompt settings', 'elementor' ) );
1415 }
1416
1417 if ( ! $app->is_connected() ) {
1418 throw new \Exception( esc_html__( 'not_connected', 'elementor' ) );
1419 }
1420
1421 $context = $this->get_request_context( $data );
1422 $request_ids = $this->get_request_ids( $data['payload'] );
1423
1424 $result = $app->get_unify_product_images( [
1425 'promptSettings' => $data['payload']['settings'],
1426 'attachment_id' => $data['payload']['image']['id'],
1427 ], $context, $request_ids );
1428
1429 $this->throw_on_error( $result );
1430
1431 return [
1432 'images' => $result['images'],
1433 'response_id' => $result['responseId'],
1434 'usage' => $result['usage'],
1435 ];
1436 }
1437
1438 public function ajax_ai_get_animation( $data ): array {
1439 $this->verify_upload_permissions( $data );
1440
1441 $app = $this->get_ai_app();
1442
1443 if ( empty( $data['payload']['prompt'] ) ) {
1444 throw new \Exception( 'Missing prompt' );
1445 }
1446
1447 if ( empty( $data['payload']['motionEffectType'] ) ) {
1448 throw new \Exception( 'Missing animation type' );
1449 }
1450
1451 if ( ! $app->is_connected() ) {
1452 throw new \Exception( 'not_connected' );
1453 }
1454
1455 $context = $this->get_request_context( $data );
1456
1457 $request_ids = $this->get_request_ids( $data['payload'] );
1458
1459 $result = $app->get_animation( $data, $context, $request_ids );
1460 $this->throw_on_error( $result );
1461
1462 return [
1463 'text' => $result['text'],
1464 'response_id' => $result['responseId'],
1465 'usage' => $result['usage'],
1466 ];
1467 }
1468
1469 /**
1470 * @param mixed $result
1471 */
1472 private function throw_on_error( $result ): void {
1473 if ( is_wp_error( $result ) ) {
1474 wp_send_json_error( [
1475 'message' => esc_html( $result->get_error_message() ),
1476 'extra_data' => $result->get_error_data(),
1477 ] );
1478 }
1479 }
1480
1481 /**
1482 * @return void
1483 */
1484 public function add_wc_scripts(): void {
1485 wp_enqueue_script( 'elementor-ai-unify-product-images',
1486 $this->get_js_assets_url( 'ai-unify-product-images' ),
1487 [
1488 'jquery',
1489 'elementor-v2-ui',
1490 'elementor-v2-icons',
1491 'wp-components',
1492 'elementor-common',
1493 ],
1494 ELEMENTOR_VERSION,
1495 true
1496 );
1497
1498 wp_localize_script(
1499 'elementor-ai-unify-product-images',
1500 'UnifyProductImagesConfig',
1501 [
1502 'get_product_images_url' => admin_url( 'admin-ajax.php' ),
1503 'set_product_images_url' => admin_url( 'admin-ajax.php' ),
1504 'nonce' => wp_create_nonce( 'elementor-ai-unify-product-images_nonce' ),
1505 'placeholder' => ELEMENTOR_ASSETS_URL . 'images/app/ai/product-image-unification-example.gif?' . ELEMENTOR_VERSION,
1506 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
1507 'connect_url' => $this->get_ai_connect_url(),
1508 ]
1509 );
1510
1511 add_filter( 'bulk_actions-edit-product', function ( $data ) {
1512 return $this->add_products_bulk_action( $data );
1513 });
1514
1515 wp_set_script_translations( 'elementor-ai-unify-product-images', 'elementor' );
1516 }
1517
1518 /**
1519 * @param $product
1520 * @param int|null $image_to_remove
1521 * @param int|null $image_to_add
1522 * @return void
1523 */
1524 private function update_product_gallery( $product, ?int $image_to_remove, ?int $image_to_add ): void {
1525 $gallery_image_ids = $product->get_gallery_image_ids();
1526
1527 $index = array_search( $image_to_remove, $gallery_image_ids, true );
1528 if ( false !== $index ) {
1529 unset( $gallery_image_ids[ $index ] );
1530 }
1531
1532 if ( ! in_array( $image_to_add, $gallery_image_ids, true ) ) {
1533 $gallery_image_ids[] = $image_to_add;
1534 }
1535
1536 $product->set_gallery_image_ids( $gallery_image_ids );
1537 $product->save();
1538 }
1539 }
1540