PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.0-dev3
Elementor Website Builder – more than just a page builder v3.20.0-dev3
4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 All 454 releases
← All changes | modules/ai/connect/ai.php +52 -309 4.3.03.20.0-dev3 View file →
@@ -3,12 +3,11 @@
3 3
4 4 use Elementor\Core\Common\Modules\Connect\Apps\Library;
5 5 use Elementor\Modules\Ai\Module;
6 6 use Elementor\Utils as ElementorUtils;
7 -use Elementor\Plugin;
8 7
9 8 if ( ! defined( 'ABSPATH' ) ) {
10 - exit; // Exit if accessed directly.
9 + exit; // Exit if accessed directly
11 10 }
12 11
13 12 class Ai extends Library {
14 13 const API_URL = 'https://my.elementor.com/api/v2/ai/';
@@ -18,10 +17,8 @@
18 17 const IMAGE_STRENGTH = 'image_strength';
19 18 const ASPECT_RATIO = 'ratio';
20 19 const IMAGE_RESOLUTION = 'image_resolution';
21 20
22 - const IMAGE_BACKGROUND_COLOR = 'background_color';
23 -
24 21 const PROMPT = 'prompt';
25 22
26 23 public function get_title() {
27 24 return esc_html__( 'AI', 'elementor' );
@@ -41,8 +38,21 @@
41 38 ]
42 39 );
43 40 }
44 41
42 + public function get_cached_usage() {
43 + $cache_key = 'elementor_ai_usage';
44 + $cache_time = 24 * HOUR_IN_SECONDS;
45 + $usage = get_site_transient( $cache_key );
46 +
47 + if ( ! $usage ) {
48 + $usage = $this->get_usage();
49 + set_site_transient( $cache_key, $usage, $cache_time );
50 + }
51 +
52 + return $usage;
53 + }
54 +
45 55 public function get_remote_config() {
46 56 return $this->ai_request(
47 57 'GET',
48 58 'remote-config/config',
@@ -52,55 +62,10 @@
52 62 ]
53 63 );
54 64 }
55 65
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 66 /**
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 - *
67 + * get_file_payload
103 68 * @param $filename
104 69 * @param $file_type
105 70 * @param $file_path
106 71 * @param $boundary
@@ -178,9 +143,8 @@
178 143
179 144 ],
180 145 [
181 146 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
182 - 'with_error_data' => true,
183 147 ]
184 148 );
185 149 }
186 150
@@ -226,37 +190,14 @@
226 190 'context' => wp_json_encode( $context ),
227 191 'ids' => $request_ids,
228 192 'api_version' => ELEMENTOR_VERSION,
229 193 'site_lang' => get_bloginfo( 'language' ),
230 - ],
231 - false,
232 - '',
233 - 'json'
194 + ]
234 195 );
235 196 }
236 197
237 - public function get_excerpt( $prompt, $context, $request_ids ) {
238 - $excerpt_length = apply_filters( 'excerpt_length', 55 );
239 - return $this->ai_request(
240 - 'POST',
241 - 'text/get-excerpt',
242 - [
243 - 'content' => $prompt,
244 - 'maxLength' => $excerpt_length,
245 - 'context' => wp_json_encode( $context ),
246 - 'ids' => $request_ids,
247 - 'api_version' => ELEMENTOR_VERSION,
248 - 'site_lang' => get_bloginfo( 'language' ),
249 - ],
250 - false,
251 - '',
252 - 'json'
253 - );
254 - }
255 -
256 198 /**
257 - * Get Image Prompt Enhanced get_image_prompt_enhanced
258 - *
199 + * get_image_prompt_enhanced
259 200 * @param $prompt
260 201 *
261 202 * @return mixed|\WP_Error
262 203 */
@@ -284,12 +225,9 @@
284 225 'context' => wp_json_encode( $context ),
285 226 'ids' => $request_ids,
286 227 'api_version' => ELEMENTOR_VERSION,
287 228 'site_lang' => get_bloginfo( 'language' ),
288 - ],
289 - false,
290 - '',
291 - 'json'
229 + ]
292 230 );
293 231 }
294 232
295 233 public function get_custom_code( $data, $context, $request_ids ) {
@@ -302,12 +240,9 @@
302 240 'context' => wp_json_encode( $context ),
303 241 'ids' => $request_ids,
304 242 'api_version' => ELEMENTOR_VERSION,
305 243 'site_lang' => get_bloginfo( 'language' ),
306 - ],
307 - false,
308 - '',
309 - 'json'
244 + ]
310 245 );
311 246 }
312 247
313 248 public function get_custom_css( $data, $context, $request_ids ) {
@@ -321,18 +256,14 @@
321 256 'context' => wp_json_encode( $context ),
322 257 'ids' => $request_ids,
323 258 'api_version' => ELEMENTOR_VERSION,
324 259 'site_lang' => get_bloginfo( 'language' ),
325 - ],
326 - false,
327 - '',
328 - 'json'
260 + ]
329 261 );
330 262 }
331 263
332 264 /**
333 - * Get text to image get_text_to_image
334 - *
265 + * get_text_to_image
335 266 * @param $prompt
336 267 * @param $prompt_settings
337 268 *
338 269 * @return mixed|\WP_Error
@@ -342,57 +273,25 @@
342 273 'POST',
343 274 'image/text-to-image',
344 275 [
345 276 self::PROMPT => $data['payload']['prompt'],
346 - self::IMAGE_TYPE => $data['payload']['settings'][ self::IMAGE_TYPE ] . '/' . $data['payload']['settings'][ self::STYLE_PRESET ],
347 - self::ASPECT_RATIO => $data['payload']['settings'][ self::ASPECT_RATIO ],
277 + self::IMAGE_TYPE => $data['payload']['promptSettings'][ self::IMAGE_TYPE ] . '/' . $data['payload']['promptSettings'][ self::STYLE_PRESET ],
278 + self::ASPECT_RATIO => $data['payload']['promptSettings'][ self::ASPECT_RATIO ],
348 279 'context' => wp_json_encode( $context ),
349 280 'ids' => $request_ids,
350 281 'api_version' => ELEMENTOR_VERSION,
351 282 'site_lang' => get_bloginfo( 'language' ),
352 - ],
353 - false,
354 - '',
355 - 'json'
283 + ]
356 284 );
357 285 }
358 286
359 287 /**
360 - * Get_Featured_Image get_featured_image
288 + * get_image_to_image
289 + * @param $image_data
361 290 *
362 - * @param $data
363 - * @param $context
364 - * @param $request_ids
365 291 * @return mixed|\WP_Error
292 + * @throws \Exception
366 293 */
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 294 public function get_image_to_image( $image_data, $context, $request_ids ) {
396 295 $image_file = get_attached_file( $image_data['attachment_id'] );
397 296
398 297 if ( ! $image_file ) {
@@ -418,85 +317,14 @@
418 317
419 318 return $result;
420 319 }
421 320
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 321 /**
492 - * Get Image To Image Upscale get_image_to_image_upscale
322 + * get_image_to_image_upscale
323 + * @param $image_data
493 324 *
494 - * @param $image_data
495 - * @param $context
496 - * @param $request_ids
497 325 * @return mixed|\WP_Error
498 - * @throws \Exception If image file not found.
326 + * @throws \Exception
499 327 */
500 328 public function get_image_to_image_upscale( $image_data, $context, $request_ids ) {
501 329 $image_file = get_attached_file( $image_data['attachment_id'] );
502 330
@@ -521,15 +349,13 @@
521 349 return $result;
522 350 }
523 351
524 352 /**
525 - * Get Image To Image Remove Background get_image_to_image_remove_background
353 + * get_image_to_image_remove_background
354 + * @param $image_data
526 355 *
527 - * @param $image_data
528 - * @param $context
529 - * @param $request_ids
530 356 * @return mixed|\WP_Error
531 - * @throws \Exception If image file not found.
357 + * @throws \Exception
532 358 */
533 359 public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) {
534 360 $image_file = get_attached_file( $image_data['attachment_id'] );
535 361
@@ -553,15 +379,13 @@
553 379 return $result;
554 380 }
555 381
556 382 /**
557 - * Get Image To Image Remove Text get_image_to_image_remove_text
383 + * get_image_to_image_remove_text
384 + * @param $image_data
558 385 *
559 - * @param $image_data
560 - * @param $context
561 - * @param $request_ids
562 386 * @return mixed|\WP_Error
563 - * @throws \Exception If image file not found.
387 + * @throws \Exception
564 388 */
565 389 public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) {
566 390 $image_file = get_attached_file( $image_data['attachment_id'] );
567 391
@@ -586,11 +410,10 @@
586 410 return $result;
587 411 }
588 412
589 413 /**
590 - * Store Temp File store_temp_file
414 + * store_temp_file
591 415 * used to store a temp file for the AI request and deletes it once the request is done
592 - *
593 416 * @param $file_content
594 417 * @param $file_ext
595 418 *
596 419 * @return string
@@ -607,15 +430,13 @@
607 430 return $temp_file;
608 431 }
609 432
610 433 /**
611 - * Get Image To Image Out Painting get_image_to_image_out_painting
434 + * get_image_to_image_out_painting
435 + * @param $image_data
612 436 *
613 - * @param $image_data
614 - * @param $context
615 - * @param $request_ids
616 437 * @return mixed|\WP_Error
617 - * @throws \Exception If image file not found.
438 + * @throws \Exception
618 439 */
619 440 public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) {
620 441 $img_content = str_replace( ' ', '+', $image_data['mask'] );
621 442 $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 );
@@ -629,16 +450,14 @@
629 450 $result = $this->ai_request(
630 451 'POST',
631 452 'image/image-to-image/outpainting',
632 453 [
454 + self::PROMPT => $image_data[ self::PROMPT ],
455 + self::IMAGE_TYPE => '',
633 456 'context' => wp_json_encode( $context ),
634 457 'ids' => $request_ids,
635 458 'api_version' => ELEMENTOR_VERSION,
636 459 'site_lang' => get_bloginfo( 'language' ),
637 - 'size' => wp_json_encode( $image_data['size'] ),
638 - 'position' => wp_json_encode( $image_data['position'] ),
639 - 'image_base64' => $image_data['image_base64'],
640 - $image_data['image'],
641 460 ],
642 461 [
643 462 [
644 463 'name' => 'image',
@@ -651,15 +470,13 @@
651 470 return $result;
652 471 }
653 472
654 473 /**
655 - * Get Image To Image Mask get_image_to_image_mask
474 + * get_image_to_image_mask
475 + * @param $image_data
656 476 *
657 - * @param $image_data
658 - * @param $context
659 - * @param $request_ids
660 477 * @return mixed|\WP_Error
661 - * @throws \Exception If image file not found.
478 + * @throws \Exception
662 479 */
663 480 public function get_image_to_image_mask( $image_data, $context, $request_ids ) {
664 481 $image_file = get_attached_file( $image_data['attachment_id'] );
665 482 $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' );
@@ -676,13 +493,14 @@
676 493 'POST',
677 494 'image/image-to-image/inpainting',
678 495 [
679 496 self::PROMPT => $image_data[ self::PROMPT ],
497 + self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ],
498 + self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ],
680 499 'context' => wp_json_encode( $context ),
681 500 'ids' => $request_ids,
682 501 'api_version' => ELEMENTOR_VERSION,
683 502 'site_lang' => get_bloginfo( 'language' ),
684 - 'image_base64' => $image_data['image_base64'],
685 503 ],
686 504 [
687 505 [
688 506 'name' => 'image',
@@ -698,48 +516,9 @@
698 516 );
699 517
700 518 return $result;
701 519 }
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 520
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 521 public function generate_layout( $data, $context ) {
743 522 $endpoint = 'generate/layout';
744 523
745 524 $body = [
@@ -771,9 +550,8 @@
771 550
772 551 $html = wp_json_encode( $attachment['content'] );
773 552
774 553 $body['html'] = $html;
775 - $body['htmlFetchedUrl'] = $attachment['label'];
776 554
777 555 break;
778 556 }
779 557 }
@@ -778,28 +556,15 @@
778 556 }
779 557 }
780 558
781 559 $context['currentContext'] = $data['currentContext'];
782 - $context['features'] = [
783 - 'supportedFeatures' => [ 'Taxonomy' ],
784 - ];
785 560
786 561 if ( ElementorUtils::has_pro() ) {
787 - $context['features']['subscriptions'] = [ 'Pro' ];
562 + $context['features'] = [
563 + 'subscriptions' => [ 'Pro' ],
564 + ];
788 565 }
789 566
790 - if ( Plugin::instance()->experiments->is_feature_active( 'container' ) ) {
791 - $context['features']['supportedFeatures'][] = 'Nested';
792 - }
793 -
794 - if ( Plugin::instance()->experiments->get_active_features()['mega-menu'] ) {
795 - $context['features']['supportedFeatures'][] = 'MegaMenu';
796 - }
797 -
798 - if ( class_exists( 'WC' ) ) {
799 - $context['features']['supportedFeatures'][] = 'WooCommerce';
800 - }
801 -
802 567 $metadata = [
803 568 'context' => $context,
804 569 'api_version' => ELEMENTOR_VERSION,
805 570 'site_lang' => get_bloginfo( 'language' ),
@@ -834,12 +599,9 @@
834 599 'enhance_type' => $enhance_type,
835 600 'context' => wp_json_encode( $context ),
836 601 'api_version' => ELEMENTOR_VERSION,
837 602 'site_lang' => get_bloginfo( 'language' ),
838 - ],
839 - false,
840 - '',
841 - 'json'
603 + ]
842 604 );
843 605 }
844 606
845 607 public function get_history_by_type( $type, $page, $limit, $context = [] ) {
@@ -872,34 +634,15 @@
872 634 );
873 635 }
874 636
875 637 public function toggle_favorite_history_item( $id, $context = [] ) {
876 - $sanitized_id = str_replace( '%', '%%', $id );
877 638 return $this->ai_request(
878 - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ),
639 + 'POST', sprintf( 'history/%s/favorite', $id ),
879 640 [
880 641 'context' => wp_json_encode( $context ),
881 642 'api_version' => ELEMENTOR_VERSION,
882 643 'site_lang' => get_bloginfo( 'language' ),
883 644 ]
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 645 );
903 646 }
904 647
905 648 protected function init() {}