PluginProbe
Elementor Website Builder – more than just a page builder / 3.21.0-beta2
Elementor Website Builder – more than just a page builder v3.21.0-beta2
4.3.3 4.3.2 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 All 456 releases
← All changes | modules/ai/connect/ai.php +54 -300 4.3.0 → 3.21.0-beta2 View file →
@@ -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
@@ -226,37 +192,14 @@
226 192 'context' => wp_json_encode( $context ),
227 193 'ids' => $request_ids,
228 194 'api_version' => ELEMENTOR_VERSION,
229 195 'site_lang' => get_bloginfo( 'language' ),
230 - ],
231 - false,
232 - '',
233 - 'json'
196 + ]
234 197 );
235 198 }
236 199
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 200 /**
257 - * Get Image Prompt Enhanced get_image_prompt_enhanced
258 - *
201 + * get_image_prompt_enhanced
259 202 * @param $prompt
260 203 *
261 204 * @return mixed|\WP_Error
262 205 */
@@ -284,12 +227,9 @@
284 227 'context' => wp_json_encode( $context ),
285 228 'ids' => $request_ids,
286 229 'api_version' => ELEMENTOR_VERSION,
287 230 'site_lang' => get_bloginfo( 'language' ),
288 - ],
289 - false,
290 - '',
291 - 'json'
231 + ]
292 232 );
293 233 }
294 234
295 235 public function get_custom_code( $data, $context, $request_ids ) {
@@ -302,12 +242,9 @@
302 242 'context' => wp_json_encode( $context ),
303 243 'ids' => $request_ids,
304 244 'api_version' => ELEMENTOR_VERSION,
305 245 'site_lang' => get_bloginfo( 'language' ),
306 - ],
307 - false,
308 - '',
309 - 'json'
246 + ]
310 247 );
311 248 }
312 249
313 250 public function get_custom_css( $data, $context, $request_ids ) {
@@ -321,18 +258,14 @@
321 258 'context' => wp_json_encode( $context ),
322 259 'ids' => $request_ids,
323 260 'api_version' => ELEMENTOR_VERSION,
324 261 'site_lang' => get_bloginfo( 'language' ),
325 - ],
326 - false,
327 - '',
328 - 'json'
262 + ]
329 263 );
330 264 }
331 265
332 266 /**
333 - * Get text to image get_text_to_image
334 - *
267 + * get_text_to_image
335 268 * @param $prompt
336 269 * @param $prompt_settings
337 270 *
338 271 * @return mixed|\WP_Error
@@ -342,57 +275,25 @@
342 275 'POST',
343 276 'image/text-to-image',
344 277 [
345 278 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 ],
279 + self::IMAGE_TYPE => $data['payload']['promptSettings'][ self::IMAGE_TYPE ] . '/' . $data['payload']['promptSettings'][ self::STYLE_PRESET ],
280 + self::ASPECT_RATIO => $data['payload']['promptSettings'][ self::ASPECT_RATIO ],
348 281 'context' => wp_json_encode( $context ),
349 282 'ids' => $request_ids,
350 283 'api_version' => ELEMENTOR_VERSION,
351 284 'site_lang' => get_bloginfo( 'language' ),
352 - ],
353 - false,
354 - '',
355 - 'json'
285 + ]
356 286 );
357 287 }
358 288
359 289 /**
360 - * Get_Featured_Image get_featured_image
290 + * get_image_to_image
291 + * @param $image_data
361 292 *
362 - * @param $data
363 - * @param $context
364 - * @param $request_ids
365 293 * @return mixed|\WP_Error
294 + * @throws \Exception
366 295 */
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 296 public function get_image_to_image( $image_data, $context, $request_ids ) {
396 297 $image_file = get_attached_file( $image_data['attachment_id'] );
397 298
398 299 if ( ! $image_file ) {
@@ -418,85 +319,14 @@
418 319
419 320 return $result;
420 321 }
421 322
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 323 /**
492 - * Get Image To Image Upscale get_image_to_image_upscale
324 + * get_image_to_image_upscale
325 + * @param $image_data
493 326 *
494 - * @param $image_data
495 - * @param $context
496 - * @param $request_ids
497 327 * @return mixed|\WP_Error
498 - * @throws \Exception If image file not found.
328 + * @throws \Exception
499 329 */
500 330 public function get_image_to_image_upscale( $image_data, $context, $request_ids ) {
501 331 $image_file = get_attached_file( $image_data['attachment_id'] );
502 332
@@ -521,15 +351,13 @@
521 351 return $result;
522 352 }
523 353
524 354 /**
525 - * Get Image To Image Remove Background get_image_to_image_remove_background
355 + * get_image_to_image_remove_background
356 + * @param $image_data
526 357 *
527 - * @param $image_data
528 - * @param $context
529 - * @param $request_ids
530 358 * @return mixed|\WP_Error
531 - * @throws \Exception If image file not found.
359 + * @throws \Exception
532 360 */
533 361 public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) {
534 362 $image_file = get_attached_file( $image_data['attachment_id'] );
535 363
@@ -553,15 +381,13 @@
553 381 return $result;
554 382 }
555 383
556 384 /**
557 - * Get Image To Image Remove Text get_image_to_image_remove_text
385 + * get_image_to_image_remove_text
386 + * @param $image_data
558 387 *
559 - * @param $image_data
560 - * @param $context
561 - * @param $request_ids
562 388 * @return mixed|\WP_Error
563 - * @throws \Exception If image file not found.
389 + * @throws \Exception
564 390 */
565 391 public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) {
566 392 $image_file = get_attached_file( $image_data['attachment_id'] );
567 393
@@ -586,11 +412,10 @@
586 412 return $result;
587 413 }
588 414
589 415 /**
590 - * Store Temp File store_temp_file
416 + * store_temp_file
591 417 * used to store a temp file for the AI request and deletes it once the request is done
592 - *
593 418 * @param $file_content
594 419 * @param $file_ext
595 420 *
596 421 * @return string
@@ -607,15 +432,13 @@
607 432 return $temp_file;
608 433 }
609 434
610 435 /**
611 - * Get Image To Image Out Painting get_image_to_image_out_painting
436 + * get_image_to_image_out_painting
437 + * @param $image_data
612 438 *
613 - * @param $image_data
614 - * @param $context
615 - * @param $request_ids
616 439 * @return mixed|\WP_Error
617 - * @throws \Exception If image file not found.
440 + * @throws \Exception
618 441 */
619 442 public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) {
620 443 $img_content = str_replace( ' ', '+', $image_data['mask'] );
621 444 $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 );
@@ -629,16 +452,14 @@
629 452 $result = $this->ai_request(
630 453 'POST',
631 454 'image/image-to-image/outpainting',
632 455 [
456 + self::PROMPT => $image_data[ self::PROMPT ],
457 + self::IMAGE_TYPE => '',
633 458 'context' => wp_json_encode( $context ),
634 459 'ids' => $request_ids,
635 460 'api_version' => ELEMENTOR_VERSION,
636 461 '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 462 ],
642 463 [
643 464 [
644 465 'name' => 'image',
@@ -651,15 +472,13 @@
651 472 return $result;
652 473 }
653 474
654 475 /**
655 - * Get Image To Image Mask get_image_to_image_mask
476 + * get_image_to_image_mask
477 + * @param $image_data
656 478 *
657 - * @param $image_data
658 - * @param $context
659 - * @param $request_ids
660 479 * @return mixed|\WP_Error
661 - * @throws \Exception If image file not found.
480 + * @throws \Exception
662 481 */
663 482 public function get_image_to_image_mask( $image_data, $context, $request_ids ) {
664 483 $image_file = get_attached_file( $image_data['attachment_id'] );
665 484 $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' );
@@ -676,13 +495,14 @@
676 495 'POST',
677 496 'image/image-to-image/inpainting',
678 497 [
679 498 self::PROMPT => $image_data[ self::PROMPT ],
499 + self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ],
500 + self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ],
680 501 'context' => wp_json_encode( $context ),
681 502 'ids' => $request_ids,
682 503 'api_version' => ELEMENTOR_VERSION,
683 504 'site_lang' => get_bloginfo( 'language' ),
684 - 'image_base64' => $image_data['image_base64'],
685 505 ],
686 506 [
687 507 [
688 508 'name' => 'image',
@@ -698,48 +518,9 @@
698 518 );
699 519
700 520 return $result;
701 521 }
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 522
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 523 public function generate_layout( $data, $context ) {
743 524 $endpoint = 'generate/layout';
744 525
745 526 $body = [
@@ -771,9 +552,8 @@
771 552
772 553 $html = wp_json_encode( $attachment['content'] );
773 554
774 555 $body['html'] = $html;
775 - $body['htmlFetchedUrl'] = $attachment['label'];
776 556
777 557 break;
778 558 }
779 559 }
@@ -779,9 +559,9 @@
779 559 }
780 560
781 561 $context['currentContext'] = $data['currentContext'];
782 562 $context['features'] = [
783 - 'supportedFeatures' => [ 'Taxonomy' ],
563 + 'supportedFeatures' => [],
784 564 ];
785 565
786 566 if ( ElementorUtils::has_pro() ) {
787 567 $context['features']['subscriptions'] = [ 'Pro' ];
@@ -786,20 +566,16 @@
786 566 if ( ElementorUtils::has_pro() ) {
787 567 $context['features']['subscriptions'] = [ 'Pro' ];
788 568 }
789 569
790 - if ( Plugin::instance()->experiments->is_feature_active( 'container' ) ) {
791 - $context['features']['supportedFeatures'][] = 'Nested';
570 + if ( Plugin::$instance->experiments->is_feature_active( 'container_grid' ) ) {
571 + $context['features']['supportedFeatures'][] = 'Grid';
792 572 }
793 573
794 - if ( Plugin::instance()->experiments->get_active_features()['mega-menu'] ) {
795 - $context['features']['supportedFeatures'][] = 'MegaMenu';
574 + if ( Plugin::instance()->experiments->get_active_features()['nested-elements'] ) {
575 + $context['features']['supportedFeatures'][] = 'NestedElements';
796 576 }
797 577
798 - if ( class_exists( 'WC' ) ) {
799 - $context['features']['supportedFeatures'][] = 'WooCommerce';
800 - }
801 -
802 578 $metadata = [
803 579 'context' => $context,
804 580 'api_version' => ELEMENTOR_VERSION,
805 581 'site_lang' => get_bloginfo( 'language' ),
@@ -834,12 +610,9 @@
834 610 'enhance_type' => $enhance_type,
835 611 'context' => wp_json_encode( $context ),
836 612 'api_version' => ELEMENTOR_VERSION,
837 613 'site_lang' => get_bloginfo( 'language' ),
838 - ],
839 - false,
840 - '',
841 - 'json'
614 + ]
842 615 );
843 616 }
844 617
845 618 public function get_history_by_type( $type, $page, $limit, $context = [] ) {
@@ -872,34 +645,15 @@
872 645 );
873 646 }
874 647
875 648 public function toggle_favorite_history_item( $id, $context = [] ) {
876 - $sanitized_id = str_replace( '%', '%%', $id );
877 649 return $this->ai_request(
878 - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ),
650 + 'POST', sprintf( 'history/%s/favorite', $id ),
879 651 [
880 652 'context' => wp_json_encode( $context ),
881 653 'api_version' => ELEMENTOR_VERSION,
882 654 'site_lang' => get_bloginfo( 'language' ),
883 655 ]
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 656 );
903 657 }
904 658
905 659 protected function init() {}