| @@ -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/'; |
| @@ -41,8 +41,21 @@ | ||
| 41 | 41 | ] |
| 42 | 42 | ); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | + public function get_cached_usage() { | |
| 46 | + $cache_key = 'elementor_ai_usage'; | |
| 47 | + $cache_time = 24 * HOUR_IN_SECONDS; | |
| 48 | + $usage = get_site_transient( $cache_key ); | |
| 49 | + | |
| 50 | + if ( ! $usage ) { | |
| 51 | + $usage = $this->get_usage(); | |
| 52 | + set_site_transient( $cache_key, $usage, $cache_time ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + return $usage; | |
| 56 | + } | |
| 57 | + | |
| 45 | 58 | public function get_remote_config() { |
| 46 | 59 | return $this->ai_request( |
| 47 | 60 | 'GET', |
| 48 | 61 | 'remote-config/config', |
| @@ -71,36 +84,9 @@ | ||
| 71 | 84 | ); |
| 72 | 85 | } |
| 73 | 86 | |
| 74 | 87 | /** |
| 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 | - * | |
| 88 | + * get_file_payload | |
| 103 | 89 | * @param $filename |
| 104 | 90 | * @param $file_type |
| 105 | 91 | * @param $file_path |
| 106 | 92 | * @param $boundary |
| @@ -253,10 +239,9 @@ | ||
| 253 | 239 | ); |
| 254 | 240 | } |
| 255 | 241 | |
| 256 | 242 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 243 | + * get_image_prompt_enhanced | |
| 259 | 244 | * @param $prompt |
| 260 | 245 | * |
| 261 | 246 | * @return mixed|\WP_Error |
| 262 | 247 | */ |
| @@ -329,10 +314,9 @@ | ||
| 329 | 314 | ); |
| 330 | 315 | } |
| 331 | 316 | |
| 332 | 317 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 318 | + * get_text_to_image | |
| 335 | 319 | * @param $prompt |
| 336 | 320 | * @param $prompt_settings |
| 337 | 321 | * |
| 338 | 322 | * @return mixed|\WP_Error |
| @@ -356,13 +340,12 @@ | ||
| 356 | 340 | ); |
| 357 | 341 | } |
| 358 | 342 | |
| 359 | 343 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 344 | + * get_featured_image | |
| 345 | + * @param $prompt | |
| 346 | + * @param $prompt_settings | |
| 361 | 347 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 348 | * @return mixed|\WP_Error |
| 366 | 349 | */ |
| 367 | 350 | public function get_featured_image( $data, $context, $request_ids ) { |
| 368 | 351 | return $this->ai_request( |
| @@ -383,15 +366,13 @@ | ||
| 383 | 366 | ); |
| 384 | 367 | } |
| 385 | 368 | |
| 386 | 369 | /** |
| 387 | - * Get Image To Image get_image_to_image | |
| 370 | + * get_image_to_image | |
| 371 | + * @param $image_data | |
| 388 | 372 | * |
| 389 | - * @param $image_data | |
| 390 | - * @param $context | |
| 391 | - * @param $request_ids | |
| 392 | 373 | * @return mixed|\WP_Error |
| 393 | - * @throws \Exception If image file not found. | |
| 374 | + * @throws \Exception | |
| 394 | 375 | */ |
| 395 | 376 | public function get_image_to_image( $image_data, $context, $request_ids ) { |
| 396 | 377 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 378 | |
| @@ -470,9 +451,8 @@ | ||
| 470 | 451 | 'image/image-to-image/unify-product-images', |
| 471 | 452 | [ |
| 472 | 453 | 'aspectRatio' => $image_data['promptSettings'][ self::ASPECT_RATIO ], |
| 473 | 454 | 'backgroundColor' => $image_data['promptSettings'][ self::IMAGE_BACKGROUND_COLOR ], |
| 474 | - 'featureIdentifier' => $image_data['featureIdentifier'], | |
| 475 | 455 | 'context' => wp_json_encode( $context ), |
| 476 | 456 | 'ids' => $request_ids, |
| 477 | 457 | 'api_version' => ELEMENTOR_VERSION, |
| 478 | 458 | 'site_lang' => get_bloginfo( 'language' ), |
| @@ -488,15 +468,13 @@ | ||
| 488 | 468 | return $result; |
| 489 | 469 | } |
| 490 | 470 | |
| 491 | 471 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 472 | + * get_image_to_image_upscale | |
| 473 | + * @param $image_data | |
| 493 | 474 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 475 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 476 | + * @throws \Exception | |
| 499 | 477 | */ |
| 500 | 478 | public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { |
| 501 | 479 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 480 | |
| @@ -521,15 +499,13 @@ | ||
| 521 | 499 | return $result; |
| 522 | 500 | } |
| 523 | 501 | |
| 524 | 502 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 503 | + * get_image_to_image_remove_background | |
| 504 | + * @param $image_data | |
| 526 | 505 | * |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | 506 | * @return mixed|\WP_Error |
| 531 | - * @throws \Exception If image file not found. | |
| 507 | + * @throws \Exception | |
| 532 | 508 | */ |
| 533 | 509 | public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { |
| 534 | 510 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 535 | 511 | |
| @@ -553,15 +529,13 @@ | ||
| 553 | 529 | return $result; |
| 554 | 530 | } |
| 555 | 531 | |
| 556 | 532 | /** |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 533 | + * get_image_to_image_remove_text | |
| 534 | + * @param $image_data | |
| 558 | 535 | * |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | 536 | * @return mixed|\WP_Error |
| 563 | - * @throws \Exception If image file not found. | |
| 537 | + * @throws \Exception | |
| 564 | 538 | */ |
| 565 | 539 | public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { |
| 566 | 540 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 567 | 541 | |
| @@ -586,11 +560,10 @@ | ||
| 586 | 560 | return $result; |
| 587 | 561 | } |
| 588 | 562 | |
| 589 | 563 | /** |
| 590 | - * Store Temp File store_temp_file | |
| 564 | + * store_temp_file | |
| 591 | 565 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 566 | * @param $file_content |
| 594 | 567 | * @param $file_ext |
| 595 | 568 | * |
| 596 | 569 | * @return string |
| @@ -607,15 +580,13 @@ | ||
| 607 | 580 | return $temp_file; |
| 608 | 581 | } |
| 609 | 582 | |
| 610 | 583 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 584 | + * get_image_to_image_out_painting | |
| 585 | + * @param $image_data | |
| 612 | 586 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 587 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 588 | + * @throws \Exception | |
| 618 | 589 | */ |
| 619 | 590 | public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { |
| 620 | 591 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 592 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| @@ -651,15 +622,13 @@ | ||
| 651 | 622 | return $result; |
| 652 | 623 | } |
| 653 | 624 | |
| 654 | 625 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 626 | + * get_image_to_image_mask | |
| 627 | + * @param $image_data | |
| 656 | 628 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 629 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 630 | + * @throws \Exception | |
| 662 | 631 | */ |
| 663 | 632 | public function get_image_to_image_mask( $image_data, $context, $request_ids ) { |
| 664 | 633 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 634 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| @@ -786,9 +755,9 @@ | ||
| 786 | 755 | if ( ElementorUtils::has_pro() ) { |
| 787 | 756 | $context['features']['subscriptions'] = [ 'Pro' ]; |
| 788 | 757 | } |
| 789 | 758 | |
| 790 | - if ( Plugin::instance()->experiments->is_feature_active( 'container' ) ) { | |
| 759 | + if ( Plugin::instance()->experiments->get_active_features()['nested-elements'] ) { | |
| 791 | 760 | $context['features']['supportedFeatures'][] = 'Nested'; |
| 792 | 761 | } |
| 793 | 762 | |
| 794 | 763 | if ( Plugin::instance()->experiments->get_active_features()['mega-menu'] ) { |
| @@ -872,34 +841,15 @@ | ||
| 872 | 841 | ); |
| 873 | 842 | } |
| 874 | 843 | |
| 875 | 844 | public function toggle_favorite_history_item( $id, $context = [] ) { |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | 845 | return $this->ai_request( |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 846 | + 'POST', sprintf( 'history/%s/favorite', $id ), | |
| 879 | 847 | [ |
| 880 | 848 | 'context' => wp_json_encode( $context ), |
| 881 | 849 | 'api_version' => ELEMENTOR_VERSION, |
| 882 | 850 | 'site_lang' => get_bloginfo( 'language' ), |
| 883 | 851 | ] |
| 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 | 852 | ); |
| 903 | 853 | } |
| 904 | 854 | |
| 905 | 855 | protected function init() {} |