| @@ -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 | |
| @@ -216,9 +180,9 @@ | ||
| 216 | 180 | ] |
| 217 | 181 | ); |
| 218 | 182 | } |
| 219 | 183 | |
| 220 | - public function get_completion_text( $prompt, $context, $request_ids ) { | |
| 184 | + public function get_completion_text( $prompt, $context = [] ) { | |
| 221 | 185 | return $this->ai_request( |
| 222 | 186 | 'POST', |
| 223 | 187 | 'text/completion', |
| 224 | 188 | [ |
| @@ -223,45 +187,21 @@ | ||
| 223 | 187 | 'text/completion', |
| 224 | 188 | [ |
| 225 | 189 | 'prompt' => $prompt, |
| 226 | 190 | 'context' => wp_json_encode( $context ), |
| 227 | - 'ids' => $request_ids, | |
| 228 | 191 | 'api_version' => ELEMENTOR_VERSION, |
| 229 | 192 | 'site_lang' => get_bloginfo( 'language' ), |
| 230 | - ], | |
| 231 | - false, | |
| 232 | - '', | |
| 233 | - 'json' | |
| 193 | + ] | |
| 234 | 194 | ); |
| 235 | 195 | } |
| 236 | 196 | |
| 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 | 197 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 198 | + * get_image_prompt_enhanced | |
| 259 | 199 | * @param $prompt |
| 260 | 200 | * |
| 261 | 201 | * @return mixed|\WP_Error |
| 262 | 202 | */ |
| 263 | - public function get_image_prompt_enhanced( $prompt, $context, $request_ids ) { | |
| 203 | + public function get_image_prompt_enhanced( $prompt, $context = [] ) { | |
| 264 | 204 | return $this->ai_request( |
| 265 | 205 | 'POST', |
| 266 | 206 | 'text/enhance-image-prompt', |
| 267 | 207 | [ |
| @@ -266,9 +206,8 @@ | ||
| 266 | 206 | 'text/enhance-image-prompt', |
| 267 | 207 | [ |
| 268 | 208 | 'prompt' => $prompt, |
| 269 | 209 | 'context' => wp_json_encode( $context ), |
| 270 | - 'ids' => $request_ids, | |
| 271 | 210 | 'api_version' => ELEMENTOR_VERSION, |
| 272 | 211 | 'site_lang' => get_bloginfo( 'language' ), |
| 273 | 212 | ] |
| 274 | 213 | ); |
| @@ -273,127 +212,81 @@ | ||
| 273 | 212 | ] |
| 274 | 213 | ); |
| 275 | 214 | } |
| 276 | 215 | |
| 277 | - public function get_edit_text( $data, $context, $request_ids ) { | |
| 216 | + public function get_edit_text( $input, $instruction, $context = [] ) { | |
| 278 | 217 | return $this->ai_request( |
| 279 | 218 | 'POST', |
| 280 | 219 | 'text/edit', |
| 281 | 220 | [ |
| 282 | - 'input' => $data['payload']['input'], | |
| 283 | - 'instruction' => $data['payload']['instruction'], | |
| 221 | + 'input' => $input, | |
| 222 | + 'instruction' => $instruction, | |
| 284 | 223 | 'context' => wp_json_encode( $context ), |
| 285 | - 'ids' => $request_ids, | |
| 286 | 224 | 'api_version' => ELEMENTOR_VERSION, |
| 287 | 225 | 'site_lang' => get_bloginfo( 'language' ), |
| 288 | - ], | |
| 289 | - false, | |
| 290 | - '', | |
| 291 | - 'json' | |
| 226 | + ] | |
| 292 | 227 | ); |
| 293 | 228 | } |
| 294 | 229 | |
| 295 | - public function get_custom_code( $data, $context, $request_ids ) { | |
| 230 | + public function get_custom_code( $prompt, $language, $context = [] ) { | |
| 296 | 231 | return $this->ai_request( |
| 297 | 232 | 'POST', |
| 298 | 233 | 'text/custom-code', |
| 299 | 234 | [ |
| 300 | - 'prompt' => $data['payload']['prompt'], | |
| 301 | - 'language' => $data['payload']['language'], | |
| 235 | + 'prompt' => $prompt, | |
| 236 | + 'language' => $language, | |
| 302 | 237 | 'context' => wp_json_encode( $context ), |
| 303 | - 'ids' => $request_ids, | |
| 304 | 238 | 'api_version' => ELEMENTOR_VERSION, |
| 305 | 239 | 'site_lang' => get_bloginfo( 'language' ), |
| 306 | - ], | |
| 307 | - false, | |
| 308 | - '', | |
| 309 | - 'json' | |
| 240 | + ] | |
| 310 | 241 | ); |
| 311 | 242 | } |
| 312 | 243 | |
| 313 | - public function get_custom_css( $data, $context, $request_ids ) { | |
| 244 | + public function get_custom_css( $prompt, $html_markup, $element_id, $context = [] ) { | |
| 314 | 245 | return $this->ai_request( |
| 315 | 246 | 'POST', |
| 316 | 247 | 'text/custom-css', |
| 317 | 248 | [ |
| 318 | - 'prompt' => $data['payload']['prompt'], | |
| 319 | - 'html_markup' => $data['payload']['html_markup'], | |
| 320 | - 'element_id' => $data['payload']['element_id'], | |
| 249 | + 'prompt' => $prompt, | |
| 250 | + 'html_markup' => $html_markup, | |
| 251 | + 'element_id' => $element_id, | |
| 321 | 252 | 'context' => wp_json_encode( $context ), |
| 322 | - 'ids' => $request_ids, | |
| 323 | 253 | 'api_version' => ELEMENTOR_VERSION, |
| 324 | 254 | 'site_lang' => get_bloginfo( 'language' ), |
| 325 | - ], | |
| 326 | - false, | |
| 327 | - '', | |
| 328 | - 'json' | |
| 255 | + ] | |
| 329 | 256 | ); |
| 330 | 257 | } |
| 331 | 258 | |
| 332 | 259 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 260 | + * get_text_to_image | |
| 335 | 261 | * @param $prompt |
| 336 | 262 | * @param $prompt_settings |
| 337 | 263 | * |
| 338 | 264 | * @return mixed|\WP_Error |
| 339 | 265 | */ |
| 340 | - public function get_text_to_image( $data, $context, $request_ids ) { | |
| 266 | + public function get_text_to_image( $prompt, $prompt_settings, $context = [] ) { | |
| 341 | 267 | return $this->ai_request( |
| 342 | 268 | 'POST', |
| 343 | 269 | 'image/text-to-image', |
| 344 | 270 | [ |
| 345 | - 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 ], | |
| 271 | + self::PROMPT => $prompt, | |
| 272 | + self::IMAGE_TYPE => $prompt_settings[ self::IMAGE_TYPE ] . '/' . $prompt_settings[ self::STYLE_PRESET ], | |
| 273 | + self::ASPECT_RATIO => $prompt_settings[ self::ASPECT_RATIO ], | |
| 348 | 274 | 'context' => wp_json_encode( $context ), |
| 349 | - 'ids' => $request_ids, | |
| 350 | 275 | 'api_version' => ELEMENTOR_VERSION, |
| 351 | 276 | 'site_lang' => get_bloginfo( 'language' ), |
| 352 | - ], | |
| 353 | - false, | |
| 354 | - '', | |
| 355 | - 'json' | |
| 277 | + ] | |
| 356 | 278 | ); |
| 357 | 279 | } |
| 358 | 280 | |
| 359 | 281 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 282 | + * get_image_to_image | |
| 283 | + * @param $image_data | |
| 361 | 284 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 285 | * @return mixed|\WP_Error |
| 286 | + * @throws \Exception | |
| 366 | 287 | */ |
| 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 | - public function get_image_to_image( $image_data, $context, $request_ids ) { | |
| 288 | + public function get_image_to_image( $image_data, $context = [] ) { | |
| 396 | 289 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 290 | |
| 398 | 291 | if ( ! $image_file ) { |
| 399 | 292 | throw new \Exception( 'Image file not found' ); |
| @@ -407,9 +300,8 @@ | ||
| 407 | 300 | self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ], |
| 408 | 301 | self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ], |
| 409 | 302 | self::ASPECT_RATIO => $image_data['promptSettings'][ self::ASPECT_RATIO ], |
| 410 | 303 | 'context' => wp_json_encode( $context ), |
| 411 | - 'ids' => $request_ids, | |
| 412 | 304 | 'api_version' => ELEMENTOR_VERSION, |
| 413 | 305 | 'site_lang' => get_bloginfo( 'language' ), |
| 414 | 306 | ], |
| 415 | 307 | $image_file, |
| @@ -418,87 +310,16 @@ | ||
| 418 | 310 | |
| 419 | 311 | return $result; |
| 420 | 312 | } |
| 421 | 313 | |
| 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 | 314 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 315 | + * get_image_to_image_upscale | |
| 316 | + * @param $image_data | |
| 493 | 317 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 318 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 319 | + * @throws \Exception | |
| 499 | 320 | */ |
| 500 | - public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { | |
| 321 | + public function get_image_to_image_upscale( $image_data, $context = [] ) { | |
| 501 | 322 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 323 | |
| 503 | 324 | if ( ! $image_file ) { |
| 504 | 325 | throw new \Exception( 'Image file not found' ); |
| @@ -509,9 +330,8 @@ | ||
| 509 | 330 | 'image/image-to-image/upscale', |
| 510 | 331 | [ |
| 511 | 332 | self::IMAGE_RESOLUTION => $image_data['promptSettings']['upscale_to'], |
| 512 | 333 | 'context' => wp_json_encode( $context ), |
| 513 | - 'ids' => $request_ids, | |
| 514 | 334 | 'api_version' => ELEMENTOR_VERSION, |
| 515 | 335 | 'site_lang' => get_bloginfo( 'language' ), |
| 516 | 336 | ], |
| 517 | 337 | $image_file, |
| @@ -521,17 +341,15 @@ | ||
| 521 | 341 | return $result; |
| 522 | 342 | } |
| 523 | 343 | |
| 524 | 344 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 345 | + * get_image_to_image_remove_background | |
| 346 | + * @param $image_data | |
| 526 | 347 | * |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | 348 | * @return mixed|\WP_Error |
| 531 | - * @throws \Exception If image file not found. | |
| 349 | + * @throws \Exception | |
| 532 | 350 | */ |
| 533 | - public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { | |
| 351 | + public function get_image_to_image_remove_background( $image_data, $context = [] ) { | |
| 534 | 352 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 535 | 353 | |
| 536 | 354 | if ( ! $image_file ) { |
| 537 | 355 | throw new \Exception( 'Image file not found' ); |
| @@ -541,9 +359,8 @@ | ||
| 541 | 359 | 'POST', |
| 542 | 360 | 'image/image-to-image/remove-background', |
| 543 | 361 | [ |
| 544 | 362 | 'context' => wp_json_encode( $context ), |
| 545 | - 'ids' => $request_ids, | |
| 546 | 363 | 'api_version' => ELEMENTOR_VERSION, |
| 547 | 364 | 'site_lang' => get_bloginfo( 'language' ), |
| 548 | 365 | ], |
| 549 | 366 | $image_file, |
| @@ -553,17 +370,15 @@ | ||
| 553 | 370 | return $result; |
| 554 | 371 | } |
| 555 | 372 | |
| 556 | 373 | /** |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 374 | + * get_image_to_image_remove_text | |
| 375 | + * @param $image_data | |
| 558 | 376 | * |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | 377 | * @return mixed|\WP_Error |
| 563 | - * @throws \Exception If image file not found. | |
| 378 | + * @throws \Exception | |
| 564 | 379 | */ |
| 565 | - public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { | |
| 380 | + public function get_image_to_image_replace_background( $image_data, $context = [] ) { | |
| 566 | 381 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 567 | 382 | |
| 568 | 383 | if ( ! $image_file ) { |
| 569 | 384 | throw new \Exception( 'Image file not found' ); |
| @@ -574,9 +389,8 @@ | ||
| 574 | 389 | 'image/image-to-image/replace-background', |
| 575 | 390 | [ |
| 576 | 391 | self::PROMPT => $image_data[ self::PROMPT ], |
| 577 | 392 | 'context' => wp_json_encode( $context ), |
| 578 | - 'ids' => $request_ids, | |
| 579 | 393 | 'api_version' => ELEMENTOR_VERSION, |
| 580 | 394 | 'site_lang' => get_bloginfo( 'language' ), |
| 581 | 395 | ], |
| 582 | 396 | $image_file, |
| @@ -586,11 +400,10 @@ | ||
| 586 | 400 | return $result; |
| 587 | 401 | } |
| 588 | 402 | |
| 589 | 403 | /** |
| 590 | - * Store Temp File store_temp_file | |
| 404 | + * store_temp_file | |
| 591 | 405 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 406 | * @param $file_content |
| 594 | 407 | * @param $file_ext |
| 595 | 408 | * |
| 596 | 409 | * @return string |
| @@ -607,17 +420,15 @@ | ||
| 607 | 420 | return $temp_file; |
| 608 | 421 | } |
| 609 | 422 | |
| 610 | 423 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 424 | + * get_image_to_image_out_painting | |
| 425 | + * @param $image_data | |
| 612 | 426 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 427 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 428 | + * @throws \Exception | |
| 618 | 429 | */ |
| 619 | - public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { | |
| 430 | + public function get_image_to_image_out_painting( $image_data, $context = [] ) { | |
| 620 | 431 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 432 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| 622 | 433 | $img_content = base64_decode( $img_content ); |
| 623 | 434 | $mask_file = $this->store_temp_file( $img_content, '.png' ); |
| @@ -629,16 +440,13 @@ | ||
| 629 | 440 | $result = $this->ai_request( |
| 630 | 441 | 'POST', |
| 631 | 442 | 'image/image-to-image/outpainting', |
| 632 | 443 | [ |
| 444 | + self::PROMPT => $image_data[ self::PROMPT ], | |
| 445 | + self::IMAGE_TYPE => '', | |
| 633 | 446 | 'context' => wp_json_encode( $context ), |
| 634 | - 'ids' => $request_ids, | |
| 635 | 447 | 'api_version' => ELEMENTOR_VERSION, |
| 636 | 448 | '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 | 449 | ], |
| 642 | 450 | [ |
| 643 | 451 | [ |
| 644 | 452 | 'name' => 'image', |
| @@ -651,17 +459,15 @@ | ||
| 651 | 459 | return $result; |
| 652 | 460 | } |
| 653 | 461 | |
| 654 | 462 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 463 | + * get_image_to_image_mask | |
| 464 | + * @param $image_data | |
| 656 | 465 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 466 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 467 | + * @throws \Exception | |
| 662 | 468 | */ |
| 663 | - public function get_image_to_image_mask( $image_data, $context, $request_ids ) { | |
| 469 | + public function get_image_to_image_mask( $image_data, $context = [] ) { | |
| 664 | 470 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 471 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| 666 | 472 | |
| 667 | 473 | if ( ! $image_file ) { |
| @@ -676,13 +482,13 @@ | ||
| 676 | 482 | 'POST', |
| 677 | 483 | 'image/image-to-image/inpainting', |
| 678 | 484 | [ |
| 679 | 485 | self::PROMPT => $image_data[ self::PROMPT ], |
| 486 | + self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ], | |
| 487 | + self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ], | |
| 680 | 488 | 'context' => wp_json_encode( $context ), |
| 681 | - 'ids' => $request_ids, | |
| 682 | 489 | 'api_version' => ELEMENTOR_VERSION, |
| 683 | 490 | 'site_lang' => get_bloginfo( 'language' ), |
| 684 | - 'image_base64' => $image_data['image_base64'], | |
| 685 | 491 | ], |
| 686 | 492 | [ |
| 687 | 493 | [ |
| 688 | 494 | 'name' => 'image', |
| @@ -698,48 +504,9 @@ | ||
| 698 | 504 | ); |
| 699 | 505 | |
| 700 | 506 | return $result; |
| 701 | 507 | } |
| 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 | 508 | |
| 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 | 509 | public function generate_layout( $data, $context ) { |
| 743 | 510 | $endpoint = 'generate/layout'; |
| 744 | 511 | |
| 745 | 512 | $body = [ |
| @@ -771,9 +538,8 @@ | ||
| 771 | 538 | |
| 772 | 539 | $html = wp_json_encode( $attachment['content'] ); |
| 773 | 540 | |
| 774 | 541 | $body['html'] = $html; |
| 775 | - $body['htmlFetchedUrl'] = $attachment['label']; | |
| 776 | 542 | |
| 777 | 543 | break; |
| 778 | 544 | } |
| 779 | 545 | } |
| @@ -778,28 +544,15 @@ | ||
| 778 | 544 | } |
| 779 | 545 | } |
| 780 | 546 | |
| 781 | 547 | $context['currentContext'] = $data['currentContext']; |
| 782 | - $context['features'] = [ | |
| 783 | - 'supportedFeatures' => [ 'Taxonomy' ], | |
| 784 | - ]; | |
| 785 | 548 | |
| 786 | 549 | if ( ElementorUtils::has_pro() ) { |
| 787 | - $context['features']['subscriptions'] = [ 'Pro' ]; | |
| 550 | + $context['features'] = [ | |
| 551 | + 'subscriptions' => [ 'Pro' ], | |
| 552 | + ]; | |
| 788 | 553 | } |
| 789 | 554 | |
| 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 | 555 | $metadata = [ |
| 803 | 556 | 'context' => $context, |
| 804 | 557 | 'api_version' => ELEMENTOR_VERSION, |
| 805 | 558 | 'site_lang' => get_bloginfo( 'language' ), |
| @@ -834,12 +587,9 @@ | ||
| 834 | 587 | 'enhance_type' => $enhance_type, |
| 835 | 588 | 'context' => wp_json_encode( $context ), |
| 836 | 589 | 'api_version' => ELEMENTOR_VERSION, |
| 837 | 590 | 'site_lang' => get_bloginfo( 'language' ), |
| 838 | - ], | |
| 839 | - false, | |
| 840 | - '', | |
| 841 | - 'json' | |
| 591 | + ] | |
| 842 | 592 | ); |
| 843 | 593 | } |
| 844 | 594 | |
| 845 | 595 | public function get_history_by_type( $type, $page, $limit, $context = [] ) { |
| @@ -872,34 +622,15 @@ | ||
| 872 | 622 | ); |
| 873 | 623 | } |
| 874 | 624 | |
| 875 | 625 | public function toggle_favorite_history_item( $id, $context = [] ) { |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | 626 | return $this->ai_request( |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 627 | + 'POST', sprintf( 'history/%s/favorite', $id ), | |
| 879 | 628 | [ |
| 880 | 629 | 'context' => wp_json_encode( $context ), |
| 881 | 630 | 'api_version' => ELEMENTOR_VERSION, |
| 882 | 631 | 'site_lang' => get_bloginfo( 'language' ), |
| 883 | 632 | ] |
| 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 | 633 | ); |
| 903 | 634 | } |
| 904 | 635 | |
| 905 | 636 | protected function init() {} |