| @@ -2,13 +2,11 @@ | ||
| 2 | 2 | namespace Elementor\Modules\Ai\Connect; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 5 | 5 | use Elementor\Modules\Ai\Module; |
| 6 | -use Elementor\Utils as ElementorUtils; | |
| 7 | -use Elementor\Plugin; | |
| 8 | 6 | |
| 9 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | - exit; // Exit if accessed directly. | |
| 8 | + exit; // Exit if accessed directly | |
| 11 | 9 | } |
| 12 | 10 | |
| 13 | 11 | class Ai extends Library { |
| 14 | 12 | const API_URL = 'https://my.elementor.com/api/v2/ai/'; |
| @@ -18,10 +16,8 @@ | ||
| 18 | 16 | const IMAGE_STRENGTH = 'image_strength'; |
| 19 | 17 | const ASPECT_RATIO = 'ratio'; |
| 20 | 18 | const IMAGE_RESOLUTION = 'image_resolution'; |
| 21 | 19 | |
| 22 | - const IMAGE_BACKGROUND_COLOR = 'background_color'; | |
| 23 | - | |
| 24 | 20 | const PROMPT = 'prompt'; |
| 25 | 21 | |
| 26 | 22 | public function get_title() { |
| 27 | 23 | return esc_html__( 'AI', 'elementor' ); |
| @@ -41,66 +37,10 @@ | ||
| 41 | 37 | ] |
| 42 | 38 | ); |
| 43 | 39 | } |
| 44 | 40 | |
| 45 | - public function get_remote_config() { | |
| 46 | - return $this->ai_request( | |
| 47 | - 'GET', | |
| 48 | - 'remote-config/config', | |
| 49 | - [ | |
| 50 | - 'api_version' => ELEMENTOR_VERSION, | |
| 51 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 52 | - ] | |
| 53 | - ); | |
| 54 | - } | |
| 55 | - | |
| 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 | 41 | /** |
| 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 | - * | |
| 42 | + * get_file_payload | |
| 103 | 43 | * @param $filename |
| 104 | 44 | * @param $file_type |
| 105 | 45 | * @param $file_path |
| 106 | 46 | * @param $boundary |
| @@ -152,9 +92,9 @@ | ||
| 152 | 92 | |
| 153 | 93 | return $payload; |
| 154 | 94 | } |
| 155 | 95 | |
| 156 | - private function ai_request( $method, $endpoint, $body, $file = false, $file_name = '', $format = 'default' ) { | |
| 96 | + private function ai_request( $method, $endpoint, $body, $file = false, $file_name = '' ) { | |
| 157 | 97 | $headers = [ |
| 158 | 98 | 'x-elementor-ai-version' => '2', |
| 159 | 99 | ]; |
| 160 | 100 | |
| @@ -162,11 +102,8 @@ | ||
| 162 | 102 | $boundary = wp_generate_password( 24, false ); |
| 163 | 103 | $body = $this->get_upload_request_body( $body, $file, $boundary, $file_name ); |
| 164 | 104 | // add content type header |
| 165 | 105 | $headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary; |
| 166 | - } elseif ( 'json' === $format ) { | |
| 167 | - $headers['Content-Type'] = 'application/json'; | |
| 168 | - $body = wp_json_encode( $body ); | |
| 169 | 106 | } |
| 170 | 107 | |
| 171 | 108 | return $this->http_request( |
| 172 | 109 | $method, |
| @@ -174,13 +111,11 @@ | ||
| 174 | 111 | [ |
| 175 | 112 | 'timeout' => 100, |
| 176 | 113 | 'headers' => $headers, |
| 177 | 114 | 'body' => $body, |
| 178 | - | |
| 179 | 115 | ], |
| 180 | 116 | [ |
| 181 | 117 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 182 | - 'with_error_data' => true, | |
| 183 | 118 | ] |
| 184 | 119 | ); |
| 185 | 120 | } |
| 186 | 121 | |
| @@ -216,9 +151,9 @@ | ||
| 216 | 151 | ] |
| 217 | 152 | ); |
| 218 | 153 | } |
| 219 | 154 | |
| 220 | - public function get_completion_text( $prompt, $context, $request_ids ) { | |
| 155 | + public function get_completion_text( $prompt, $context = [] ) { | |
| 221 | 156 | return $this->ai_request( |
| 222 | 157 | 'POST', |
| 223 | 158 | 'text/completion', |
| 224 | 159 | [ |
| @@ -223,45 +158,21 @@ | ||
| 223 | 158 | 'text/completion', |
| 224 | 159 | [ |
| 225 | 160 | 'prompt' => $prompt, |
| 226 | 161 | 'context' => wp_json_encode( $context ), |
| 227 | - 'ids' => $request_ids, | |
| 228 | 162 | 'api_version' => ELEMENTOR_VERSION, |
| 229 | 163 | 'site_lang' => get_bloginfo( 'language' ), |
| 230 | - ], | |
| 231 | - false, | |
| 232 | - '', | |
| 233 | - 'json' | |
| 164 | + ] | |
| 234 | 165 | ); |
| 235 | 166 | } |
| 236 | 167 | |
| 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 | 168 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 169 | + * get_image_prompt_enhanced | |
| 259 | 170 | * @param $prompt |
| 260 | 171 | * |
| 261 | 172 | * @return mixed|\WP_Error |
| 262 | 173 | */ |
| 263 | - public function get_image_prompt_enhanced( $prompt, $context, $request_ids ) { | |
| 174 | + public function get_image_prompt_enhanced( $prompt, $context = [] ) { | |
| 264 | 175 | return $this->ai_request( |
| 265 | 176 | 'POST', |
| 266 | 177 | 'text/enhance-image-prompt', |
| 267 | 178 | [ |
| @@ -266,9 +177,8 @@ | ||
| 266 | 177 | 'text/enhance-image-prompt', |
| 267 | 178 | [ |
| 268 | 179 | 'prompt' => $prompt, |
| 269 | 180 | 'context' => wp_json_encode( $context ), |
| 270 | - 'ids' => $request_ids, | |
| 271 | 181 | 'api_version' => ELEMENTOR_VERSION, |
| 272 | 182 | 'site_lang' => get_bloginfo( 'language' ), |
| 273 | 183 | ] |
| 274 | 184 | ); |
| @@ -273,127 +183,81 @@ | ||
| 273 | 183 | ] |
| 274 | 184 | ); |
| 275 | 185 | } |
| 276 | 186 | |
| 277 | - public function get_edit_text( $data, $context, $request_ids ) { | |
| 187 | + public function get_edit_text( $input, $instruction, $context = [] ) { | |
| 278 | 188 | return $this->ai_request( |
| 279 | 189 | 'POST', |
| 280 | 190 | 'text/edit', |
| 281 | 191 | [ |
| 282 | - 'input' => $data['payload']['input'], | |
| 283 | - 'instruction' => $data['payload']['instruction'], | |
| 192 | + 'input' => $input, | |
| 193 | + 'instruction' => $instruction, | |
| 284 | 194 | 'context' => wp_json_encode( $context ), |
| 285 | - 'ids' => $request_ids, | |
| 286 | 195 | 'api_version' => ELEMENTOR_VERSION, |
| 287 | 196 | 'site_lang' => get_bloginfo( 'language' ), |
| 288 | - ], | |
| 289 | - false, | |
| 290 | - '', | |
| 291 | - 'json' | |
| 197 | + ] | |
| 292 | 198 | ); |
| 293 | 199 | } |
| 294 | 200 | |
| 295 | - public function get_custom_code( $data, $context, $request_ids ) { | |
| 201 | + public function get_custom_code( $prompt, $language, $context = [] ) { | |
| 296 | 202 | return $this->ai_request( |
| 297 | 203 | 'POST', |
| 298 | 204 | 'text/custom-code', |
| 299 | 205 | [ |
| 300 | - 'prompt' => $data['payload']['prompt'], | |
| 301 | - 'language' => $data['payload']['language'], | |
| 206 | + 'prompt' => $prompt, | |
| 207 | + 'language' => $language, | |
| 302 | 208 | 'context' => wp_json_encode( $context ), |
| 303 | - 'ids' => $request_ids, | |
| 304 | 209 | 'api_version' => ELEMENTOR_VERSION, |
| 305 | 210 | 'site_lang' => get_bloginfo( 'language' ), |
| 306 | - ], | |
| 307 | - false, | |
| 308 | - '', | |
| 309 | - 'json' | |
| 211 | + ] | |
| 310 | 212 | ); |
| 311 | 213 | } |
| 312 | 214 | |
| 313 | - public function get_custom_css( $data, $context, $request_ids ) { | |
| 215 | + public function get_custom_css( $prompt, $html_markup, $element_id, $context = [] ) { | |
| 314 | 216 | return $this->ai_request( |
| 315 | 217 | 'POST', |
| 316 | 218 | 'text/custom-css', |
| 317 | 219 | [ |
| 318 | - 'prompt' => $data['payload']['prompt'], | |
| 319 | - 'html_markup' => $data['payload']['html_markup'], | |
| 320 | - 'element_id' => $data['payload']['element_id'], | |
| 220 | + 'prompt' => $prompt, | |
| 221 | + 'html_markup' => $html_markup, | |
| 222 | + 'element_id' => $element_id, | |
| 321 | 223 | 'context' => wp_json_encode( $context ), |
| 322 | - 'ids' => $request_ids, | |
| 323 | 224 | 'api_version' => ELEMENTOR_VERSION, |
| 324 | 225 | 'site_lang' => get_bloginfo( 'language' ), |
| 325 | - ], | |
| 326 | - false, | |
| 327 | - '', | |
| 328 | - 'json' | |
| 226 | + ] | |
| 329 | 227 | ); |
| 330 | 228 | } |
| 331 | 229 | |
| 332 | 230 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 231 | + * get_text_to_image | |
| 335 | 232 | * @param $prompt |
| 336 | 233 | * @param $prompt_settings |
| 337 | 234 | * |
| 338 | 235 | * @return mixed|\WP_Error |
| 339 | 236 | */ |
| 340 | - public function get_text_to_image( $data, $context, $request_ids ) { | |
| 237 | + public function get_text_to_image( $prompt, $prompt_settings, $context = [] ) { | |
| 341 | 238 | return $this->ai_request( |
| 342 | 239 | 'POST', |
| 343 | 240 | 'image/text-to-image', |
| 344 | 241 | [ |
| 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 ], | |
| 242 | + self::PROMPT => $prompt, | |
| 243 | + self::IMAGE_TYPE => $prompt_settings[ self::IMAGE_TYPE ] . '/' . $prompt_settings[ self::STYLE_PRESET ], | |
| 244 | + self::ASPECT_RATIO => $prompt_settings[ self::ASPECT_RATIO ], | |
| 348 | 245 | 'context' => wp_json_encode( $context ), |
| 349 | - 'ids' => $request_ids, | |
| 350 | 246 | 'api_version' => ELEMENTOR_VERSION, |
| 351 | 247 | 'site_lang' => get_bloginfo( 'language' ), |
| 352 | - ], | |
| 353 | - false, | |
| 354 | - '', | |
| 355 | - 'json' | |
| 248 | + ] | |
| 356 | 249 | ); |
| 357 | 250 | } |
| 358 | 251 | |
| 359 | 252 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 253 | + * get_image_to_image | |
| 254 | + * @param $image_data | |
| 361 | 255 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 256 | * @return mixed|\WP_Error |
| 257 | + * @throws \Exception | |
| 366 | 258 | */ |
| 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 ) { | |
| 259 | + public function get_image_to_image( $image_data, $context = [] ) { | |
| 396 | 260 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 261 | |
| 398 | 262 | if ( ! $image_file ) { |
| 399 | 263 | throw new \Exception( 'Image file not found' ); |
| @@ -407,9 +271,8 @@ | ||
| 407 | 271 | self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ], |
| 408 | 272 | self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ], |
| 409 | 273 | self::ASPECT_RATIO => $image_data['promptSettings'][ self::ASPECT_RATIO ], |
| 410 | 274 | 'context' => wp_json_encode( $context ), |
| 411 | - 'ids' => $request_ids, | |
| 412 | 275 | 'api_version' => ELEMENTOR_VERSION, |
| 413 | 276 | 'site_lang' => get_bloginfo( 'language' ), |
| 414 | 277 | ], |
| 415 | 278 | $image_file, |
| @@ -418,87 +281,16 @@ | ||
| 418 | 281 | |
| 419 | 282 | return $result; |
| 420 | 283 | } |
| 421 | 284 | |
| 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 | 285 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 286 | + * get_image_to_image_upscale | |
| 287 | + * @param $image_data | |
| 493 | 288 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 289 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 290 | + * @throws \Exception | |
| 499 | 291 | */ |
| 500 | - public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { | |
| 292 | + public function get_image_to_image_upscale( $image_data, $context = [] ) { | |
| 501 | 293 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 294 | |
| 503 | 295 | if ( ! $image_file ) { |
| 504 | 296 | throw new \Exception( 'Image file not found' ); |
| @@ -509,9 +301,8 @@ | ||
| 509 | 301 | 'image/image-to-image/upscale', |
| 510 | 302 | [ |
| 511 | 303 | self::IMAGE_RESOLUTION => $image_data['promptSettings']['upscale_to'], |
| 512 | 304 | 'context' => wp_json_encode( $context ), |
| 513 | - 'ids' => $request_ids, | |
| 514 | 305 | 'api_version' => ELEMENTOR_VERSION, |
| 515 | 306 | 'site_lang' => get_bloginfo( 'language' ), |
| 516 | 307 | ], |
| 517 | 308 | $image_file, |
| @@ -521,17 +312,15 @@ | ||
| 521 | 312 | return $result; |
| 522 | 313 | } |
| 523 | 314 | |
| 524 | 315 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 316 | + * get_image_to_image_remove_background | |
| 317 | + * @param $image_data | |
| 526 | 318 | * |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | 319 | * @return mixed|\WP_Error |
| 531 | - * @throws \Exception If image file not found. | |
| 320 | + * @throws \Exception | |
| 532 | 321 | */ |
| 533 | - public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { | |
| 322 | + public function get_image_to_image_remove_background( $image_data, $context = [] ) { | |
| 534 | 323 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 535 | 324 | |
| 536 | 325 | if ( ! $image_file ) { |
| 537 | 326 | throw new \Exception( 'Image file not found' ); |
| @@ -541,9 +330,8 @@ | ||
| 541 | 330 | 'POST', |
| 542 | 331 | 'image/image-to-image/remove-background', |
| 543 | 332 | [ |
| 544 | 333 | 'context' => wp_json_encode( $context ), |
| 545 | - 'ids' => $request_ids, | |
| 546 | 334 | 'api_version' => ELEMENTOR_VERSION, |
| 547 | 335 | 'site_lang' => get_bloginfo( 'language' ), |
| 548 | 336 | ], |
| 549 | 337 | $image_file, |
| @@ -553,17 +341,15 @@ | ||
| 553 | 341 | return $result; |
| 554 | 342 | } |
| 555 | 343 | |
| 556 | 344 | /** |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 345 | + * get_image_to_image_remove_text | |
| 346 | + * @param $image_data | |
| 558 | 347 | * |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | 348 | * @return mixed|\WP_Error |
| 563 | - * @throws \Exception If image file not found. | |
| 349 | + * @throws \Exception | |
| 564 | 350 | */ |
| 565 | - public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { | |
| 351 | + public function get_image_to_image_replace_background( $image_data, $context = [] ) { | |
| 566 | 352 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 567 | 353 | |
| 568 | 354 | if ( ! $image_file ) { |
| 569 | 355 | throw new \Exception( 'Image file not found' ); |
| @@ -574,9 +360,8 @@ | ||
| 574 | 360 | 'image/image-to-image/replace-background', |
| 575 | 361 | [ |
| 576 | 362 | self::PROMPT => $image_data[ self::PROMPT ], |
| 577 | 363 | 'context' => wp_json_encode( $context ), |
| 578 | - 'ids' => $request_ids, | |
| 579 | 364 | 'api_version' => ELEMENTOR_VERSION, |
| 580 | 365 | 'site_lang' => get_bloginfo( 'language' ), |
| 581 | 366 | ], |
| 582 | 367 | $image_file, |
| @@ -586,11 +371,10 @@ | ||
| 586 | 371 | return $result; |
| 587 | 372 | } |
| 588 | 373 | |
| 589 | 374 | /** |
| 590 | - * Store Temp File store_temp_file | |
| 375 | + * store_temp_file | |
| 591 | 376 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 377 | * @param $file_content |
| 594 | 378 | * @param $file_ext |
| 595 | 379 | * |
| 596 | 380 | * @return string |
| @@ -607,17 +391,15 @@ | ||
| 607 | 391 | return $temp_file; |
| 608 | 392 | } |
| 609 | 393 | |
| 610 | 394 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 395 | + * get_image_to_image_out_painting | |
| 396 | + * @param $image_data | |
| 612 | 397 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 398 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 399 | + * @throws \Exception | |
| 618 | 400 | */ |
| 619 | - public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { | |
| 401 | + public function get_image_to_image_out_painting( $image_data, $context = [] ) { | |
| 620 | 402 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 403 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| 622 | 404 | $img_content = base64_decode( $img_content ); |
| 623 | 405 | $mask_file = $this->store_temp_file( $img_content, '.png' ); |
| @@ -629,16 +411,13 @@ | ||
| 629 | 411 | $result = $this->ai_request( |
| 630 | 412 | 'POST', |
| 631 | 413 | 'image/image-to-image/outpainting', |
| 632 | 414 | [ |
| 415 | + self::PROMPT => $image_data[ self::PROMPT ], | |
| 416 | + self::IMAGE_TYPE => '', | |
| 633 | 417 | 'context' => wp_json_encode( $context ), |
| 634 | - 'ids' => $request_ids, | |
| 635 | 418 | 'api_version' => ELEMENTOR_VERSION, |
| 636 | 419 | '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 | 420 | ], |
| 642 | 421 | [ |
| 643 | 422 | [ |
| 644 | 423 | 'name' => 'image', |
| @@ -651,17 +430,15 @@ | ||
| 651 | 430 | return $result; |
| 652 | 431 | } |
| 653 | 432 | |
| 654 | 433 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 434 | + * get_image_to_image_mask | |
| 435 | + * @param $image_data | |
| 656 | 436 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 437 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 438 | + * @throws \Exception | |
| 662 | 439 | */ |
| 663 | - public function get_image_to_image_mask( $image_data, $context, $request_ids ) { | |
| 440 | + public function get_image_to_image_mask( $image_data, $context = [] ) { | |
| 664 | 441 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 442 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| 666 | 443 | |
| 667 | 444 | if ( ! $image_file ) { |
| @@ -676,13 +453,13 @@ | ||
| 676 | 453 | 'POST', |
| 677 | 454 | 'image/image-to-image/inpainting', |
| 678 | 455 | [ |
| 679 | 456 | self::PROMPT => $image_data[ self::PROMPT ], |
| 457 | + self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ], | |
| 458 | + self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ], | |
| 680 | 459 | 'context' => wp_json_encode( $context ), |
| 681 | - 'ids' => $request_ids, | |
| 682 | 460 | 'api_version' => ELEMENTOR_VERSION, |
| 683 | 461 | 'site_lang' => get_bloginfo( 'language' ), |
| 684 | - 'image_base64' => $image_data['image_base64'], | |
| 685 | 462 | ], |
| 686 | 463 | [ |
| 687 | 464 | [ |
| 688 | 465 | 'name' => 'image', |
| @@ -698,148 +475,33 @@ | ||
| 698 | 475 | ); |
| 699 | 476 | |
| 700 | 477 | return $result; |
| 701 | 478 | } |
| 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 | 479 | |
| 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( | |
| 480 | + public function generate_layout( $prompt, $context, $variation_type ) { | |
| 481 | + return $this->ai_request( | |
| 715 | 482 | 'POST', |
| 716 | - 'image/image-to-image/cleanup', | |
| 483 | + 'generate/layout', | |
| 717 | 484 | [ |
| 718 | - self::PROMPT => $image_data[ self::PROMPT ], | |
| 719 | - 'context' => wp_json_encode( $context ), | |
| 720 | - 'ids' => $request_ids, | |
| 485 | + 'prompt' => $prompt, | |
| 486 | + 'context' => $context ?? [], | |
| 721 | 487 | 'api_version' => ELEMENTOR_VERSION, |
| 722 | 488 | '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 | - ], | |
| 489 | + 'variationType' => (int) $variation_type, | |
| 736 | 490 | ] |
| 737 | 491 | ); |
| 738 | - | |
| 739 | - return $result; | |
| 740 | 492 | } |
| 741 | 493 | |
| 742 | - public function generate_layout( $data, $context ) { | |
| 743 | - $endpoint = 'generate/layout'; | |
| 744 | - | |
| 745 | - $body = [ | |
| 746 | - 'prompt' => $data['prompt'], | |
| 747 | - 'variationType' => (int) $data['variationType'], | |
| 748 | - 'ids' => $data['ids'], | |
| 749 | - ]; | |
| 750 | - | |
| 751 | - if ( ! empty( $data['prevGeneratedIds'] ) ) { | |
| 752 | - $body['generatedBaseTemplatesIds'] = $data['prevGeneratedIds']; | |
| 753 | - } | |
| 754 | - | |
| 755 | - if ( ! empty( $data['attachments'] ) ) { | |
| 756 | - $attachment = $data['attachments'][0]; | |
| 757 | - | |
| 758 | - switch ( $attachment['type'] ) { | |
| 759 | - case 'json': | |
| 760 | - $endpoint = 'generate/generate-json-variation'; | |
| 761 | - | |
| 762 | - $body['json'] = [ | |
| 763 | - 'type' => 'elementor', | |
| 764 | - 'elements' => [ $attachment['content'] ], | |
| 765 | - 'label' => $attachment['label'], | |
| 766 | - 'source' => $attachment['source'], | |
| 767 | - ]; | |
| 768 | - break; | |
| 769 | - case 'url': | |
| 770 | - $endpoint = 'generate/html-to-elementor'; | |
| 771 | - | |
| 772 | - $html = wp_json_encode( $attachment['content'] ); | |
| 773 | - | |
| 774 | - $body['html'] = $html; | |
| 775 | - $body['htmlFetchedUrl'] = $attachment['label']; | |
| 776 | - | |
| 777 | - break; | |
| 778 | - } | |
| 779 | - } | |
| 780 | - | |
| 781 | - $context['currentContext'] = $data['currentContext']; | |
| 782 | - $context['features'] = [ | |
| 783 | - 'supportedFeatures' => [ 'Taxonomy' ], | |
| 784 | - ]; | |
| 785 | - | |
| 786 | - if ( ElementorUtils::has_pro() ) { | |
| 787 | - $context['features']['subscriptions'] = [ 'Pro' ]; | |
| 788 | - } | |
| 789 | - | |
| 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 | - $metadata = [ | |
| 803 | - 'context' => $context, | |
| 804 | - 'api_version' => ELEMENTOR_VERSION, | |
| 805 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 806 | - 'config' => [ | |
| 807 | - 'generate' => [ | |
| 808 | - 'all' => true, | |
| 809 | - ], | |
| 810 | - ], | |
| 811 | - ]; | |
| 812 | - | |
| 813 | - $body = array_merge( $body, $metadata ); | |
| 814 | - | |
| 815 | - // Temp hack for platforms that filters the http_request_args, and it breaks JSON requests. | |
| 816 | - remove_all_filters( 'http_request_args' ); | |
| 817 | - | |
| 494 | + public function get_layout_prompt_enhanced( $prompt, $context = [] ) { | |
| 818 | 495 | return $this->ai_request( |
| 819 | 496 | 'POST', |
| 820 | - $endpoint, | |
| 821 | - $body, | |
| 822 | - false, | |
| 823 | - '', | |
| 824 | - 'json' | |
| 825 | - ); | |
| 826 | - } | |
| 827 | - | |
| 828 | - public function get_layout_prompt_enhanced( $prompt, $enhance_type, $context ) { | |
| 829 | - return $this->ai_request( | |
| 830 | - 'POST', | |
| 831 | 497 | 'generate/enhance-prompt', |
| 832 | 498 | [ |
| 833 | 499 | 'prompt' => $prompt, |
| 834 | - 'enhance_type' => $enhance_type, | |
| 835 | 500 | 'context' => wp_json_encode( $context ), |
| 836 | 501 | 'api_version' => ELEMENTOR_VERSION, |
| 837 | 502 | 'site_lang' => get_bloginfo( 'language' ), |
| 838 | - ], | |
| 839 | - false, | |
| 840 | - '', | |
| 841 | - 'json' | |
| 503 | + ] | |
| 842 | 504 | ); |
| 843 | 505 | } |
| 844 | 506 | |
| 845 | 507 | public function get_history_by_type( $type, $page, $limit, $context = [] ) { |
| @@ -872,34 +534,15 @@ | ||
| 872 | 534 | ); |
| 873 | 535 | } |
| 874 | 536 | |
| 875 | 537 | public function toggle_favorite_history_item( $id, $context = [] ) { |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | 538 | return $this->ai_request( |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 539 | + 'POST', sprintf( 'history/%s/favorite', $id ), | |
| 879 | 540 | [ |
| 880 | 541 | 'context' => wp_json_encode( $context ), |
| 881 | 542 | 'api_version' => ELEMENTOR_VERSION, |
| 882 | 543 | 'site_lang' => get_bloginfo( 'language' ), |
| 883 | 544 | ] |
| 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 | 545 | ); |
| 903 | 546 | } |
| 904 | 547 | |
| 905 | 548 | protected function init() {} |