| @@ -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' ); |
| @@ -41,8 +39,21 @@ | ||
| 41 | 39 | ] |
| 42 | 40 | ); |
| 43 | 41 | } |
| 44 | 42 | |
| 43 | + public function get_cached_usage() { | |
| 44 | + $cache_key = 'elementor_ai_usage'; | |
| 45 | + $cache_time = 24 * HOUR_IN_SECONDS; | |
| 46 | + $usage = get_site_transient( $cache_key ); | |
| 47 | + | |
| 48 | + if ( ! $usage ) { | |
| 49 | + $usage = $this->get_usage(); | |
| 50 | + set_site_transient( $cache_key, $usage, $cache_time ); | |
| 51 | + } | |
| 52 | + | |
| 53 | + return $usage; | |
| 54 | + } | |
| 55 | + | |
| 45 | 56 | public function get_remote_config() { |
| 46 | 57 | return $this->ai_request( |
| 47 | 58 | 'GET', |
| 48 | 59 | 'remote-config/config', |
| @@ -52,55 +63,10 @@ | ||
| 52 | 63 | ] |
| 53 | 64 | ); |
| 54 | 65 | } |
| 55 | 66 | |
| 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 | 67 | /** |
| 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 | - * | |
| 68 | + * get_file_payload | |
| 103 | 69 | * @param $filename |
| 104 | 70 | * @param $file_type |
| 105 | 71 | * @param $file_path |
| 106 | 72 | * @param $boundary |
| @@ -253,10 +219,9 @@ | ||
| 253 | 219 | ); |
| 254 | 220 | } |
| 255 | 221 | |
| 256 | 222 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 223 | + * get_image_prompt_enhanced | |
| 259 | 224 | * @param $prompt |
| 260 | 225 | * |
| 261 | 226 | * @return mixed|\WP_Error |
| 262 | 227 | */ |
| @@ -329,10 +294,9 @@ | ||
| 329 | 294 | ); |
| 330 | 295 | } |
| 331 | 296 | |
| 332 | 297 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 298 | + * get_text_to_image | |
| 335 | 299 | * @param $prompt |
| 336 | 300 | * @param $prompt_settings |
| 337 | 301 | * |
| 338 | 302 | * @return mixed|\WP_Error |
| @@ -356,43 +320,14 @@ | ||
| 356 | 320 | ); |
| 357 | 321 | } |
| 358 | 322 | |
| 359 | 323 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 324 | + * get_image_to_image | |
| 325 | + * @param $image_data | |
| 361 | 326 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 327 | * @return mixed|\WP_Error |
| 328 | + * @throws \Exception | |
| 366 | 329 | */ |
| 367 | - public function get_featured_image( $data, $context, $request_ids ) { | |
| 368 | - return $this->ai_request( | |
| 369 | - 'POST', | |
| 370 | - 'image/text-to-image/featured-image', | |
| 371 | - [ | |
| 372 | - self::PROMPT => $data['payload']['prompt'], | |
| 373 | - self::IMAGE_TYPE => $data['payload']['settings'][ self::IMAGE_TYPE ] . '/' . $data['payload']['settings'][ self::STYLE_PRESET ], | |
| 374 | - self::ASPECT_RATIO => $data['payload']['settings'][ self::ASPECT_RATIO ], | |
| 375 | - 'context' => wp_json_encode( $context ), | |
| 376 | - 'ids' => $request_ids, | |
| 377 | - 'api_version' => ELEMENTOR_VERSION, | |
| 378 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 379 | - ], | |
| 380 | - false, | |
| 381 | - '', | |
| 382 | - 'json' | |
| 383 | - ); | |
| 384 | - } | |
| 385 | - | |
| 386 | - /** | |
| 387 | - * Get Image To Image get_image_to_image | |
| 388 | - * | |
| 389 | - * @param $image_data | |
| 390 | - * @param $context | |
| 391 | - * @param $request_ids | |
| 392 | - * @return mixed|\WP_Error | |
| 393 | - * @throws \Exception If image file not found. | |
| 394 | - */ | |
| 395 | 330 | public function get_image_to_image( $image_data, $context, $request_ids ) { |
| 396 | 331 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 332 | |
| 398 | 333 | if ( ! $image_file ) { |
| @@ -418,85 +353,14 @@ | ||
| 418 | 353 | |
| 419 | 354 | return $result; |
| 420 | 355 | } |
| 421 | 356 | |
| 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 | 357 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 358 | + * get_image_to_image_upscale | |
| 359 | + * @param $image_data | |
| 493 | 360 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 361 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 362 | + * @throws \Exception | |
| 499 | 363 | */ |
| 500 | 364 | public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { |
| 501 | 365 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 366 | |
| @@ -521,15 +385,13 @@ | ||
| 521 | 385 | return $result; |
| 522 | 386 | } |
| 523 | 387 | |
| 524 | 388 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 389 | + * get_image_to_image_remove_background | |
| 390 | + * @param $image_data | |
| 526 | 391 | * |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | 392 | * @return mixed|\WP_Error |
| 531 | - * @throws \Exception If image file not found. | |
| 393 | + * @throws \Exception | |
| 532 | 394 | */ |
| 533 | 395 | public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { |
| 534 | 396 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 535 | 397 | |
| @@ -553,15 +415,13 @@ | ||
| 553 | 415 | return $result; |
| 554 | 416 | } |
| 555 | 417 | |
| 556 | 418 | /** |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 419 | + * get_image_to_image_remove_text | |
| 420 | + * @param $image_data | |
| 558 | 421 | * |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | 422 | * @return mixed|\WP_Error |
| 563 | - * @throws \Exception If image file not found. | |
| 423 | + * @throws \Exception | |
| 564 | 424 | */ |
| 565 | 425 | public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { |
| 566 | 426 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 567 | 427 | |
| @@ -586,11 +446,10 @@ | ||
| 586 | 446 | return $result; |
| 587 | 447 | } |
| 588 | 448 | |
| 589 | 449 | /** |
| 590 | - * Store Temp File store_temp_file | |
| 450 | + * store_temp_file | |
| 591 | 451 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 452 | * @param $file_content |
| 594 | 453 | * @param $file_ext |
| 595 | 454 | * |
| 596 | 455 | * @return string |
| @@ -607,15 +466,13 @@ | ||
| 607 | 466 | return $temp_file; |
| 608 | 467 | } |
| 609 | 468 | |
| 610 | 469 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 470 | + * get_image_to_image_out_painting | |
| 471 | + * @param $image_data | |
| 612 | 472 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 473 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 474 | + * @throws \Exception | |
| 618 | 475 | */ |
| 619 | 476 | public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { |
| 620 | 477 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 478 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| @@ -651,15 +508,13 @@ | ||
| 651 | 508 | return $result; |
| 652 | 509 | } |
| 653 | 510 | |
| 654 | 511 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 512 | + * get_image_to_image_mask | |
| 513 | + * @param $image_data | |
| 656 | 514 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 515 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 516 | + * @throws \Exception | |
| 662 | 517 | */ |
| 663 | 518 | public function get_image_to_image_mask( $image_data, $context, $request_ids ) { |
| 664 | 519 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 520 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| @@ -698,48 +553,9 @@ | ||
| 698 | 553 | ); |
| 699 | 554 | |
| 700 | 555 | return $result; |
| 701 | 556 | } |
| 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 | 557 | |
| 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 | 558 | public function generate_layout( $data, $context ) { |
| 743 | 559 | $endpoint = 'generate/layout'; |
| 744 | 560 | |
| 745 | 561 | $body = [ |
| @@ -771,9 +587,8 @@ | ||
| 771 | 587 | |
| 772 | 588 | $html = wp_json_encode( $attachment['content'] ); |
| 773 | 589 | |
| 774 | 590 | $body['html'] = $html; |
| 775 | - $body['htmlFetchedUrl'] = $attachment['label']; | |
| 776 | 591 | |
| 777 | 592 | break; |
| 778 | 593 | } |
| 779 | 594 | } |
| @@ -779,9 +594,9 @@ | ||
| 779 | 594 | } |
| 780 | 595 | |
| 781 | 596 | $context['currentContext'] = $data['currentContext']; |
| 782 | 597 | $context['features'] = [ |
| 783 | - 'supportedFeatures' => [ 'Taxonomy' ], | |
| 598 | + 'supportedFeatures' => [], | |
| 784 | 599 | ]; |
| 785 | 600 | |
| 786 | 601 | if ( ElementorUtils::has_pro() ) { |
| 787 | 602 | $context['features']['subscriptions'] = [ 'Pro' ]; |
| @@ -786,20 +601,24 @@ | ||
| 786 | 601 | if ( ElementorUtils::has_pro() ) { |
| 787 | 602 | $context['features']['subscriptions'] = [ 'Pro' ]; |
| 788 | 603 | } |
| 789 | 604 | |
| 790 | - if ( Plugin::instance()->experiments->is_feature_active( 'container' ) ) { | |
| 605 | + if ( Plugin::$instance->experiments->is_feature_active( 'container_grid' ) ) { | |
| 606 | + $context['features']['supportedFeatures'][] = 'Grid'; | |
| 607 | + } | |
| 608 | + | |
| 609 | + if ( Plugin::instance()->experiments->get_active_features()['nested-elements'] ) { | |
| 791 | 610 | $context['features']['supportedFeatures'][] = 'Nested'; |
| 792 | 611 | } |
| 793 | 612 | |
| 613 | + if ( Plugin::instance()->experiments->get_active_features()['taxonomy-filter'] ) { | |
| 614 | + $context['features']['supportedFeatures'][] = 'Taxonomy'; | |
| 615 | + } | |
| 616 | + | |
| 794 | 617 | if ( Plugin::instance()->experiments->get_active_features()['mega-menu'] ) { |
| 795 | 618 | $context['features']['supportedFeatures'][] = 'MegaMenu'; |
| 796 | 619 | } |
| 797 | 620 | |
| 798 | - if ( class_exists( 'WC' ) ) { | |
| 799 | - $context['features']['supportedFeatures'][] = 'WooCommerce'; | |
| 800 | - } | |
| 801 | - | |
| 802 | 621 | $metadata = [ |
| 803 | 622 | 'context' => $context, |
| 804 | 623 | 'api_version' => ELEMENTOR_VERSION, |
| 805 | 624 | 'site_lang' => get_bloginfo( 'language' ), |
| @@ -872,34 +691,15 @@ | ||
| 872 | 691 | ); |
| 873 | 692 | } |
| 874 | 693 | |
| 875 | 694 | public function toggle_favorite_history_item( $id, $context = [] ) { |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | 695 | return $this->ai_request( |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 696 | + 'POST', sprintf( 'history/%s/favorite', $id ), | |
| 879 | 697 | [ |
| 880 | 698 | 'context' => wp_json_encode( $context ), |
| 881 | 699 | 'api_version' => ELEMENTOR_VERSION, |
| 882 | 700 | 'site_lang' => get_bloginfo( 'language' ), |
| 883 | 701 | ] |
| 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 | 702 | ); |
| 903 | 703 | } |
| 904 | 704 | |
| 905 | 705 | protected function init() {} |