| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | use Elementor\Utils as ElementorUtils; |
| 7 | 7 | use Elementor\Plugin; |
| 8 | 8 | |
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | - exit; // Exit if accessed directly. | |
| 10 | + exit; // Exit if accessed directly | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | class Ai extends Library { |
| 14 | 14 | const API_URL = 'https://my.elementor.com/api/v2/ai/'; |
| @@ -18,10 +18,8 @@ | ||
| 18 | 18 | const IMAGE_STRENGTH = 'image_strength'; |
| 19 | 19 | const ASPECT_RATIO = 'ratio'; |
| 20 | 20 | const IMAGE_RESOLUTION = 'image_resolution'; |
| 21 | 21 | |
| 22 | - const IMAGE_BACKGROUND_COLOR = 'background_color'; | |
| 23 | - | |
| 24 | 22 | const PROMPT = 'prompt'; |
| 25 | 23 | |
| 26 | 24 | public function get_title() { |
| 27 | 25 | return esc_html__( 'AI', 'elementor' ); |
| @@ -30,9 +28,9 @@ | ||
| 30 | 28 | protected function get_api_url() { |
| 31 | 29 | return static::API_URL . '/'; |
| 32 | 30 | } |
| 33 | 31 | |
| 34 | - public function get_usage() { | |
| 32 | + public function get_usage( $client_name, $client_session_id ) { | |
| 35 | 33 | return $this->ai_request( |
| 36 | 34 | 'POST', |
| 37 | 35 | 'status/check', |
| 38 | 36 | [ |
| @@ -37,8 +35,11 @@ | ||
| 37 | 35 | 'status/check', |
| 38 | 36 | [ |
| 39 | 37 | 'api_version' => ELEMENTOR_VERSION, |
| 40 | 38 | 'site_lang' => get_bloginfo( 'language' ), |
| 39 | + 'client_name' => esc_attr( $client_name ), | |
| 40 | + 'client_version' => ELEMENTOR_VERSION, | |
| 41 | + 'client_session_id' => esc_attr( $client_session_id ), | |
| 41 | 42 | ] |
| 42 | 43 | ); |
| 43 | 44 | } |
| 44 | 45 | |
| @@ -52,55 +53,10 @@ | ||
| 52 | 53 | ] |
| 53 | 54 | ); |
| 54 | 55 | } |
| 55 | 56 | |
| 56 | - public function get_remote_frontend_config( $data ) { | |
| 57 | - return $this->ai_request( | |
| 58 | - 'POST', | |
| 59 | - 'remote-config/frontend-config', | |
| 60 | - [ | |
| 61 | - 'client_name' => $data['payload']['client_name'], | |
| 62 | - 'client_version' => $data['payload']['client_version'], | |
| 63 | - 'client_session_id' => $data['payload']['client_session_id'], | |
| 64 | - | |
| 65 | - 'api_version' => ELEMENTOR_VERSION, | |
| 66 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 67 | - ], | |
| 68 | - false, | |
| 69 | - '', | |
| 70 | - 'json' | |
| 71 | - ); | |
| 72 | - } | |
| 73 | - | |
| 74 | 57 | /** |
| 75 | - * @param array $event_data { | |
| 76 | - * @type string $name | |
| 77 | - * @type array $data | |
| 78 | - * @type array $client { | |
| 79 | - * @type string $name | |
| 80 | - * @type string $version | |
| 81 | - * @type string $session_id | |
| 82 | - * } | |
| 83 | - * } | |
| 84 | - */ | |
| 85 | - public function send_event( array $event_data ): void { | |
| 86 | - $this->ai_request( | |
| 87 | - 'POST', | |
| 88 | - 'client-events/events', | |
| 89 | - [ | |
| 90 | - 'payload' => $event_data, | |
| 91 | - 'api_version' => ELEMENTOR_VERSION, | |
| 92 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 93 | - ], | |
| 94 | - false, | |
| 95 | - '', | |
| 96 | - 'json' | |
| 97 | - ); | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * Get file upload get_file_payload | |
| 102 | - * | |
| 58 | + * get_file_payload | |
| 103 | 59 | * @param $filename |
| 104 | 60 | * @param $file_type |
| 105 | 61 | * @param $file_path |
| 106 | 62 | * @param $boundary |
| @@ -253,10 +209,9 @@ | ||
| 253 | 209 | ); |
| 254 | 210 | } |
| 255 | 211 | |
| 256 | 212 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 213 | + * get_image_prompt_enhanced | |
| 259 | 214 | * @param $prompt |
| 260 | 215 | * |
| 261 | 216 | * @return mixed|\WP_Error |
| 262 | 217 | */ |
| @@ -329,10 +284,9 @@ | ||
| 329 | 284 | ); |
| 330 | 285 | } |
| 331 | 286 | |
| 332 | 287 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 288 | + * get_text_to_image | |
| 335 | 289 | * @param $prompt |
| 336 | 290 | * @param $prompt_settings |
| 337 | 291 | * |
| 338 | 292 | * @return mixed|\WP_Error |
| @@ -356,13 +310,12 @@ | ||
| 356 | 310 | ); |
| 357 | 311 | } |
| 358 | 312 | |
| 359 | 313 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 314 | + * get_featured_image | |
| 315 | + * @param $prompt | |
| 316 | + * @param $prompt_settings | |
| 361 | 317 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 318 | * @return mixed|\WP_Error |
| 366 | 319 | */ |
| 367 | 320 | public function get_featured_image( $data, $context, $request_ids ) { |
| 368 | 321 | return $this->ai_request( |
| @@ -383,15 +336,13 @@ | ||
| 383 | 336 | ); |
| 384 | 337 | } |
| 385 | 338 | |
| 386 | 339 | /** |
| 387 | - * Get Image To Image get_image_to_image | |
| 340 | + * get_image_to_image | |
| 341 | + * @param $image_data | |
| 388 | 342 | * |
| 389 | - * @param $image_data | |
| 390 | - * @param $context | |
| 391 | - * @param $request_ids | |
| 392 | 343 | * @return mixed|\WP_Error |
| 393 | - * @throws \Exception If image file not found. | |
| 344 | + * @throws \Exception | |
| 394 | 345 | */ |
| 395 | 346 | public function get_image_to_image( $image_data, $context, $request_ids ) { |
| 396 | 347 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 348 | |
| @@ -418,85 +369,14 @@ | ||
| 418 | 369 | |
| 419 | 370 | return $result; |
| 420 | 371 | } |
| 421 | 372 | |
| 422 | - | |
| 423 | - private function resizeImageIfNeeded( $original_url ) { | |
| 424 | - try { | |
| 425 | - $max_file_size = 4194304; | |
| 426 | - $current_size = filesize( $original_url ); | |
| 427 | - | |
| 428 | - if ( $current_size <= $max_file_size ) { | |
| 429 | - return $original_url; | |
| 430 | - } | |
| 431 | - | |
| 432 | - $image_editor = wp_get_image_editor( $original_url ); | |
| 433 | - | |
| 434 | - if ( is_wp_error( $image_editor ) ) { | |
| 435 | - return $original_url; | |
| 436 | - } | |
| 437 | - | |
| 438 | - $dimensions = $image_editor->get_size(); | |
| 439 | - $original_width = $dimensions['width']; | |
| 440 | - $original_height = $dimensions['height']; | |
| 441 | - | |
| 442 | - $scaling_factor = sqrt( $max_file_size / $current_size ); | |
| 443 | - | |
| 444 | - $new_width = (int) ( $original_width * $scaling_factor ); | |
| 445 | - $new_height = (int) ( $original_height * $scaling_factor ); | |
| 446 | - | |
| 447 | - $image_editor->resize( $new_width, $new_height, true ); | |
| 448 | - | |
| 449 | - $file_extension = pathinfo( $original_url, PATHINFO_EXTENSION ); | |
| 450 | - $temp_image = tempnam( sys_get_temp_dir(), 'resized_' ) . '.' . $file_extension; | |
| 451 | - | |
| 452 | - $image_editor->save( $temp_image ); | |
| 453 | - return $temp_image; | |
| 454 | - } catch ( \Exception $e ) { | |
| 455 | - return $original_url; | |
| 456 | - } | |
| 457 | - } | |
| 458 | - | |
| 459 | - public function get_unify_product_images( $image_data, $context, $request_ids ) { | |
| 460 | - $image_file = get_attached_file( $image_data['attachment_id'] ); | |
| 461 | - | |
| 462 | - if ( ! $image_file ) { | |
| 463 | - throw new \Exception( 'Image file not found' ); | |
| 464 | - } | |
| 465 | - | |
| 466 | - $final_path = $this->resizeImageIfNeeded( $image_file ); | |
| 467 | - | |
| 468 | - $result = $this->ai_request( | |
| 469 | - 'POST', | |
| 470 | - 'image/image-to-image/unify-product-images', | |
| 471 | - [ | |
| 472 | - 'aspectRatio' => $image_data['promptSettings'][ self::ASPECT_RATIO ], | |
| 473 | - 'backgroundColor' => $image_data['promptSettings'][ self::IMAGE_BACKGROUND_COLOR ], | |
| 474 | - 'featureIdentifier' => $image_data['featureIdentifier'], | |
| 475 | - 'context' => wp_json_encode( $context ), | |
| 476 | - 'ids' => $request_ids, | |
| 477 | - 'api_version' => ELEMENTOR_VERSION, | |
| 478 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 479 | - ], | |
| 480 | - $final_path, | |
| 481 | - 'image' | |
| 482 | - ); | |
| 483 | - | |
| 484 | - if ( $image_file !== $final_path ) { | |
| 485 | - unlink( $final_path ); | |
| 486 | - } | |
| 487 | - | |
| 488 | - return $result; | |
| 489 | - } | |
| 490 | - | |
| 491 | 373 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 374 | + * get_image_to_image_upscale | |
| 375 | + * @param $image_data | |
| 493 | 376 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 377 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 378 | + * @throws \Exception | |
| 499 | 379 | */ |
| 500 | 380 | public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { |
| 501 | 381 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 382 | |
| @@ -521,15 +401,13 @@ | ||
| 521 | 401 | return $result; |
| 522 | 402 | } |
| 523 | 403 | |
| 524 | 404 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 405 | + * get_image_to_image_remove_background | |
| 406 | + * @param $image_data | |
| 526 | 407 | * |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | 408 | * @return mixed|\WP_Error |
| 531 | - * @throws \Exception If image file not found. | |
| 409 | + * @throws \Exception | |
| 532 | 410 | */ |
| 533 | 411 | public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { |
| 534 | 412 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 535 | 413 | |
| @@ -553,15 +431,13 @@ | ||
| 553 | 431 | return $result; |
| 554 | 432 | } |
| 555 | 433 | |
| 556 | 434 | /** |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 435 | + * get_image_to_image_remove_text | |
| 436 | + * @param $image_data | |
| 558 | 437 | * |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | 438 | * @return mixed|\WP_Error |
| 563 | - * @throws \Exception If image file not found. | |
| 439 | + * @throws \Exception | |
| 564 | 440 | */ |
| 565 | 441 | public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { |
| 566 | 442 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 567 | 443 | |
| @@ -586,11 +462,10 @@ | ||
| 586 | 462 | return $result; |
| 587 | 463 | } |
| 588 | 464 | |
| 589 | 465 | /** |
| 590 | - * Store Temp File store_temp_file | |
| 466 | + * store_temp_file | |
| 591 | 467 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 468 | * @param $file_content |
| 594 | 469 | * @param $file_ext |
| 595 | 470 | * |
| 596 | 471 | * @return string |
| @@ -607,15 +482,13 @@ | ||
| 607 | 482 | return $temp_file; |
| 608 | 483 | } |
| 609 | 484 | |
| 610 | 485 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 486 | + * get_image_to_image_out_painting | |
| 487 | + * @param $image_data | |
| 612 | 488 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 489 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 490 | + * @throws \Exception | |
| 618 | 491 | */ |
| 619 | 492 | public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { |
| 620 | 493 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 494 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| @@ -651,15 +524,13 @@ | ||
| 651 | 524 | return $result; |
| 652 | 525 | } |
| 653 | 526 | |
| 654 | 527 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 528 | + * get_image_to_image_mask | |
| 529 | + * @param $image_data | |
| 656 | 530 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 531 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 532 | + * @throws \Exception | |
| 662 | 533 | */ |
| 663 | 534 | public function get_image_to_image_mask( $image_data, $context, $request_ids ) { |
| 664 | 535 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 536 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| @@ -698,48 +569,9 @@ | ||
| 698 | 569 | ); |
| 699 | 570 | |
| 700 | 571 | return $result; |
| 701 | 572 | } |
| 702 | - public function get_image_to_image_mask_cleanup( $image_data, $context, $request_ids ) { | |
| 703 | - $image_file = get_attached_file( $image_data['attachment_id'] ); | |
| 704 | - $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); | |
| 705 | 573 | |
| 706 | - if ( ! $image_file ) { | |
| 707 | - throw new \Exception( 'Image file not found' ); | |
| 708 | - } | |
| 709 | - | |
| 710 | - if ( ! $mask_file ) { | |
| 711 | - throw new \Exception( 'Mask file not found' ); | |
| 712 | - } | |
| 713 | - | |
| 714 | - $result = $this->ai_request( | |
| 715 | - 'POST', | |
| 716 | - 'image/image-to-image/cleanup', | |
| 717 | - [ | |
| 718 | - self::PROMPT => $image_data[ self::PROMPT ], | |
| 719 | - 'context' => wp_json_encode( $context ), | |
| 720 | - 'ids' => $request_ids, | |
| 721 | - 'api_version' => ELEMENTOR_VERSION, | |
| 722 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 723 | - 'image_base64' => $image_data['image_base64'], | |
| 724 | - ], | |
| 725 | - [ | |
| 726 | - [ | |
| 727 | - 'name' => 'image', | |
| 728 | - 'type' => 'image', | |
| 729 | - 'path' => $image_file, | |
| 730 | - ], | |
| 731 | - [ | |
| 732 | - 'name' => 'mask_image', | |
| 733 | - 'type' => 'image/svg+xml', | |
| 734 | - 'path' => $mask_file, | |
| 735 | - ], | |
| 736 | - ] | |
| 737 | - ); | |
| 738 | - | |
| 739 | - return $result; | |
| 740 | - } | |
| 741 | - | |
| 742 | 574 | public function generate_layout( $data, $context ) { |
| 743 | 575 | $endpoint = 'generate/layout'; |
| 744 | 576 | |
| 745 | 577 | $body = [ |
| @@ -771,9 +603,8 @@ | ||
| 771 | 603 | |
| 772 | 604 | $html = wp_json_encode( $attachment['content'] ); |
| 773 | 605 | |
| 774 | 606 | $body['html'] = $html; |
| 775 | - $body['htmlFetchedUrl'] = $attachment['label']; | |
| 776 | 607 | |
| 777 | 608 | break; |
| 778 | 609 | } |
| 779 | 610 | } |
| @@ -779,9 +610,9 @@ | ||
| 779 | 610 | } |
| 780 | 611 | |
| 781 | 612 | $context['currentContext'] = $data['currentContext']; |
| 782 | 613 | $context['features'] = [ |
| 783 | - 'supportedFeatures' => [ 'Taxonomy' ], | |
| 614 | + 'supportedFeatures' => [], | |
| 784 | 615 | ]; |
| 785 | 616 | |
| 786 | 617 | if ( ElementorUtils::has_pro() ) { |
| 787 | 618 | $context['features']['subscriptions'] = [ 'Pro' ]; |
| @@ -786,20 +617,24 @@ | ||
| 786 | 617 | if ( ElementorUtils::has_pro() ) { |
| 787 | 618 | $context['features']['subscriptions'] = [ 'Pro' ]; |
| 788 | 619 | } |
| 789 | 620 | |
| 621 | + if ( Plugin::$instance->experiments->is_feature_active( 'container_grid' ) ) { | |
| 622 | + $context['features']['supportedFeatures'][] = 'Grid'; | |
| 623 | + } | |
| 624 | + | |
| 790 | 625 | if ( Plugin::instance()->experiments->get_active_features()['nested-elements'] ) { |
| 791 | 626 | $context['features']['supportedFeatures'][] = 'Nested'; |
| 792 | 627 | } |
| 793 | 628 | |
| 629 | + if ( Plugin::instance()->experiments->get_active_features()['taxonomy-filter'] ) { | |
| 630 | + $context['features']['supportedFeatures'][] = 'Taxonomy'; | |
| 631 | + } | |
| 632 | + | |
| 794 | 633 | if ( Plugin::instance()->experiments->get_active_features()['mega-menu'] ) { |
| 795 | 634 | $context['features']['supportedFeatures'][] = 'MegaMenu'; |
| 796 | 635 | } |
| 797 | 636 | |
| 798 | - if ( class_exists( 'WC' ) ) { | |
| 799 | - $context['features']['supportedFeatures'][] = 'WooCommerce'; | |
| 800 | - } | |
| 801 | - | |
| 802 | 637 | $metadata = [ |
| 803 | 638 | 'context' => $context, |
| 804 | 639 | 'api_version' => ELEMENTOR_VERSION, |
| 805 | 640 | 'site_lang' => get_bloginfo( 'language' ), |
| @@ -872,34 +707,15 @@ | ||
| 872 | 707 | ); |
| 873 | 708 | } |
| 874 | 709 | |
| 875 | 710 | public function toggle_favorite_history_item( $id, $context = [] ) { |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | 711 | return $this->ai_request( |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 712 | + 'POST', sprintf( 'history/%s/favorite', $id ), | |
| 879 | 713 | [ |
| 880 | 714 | 'context' => wp_json_encode( $context ), |
| 881 | 715 | 'api_version' => ELEMENTOR_VERSION, |
| 882 | 716 | 'site_lang' => get_bloginfo( 'language' ), |
| 883 | 717 | ] |
| 884 | - ); | |
| 885 | - } | |
| 886 | - | |
| 887 | - public function get_animation( $data, $context, $request_ids ) { | |
| 888 | - return $this->ai_request( | |
| 889 | - 'POST', | |
| 890 | - 'text/get-motion-effect', | |
| 891 | - [ | |
| 892 | - 'prompt' => $data['payload']['prompt'], | |
| 893 | - 'motionEffectType' => $data['payload']['motionEffectType'], | |
| 894 | - 'context' => wp_json_encode( $context ), | |
| 895 | - 'ids' => $request_ids, | |
| 896 | - 'api_version' => ELEMENTOR_VERSION, | |
| 897 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 898 | - ], | |
| 899 | - false, | |
| 900 | - '', | |
| 901 | - 'json' | |
| 902 | 718 | ); |
| 903 | 719 | } |
| 904 | 720 | |
| 905 | 721 | protected function init() {} |