| @@ -28,9 +28,8 @@ | ||
| 28 | 28 | self::HISTORY_TYPE_CODE, |
| 29 | 29 | self::HISTORY_TYPE_IMAGE, |
| 30 | 30 | self::HISTORY_TYPE_BLOCK, |
| 31 | 31 | ]; |
| 32 | - const MIN_PAGES_FOR_CREATE_WITH_AI_BANNER = 10; | |
| 33 | 32 | |
| 34 | 33 | public function get_name() { |
| 35 | 34 | return 'ai'; |
| 36 | 35 | } |
| @@ -48,10 +47,8 @@ | ||
| 48 | 47 | if ( ! $this->is_ai_enabled() ) { |
| 49 | 48 | return; |
| 50 | 49 | } |
| 51 | 50 | |
| 52 | - add_filter( 'elementor/core/admin/homescreen', [ $this, 'add_create_with_ai_banner_to_homescreen' ] ); | |
| 53 | - | |
| 54 | 51 | add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) { |
| 55 | 52 | $connect_module->register_app( 'ai', Ai::get_class_name() ); |
| 56 | 53 | } ); |
| 57 | 54 | |
| @@ -84,9 +81,8 @@ | ||
| 84 | 81 | 'ai_delete_history_item' => [ $this, 'ajax_ai_delete_history_item' ], |
| 85 | 82 | 'ai_toggle_favorite_history_item' => [ $this, 'ajax_ai_toggle_favorite_history_item' ], |
| 86 | 83 | 'ai_get_product_image_unification' => [ $this, 'ajax_ai_get_product_image_unification' ], |
| 87 | 84 | 'ai_get_animation' => [ $this, 'ajax_ai_get_animation' ], |
| 88 | - 'ai_get_image_to_image_isolate_objects' => [ $this, 'ajax_ai_get_product_image_unification' ], | |
| 89 | 85 | ]; |
| 90 | 86 | |
| 91 | 87 | foreach ( $handlers as $tag => $callback ) { |
| 92 | 88 | $ajax->register_ajax_action( $tag, $callback ); |
| @@ -306,8 +302,9 @@ | ||
| 306 | 302 | return; |
| 307 | 303 | } |
| 308 | 304 | |
| 309 | 305 | $this->add_wc_scripts(); |
| 306 | + | |
| 310 | 307 | } |
| 311 | 308 | |
| 312 | 309 | public function enqueue_ai_single_product_page_scripts() { |
| 313 | 310 | if ( 'wc-single-product' !== $this->get_current_screen() ) { |
| @@ -314,8 +311,9 @@ | ||
| 314 | 311 | return; |
| 315 | 312 | } |
| 316 | 313 | |
| 317 | 314 | $this->add_wc_scripts(); |
| 315 | + | |
| 318 | 316 | } |
| 319 | 317 | |
| 320 | 318 | private function add_products_bulk_action( $bulk_actions ) { |
| 321 | 319 | $bulk_actions['elementor-ai-unify-product-images'] = __( 'Unify with Elementor AI', 'elementor' ); |
| @@ -596,10 +594,12 @@ | ||
| 596 | 594 | if ( $document->is_built_with_elementor() ) { |
| 597 | 595 | if ( ! $document->is_editable_by_current_user() ) { |
| 598 | 596 | throw new \Exception( 'Access denied' ); |
| 599 | 597 | } |
| 600 | - } elseif ( ! current_user_can( 'edit_post', $editor_post_id ) ) { | |
| 598 | + } else { | |
| 599 | + if ( ! current_user_can( 'edit_post', $editor_post_id ) ) { | |
| 601 | 600 | throw new \Exception( 'Access denied' ); |
| 601 | + } | |
| 602 | 602 | } |
| 603 | 603 | } |
| 604 | 604 | |
| 605 | 605 | public function ajax_ai_get_image_prompt_enhancer( $data ) { |
| @@ -705,9 +705,9 @@ | ||
| 705 | 705 | 'usage' => $result['usage'], |
| 706 | 706 | ]; |
| 707 | 707 | } |
| 708 | 708 | |
| 709 | - private function get_ai_app(): Ai { | |
| 709 | + private function get_ai_app() : Ai { | |
| 710 | 710 | return Plugin::$instance->common->get_component( 'connect' )->get_app( 'ai' ); |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | private function get_request_context( $data ) { |
| @@ -1020,8 +1020,12 @@ | ||
| 1020 | 1020 | if ( empty( $data['payload']['settings'] ) ) { |
| 1021 | 1021 | throw new \Exception( 'Missing prompt settings' ); |
| 1022 | 1022 | } |
| 1023 | 1023 | |
| 1024 | + if ( ! $app->is_connected() ) { | |
| 1025 | + throw new \Exception( 'not_connected' ); | |
| 1026 | + } | |
| 1027 | + | |
| 1024 | 1028 | if ( empty( $data['payload']['mask'] ) ) { |
| 1025 | 1029 | throw new \Exception( 'Missing Mask' ); |
| 1026 | 1030 | } |
| 1027 | 1031 | |
| @@ -1111,10 +1115,8 @@ | ||
| 1111 | 1115 | ]; |
| 1112 | 1116 | } |
| 1113 | 1117 | |
| 1114 | 1118 | public function ajax_ai_upload_image( $data ) { |
| 1115 | - $this->verify_upload_permissions( $data ); | |
| 1116 | - | |
| 1117 | 1119 | if ( empty( $data['image'] ) ) { |
| 1118 | 1120 | throw new \Exception( 'Missing image data' ); |
| 1119 | 1121 | } |
| 1120 | 1122 | |
| @@ -1312,24 +1314,10 @@ | ||
| 1312 | 1314 | if ( ! current_user_can( 'upload_files' ) ) { |
| 1313 | 1315 | throw new \Exception( 'Not Allowed to Upload images' ); |
| 1314 | 1316 | } |
| 1315 | 1317 | |
| 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 | 1318 | $attachment_id = media_sideload_image( $image_url, $parent_post_id, $image_title, 'id' ); |
| 1327 | 1319 | |
| 1328 | - if ( is_wp_error( $attachment_id ) ) { | |
| 1329 | - return new \WP_Error( 'upload_error', $attachment_id->get_error_message() ); | |
| 1330 | - } | |
| 1331 | - | |
| 1332 | 1320 | if ( ! empty( $attachment_id['error'] ) ) { |
| 1333 | 1321 | return new \WP_Error( 'upload_error', $attachment_id['error'] ); |
| 1334 | 1322 | } |
| 1335 | 1323 | |
| @@ -1412,25 +1400,23 @@ | ||
| 1412 | 1400 | return []; |
| 1413 | 1401 | } |
| 1414 | 1402 | |
| 1415 | 1403 | 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 | - } | |
| 1404 | + $data['editor_post_id'] = $data['payload']['postId']; | |
| 1419 | 1405 | $this->verify_upload_permissions( $data ); |
| 1420 | 1406 | |
| 1421 | 1407 | $app = $this->get_ai_app(); |
| 1422 | 1408 | |
| 1423 | 1409 | if ( empty( $data['payload']['image'] ) || empty( $data['payload']['image']['id'] ) ) { |
| 1424 | - throw new \Exception( 'Missing Image' ); | |
| 1410 | + throw new \Exception( esc_html__( 'Missing Image', 'elementor' ) ); | |
| 1425 | 1411 | } |
| 1426 | 1412 | |
| 1427 | 1413 | if ( empty( $data['payload']['settings'] ) ) { |
| 1428 | - throw new \Exception( 'Missing prompt settings' ); | |
| 1414 | + throw new \Exception( esc_html__( 'Missing prompt settings', 'elementor' ) ); | |
| 1429 | 1415 | } |
| 1430 | 1416 | |
| 1431 | 1417 | if ( ! $app->is_connected() ) { |
| 1432 | - throw new \Exception( 'not_connected' ); | |
| 1418 | + throw new \Exception( esc_html__( 'not_connected', 'elementor' ) ); | |
| 1433 | 1419 | } |
| 1434 | 1420 | |
| 1435 | 1421 | $context = $this->get_request_context( $data ); |
| 1436 | 1422 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| @@ -1437,9 +1423,8 @@ | ||
| 1437 | 1423 | |
| 1438 | 1424 | $result = $app->get_unify_product_images( [ |
| 1439 | 1425 | 'promptSettings' => $data['payload']['settings'], |
| 1440 | 1426 | 'attachment_id' => $data['payload']['image']['id'], |
| 1441 | - 'featureIdentifier' => $data['payload']['featureIdentifier'] ?? '', | |
| 1442 | 1427 | ], $context, $request_ids ); |
| 1443 | 1428 | |
| 1444 | 1429 | $this->throw_on_error( $result ); |
| 1445 | 1430 | |
| @@ -1549,49 +1534,6 @@ | ||
| 1549 | 1534 | } |
| 1550 | 1535 | |
| 1551 | 1536 | $product->set_gallery_image_ids( $gallery_image_ids ); |
| 1552 | 1537 | $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 | 1538 | } |
| 1597 | 1539 | } |