| @@ -1,11 +1,9 @@ | ||
| 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 | 6 | use Elementor\Modules\Ai\Feature_Intro\Product_Image_Unification_Intro; |
| 9 | 7 | use Elementor\Plugin; |
| 10 | 8 | use Elementor\Core\Utils\Collection; |
| 11 | 9 | use Elementor\Modules\Ai\Connect\Ai; |
| @@ -28,9 +26,8 @@ | ||
| 28 | 26 | self::HISTORY_TYPE_CODE, |
| 29 | 27 | self::HISTORY_TYPE_IMAGE, |
| 30 | 28 | self::HISTORY_TYPE_BLOCK, |
| 31 | 29 | ]; |
| 32 | - const MIN_PAGES_FOR_CREATE_WITH_AI_BANNER = 10; | |
| 33 | 30 | |
| 34 | 31 | public function get_name() { |
| 35 | 32 | return 'ai'; |
| 36 | 33 | } |
| @@ -37,20 +34,19 @@ | ||
| 37 | 34 | |
| 38 | 35 | public function __construct() { |
| 39 | 36 | parent::__construct(); |
| 40 | 37 | |
| 41 | - ( new SitePlannerConnect\Module() ); | |
| 42 | - | |
| 43 | 38 | if ( is_admin() ) { |
| 44 | 39 | ( new Preferences() )->register(); |
| 45 | - add_action( 'elementor/import-export/import-kit/runner/after-run', [ $this, 'handle_kit_install' ] ); | |
| 46 | 40 | } |
| 47 | 41 | |
| 48 | - if ( ! $this->is_ai_enabled() ) { | |
| 42 | + if ( ! Plugin::$instance->experiments->is_feature_active( 'container' ) ) { | |
| 49 | 43 | return; |
| 50 | 44 | } |
| 51 | 45 | |
| 52 | - add_filter( 'elementor/core/admin/homescreen', [ $this, 'add_create_with_ai_banner_to_homescreen' ] ); | |
| 46 | + if ( ! Preferences::is_ai_enabled( get_current_user_id() ) ) { | |
| 47 | + return; | |
| 48 | + } | |
| 53 | 49 | |
| 54 | 50 | add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) { |
| 55 | 51 | $connect_module->register_app( 'ai', Ai::get_class_name() ); |
| 56 | 52 | } ); |
| @@ -83,10 +79,8 @@ | ||
| 83 | 79 | 'ai_get_history' => [ $this, 'ajax_ai_get_history' ], |
| 84 | 80 | 'ai_delete_history_item' => [ $this, 'ajax_ai_delete_history_item' ], |
| 85 | 81 | 'ai_toggle_favorite_history_item' => [ $this, 'ajax_ai_toggle_favorite_history_item' ], |
| 86 | 82 | 'ai_get_product_image_unification' => [ $this, 'ajax_ai_get_product_image_unification' ], |
| 87 | - 'ai_get_animation' => [ $this, 'ajax_ai_get_animation' ], | |
| 88 | - 'ai_get_image_to_image_isolate_objects' => [ $this, 'ajax_ai_get_product_image_unification' ], | |
| 89 | 83 | ]; |
| 90 | 84 | |
| 91 | 85 | foreach ( $handlers as $tag => $callback ) { |
| 92 | 86 | $ajax->register_ajax_action( $tag, $callback ); |
| @@ -162,126 +156,10 @@ | ||
| 162 | 156 | |
| 163 | 157 | add_filter( 'elementor/document/save/data', function ( $data ) { |
| 164 | 158 | return $this->remove_temporary_containers( $data ); |
| 165 | 159 | } ); |
| 166 | - | |
| 167 | - add_action( 'elementor/element/common/section_effects/after_section_start', [ $this, 'register_ai_motion_effect_control' ], 10, 1 ); | |
| 168 | - add_action( 'elementor/element/container/section_effects/after_section_start', [ $this, 'register_ai_motion_effect_control' ], 10, 1 ); | |
| 169 | - add_action( 'elementor/element/common/_section_transform/after_section_end', [ $this, 'register_ai_hover_effect_control' ], 10, 1 ); | |
| 170 | - add_action( 'elementor/element/container/_section_transform/after_section_end', [ $this, 'register_ai_hover_effect_control' ], 10, 1 ); | |
| 171 | 160 | } |
| 172 | 161 | |
| 173 | - public function is_ai_enabled() { | |
| 174 | - if ( ! Plugin::$instance->experiments->is_feature_active( 'container' ) ) { | |
| 175 | - return false; | |
| 176 | - } | |
| 177 | - | |
| 178 | - return Preferences::is_ai_enabled( get_current_user_id() ); | |
| 179 | - } | |
| 180 | - | |
| 181 | - public function handle_kit_install( $imported_data ) { | |
| 182 | - if ( ! $this->is_ai_enabled() ) { | |
| 183 | - return; | |
| 184 | - } | |
| 185 | - | |
| 186 | - if ( ! isset( $imported_data['status'] ) || 'success' !== $imported_data['status'] ) { | |
| 187 | - return; | |
| 188 | - } | |
| 189 | - | |
| 190 | - if ( ! isset( $imported_data['runner'] ) || 'site-settings' !== $imported_data['runner'] ) { | |
| 191 | - return; | |
| 192 | - } | |
| 193 | - | |
| 194 | - if ( ! isset( $imported_data['configData']['lastImportedSession']['instance_data']['site_settings']['settings']['ai'] ) ) { | |
| 195 | - return; | |
| 196 | - } | |
| 197 | - | |
| 198 | - $is_connected = $this->get_ai_app()->is_connected() && User::get_introduction_meta( 'ai_get_started' ); | |
| 199 | - | |
| 200 | - if ( ! $is_connected ) { | |
| 201 | - return; | |
| 202 | - } | |
| 203 | - | |
| 204 | - $last_imported_session = $imported_data['configData']['lastImportedSession']; | |
| 205 | - $imported_ai_data = $last_imported_session['instance_data']['site_settings']['settings']['ai']; | |
| 206 | - | |
| 207 | - $this->get_ai_app()->send_event( [ | |
| 208 | - 'name' => 'kit_installed', | |
| 209 | - 'data' => $imported_ai_data, | |
| 210 | - 'client' => [ | |
| 211 | - 'name' => 'elementor', | |
| 212 | - 'version' => ELEMENTOR_VERSION, | |
| 213 | - 'session_id' => $last_imported_session['session_id'], | |
| 214 | - ], | |
| 215 | - ] ); | |
| 216 | - } | |
| 217 | - | |
| 218 | - public function register_ai_hover_effect_control( Element_Base $element ) { | |
| 219 | - if ( ! $element->get_controls( 'ai_hover_animation' ) ) { | |
| 220 | - $element->add_control( | |
| 221 | - 'ai_hover_animation', | |
| 222 | - [ | |
| 223 | - 'tabs_wrapper' => '_tabs_positioning', | |
| 224 | - 'inner_tab' => '_tab_positioning_hover', | |
| 225 | - 'label' => esc_html__( 'Animate With AI', 'elementor' ), | |
| 226 | - 'type' => Controls_Manager::RAW_HTML, | |
| 227 | - 'raw' => ' | |
| 228 | -<style> | |
| 229 | - .elementor-control-ai_hover_animation .elementor-control-content { | |
| 230 | - display: flex; | |
| 231 | - flex-direction: row; | |
| 232 | - justify-content: space-between; | |
| 233 | - align-items: center; | |
| 234 | - } | |
| 235 | - .elementor-control-ai_hover_animation .elementor-control-raw-html { | |
| 236 | - display: none; | |
| 237 | - } | |
| 238 | -</style>', | |
| 239 | - 'render_type' => 'none', | |
| 240 | - 'ai' => [ | |
| 241 | - 'active' => true, | |
| 242 | - 'type' => 'hover_animation', | |
| 243 | - ], | |
| 244 | - ], | |
| 245 | - [ | |
| 246 | - 'position' => [ | |
| 247 | - 'of' => '_transform_rotate_popover_hover', | |
| 248 | - 'type' => 'control', | |
| 249 | - 'at' => 'before', | |
| 250 | - ], | |
| 251 | - ] | |
| 252 | - ); | |
| 253 | - } | |
| 254 | - } | |
| 255 | - public function register_ai_motion_effect_control( $element ) { | |
| 256 | - if ( Utils::has_pro() && ! $element->get_controls( 'ai_animation' ) ) { | |
| 257 | - $element->add_control( | |
| 258 | - 'ai_animation', | |
| 259 | - [ | |
| 260 | - 'label' => esc_html__( 'Animate With AI', 'elementor' ), | |
| 261 | - 'type' => Controls_Manager::RAW_HTML, | |
| 262 | - 'raw' => ' | |
| 263 | - <style> | |
| 264 | - .elementor-control-ai_animation .elementor-control-content { | |
| 265 | - display: flex; | |
| 266 | - flex-direction: row; | |
| 267 | - justify-content: space-between; | |
| 268 | - align-items: center; | |
| 269 | - } | |
| 270 | - .elementor-control-ai_animation .elementor-control-raw-html { | |
| 271 | - display: none; | |
| 272 | - } | |
| 273 | - </style>', | |
| 274 | - 'render_type' => 'none', | |
| 275 | - 'ai' => [ | |
| 276 | - 'active' => true, | |
| 277 | - 'type' => 'animation', | |
| 278 | - ], | |
| 279 | - ] | |
| 280 | - ); | |
| 281 | - } | |
| 282 | - } | |
| 283 | - | |
| 284 | 162 | private function get_current_screen() { |
| 285 | 163 | $is_wc = class_exists( 'WooCommerce' ) && post_type_exists( 'product' ); |
| 286 | 164 | if ( ! $is_wc ) { |
| 287 | 165 | return 'other'; |
| @@ -306,8 +184,9 @@ | ||
| 306 | 184 | return; |
| 307 | 185 | } |
| 308 | 186 | |
| 309 | 187 | $this->add_wc_scripts(); |
| 188 | + | |
| 310 | 189 | } |
| 311 | 190 | |
| 312 | 191 | public function enqueue_ai_single_product_page_scripts() { |
| 313 | 192 | if ( 'wc-single-product' !== $this->get_current_screen() ) { |
| @@ -314,8 +193,9 @@ | ||
| 314 | 193 | return; |
| 315 | 194 | } |
| 316 | 195 | |
| 317 | 196 | $this->add_wc_scripts(); |
| 197 | + | |
| 318 | 198 | } |
| 319 | 199 | |
| 320 | 200 | private function add_products_bulk_action( $bulk_actions ) { |
| 321 | 201 | $bulk_actions['elementor-ai-unify-product-images'] = __( 'Unify with Elementor AI', 'elementor' ); |
| @@ -414,8 +294,9 @@ | ||
| 414 | 294 | } |
| 415 | 295 | |
| 416 | 296 | wp_send_json_success( [ |
| 417 | 297 | 'message' => __( 'Image added successfully', 'elementor' ), |
| 298 | + 'refresh' => true, | |
| 418 | 299 | ] ); |
| 419 | 300 | } |
| 420 | 301 | |
| 421 | 302 | public function enqueue_ai_media_library_upload_screen() { |
| @@ -476,8 +357,13 @@ | ||
| 476 | 357 | 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ), |
| 477 | 358 | 'connect_url' => $this->get_ai_connect_url(), |
| 478 | 359 | ]; |
| 479 | 360 | |
| 361 | + if ( $this->get_ai_app()->is_connected() ) { | |
| 362 | + // Use a cached version, don't call the API on every editor load. | |
| 363 | + $config['usage'] = $this->get_ai_app()->get_cached_usage(); | |
| 364 | + } | |
| 365 | + | |
| 480 | 366 | wp_localize_script( |
| 481 | 367 | 'elementor-ai', |
| 482 | 368 | 'ElementorAiConfig', |
| 483 | 369 | $config |
| @@ -508,9 +394,9 @@ | ||
| 508 | 394 | wp_set_script_translations( 'elementor-ai-layout', 'elementor' ); |
| 509 | 395 | } |
| 510 | 396 | |
| 511 | 397 | private function remove_temporary_containers( $data ) { |
| 512 | - if ( empty( $data['elements'] ) || ! is_array( $data['elements'] ) ) { | |
| 398 | + if ( empty( $data['elements'] ) ) { | |
| 513 | 399 | return $data; |
| 514 | 400 | } |
| 515 | 401 | |
| 516 | 402 | // If for some reason the document has been saved during an AI Layout session, |
| @@ -596,10 +482,12 @@ | ||
| 596 | 482 | if ( $document->is_built_with_elementor() ) { |
| 597 | 483 | if ( ! $document->is_editable_by_current_user() ) { |
| 598 | 484 | throw new \Exception( 'Access denied' ); |
| 599 | 485 | } |
| 600 | - } elseif ( ! current_user_can( 'edit_post', $editor_post_id ) ) { | |
| 486 | + } else { | |
| 487 | + if ( ! current_user_can( 'edit_post', $editor_post_id ) ) { | |
| 601 | 488 | throw new \Exception( 'Access denied' ); |
| 489 | + } | |
| 602 | 490 | } |
| 603 | 491 | } |
| 604 | 492 | |
| 605 | 493 | public function ajax_ai_get_image_prompt_enhancer( $data ) { |
| @@ -705,9 +593,9 @@ | ||
| 705 | 593 | 'usage' => $result['usage'], |
| 706 | 594 | ]; |
| 707 | 595 | } |
| 708 | 596 | |
| 709 | - private function get_ai_app(): Ai { | |
| 597 | + private function get_ai_app() : Ai { | |
| 710 | 598 | return Plugin::$instance->common->get_component( 'connect' )->get_app( 'ai' ); |
| 711 | 599 | } |
| 712 | 600 | |
| 713 | 601 | private function get_request_context( $data ) { |
| @@ -1020,8 +908,12 @@ | ||
| 1020 | 908 | if ( empty( $data['payload']['settings'] ) ) { |
| 1021 | 909 | throw new \Exception( 'Missing prompt settings' ); |
| 1022 | 910 | } |
| 1023 | 911 | |
| 912 | + if ( ! $app->is_connected() ) { | |
| 913 | + throw new \Exception( 'not_connected' ); | |
| 914 | + } | |
| 915 | + | |
| 1024 | 916 | if ( empty( $data['payload']['mask'] ) ) { |
| 1025 | 917 | throw new \Exception( 'Missing Mask' ); |
| 1026 | 918 | } |
| 1027 | 919 | |
| @@ -1111,10 +1003,8 @@ | ||
| 1111 | 1003 | ]; |
| 1112 | 1004 | } |
| 1113 | 1005 | |
| 1114 | 1006 | public function ajax_ai_upload_image( $data ) { |
| 1115 | - $this->verify_upload_permissions( $data ); | |
| 1116 | - | |
| 1117 | 1007 | if ( empty( $data['image'] ) ) { |
| 1118 | 1008 | throw new \Exception( 'Missing image data' ); |
| 1119 | 1009 | } |
| 1120 | 1010 | |
| @@ -1126,14 +1016,14 @@ | ||
| 1126 | 1016 | |
| 1127 | 1017 | $image_data = $this->upload_image( $image['image_url'], $data['prompt'], $data['editor_post_id'] ); |
| 1128 | 1018 | |
| 1129 | 1019 | if ( is_wp_error( $image_data ) ) { |
| 1130 | - throw new \Exception( esc_html( $image_data->get_error_message() ) ); | |
| 1020 | + throw new \Exception( $image_data->get_error_message() ); | |
| 1131 | 1021 | } |
| 1132 | 1022 | |
| 1133 | 1023 | if ( ! empty( $image['use_gallery_image'] ) && ! empty( $image['id'] ) ) { |
| 1134 | - $app = $this->get_ai_app(); | |
| 1135 | - $app->set_used_gallery_image( $image['id'] ); | |
| 1024 | + $app = $this->get_ai_app(); | |
| 1025 | + $app->set_used_gallery_image( $image['id'] ); | |
| 1136 | 1026 | } |
| 1137 | 1027 | |
| 1138 | 1028 | return [ |
| 1139 | 1029 | 'image' => array_merge( $image_data, $data ), |
| @@ -1162,9 +1052,9 @@ | ||
| 1162 | 1052 | $message = $result->get_error_message(); |
| 1163 | 1053 | |
| 1164 | 1054 | if ( is_array( $message ) ) { |
| 1165 | 1055 | $message = implode( ', ', $message ); |
| 1166 | - throw new \Exception( esc_html( $message ) ); | |
| 1056 | + throw new \Exception( $message ); | |
| 1167 | 1057 | } |
| 1168 | 1058 | |
| 1169 | 1059 | $this->throw_on_error( $result ); |
| 1170 | 1060 | } |
| @@ -1312,24 +1202,10 @@ | ||
| 1312 | 1202 | if ( ! current_user_can( 'upload_files' ) ) { |
| 1313 | 1203 | throw new \Exception( 'Not Allowed to Upload images' ); |
| 1314 | 1204 | } |
| 1315 | 1205 | |
| 1316 | - $uploads_manager = new \Elementor\Core\Files\Uploads_Manager(); | |
| 1317 | - if ( $uploads_manager::are_unfiltered_uploads_enabled() ) { | |
| 1318 | - Plugin::$instance->uploads_manager->set_elementor_upload_state( true ); | |
| 1319 | - add_filter( 'wp_handle_sideload_prefilter', [ Plugin::$instance->uploads_manager, 'handle_elementor_upload' ] ); | |
| 1320 | - add_filter( 'image_sideload_extensions', function( $extensions ) { | |
| 1321 | - $extensions[] = 'svg'; | |
| 1322 | - return $extensions; | |
| 1323 | - }); | |
| 1324 | - } | |
| 1325 | - | |
| 1326 | 1206 | $attachment_id = media_sideload_image( $image_url, $parent_post_id, $image_title, 'id' ); |
| 1327 | 1207 | |
| 1328 | - if ( is_wp_error( $attachment_id ) ) { | |
| 1329 | - return new \WP_Error( 'upload_error', $attachment_id->get_error_message() ); | |
| 1330 | - } | |
| 1331 | - | |
| 1332 | 1208 | if ( ! empty( $attachment_id['error'] ) ) { |
| 1333 | 1209 | return new \WP_Error( 'upload_error', $attachment_id['error'] ); |
| 1334 | 1210 | } |
| 1335 | 1211 | |
| @@ -1334,10 +1210,10 @@ | ||
| 1334 | 1210 | } |
| 1335 | 1211 | |
| 1336 | 1212 | return [ |
| 1337 | 1213 | 'id' => $attachment_id, |
| 1338 | - 'url' => esc_url( wp_get_attachment_image_url( $attachment_id, 'full' ) ), | |
| 1339 | - 'alt' => esc_attr( $image_title ), | |
| 1214 | + 'url' => wp_get_attachment_image_url( $attachment_id, 'full' ), | |
| 1215 | + 'alt' => $image_title, | |
| 1340 | 1216 | 'source' => 'library', |
| 1341 | 1217 | ]; |
| 1342 | 1218 | } |
| 1343 | 1219 | |
| @@ -1361,9 +1237,9 @@ | ||
| 1361 | 1237 | |
| 1362 | 1238 | $result = $app->get_history_by_type( $type, $page, $limit, $context ); |
| 1363 | 1239 | |
| 1364 | 1240 | if ( is_wp_error( $result ) ) { |
| 1365 | - throw new \Exception( esc_html( $result->get_error_message() ) ); | |
| 1241 | + throw new \Exception( $result->get_error_message() ); | |
| 1366 | 1242 | } |
| 1367 | 1243 | |
| 1368 | 1244 | return $result; |
| 1369 | 1245 | } |
| @@ -1383,9 +1259,9 @@ | ||
| 1383 | 1259 | |
| 1384 | 1260 | $result = $app->delete_history_item( $data['id'], $context ); |
| 1385 | 1261 | |
| 1386 | 1262 | if ( is_wp_error( $result ) ) { |
| 1387 | - throw new \Exception( esc_html( $result->get_error_message() ) ); | |
| 1263 | + throw new \Exception( $result->get_error_message() ); | |
| 1388 | 1264 | } |
| 1389 | 1265 | |
| 1390 | 1266 | return []; |
| 1391 | 1267 | } |
| @@ -1405,9 +1281,9 @@ | ||
| 1405 | 1281 | |
| 1406 | 1282 | $result = $app->toggle_favorite_history_item( $data['id'], $context ); |
| 1407 | 1283 | |
| 1408 | 1284 | if ( is_wp_error( $result ) ) { |
| 1409 | - throw new \Exception( esc_html( $result->get_error_message() ) ); | |
| 1285 | + throw new \Exception( $result->get_error_message() ); | |
| 1410 | 1286 | } |
| 1411 | 1287 | |
| 1412 | 1288 | return []; |
| 1413 | 1289 | } |
| @@ -1412,25 +1288,23 @@ | ||
| 1412 | 1288 | return []; |
| 1413 | 1289 | } |
| 1414 | 1290 | |
| 1415 | 1291 | public function ajax_ai_get_product_image_unification( $data ): array { |
| 1416 | - if ( ! empty( $data['payload']['postId'] ) ) { | |
| 1417 | - $data['editor_post_id'] = $data['payload']['postId']; | |
| 1418 | - } | |
| 1292 | + $data['editor_post_id'] = $data['payload']['postId']; | |
| 1419 | 1293 | $this->verify_upload_permissions( $data ); |
| 1420 | 1294 | |
| 1421 | 1295 | $app = $this->get_ai_app(); |
| 1422 | 1296 | |
| 1423 | 1297 | if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) { |
| 1424 | - throw new \Exception( 'Missing Image' ); | |
| 1298 | + throw new \Exception( __( 'Missing Image', 'elementor' ) ); | |
| 1425 | 1299 | } |
| 1426 | 1300 | |
| 1427 | 1301 | if ( empty( $data['payload']['settings'] ) ) { |
| 1428 | - throw new \Exception( 'Missing prompt settings' ); | |
| 1302 | + throw new \Exception( __( 'Missing prompt settings', 'elementor' ) ); | |
| 1429 | 1303 | } |
| 1430 | 1304 | |
| 1431 | 1305 | if ( ! $app->is_connected() ) { |
| 1432 | - throw new \Exception( 'not_connected' ); | |
| 1306 | + throw new \Exception( __( 'not_connected', 'elementor' ) ); | |
| 1433 | 1307 | } |
| 1434 | 1308 | |
| 1435 | 1309 | $context = $this->get_request_context( $data ); |
| 1436 | 1310 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| @@ -1437,9 +1311,8 @@ | ||
| 1437 | 1311 | |
| 1438 | 1312 | $result = $app->get_unify_product_images( [ |
| 1439 | 1313 | 'promptSettings' => $data['payload']['settings'], |
| 1440 | 1314 | 'attachment_id' => $data['payload']['image']['id'], |
| 1441 | - 'featureIdentifier' => $data['payload']['featureIdentifier'] ?? '', | |
| 1442 | 1315 | ], $context, $request_ids ); |
| 1443 | 1316 | |
| 1444 | 1317 | $this->throw_on_error( $result ); |
| 1445 | 1318 | |
| @@ -1449,39 +1322,8 @@ | ||
| 1449 | 1322 | 'usage' => $result['usage'], |
| 1450 | 1323 | ]; |
| 1451 | 1324 | } |
| 1452 | 1325 | |
| 1453 | - public function ajax_ai_get_animation( $data ): array { | |
| 1454 | - $this->verify_upload_permissions( $data ); | |
| 1455 | - | |
| 1456 | - $app = $this->get_ai_app(); | |
| 1457 | - | |
| 1458 | - if ( empty( $data['payload']['prompt'] ) ) { | |
| 1459 | - throw new \Exception( 'Missing prompt' ); | |
| 1460 | - } | |
| 1461 | - | |
| 1462 | - if ( empty( $data['payload']['motionEffectType'] ) ) { | |
| 1463 | - throw new \Exception( 'Missing animation type' ); | |
| 1464 | - } | |
| 1465 | - | |
| 1466 | - if ( ! $app->is_connected() ) { | |
| 1467 | - throw new \Exception( 'not_connected' ); | |
| 1468 | - } | |
| 1469 | - | |
| 1470 | - $context = $this->get_request_context( $data ); | |
| 1471 | - | |
| 1472 | - $request_ids = $this->get_request_ids( $data['payload'] ); | |
| 1473 | - | |
| 1474 | - $result = $app->get_animation( $data, $context, $request_ids ); | |
| 1475 | - $this->throw_on_error( $result ); | |
| 1476 | - | |
| 1477 | - return [ | |
| 1478 | - 'text' => $result['text'], | |
| 1479 | - 'response_id' => $result['responseId'], | |
| 1480 | - 'usage' => $result['usage'], | |
| 1481 | - ]; | |
| 1482 | - } | |
| 1483 | - | |
| 1484 | 1326 | /** |
| 1485 | 1327 | * @param mixed $result |
| 1486 | 1328 | */ |
| 1487 | 1329 | private function throw_on_error( $result ): void { |
| @@ -1486,9 +1328,9 @@ | ||
| 1486 | 1328 | */ |
| 1487 | 1329 | private function throw_on_error( $result ): void { |
| 1488 | 1330 | if ( is_wp_error( $result ) ) { |
| 1489 | 1331 | wp_send_json_error( [ |
| 1490 | - 'message' => esc_html( $result->get_error_message() ), | |
| 1332 | + 'message' => $result->get_error_message(), | |
| 1491 | 1333 | 'extra_data' => $result->get_error_data(), |
| 1492 | 1334 | ] ); |
| 1493 | 1335 | } |
| 1494 | 1336 | } |
| @@ -1534,64 +1376,22 @@ | ||
| 1534 | 1376 | * @param $product |
| 1535 | 1377 | * @param int|null $image_to_remove |
| 1536 | 1378 | * @param int|null $image_to_add |
| 1537 | 1379 | * @return void |
| 1380 | + * @throws \Exception | |
| 1538 | 1381 | */ |
| 1539 | 1382 | private function update_product_gallery( $product, ?int $image_to_remove, ?int $image_to_add ): void { |
| 1540 | 1383 | $gallery_image_ids = $product->get_gallery_image_ids(); |
| 1541 | 1384 | |
| 1542 | - $index = array_search( $image_to_remove, $gallery_image_ids, true ); | |
| 1385 | + $index = array_search( $image_to_remove, $gallery_image_ids ); | |
| 1543 | 1386 | if ( false !== $index ) { |
| 1544 | 1387 | unset( $gallery_image_ids[ $index ] ); |
| 1545 | 1388 | } |
| 1546 | 1389 | |
| 1547 | - if ( ! in_array( $image_to_add, $gallery_image_ids, true ) ) { | |
| 1390 | + if ( ! in_array( $image_to_add, $gallery_image_ids ) ) { | |
| 1548 | 1391 | $gallery_image_ids[] = $image_to_add; |
| 1549 | 1392 | } |
| 1550 | 1393 | |
| 1551 | 1394 | $product->set_gallery_image_ids( $gallery_image_ids ); |
| 1552 | 1395 | $product->save(); |
| 1553 | - } | |
| 1554 | - | |
| 1555 | - private function should_display_create_with_ai_banner() { | |
| 1556 | - $elementor_pages = new \WP_Query( [ | |
| 1557 | - 'post_type' => 'page', | |
| 1558 | - 'post_status' => 'publish', | |
| 1559 | - 'fields' => 'ids', | |
| 1560 | - 'posts_per_page' => self::MIN_PAGES_FOR_CREATE_WITH_AI_BANNER + 1, | |
| 1561 | - ] ); | |
| 1562 | - | |
| 1563 | - if ( $elementor_pages->post_count > self::MIN_PAGES_FOR_CREATE_WITH_AI_BANNER ) { | |
| 1564 | - return false; | |
| 1565 | - } | |
| 1566 | - | |
| 1567 | - if ( Utils::is_custom_kit_applied() ) { | |
| 1568 | - return false; | |
| 1569 | - } | |
| 1570 | - | |
| 1571 | - return true; | |
| 1572 | - } | |
| 1573 | - | |
| 1574 | - private function get_create_with_ai_banner_data() { | |
| 1575 | - return [ | |
| 1576 | - 'title' => 'Create and launch your site faster with AI', | |
| 1577 | - 'description' => 'Share your vision with our AI Chat and watch as it becomes a brief, sitemap, and wireframes in minutes:', | |
| 1578 | - 'input_placeholder' => 'Start describing the site you want to create...', | |
| 1579 | - 'button_title' => 'Create with AI', | |
| 1580 | - 'button_cta_url' => 'http://planner.elementor.com/chat.html', | |
| 1581 | - 'background_image' => ELEMENTOR_ASSETS_URL . 'images/app/ai/ai-site-creator-homepage-bg.svg', | |
| 1582 | - 'utm_source' => 'editor-home', | |
| 1583 | - 'utm_medium' => 'wp-dash', | |
| 1584 | - 'utm_campaign' => 'generate-with-ai', | |
| 1585 | - ]; | |
| 1586 | - } | |
| 1587 | - | |
| 1588 | - public function add_create_with_ai_banner_to_homescreen( $home_screen_data ) { | |
| 1589 | - if ( $this->should_display_create_with_ai_banner() ) { | |
| 1590 | - $home_screen_data['create_with_ai'] = $this->get_create_with_ai_banner_data(); | |
| 1591 | - } else { | |
| 1592 | - $home_screen_data['create_with_ai'] = null; | |
| 1593 | - } | |
| 1594 | - | |
| 1595 | - return $home_screen_data; | |
| 1596 | 1396 | } |
| 1597 | 1397 | } |