| @@ -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', |
| @@ -71,36 +82,9 @@ | ||
| 71 | 82 | ); |
| 72 | 83 | } |
| 73 | 84 | |
| 74 | 85 | /** |
| 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 | - * | |
| 86 | + * get_file_payload | |
| 103 | 87 | * @param $filename |
| 104 | 88 | * @param $file_type |
| 105 | 89 | * @param $file_path |
| 106 | 90 | * @param $boundary |
| @@ -253,10 +237,9 @@ | ||
| 253 | 237 | ); |
| 254 | 238 | } |
| 255 | 239 | |
| 256 | 240 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 241 | + * get_image_prompt_enhanced | |
| 259 | 242 | * @param $prompt |
| 260 | 243 | * |
| 261 | 244 | * @return mixed|\WP_Error |
| 262 | 245 | */ |
| @@ -329,10 +312,9 @@ | ||
| 329 | 312 | ); |
| 330 | 313 | } |
| 331 | 314 | |
| 332 | 315 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 316 | + * get_text_to_image | |
| 335 | 317 | * @param $prompt |
| 336 | 318 | * @param $prompt_settings |
| 337 | 319 | * |
| 338 | 320 | * @return mixed|\WP_Error |
| @@ -356,13 +338,12 @@ | ||
| 356 | 338 | ); |
| 357 | 339 | } |
| 358 | 340 | |
| 359 | 341 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 342 | + * get_featured_image | |
| 343 | + * @param $prompt | |
| 344 | + * @param $prompt_settings | |
| 361 | 345 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 346 | * @return mixed|\WP_Error |
| 366 | 347 | */ |
| 367 | 348 | public function get_featured_image( $data, $context, $request_ids ) { |
| 368 | 349 | return $this->ai_request( |
| @@ -383,15 +364,13 @@ | ||
| 383 | 364 | ); |
| 384 | 365 | } |
| 385 | 366 | |
| 386 | 367 | /** |
| 387 | - * Get Image To Image get_image_to_image | |
| 368 | + * get_image_to_image | |
| 369 | + * @param $image_data | |
| 388 | 370 | * |
| 389 | - * @param $image_data | |
| 390 | - * @param $context | |
| 391 | - * @param $request_ids | |
| 392 | 371 | * @return mixed|\WP_Error |
| 393 | - * @throws \Exception If image file not found. | |
| 372 | + * @throws \Exception | |
| 394 | 373 | */ |
| 395 | 374 | public function get_image_to_image( $image_data, $context, $request_ids ) { |
| 396 | 375 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 376 | |
| @@ -418,85 +397,14 @@ | ||
| 418 | 397 | |
| 419 | 398 | return $result; |
| 420 | 399 | } |
| 421 | 400 | |
| 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 | 401 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 402 | + * get_image_to_image_upscale | |
| 403 | + * @param $image_data | |
| 493 | 404 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 405 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 406 | + * @throws \Exception | |
| 499 | 407 | */ |
| 500 | 408 | public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { |
| 501 | 409 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 410 | |
| @@ -521,15 +429,13 @@ | ||
| 521 | 429 | return $result; |
| 522 | 430 | } |
| 523 | 431 | |
| 524 | 432 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 433 | + * get_image_to_image_remove_background | |
| 434 | + * @param $image_data | |
| 526 | 435 | * |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | 436 | * @return mixed|\WP_Error |
| 531 | - * @throws \Exception If image file not found. | |
| 437 | + * @throws \Exception | |
| 532 | 438 | */ |
| 533 | 439 | public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { |
| 534 | 440 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 535 | 441 | |
| @@ -553,15 +459,13 @@ | ||
| 553 | 459 | return $result; |
| 554 | 460 | } |
| 555 | 461 | |
| 556 | 462 | /** |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 463 | + * get_image_to_image_remove_text | |
| 464 | + * @param $image_data | |
| 558 | 465 | * |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | 466 | * @return mixed|\WP_Error |
| 563 | - * @throws \Exception If image file not found. | |
| 467 | + * @throws \Exception | |
| 564 | 468 | */ |
| 565 | 469 | public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { |
| 566 | 470 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 567 | 471 | |
| @@ -586,11 +490,10 @@ | ||
| 586 | 490 | return $result; |
| 587 | 491 | } |
| 588 | 492 | |
| 589 | 493 | /** |
| 590 | - * Store Temp File store_temp_file | |
| 494 | + * store_temp_file | |
| 591 | 495 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 496 | * @param $file_content |
| 594 | 497 | * @param $file_ext |
| 595 | 498 | * |
| 596 | 499 | * @return string |
| @@ -607,15 +510,13 @@ | ||
| 607 | 510 | return $temp_file; |
| 608 | 511 | } |
| 609 | 512 | |
| 610 | 513 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 514 | + * get_image_to_image_out_painting | |
| 515 | + * @param $image_data | |
| 612 | 516 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 517 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 518 | + * @throws \Exception | |
| 618 | 519 | */ |
| 619 | 520 | public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { |
| 620 | 521 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 522 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| @@ -651,15 +552,13 @@ | ||
| 651 | 552 | return $result; |
| 652 | 553 | } |
| 653 | 554 | |
| 654 | 555 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 556 | + * get_image_to_image_mask | |
| 557 | + * @param $image_data | |
| 656 | 558 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 559 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 560 | + * @throws \Exception | |
| 662 | 561 | */ |
| 663 | 562 | public function get_image_to_image_mask( $image_data, $context, $request_ids ) { |
| 664 | 563 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 564 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| @@ -786,8 +685,12 @@ | ||
| 786 | 685 | if ( ElementorUtils::has_pro() ) { |
| 787 | 686 | $context['features']['subscriptions'] = [ 'Pro' ]; |
| 788 | 687 | } |
| 789 | 688 | |
| 689 | + if ( Plugin::$instance->experiments->is_feature_active( 'container_grid' ) ) { | |
| 690 | + $context['features']['supportedFeatures'][] = 'Grid'; | |
| 691 | + } | |
| 692 | + | |
| 790 | 693 | if ( Plugin::instance()->experiments->get_active_features()['nested-elements'] ) { |
| 791 | 694 | $context['features']['supportedFeatures'][] = 'Nested'; |
| 792 | 695 | } |
| 793 | 696 | |
| @@ -872,34 +775,15 @@ | ||
| 872 | 775 | ); |
| 873 | 776 | } |
| 874 | 777 | |
| 875 | 778 | public function toggle_favorite_history_item( $id, $context = [] ) { |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | 779 | return $this->ai_request( |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 780 | + 'POST', sprintf( 'history/%s/favorite', $id ), | |
| 879 | 781 | [ |
| 880 | 782 | 'context' => wp_json_encode( $context ), |
| 881 | 783 | 'api_version' => ELEMENTOR_VERSION, |
| 882 | 784 | 'site_lang' => get_bloginfo( 'language' ), |
| 883 | 785 | ] |
| 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 | 786 | ); |
| 903 | 787 | } |
| 904 | 788 | |
| 905 | 789 | protected function init() {} |