PluginProbe
Elementor Website Builder – more than just a page builder / 3.17.2
Elementor Website Builder – more than just a page builder v3.17.2
4.3.1 4.3.0 4.3.0-beta3 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 All 454 releases
← All changes | modules/ai/module.php +170 -841 4.3.0-beta33.17.2 View file →
@@ -1,16 +1,14 @@
1 1 <?php
2 2 namespace Elementor\Modules\Ai;
3 3
4 -use Elementor\Controls_Manager;
5 4 use Elementor\Core\Base\Module as BaseModule;
6 5 use Elementor\Core\Common\Modules\Connect\Module as ConnectModule;
7 -use Elementor\Element_Base;
8 -use Elementor\Plugin;
6 +use Elementor\Core\Experiments\Manager as Experiments_Manager;
9 7 use Elementor\Core\Utils\Collection;
10 8 use Elementor\Modules\Ai\Connect\Ai;
9 +use Elementor\Plugin;
11 10 use Elementor\User;
12 -use Elementor\Utils;
13 11
14 12 if ( ! defined( 'ABSPATH' ) ) {
15 13 exit; // Exit if accessed directly.
16 14 }
@@ -27,10 +25,11 @@
27 25 self::HISTORY_TYPE_CODE,
28 26 self::HISTORY_TYPE_IMAGE,
29 27 self::HISTORY_TYPE_BLOCK,
30 28 ];
31 - const MIN_PAGES_FOR_CREATE_WITH_AI_BANNER = 10;
32 29
30 + const LAYOUT_EXPERIMENT = 'ai-layout';
31 +
33 32 public function get_name() {
34 33 return 'ai';
35 34 }
36 35
@@ -36,21 +35,10 @@
36 35
37 36 public function __construct() {
38 37 parent::__construct();
39 38
40 - ( new SitePlannerConnect\Module() );
39 + $this->register_layout_experiment();
41 40
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_filter( 'elementor/core/admin/homescreen', [ $this, 'add_create_with_ai_banner_to_homescreen' ] );
52 -
53 41 add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) {
54 42 $connect_module->register_app( 'ai', Ai::get_class_name() );
55 43 } );
56 44
@@ -56,13 +44,9 @@
56 44
57 45 add_action( 'elementor/ajax/register_actions', function( $ajax ) {
58 46 $handlers = [
59 47 'ai_get_user_information' => [ $this, 'ajax_ai_get_user_information' ],
60 - 'ai_get_remote_config' => [ $this, 'ajax_ai_get_remote_config' ],
61 - 'ai_get_remote_frontend_config' => [ $this, 'ajax_ai_get_remote_frontend_config' ],
62 48 'ai_get_completion_text' => [ $this, 'ajax_ai_get_completion_text' ],
63 - 'ai_get_excerpt' => [ $this, 'ajax_ai_get_excerpt' ],
64 - 'ai_get_featured_image' => [ $this, 'ajax_ai_get_featured_image' ],
65 49 'ai_get_edit_text' => [ $this, 'ajax_ai_get_edit_text' ],
66 50 'ai_get_custom_code' => [ $this, 'ajax_ai_get_custom_code' ],
67 51 'ai_get_custom_css' => [ $this, 'ajax_ai_get_custom_css' ],
68 52 'ai_set_get_started' => [ $this, 'ajax_ai_set_get_started' ],
@@ -70,9 +54,8 @@
70 54 'ai_get_image_prompt_enhancer' => [ $this, 'ajax_ai_get_image_prompt_enhancer' ],
71 55 'ai_get_text_to_image' => [ $this, 'ajax_ai_get_text_to_image' ],
72 56 'ai_get_image_to_image' => [ $this, 'ajax_ai_get_image_to_image' ],
73 57 'ai_get_image_to_image_mask' => [ $this, 'ajax_ai_get_image_to_image_mask' ],
74 - 'ai_get_image_to_image_mask_cleanup' => [ $this, 'ajax_ai_get_image_to_image_mask_cleanup' ],
75 58 'ai_get_image_to_image_outpainting' => [ $this, 'ajax_ai_get_image_to_image_outpainting' ],
76 59 'ai_get_image_to_image_upscale' => [ $this, 'ajax_ai_get_image_to_image_upscale' ],
77 60 'ai_get_image_to_image_remove_background' => [ $this, 'ajax_ai_get_image_to_image_remove_background' ],
78 61 'ai_get_image_to_image_replace_background' => [ $this, 'ajax_ai_get_image_to_image_replace_background' ],
@@ -81,11 +64,8 @@
81 64 'ai_get_layout_prompt_enhancer' => [ $this, 'ajax_ai_get_layout_prompt_enhancer' ],
82 65 'ai_get_history' => [ $this, 'ajax_ai_get_history' ],
83 66 'ai_delete_history_item' => [ $this, 'ajax_ai_delete_history_item' ],
84 67 'ai_toggle_favorite_history_item' => [ $this, 'ajax_ai_toggle_favorite_history_item' ],
85 - 'ai_get_product_image_unification' => [ $this, 'ajax_ai_get_product_image_unification' ],
86 - 'ai_get_animation' => [ $this, 'ajax_ai_get_animation' ],
87 - 'ai_get_image_to_image_isolate_objects' => [ $this, 'ajax_ai_get_product_image_unification' ],
88 68 ];
89 69
90 70 foreach ( $handlers as $tag => $callback ) {
91 71 $ajax->register_ajax_action( $tag, $callback );
@@ -93,9 +73,12 @@
93 73 } );
94 74
95 75 add_action( 'elementor/editor/before_enqueue_scripts', function() {
96 76 $this->enqueue_main_script();
97 - $this->enqueue_layout_script();
77 +
78 + if ( $this->is_layout_active() ) {
79 + $this->enqueue_layout_script();
80 + }
98 81 } );
99 82
100 83 add_action( 'elementor/editor/after_enqueue_styles', function() {
101 84 wp_enqueue_style(
@@ -106,351 +89,40 @@
106 89 );
107 90 } );
108 91
109 92 add_action( 'elementor/preview/enqueue_styles', function() {
110 - wp_enqueue_style(
111 - 'elementor-ai-layout-preview',
112 - $this->get_css_assets_url( 'modules/ai/layout-preview' ),
113 - [],
114 - ELEMENTOR_VERSION
115 - );
93 + if ( $this->is_layout_active() ) {
94 + wp_enqueue_style(
95 + 'elementor-ai-layout-preview',
96 + $this->get_css_assets_url( 'modules/ai/layout-preview' ),
97 + [],
98 + ELEMENTOR_VERSION
99 + );
100 + }
116 101 } );
117 102
118 - if ( is_admin() ) {
119 - add_action( 'wp_enqueue_media', [ $this, 'enqueue_ai_media_library' ] );
120 - add_action( 'admin_head', [ $this, 'enqueue_ai_media_library_upload_screen' ] );
121 -
122 - if ( current_user_can( 'edit_products' ) || current_user_can( 'publish_products' ) ) {
123 - add_action( 'admin_init', [ $this, 'enqueue_ai_products_page_scripts' ] );
124 - add_action( 'current_screen', [ $this, 'enqueue_ai_single_product_page_scripts' ] );
125 - add_action( 'wp_ajax_elementor-ai-get-product-images', [ $this, 'get_product_images_ajax' ] );
126 - add_action( 'wp_ajax_elementor-ai-set-product-images', [ $this, 'set_product_images_ajax' ] );
103 + add_filter( 'elementor/document/save/data', function ( $data ) {
104 + if ( $this->is_layout_active() ) {
105 + return $this->remove_temporary_containers( $data );
127 106 }
128 - }
129 107
130 - add_action( 'enqueue_block_editor_assets', function() {
131 - wp_enqueue_script( 'elementor-ai-gutenberg',
132 - $this->get_js_assets_url( 'ai-gutenberg' ),
133 - [
134 - 'jquery',
135 - 'elementor-v2-ui',
136 - 'elementor-v2-icons',
137 - 'wp-blocks',
138 - 'wp-element',
139 - 'wp-editor',
140 - 'wp-data',
141 - 'wp-components',
142 - 'wp-compose',
143 - 'wp-i18n',
144 - 'wp-hooks',
145 - 'elementor-ai-media-library',
146 - ],
147 - ELEMENTOR_VERSION, true );
148 -
149 - wp_localize_script(
150 - 'elementor-ai-gutenberg',
151 - 'ElementorAiConfig',
152 - [
153 - 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
154 - 'connect_url' => $this->get_ai_connect_url(),
155 - ]
156 - );
157 -
158 - wp_set_script_translations( 'elementor-ai-gutenberg', 'elementor' );
159 - });
160 -
161 - add_filter( 'elementor/document/save/data', function ( $data ) {
162 - return $this->remove_temporary_containers( $data );
108 + return $data;
163 109 } );
164 -
165 - add_action( 'elementor/element/common/section_effects/after_section_start', [ $this, 'register_ai_motion_effect_control' ], 10, 1 );
166 - add_action( 'elementor/element/container/section_effects/after_section_start', [ $this, 'register_ai_motion_effect_control' ], 10, 1 );
167 - add_action( 'elementor/element/common/_section_transform/after_section_end', [ $this, 'register_ai_hover_effect_control' ], 10, 1 );
168 - add_action( 'elementor/element/container/_section_transform/after_section_end', [ $this, 'register_ai_hover_effect_control' ], 10, 1 );
169 110 }
170 111
171 - public function is_ai_enabled() {
172 - if ( ! Plugin::$instance->experiments->is_feature_active( 'container' ) ) {
173 - return false;
174 - }
175 -
176 - return Preferences::is_ai_enabled( get_current_user_id() );
177 - }
178 -
179 - public function handle_kit_install( $imported_data ) {
180 - if ( ! $this->is_ai_enabled() ) {
181 - return;
182 - }
183 -
184 - if ( ! isset( $imported_data['status'] ) || 'success' !== $imported_data['status'] ) {
185 - return;
186 - }
187 -
188 - if ( ! isset( $imported_data['runner'] ) || 'site-settings' !== $imported_data['runner'] ) {
189 - return;
190 - }
191 -
192 - if ( ! isset( $imported_data['configData']['lastImportedSession']['instance_data']['site_settings']['settings']['ai'] ) ) {
193 - return;
194 - }
195 -
196 - $is_connected = $this->get_ai_app()->is_connected() && User::get_introduction_meta( 'ai_get_started' );
197 -
198 - if ( ! $is_connected ) {
199 - return;
200 - }
201 -
202 - $last_imported_session = $imported_data['configData']['lastImportedSession'];
203 - $imported_ai_data = $last_imported_session['instance_data']['site_settings']['settings']['ai'];
204 -
205 - $this->get_ai_app()->send_event( [
206 - 'name' => 'kit_installed',
207 - 'data' => $imported_ai_data,
208 - 'client' => [
209 - 'name' => 'elementor',
210 - 'version' => ELEMENTOR_VERSION,
211 - 'session_id' => $last_imported_session['session_id'],
112 + private function register_layout_experiment() {
113 + Plugin::$instance->experiments->add_feature( [
114 + 'name' => static::LAYOUT_EXPERIMENT,
115 + 'title' => esc_html__( 'Build with AI', 'elementor' ),
116 + 'default' => Experiments_Manager::STATE_INACTIVE,
117 + 'status' => Experiments_Manager::RELEASE_STATUS_ALPHA,
118 + 'hidden' => true,
119 + 'dependencies' => [
120 + 'container',
212 121 ],
213 122 ] );
214 123 }
215 124
216 - public function register_ai_hover_effect_control( Element_Base $element ) {
217 - if ( ! $element->get_controls( 'ai_hover_animation' ) ) {
218 - $element->add_control(
219 - 'ai_hover_animation',
220 - [
221 - 'tabs_wrapper' => '_tabs_positioning',
222 - 'inner_tab' => '_tab_positioning_hover',
223 - 'label' => esc_html__( 'Animate With AI', 'elementor' ),
224 - 'type' => Controls_Manager::RAW_HTML,
225 - 'raw' => '
226 -<style>
227 - .elementor-control-ai_hover_animation .elementor-control-content {
228 - display: flex;
229 - flex-direction: row;
230 - justify-content: space-between;
231 - align-items: center;
232 - }
233 - .elementor-control-ai_hover_animation .elementor-control-raw-html {
234 - display: none;
235 - }
236 -</style>',
237 - 'render_type' => 'none',
238 - 'ai' => [
239 - 'active' => true,
240 - 'type' => 'hover_animation',
241 - ],
242 - ],
243 - [
244 - 'position' => [
245 - 'of' => '_transform_rotate_popover_hover',
246 - 'type' => 'control',
247 - 'at' => 'before',
248 - ],
249 - ]
250 - );
251 - }
252 - }
253 - public function register_ai_motion_effect_control( $element ) {
254 - if ( Utils::has_pro() && ! $element->get_controls( 'ai_animation' ) ) {
255 - $element->add_control(
256 - 'ai_animation',
257 - [
258 - 'label' => esc_html__( 'Animate With AI', 'elementor' ),
259 - 'type' => Controls_Manager::RAW_HTML,
260 - 'raw' => '
261 - <style>
262 - .elementor-control-ai_animation .elementor-control-content {
263 - display: flex;
264 - flex-direction: row;
265 - justify-content: space-between;
266 - align-items: center;
267 - }
268 - .elementor-control-ai_animation .elementor-control-raw-html {
269 - display: none;
270 - }
271 - </style>',
272 - 'render_type' => 'none',
273 - 'ai' => [
274 - 'active' => true,
275 - 'type' => 'animation',
276 - ],
277 - ]
278 - );
279 - }
280 - }
281 -
282 - private function get_current_screen() {
283 - $is_wc = class_exists( 'WooCommerce' ) && post_type_exists( 'product' );
284 - if ( ! $is_wc ) {
285 - return 'other';
286 - }
287 -
288 - $is_products_page = isset( $_GET['post_type'] ) && 'product' === $_GET['post_type'];
289 - if ( $is_products_page ) {
290 - return 'wc-products';
291 - }
292 -
293 - $screen = get_current_screen();
294 - $is_single_product_page = isset( $screen->post_type ) && ( 'product' === $screen->post_type && 'post' === $screen->base );
295 - if ( $is_single_product_page ) {
296 - return 'wc-single-product';
297 - }
298 -
299 - return 'other';
300 - }
301 -
302 - public function enqueue_ai_products_page_scripts() {
303 - if ( 'wc-products' !== $this->get_current_screen() ) {
304 - return;
305 - }
306 -
307 - $this->add_wc_scripts();
308 - }
309 -
310 - public function enqueue_ai_single_product_page_scripts() {
311 - if ( 'wc-single-product' !== $this->get_current_screen() ) {
312 - return;
313 - }
314 -
315 - $this->add_wc_scripts();
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 125 private function enqueue_main_script() {
454 126 wp_enqueue_script(
455 127 'elementor-ai',
456 128 $this->get_js_assets_url( 'ai' ),
@@ -469,17 +141,15 @@
469 141 ELEMENTOR_VERSION,
470 142 true
471 143 );
472 144
473 - $config = [
474 - 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
475 - 'connect_url' => $this->get_ai_connect_url(),
476 - ];
477 -
478 145 wp_localize_script(
479 146 'elementor-ai',
480 147 'ElementorAiConfig',
481 - $config
148 + [
149 + 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
150 + 'connect_url' => $this->get_ai_connect_url(),
151 + ]
482 152 );
483 153
484 154 wp_set_script_translations( 'elementor-ai', 'elementor' );
485 155 }
@@ -505,10 +175,14 @@
505 175
506 176 wp_set_script_translations( 'elementor-ai-layout', 'elementor' );
507 177 }
508 178
179 + private function is_layout_active() {
180 + return Plugin::$instance->experiments->is_feature_active( self::LAYOUT_EXPERIMENT );
181 + }
182 +
509 183 private function remove_temporary_containers( $data ) {
510 - if ( empty( $data['elements'] ) || ! is_array( $data['elements'] ) ) {
184 + if ( empty( $data['elements'] ) ) {
511 185 return $data;
512 186 }
513 187
514 188 // If for some reason the document has been saved during an AI Layout session,
@@ -556,35 +230,8 @@
556 230 'usage' => $user_usage,
557 231 ];
558 232 }
559 233
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 234 private function verify_permissions( $editor_post_id ) {
588 235 $document = Plugin::$instance->documents->get( $editor_post_id );
589 236
590 237 if ( ! $document ) {
@@ -590,19 +237,15 @@
590 237 if ( ! $document ) {
591 238 throw new \Exception( 'Document not found' );
592 239 }
593 240
594 - if ( $document->is_built_with_elementor() ) {
595 - if ( ! $document->is_editable_by_current_user() ) {
596 - throw new \Exception( 'Access denied' );
597 - }
598 - } elseif ( ! current_user_can( 'edit_post', $editor_post_id ) ) {
599 - throw new \Exception( 'Access denied' );
241 + if ( ! $document->is_built_with_elementor() || ! $document->is_editable_by_current_user() ) {
242 + throw new \Exception( 'Access denied' );
600 243 }
601 244 }
602 245
603 246 public function ajax_ai_get_image_prompt_enhancer( $data ) {
604 - $this->verify_upload_permissions( $data );
247 + $this->verify_permissions( $data['editor_post_id'] );
605 248
606 249 $app = $this->get_ai_app();
607 250
608 251 if ( empty( $data['prompt'] ) ) {
@@ -611,12 +254,13 @@
611 254
612 255 if ( ! $app->is_connected() ) {
613 256 throw new \Exception( 'not_connected' );
614 257 }
615 - $request_ids = $this->get_request_ids( $data['payload'] );
616 258
617 - $result = $app->get_image_prompt_enhanced( $data['prompt'], [], $request_ids );
618 - $this->throw_on_error( $result );
259 + $result = $app->get_image_prompt_enhanced( $data['prompt'] );
260 + if ( is_wp_error( $result ) ) {
261 + throw new \Exception( $result->get_error_message() );
262 + }
619 263
620 264 return [
621 265 'text' => $result['text'],
622 266 'response_id' => $result['responseId'],
@@ -628,9 +272,9 @@
628 272 $this->verify_permissions( $data['editor_post_id'] );
629 273
630 274 $app = $this->get_ai_app();
631 275
632 - if ( empty( $data['payload']['prompt'] ) ) {
276 + if ( empty( $data['prompt'] ) ) {
633 277 throw new \Exception( 'Missing prompt' );
634 278 }
635 279
636 280 if ( ! $app->is_connected() ) {
@@ -638,39 +282,13 @@
638 282 }
639 283
640 284 $context = $this->get_request_context( $data );
641 285
642 - $request_ids = $this->get_request_ids( $data['payload'] );
643 -
644 - $result = $app->get_completion_text( $data['payload']['prompt'], $context, $request_ids );
645 - $this->throw_on_error( $result );
646 -
647 - return [
648 - 'text' => $result['text'],
649 - 'response_id' => $result['responseId'],
650 - 'usage' => $result['usage'],
651 - ];
652 - }
653 -
654 -
655 - public function ajax_ai_get_excerpt( $data ): array {
656 - $app = $this->get_ai_app();
657 -
658 - if ( empty( $data['payload']['content'] ) ) {
659 - throw new \Exception( 'Missing content' );
286 + $result = $app->get_completion_text( $data['prompt'], $context );
287 + if ( is_wp_error( $result ) ) {
288 + throw new \Exception( $result->get_error_message() );
660 289 }
661 290
662 - if ( ! $app->is_connected() ) {
663 - throw new \Exception( 'Not connected' );
664 - }
665 -
666 - $context = $this->get_request_context( $data );
667 -
668 - $request_ids = $this->get_request_ids( $data['payload'] );
669 -
670 - $result = $app->get_excerpt( $data['payload']['content'], $context, $request_ids );
671 - $this->throw_on_error( $result );
672 -
673 291 return [
674 292 'text' => $result['text'],
675 293 'response_id' => $result['responseId'],
676 294 'usage' => $result['usage'],
@@ -676,36 +294,9 @@
676 294 'usage' => $result['usage'],
677 295 ];
678 296 }
679 297
680 - public function ajax_ai_get_featured_image( $data ): array {
681 - $this->verify_upload_permissions( $data );
682 -
683 - if ( empty( $data['payload']['prompt'] ) ) {
684 - throw new \Exception( 'Missing prompt' );
685 - }
686 -
687 - $app = $this->get_ai_app();
688 -
689 - if ( ! $app->is_connected() ) {
690 - throw new \Exception( 'not_connected' );
691 - }
692 -
693 - $context = $this->get_request_context( $data );
694 - $request_ids = $this->get_request_ids( $data['payload'] );
695 -
696 - $result = $app->get_featured_image( $data, $context, $request_ids );
697 -
698 - $this->throw_on_error( $result );
699 -
700 - return [
701 - 'images' => $result['images'],
702 - 'response_id' => $result['responseId'],
703 - 'usage' => $result['usage'],
704 - ];
705 - }
706 -
707 - private function get_ai_app(): Ai {
298 + private function get_ai_app() : Ai {
708 299 return Plugin::$instance->common->get_component( 'connect' )->get_app( 'ai' );
709 300 }
710 301
711 302 private function get_request_context( $data ) {
@@ -715,26 +306,18 @@
715 306
716 307 return $data['context'];
717 308 }
718 309
719 - private function get_request_ids( $data ) {
720 - if ( empty( $data['requestIds'] ) ) {
721 - return new \stdClass();
722 - }
723 -
724 - return $data['requestIds'];
725 - }
726 -
727 310 public function ajax_ai_get_edit_text( $data ) {
728 311 $this->verify_permissions( $data['editor_post_id'] );
729 312
730 313 $app = $this->get_ai_app();
731 314
732 - if ( empty( $data['payload']['input'] ) ) {
315 + if ( empty( $data['input'] ) ) {
733 316 throw new \Exception( 'Missing input' );
734 317 }
735 318
736 - if ( empty( $data['payload']['instruction'] ) ) {
319 + if ( empty( $data['instruction'] ) ) {
737 320 throw new \Exception( 'Missing instruction' );
738 321 }
739 322
740 323 if ( ! $app->is_connected() ) {
@@ -742,13 +325,13 @@
742 325 }
743 326
744 327 $context = $this->get_request_context( $data );
745 328
746 - $request_ids = $this->get_request_ids( $data['payload'] );
329 + $result = $app->get_edit_text( $data['input'], $data['instruction'], $context );
330 + if ( is_wp_error( $result ) ) {
331 + throw new \Exception( $result->get_error_message() );
332 + }
747 333
748 - $result = $app->get_edit_text( $data, $context, $request_ids );
749 - $this->throw_on_error( $result );
750 -
751 334 return [
752 335 'text' => $result['text'],
753 336 'response_id' => $result['responseId'],
754 337 'usage' => $result['usage'],
@@ -757,13 +340,13 @@
757 340
758 341 public function ajax_ai_get_custom_code( $data ) {
759 342 $app = $this->get_ai_app();
760 343
761 - if ( empty( $data['payload']['prompt'] ) ) {
344 + if ( empty( $data['prompt'] ) ) {
762 345 throw new \Exception( 'Missing prompt' );
763 346 }
764 347
765 - if ( empty( $data['payload']['language'] ) ) {
348 + if ( empty( $data['language'] ) ) {
766 349 throw new \Exception( 'Missing language' );
767 350 }
768 351
769 352 if ( ! $app->is_connected() ) {
@@ -771,13 +354,13 @@
771 354 }
772 355
773 356 $context = $this->get_request_context( $data );
774 357
775 - $request_ids = $this->get_request_ids( $data['payload'] );
358 + $result = $app->get_custom_code( $data['prompt'], $data['language'], $context );
359 + if ( is_wp_error( $result ) ) {
360 + throw new \Exception( $result->get_error_message() );
361 + }
776 362
777 - $result = $app->get_custom_code( $data, $context, $request_ids );
778 - $this->throw_on_error( $result );
779 -
780 363 return [
781 364 'text' => $result['text'],
782 365 'response_id' => $result['responseId'],
783 366 'usage' => $result['usage'],
@@ -788,17 +371,17 @@
788 371 $this->verify_permissions( $data['editor_post_id'] );
789 372
790 373 $app = $this->get_ai_app();
791 374
792 - if ( empty( $data['payload']['prompt'] ) ) {
375 + if ( empty( $data['prompt'] ) ) {
793 376 throw new \Exception( 'Missing prompt' );
794 377 }
795 378
796 - if ( empty( $data['payload']['html_markup'] ) ) {
379 + if ( empty( $data['html_markup'] ) ) {
797 380 $data['html_markup'] = '';
798 381 }
799 382
800 - if ( empty( $data['payload']['element_id'] ) ) {
383 + if ( empty( $data['element_id'] ) ) {
801 384 throw new \Exception( 'Missing element_id' );
802 385 }
803 386
804 387 if ( ! $app->is_connected() ) {
@@ -805,12 +388,13 @@
805 388 throw new \Exception( 'not_connected' );
806 389 }
807 390
808 391 $context = $this->get_request_context( $data );
809 - $request_ids = $this->get_request_ids( $data['payload'] );
810 392
811 - $result = $app->get_custom_css( $data, $context, $request_ids );
812 - $this->throw_on_error( $result );
393 + $result = $app->get_custom_css( $data['prompt'], $data['html_markup'], $data['element_id'], $context );
394 + if ( is_wp_error( $result ) ) {
395 + throw new \Exception( $result->get_error_message() );
396 + }
813 397
814 398 return [
815 399 'text' => $result['text'],
816 400 'response_id' => $result['responseId'],
@@ -844,11 +428,11 @@
844 428 return [];
845 429 }
846 430
847 431 public function ajax_ai_get_text_to_image( $data ) {
848 - $this->verify_upload_permissions( $data );
432 + $this->verify_permissions( $data['editor_post_id'] );
849 433
850 - if ( empty( $data['payload']['prompt'] ) ) {
434 + if ( empty( $data['prompt'] ) ) {
851 435 throw new \Exception( 'Missing prompt' );
852 436 }
853 437
854 438 $app = $this->get_ai_app();
@@ -857,13 +441,14 @@
857 441 throw new \Exception( 'not_connected' );
858 442 }
859 443
860 444 $context = $this->get_request_context( $data );
861 - $request_ids = $this->get_request_ids( $data['payload'] );
862 445
863 - $result = $app->get_text_to_image( $data, $context, $request_ids );
446 + $result = $app->get_text_to_image( $data['prompt'], $data['promptSettings'], $context );
864 447
865 - $this->throw_on_error( $result );
448 + if ( is_wp_error( $result ) ) {
449 + throw new \Exception( $result->get_error_message() );
450 + }
866 451
867 452 return [
868 453 'images' => $result['images'],
869 454 'response_id' => $result['responseId'],
@@ -871,17 +456,21 @@
871 456 ];
872 457 }
873 458
874 459 public function ajax_ai_get_image_to_image( $data ) {
875 - $this->verify_upload_permissions( $data );
460 + $this->verify_permissions( $data['editor_post_id'] );
876 461
877 462 $app = $this->get_ai_app();
878 463
879 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
464 + if ( empty( $data['prompt'] ) ) {
465 + throw new \Exception( 'Missing prompt' );
466 + }
467 +
468 + if ( empty( $data['image'] ) || empty( $data['image']['id'] ) ) {
880 469 throw new \Exception( 'Missing Image' );
881 470 }
882 471
883 - if ( empty( $data['payload']['settings'] ) ) {
472 + if ( empty( $data['promptSettings'] ) ) {
884 473 throw new \Exception( 'Missing prompt settings' );
885 474 }
886 475
887 476 if ( ! $app->is_connected() ) {
@@ -888,17 +477,18 @@
888 477 throw new \Exception( 'not_connected' );
889 478 }
890 479
891 480 $context = $this->get_request_context( $data );
892 - $request_ids = $this->get_request_ids( $data['payload'] );
893 481
894 482 $result = $app->get_image_to_image( [
895 - 'prompt' => $data['payload']['prompt'],
896 - 'promptSettings' => $data['payload']['settings'],
897 - 'attachment_id' => $data['payload']['image']['id'],
898 - ], $context, $request_ids );
483 + 'prompt' => $data['prompt'],
484 + 'promptSettings' => $data['promptSettings'],
485 + 'attachment_id' => $data['image']['id'],
486 + ], $context );
899 487
900 - $this->throw_on_error( $result );
488 + if ( is_wp_error( $result ) ) {
489 + throw new \Exception( $result->get_error_message() );
490 + }
901 491
902 492 return [
903 493 'images' => $result['images'],
904 494 'response_id' => $result['responseId'],
@@ -906,17 +496,17 @@
906 496 ];
907 497 }
908 498
909 499 public function ajax_ai_get_image_to_image_upscale( $data ) {
910 - $this->verify_upload_permissions( $data );
500 + $this->verify_permissions( $data['editor_post_id'] );
911 501
912 502 $app = $this->get_ai_app();
913 503
914 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
504 + if ( empty( $data['image'] ) || empty( $data['image']['id'] ) ) {
915 505 throw new \Exception( 'Missing Image' );
916 506 }
917 507
918 - if ( empty( $data['payload']['promptSettings'] ) ) {
508 + if ( empty( $data['promptSettings'] ) ) {
919 509 throw new \Exception( 'Missing prompt settings' );
920 510 }
921 511
922 512 if ( ! $app->is_connected() ) {
@@ -923,16 +513,17 @@
923 513 throw new \Exception( 'not_connected' );
924 514 }
925 515
926 516 $context = $this->get_request_context( $data );
927 - $request_ids = $this->get_request_ids( $data['payload'] );
928 517
929 518 $result = $app->get_image_to_image_upscale( [
930 - 'promptSettings' => $data['payload']['promptSettings'],
931 - 'attachment_id' => $data['payload']['image']['id'],
932 - ], $context, $request_ids );
519 + 'promptSettings' => $data['promptSettings'],
520 + 'attachment_id' => $data['image']['id'],
521 + ], $context );
933 522
934 - $this->throw_on_error( $result );
523 + if ( is_wp_error( $result ) ) {
524 + throw new \Exception( $result->get_error_message() );
525 + }
935 526
936 527 return [
937 528 'images' => $result['images'],
938 529 'response_id' => $result['responseId'],
@@ -940,17 +531,17 @@
940 531 ];
941 532 }
942 533
943 534 public function ajax_ai_get_image_to_image_replace_background( $data ) {
944 - $this->verify_upload_permissions( $data );
535 + $this->verify_permissions( $data['editor_post_id'] );
945 536
946 537 $app = $this->get_ai_app();
947 538
948 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
539 + if ( empty( $data['image'] ) || empty( $data['image']['id'] ) ) {
949 540 throw new \Exception( 'Missing Image' );
950 541 }
951 542
952 - if ( empty( $data['payload']['prompt'] ) ) {
543 + if ( empty( $data['prompt'] ) ) {
953 544 throw new \Exception( 'Prompt Missing' );
954 545 }
955 546
956 547 if ( ! $app->is_connected() ) {
@@ -957,16 +548,17 @@
957 548 throw new \Exception( 'not_connected' );
958 549 }
959 550
960 551 $context = $this->get_request_context( $data );
961 - $request_ids = $this->get_request_ids( $data['payload'] );
962 552
963 553 $result = $app->get_image_to_image_replace_background( [
964 - 'attachment_id' => $data['payload']['image']['id'],
965 - 'prompt' => $data['payload']['prompt'],
966 - ], $context, $request_ids );
554 + 'attachment_id' => $data['image']['id'],
555 + 'prompt' => $data['prompt'],
556 + ], $context );
967 557
968 - $this->throw_on_error( $result );
558 + if ( is_wp_error( $result ) ) {
559 + throw new \Exception( $result->get_error_message() );
560 + }
969 561
970 562 return [
971 563 'images' => $result['images'],
972 564 'response_id' => $result['responseId'],
@@ -974,13 +566,13 @@
974 566 ];
975 567 }
976 568
977 569 public function ajax_ai_get_image_to_image_remove_background( $data ) {
978 - $this->verify_upload_permissions( $data );
570 + $this->verify_permissions( $data['editor_post_id'] );
979 571
980 572 $app = $this->get_ai_app();
981 573
982 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
574 + if ( empty( $data['image'] ) || empty( $data['image']['id'] ) ) {
983 575 throw new \Exception( 'Missing Image' );
984 576 }
985 577
986 578 if ( ! $app->is_connected() ) {
@@ -987,14 +579,16 @@
987 579 throw new \Exception( 'not_connected' );
988 580 }
989 581
990 582 $context = $this->get_request_context( $data );
991 - $request_ids = $this->get_request_ids( $data['payload'] );
583 +
992 584 $result = $app->get_image_to_image_remove_background( [
993 - 'attachment_id' => $data['payload']['image']['id'],
994 - ], $context, $request_ids );
585 + 'attachment_id' => $data['image']['id'],
586 + ], $context );
995 587
996 - $this->throw_on_error( $result );
588 + if ( is_wp_error( $result ) ) {
589 + throw new \Exception( $result->get_error_message() );
590 + }
997 591
998 592 return [
999 593 'images' => $result['images'],
1000 594 'response_id' => $result['responseId'],
@@ -1002,75 +596,44 @@
1002 596 ];
1003 597 }
1004 598
1005 599 public function ajax_ai_get_image_to_image_mask( $data ) {
1006 - $this->verify_upload_permissions( $data );
600 + $this->verify_permissions( $data['editor_post_id'] );
1007 601
1008 602 $app = $this->get_ai_app();
1009 603
1010 - if ( empty( $data['payload']['prompt'] ) ) {
604 + if ( empty( $data['prompt'] ) ) {
1011 605 throw new \Exception( 'Missing prompt' );
1012 606 }
1013 607
1014 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
608 + if ( empty( $data['image'] ) || empty( $data['image']['id'] ) ) {
1015 609 throw new \Exception( 'Missing Image' );
1016 610 }
1017 611
1018 - if ( empty( $data['payload']['settings'] ) ) {
612 + if ( empty( $data['promptSettings'] ) ) {
1019 613 throw new \Exception( 'Missing prompt settings' );
1020 614 }
1021 615
1022 - if ( empty( $data['payload']['mask'] ) ) {
1023 - throw new \Exception( 'Missing Mask' );
1024 - }
1025 -
1026 - $context = $this->get_request_context( $data );
1027 - $request_ids = $this->get_request_ids( $data['payload'] );
1028 -
1029 - $result = $app->get_image_to_image_mask( [
1030 - 'prompt' => $data['payload']['prompt'],
1031 - 'attachment_id' => $data['payload']['image']['id'],
1032 - 'mask' => $data['payload']['mask'],
1033 - ], $context, $request_ids );
1034 -
1035 - $this->throw_on_error( $result );
1036 -
1037 - return [
1038 - 'images' => $result['images'],
1039 - 'response_id' => $result['responseId'],
1040 - 'usage' => $result['usage'],
1041 - ];
1042 - }
1043 - public function ajax_ai_get_image_to_image_mask_cleanup( $data ) {
1044 - $this->verify_upload_permissions( $data );
1045 -
1046 - $app = $this->get_ai_app();
1047 -
1048 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
1049 - throw new \Exception( 'Missing Image' );
1050 - }
1051 -
1052 - if ( empty( $data['payload']['settings'] ) ) {
1053 - throw new \Exception( 'Missing prompt settings' );
1054 - }
1055 -
1056 616 if ( ! $app->is_connected() ) {
1057 617 throw new \Exception( 'not_connected' );
1058 618 }
1059 619
1060 - if ( empty( $data['payload']['mask'] ) ) {
620 + if ( empty( $data['mask'] ) ) {
1061 621 throw new \Exception( 'Missing Mask' );
1062 622 }
1063 623
1064 624 $context = $this->get_request_context( $data );
1065 - $request_ids = $this->get_request_ids( $data['payload'] );
1066 625
1067 - $result = $app->get_image_to_image_mask_cleanup( [
1068 - 'attachment_id' => $data['payload']['image']['id'],
1069 - 'mask' => $data['payload']['mask'],
1070 - ], $context, $request_ids );
626 + $result = $app->get_image_to_image_mask( [
627 + 'prompt' => $data['prompt'],
628 + 'promptSettings' => $data['promptSettings'],
629 + 'attachment_id' => $data['image']['id'],
630 + 'mask' => $data['mask'],
631 + ], $context );
1071 632
1072 - $this->throw_on_error( $result );
633 + if ( is_wp_error( $result ) ) {
634 + throw new \Exception( $result->get_error_message() );
635 + }
1073 636
1074 637 return [
1075 638 'images' => $result['images'],
1076 639 'response_id' => $result['responseId'],
@@ -1076,32 +639,35 @@
1076 639 'response_id' => $result['responseId'],
1077 640 'usage' => $result['usage'],
1078 641 ];
1079 642 }
1080 -
1081 643 public function ajax_ai_get_image_to_image_outpainting( $data ) {
1082 - $this->verify_upload_permissions( $data );
644 + $this->verify_permissions( $data['editor_post_id'] );
1083 645
1084 646 $app = $this->get_ai_app();
1085 647
648 + if ( empty( $data['prompt'] ) ) {
649 + throw new \Exception( 'Missing prompt' );
650 + }
651 +
1086 652 if ( ! $app->is_connected() ) {
1087 653 throw new \Exception( 'not_connected' );
1088 654 }
1089 655
1090 - if ( empty( $data['payload']['mask'] ) ) {
656 + if ( empty( $data['mask'] ) ) {
1091 657 throw new \Exception( 'Missing Expended Image' );
1092 658 }
1093 659
1094 660 $context = $this->get_request_context( $data );
1095 - $request_ids = $this->get_request_ids( $data['payload'] );
661 +
1096 662 $result = $app->get_image_to_image_out_painting( [
1097 - 'size' => $data['payload']['size'],
1098 - 'position' => $data['payload']['position'],
1099 - 'mask' => $data['payload']['mask'],
1100 - 'image_base64' => $data['payload']['image_base64'],
1101 - ], $context, $request_ids );
663 + 'prompt' => $data['prompt'],
664 + 'mask' => $data['mask'],
665 + ], $context );
1102 666
1103 - $this->throw_on_error( $result );
667 + if ( is_wp_error( $result ) ) {
668 + throw new \Exception( $result->get_error_message() );
669 + }
1104 670
1105 671 return [
1106 672 'images' => $result['images'],
1107 673 'response_id' => $result['responseId'],
@@ -1109,10 +675,8 @@
1109 675 ];
1110 676 }
1111 677
1112 678 public function ajax_ai_upload_image( $data ) {
1113 - $this->verify_upload_permissions( $data );
1114 -
1115 679 if ( empty( $data['image'] ) ) {
1116 680 throw new \Exception( 'Missing image data' );
1117 681 }
1118 682
@@ -1124,14 +688,14 @@
1124 688
1125 689 $image_data = $this->upload_image( $image['image_url'], $data['prompt'], $data['editor_post_id'] );
1126 690
1127 691 if ( is_wp_error( $image_data ) ) {
1128 - throw new \Exception( esc_html( $image_data->get_error_message() ) );
692 + throw new \Exception( $image_data->get_error_message() );
1129 693 }
1130 694
1131 695 if ( ! empty( $image['use_gallery_image'] ) && ! empty( $image['id'] ) ) {
1132 - $app = $this->get_ai_app();
1133 - $app->set_used_gallery_image( $image['id'] );
696 + $app = $this->get_ai_app();
697 + $app->set_used_gallery_image( $image['id'] );
1134 698 }
1135 699
1136 700 return [
1137 701 'image' => array_merge( $image_data, $data ),
@@ -1142,10 +706,10 @@
1142 706 $this->verify_permissions( $data['editor_post_id'] );
1143 707
1144 708 $app = $this->get_ai_app();
1145 709
1146 - if ( empty( $data['prompt'] ) && empty( $data['attachments'] ) ) {
1147 - throw new \Exception( 'Missing prompt / attachments' );
710 + if ( empty( $data['prompt'] ) ) {
711 + throw new \Exception( 'Missing prompt' );
1148 712 }
1149 713
1150 714 if ( ! $app->is_connected() ) {
1151 715 throw new \Exception( 'not_connected' );
@@ -1151,63 +715,28 @@
1151 715 throw new \Exception( 'not_connected' );
1152 716 }
1153 717
1154 718 $result = $app->generate_layout(
1155 - $data,
1156 - $this->prepare_generate_layout_context( $data )
719 + $data['prompt'],
720 + $this->prepare_generate_layout_context(),
721 + $data['variationType']
1157 722 );
1158 723
1159 724 if ( is_wp_error( $result ) ) {
1160 - $message = $result->get_error_message();
1161 -
1162 - if ( is_array( $message ) ) {
1163 - $message = implode( ', ', $message );
1164 - throw new \Exception( esc_html( $message ) );
1165 - }
1166 -
1167 - $this->throw_on_error( $result );
725 + throw new \Exception( $result->get_error_message() );
1168 726 }
1169 727
1170 - $elements = $result['text']['elements'] ?? [];
1171 - $base_template_id = $result['baseTemplateId'] ?? null;
1172 - $template_type = $result['templateType'] ?? null;
728 + $template = $result['text']['elements'][0] ?? null;
1173 729
1174 - if ( empty( $elements ) || ! is_array( $elements ) ) {
730 + if ( empty( $template ) || ! is_array( $template ) ) {
1175 731 throw new \Exception( 'unknown_error' );
1176 732 }
1177 733
1178 - if ( 1 === count( $elements ) ) {
1179 - $template = $elements[0];
1180 - } else {
1181 - $template = [
1182 - 'elType' => 'container',
1183 - 'elements' => $elements,
1184 - 'settings' => [
1185 - 'content_width' => 'full',
1186 - 'flex_gap' => [
1187 - 'column' => '0',
1188 - 'row' => '0',
1189 - 'unit' => 'px',
1190 - ],
1191 - 'padding' => [
1192 - 'unit' => 'px',
1193 - 'top' => '0',
1194 - 'right' => '0',
1195 - 'bottom' => '0',
1196 - 'left' => '0',
1197 - 'isLinked' => true,
1198 - ],
1199 - ],
1200 - ];
1201 - }
1202 -
1203 734 return [
1204 735 'all' => [],
1205 736 'text' => $template,
1206 737 'response_id' => $result['responseId'],
1207 738 'usage' => $result['usage'],
1208 - 'base_template_id' => $base_template_id,
1209 - 'template_type' => $template_type,
1210 739 ];
1211 740 }
1212 741
1213 742 public function ajax_ai_get_layout_prompt_enhancer( $data ) {
@@ -1222,15 +751,13 @@
1222 751 if ( ! $app->is_connected() ) {
1223 752 throw new \Exception( 'not_connected' );
1224 753 }
1225 754
1226 - $result = $app->get_layout_prompt_enhanced(
1227 - $data['prompt'],
1228 - $data['enhance_type'],
1229 - $this->prepare_generate_layout_context( $data )
1230 - );
755 + $result = $app->get_layout_prompt_enhanced( $data['prompt'] );
1231 756
1232 - $this->throw_on_error( $result );
757 + if ( is_wp_error( $result ) ) {
758 + throw new \Exception( $result->get_error_message() );
759 + }
1233 760
1234 761 return [
1235 762 'text' => $result['text'] ?? $data['prompt'],
1236 763 'response_id' => $result['responseId'] ?? '',
@@ -1237,14 +764,13 @@
1237 764 'usage' => $result['usage'] ?? '',
1238 765 ];
1239 766 }
1240 767
1241 - private function prepare_generate_layout_context( $data ) {
1242 - $request_context = $this->get_request_context( $data );
768 + private function prepare_generate_layout_context() {
1243 769 $kit = Plugin::$instance->kits_manager->get_active_kit();
1244 770
1245 771 if ( ! $kit ) {
1246 - return $request_context;
772 + return [];
1247 773 }
1248 774
1249 775 $kits_data = Collection::make( $kit->get_data()['settings'] ?? [] );
1250 776
@@ -1297,14 +823,14 @@
1297 823 ],
1298 824 ];
1299 825 } );
1300 826
1301 - $request_context['globals'] = [
1302 - 'colors' => $colors->all(),
1303 - 'typography' => $typography->all(),
827 + return [
828 + 'globals' => [
829 + 'colors' => $colors->all(),
830 + 'typography' => $typography->all(),
831 + ],
1304 832 ];
1305 -
1306 - return $request_context;
1307 833 }
1308 834
1309 835 private function upload_image( $image_url, $image_title, $parent_post_id = 0 ) {
1310 836 if ( ! current_user_can( 'upload_files' ) ) {
@@ -1310,24 +836,10 @@
1310 836 if ( ! current_user_can( 'upload_files' ) ) {
1311 837 throw new \Exception( 'Not Allowed to Upload images' );
1312 838 }
1313 839
1314 - $uploads_manager = new \Elementor\Core\Files\Uploads_Manager();
1315 - if ( $uploads_manager::are_unfiltered_uploads_enabled() ) {
1316 - Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
1317 - add_filter( 'wp_handle_sideload_prefilter', [ Plugin::$instance->uploads_manager, 'handle_elementor_upload' ] );
1318 - add_filter( 'image_sideload_extensions', function( $extensions ) {
1319 - $extensions[] = 'svg';
1320 - return $extensions;
1321 - });
1322 - }
1323 -
1324 840 $attachment_id = media_sideload_image( $image_url, $parent_post_id, $image_title, 'id' );
1325 841
1326 - if ( is_wp_error( $attachment_id ) ) {
1327 - return new \WP_Error( 'upload_error', $attachment_id->get_error_message() );
1328 - }
1329 -
1330 842 if ( ! empty( $attachment_id['error'] ) ) {
1331 843 return new \WP_Error( 'upload_error', $attachment_id['error'] );
1332 844 }
1333 845
@@ -1332,10 +844,10 @@
1332 844 }
1333 845
1334 846 return [
1335 847 'id' => $attachment_id,
1336 - 'url' => esc_url( wp_get_attachment_image_url( $attachment_id, 'full' ) ),
1337 - 'alt' => esc_attr( $image_title ),
848 + 'url' => wp_get_attachment_image_url( $attachment_id, 'full' ),
849 + 'alt' => $image_title,
1338 850 'source' => 'library',
1339 851 ];
1340 852 }
1341 853
@@ -1359,9 +871,9 @@
1359 871
1360 872 $result = $app->get_history_by_type( $type, $page, $limit, $context );
1361 873
1362 874 if ( is_wp_error( $result ) ) {
1363 - throw new \Exception( esc_html( $result->get_error_message() ) );
875 + throw new \Exception( $result->get_error_message() );
1364 876 }
1365 877
1366 878 return $result;
1367 879 }
@@ -1381,9 +893,9 @@
1381 893
1382 894 $result = $app->delete_history_item( $data['id'], $context );
1383 895
1384 896 if ( is_wp_error( $result ) ) {
1385 - throw new \Exception( esc_html( $result->get_error_message() ) );
897 + throw new \Exception( $result->get_error_message() );
1386 898 }
1387 899
1388 900 return [];
1389 901 }
@@ -1403,193 +915,10 @@
1403 915
1404 916 $result = $app->toggle_favorite_history_item( $data['id'], $context );
1405 917
1406 918 if ( is_wp_error( $result ) ) {
1407 - throw new \Exception( esc_html( $result->get_error_message() ) );
919 + throw new \Exception( $result->get_error_message() );
1408 920 }
1409 921
1410 922 return [];
1411 - }
1412 -
1413 - public function ajax_ai_get_product_image_unification( $data ): array {
1414 - if ( ! empty( $data['payload']['postId'] ) ) {
1415 - $data['editor_post_id'] = $data['payload']['postId'];
1416 - }
1417 - $this->verify_upload_permissions( $data );
1418 -
1419 - $app = $this->get_ai_app();
1420 -
1421 - if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) {
1422 - throw new \Exception( 'Missing Image' );
1423 - }
1424 -
1425 - if ( empty( $data['payload']['settings'] ) ) {
1426 - throw new \Exception( 'Missing prompt settings' );
1427 - }
1428 -
1429 - if ( ! $app->is_connected() ) {
1430 - throw new \Exception( 'not_connected' );
1431 - }
1432 -
1433 - $context = $this->get_request_context( $data );
1434 - $request_ids = $this->get_request_ids( $data['payload'] );
1435 -
1436 - $result = $app->get_unify_product_images( [
1437 - 'promptSettings' => $data['payload']['settings'],
1438 - 'attachment_id' => $data['payload']['image']['id'],
1439 - 'featureIdentifier' => $data['payload']['featureIdentifier'] ?? '',
1440 - ], $context, $request_ids );
1441 -
1442 - $this->throw_on_error( $result );
1443 -
1444 - return [
1445 - 'images' => $result['images'],
1446 - 'response_id' => $result['responseId'],
1447 - 'usage' => $result['usage'],
1448 - ];
1449 - }
1450 -
1451 - public function ajax_ai_get_animation( $data ): array {
1452 - $this->verify_upload_permissions( $data );
1453 -
1454 - $app = $this->get_ai_app();
1455 -
1456 - if ( empty( $data['payload']['prompt'] ) ) {
1457 - throw new \Exception( 'Missing prompt' );
1458 - }
1459 -
1460 - if ( empty( $data['payload']['motionEffectType'] ) ) {
1461 - throw new \Exception( 'Missing animation type' );
1462 - }
1463 -
1464 - if ( ! $app->is_connected() ) {
1465 - throw new \Exception( 'not_connected' );
1466 - }
1467 -
1468 - $context = $this->get_request_context( $data );
1469 -
1470 - $request_ids = $this->get_request_ids( $data['payload'] );
1471 -
1472 - $result = $app->get_animation( $data, $context, $request_ids );
1473 - $this->throw_on_error( $result );
1474 -
1475 - return [
1476 - 'text' => $result['text'],
1477 - 'response_id' => $result['responseId'],
1478 - 'usage' => $result['usage'],
1479 - ];
1480 - }
1481 -
1482 - /**
1483 - * @param mixed $result
1484 - */
1485 - private function throw_on_error( $result ): void {
1486 - if ( is_wp_error( $result ) ) {
1487 - wp_send_json_error( [
1488 - 'message' => esc_html( $result->get_error_message() ),
1489 - 'extra_data' => $result->get_error_data(),
1490 - ] );
1491 - }
1492 - }
1493 -
1494 - /**
1495 - * @return void
1496 - */
1497 - public function add_wc_scripts(): void {
1498 - wp_enqueue_script( 'elementor-ai-unify-product-images',
1499 - $this->get_js_assets_url( 'ai-unify-product-images' ),
1500 - [
1501 - 'jquery',
1502 - 'elementor-v2-ui',
1503 - 'elementor-v2-icons',
1504 - 'wp-components',
1505 - 'elementor-common',
1506 - ],
1507 - ELEMENTOR_VERSION,
1508 - true
1509 - );
1510 -
1511 - wp_localize_script(
1512 - 'elementor-ai-unify-product-images',
1513 - 'UnifyProductImagesConfig',
1514 - [
1515 - 'get_product_images_url' => admin_url( 'admin-ajax.php' ),
1516 - 'set_product_images_url' => admin_url( 'admin-ajax.php' ),
1517 - 'nonce' => wp_create_nonce( 'elementor-ai-unify-product-images_nonce' ),
1518 - 'placeholder' => ELEMENTOR_ASSETS_URL . 'images/app/ai/product-image-unification-example.gif?' . ELEMENTOR_VERSION,
1519 - 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
1520 - 'connect_url' => $this->get_ai_connect_url(),
1521 - ]
1522 - );
1523 -
1524 - add_filter( 'bulk_actions-edit-product', function ( $data ) {
1525 - return $this->add_products_bulk_action( $data );
1526 - });
1527 -
1528 - wp_set_script_translations( 'elementor-ai-unify-product-images', 'elementor' );
1529 - }
1530 -
1531 - /**
1532 - * @param $product
1533 - * @param int|null $image_to_remove
1534 - * @param int|null $image_to_add
1535 - * @return void
1536 - */
1537 - private function update_product_gallery( $product, ?int $image_to_remove, ?int $image_to_add ): void {
1538 - $gallery_image_ids = $product->get_gallery_image_ids();
1539 -
1540 - $index = array_search( $image_to_remove, $gallery_image_ids, true );
1541 - if ( false !== $index ) {
1542 - unset( $gallery_image_ids[ $index ] );
1543 - }
1544 -
1545 - if ( ! in_array( $image_to_add, $gallery_image_ids, true ) ) {
1546 - $gallery_image_ids[] = $image_to_add;
1547 - }
1548 -
1549 - $product->set_gallery_image_ids( $gallery_image_ids );
1550 - $product->save();
1551 - }
1552 -
1553 - private function should_display_create_with_ai_banner() {
1554 - $elementor_pages = new \WP_Query( [
1555 - 'post_type' => 'page',
1556 - 'post_status' => 'publish',
1557 - 'fields' => 'ids',
1558 - 'posts_per_page' => self::MIN_PAGES_FOR_CREATE_WITH_AI_BANNER + 1,
1559 - ] );
1560 -
1561 - if ( $elementor_pages->post_count > self::MIN_PAGES_FOR_CREATE_WITH_AI_BANNER ) {
1562 - return false;
1563 - }
1564 -
1565 - if ( Utils::is_custom_kit_applied() ) {
1566 - return false;
1567 - }
1568 -
1569 - return true;
1570 - }
1571 -
1572 - private function get_create_with_ai_banner_data() {
1573 - return [
1574 - 'title' => 'Create and launch your site faster with AI',
1575 - 'description' => 'Share your vision with our AI Chat and watch as it becomes a brief, sitemap, and wireframes in minutes:',
1576 - 'input_placeholder' => 'Start describing the site you want to create...',
1577 - 'button_title' => 'Create with AI',
1578 - 'button_cta_url' => 'http://planner.elementor.com/chat.html',
1579 - 'background_image' => ELEMENTOR_ASSETS_URL . 'images/app/ai/ai-site-creator-homepage-bg.svg',
1580 - 'utm_source' => 'editor-home',
1581 - 'utm_medium' => 'wp-dash',
1582 - 'utm_campaign' => 'generate-with-ai',
1583 - ];
1584 - }
1585 -
1586 - public function add_create_with_ai_banner_to_homescreen( $home_screen_data ) {
1587 - if ( $this->should_display_create_with_ai_banner() ) {
1588 - $home_screen_data['create_with_ai'] = $this->get_create_with_ai_banner_data();
1589 - } else {
1590 - $home_screen_data['create_with_ai'] = null;
1591 - }
1592 -
1593 - return $home_screen_data;
1594 923 }
1595 924 }