| @@ -1,14 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Modules\Ai\Connect; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 5 | -use Elementor\Modules\Ai\Module; | |
| 6 | -use Elementor\Utils as ElementorUtils; | |
| 7 | -use Elementor\Plugin; | |
| 8 | 5 | |
| 9 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | - exit; // Exit if accessed directly. | |
| 7 | + exit; // Exit if accessed directly | |
| 11 | 8 | } |
| 12 | 9 | |
| 13 | 10 | class Ai extends Library { |
| 14 | 11 | const API_URL = 'https://my.elementor.com/api/v2/ai/'; |
| @@ -18,10 +15,8 @@ | ||
| 18 | 15 | const IMAGE_STRENGTH = 'image_strength'; |
| 19 | 16 | const ASPECT_RATIO = 'ratio'; |
| 20 | 17 | const IMAGE_RESOLUTION = 'image_resolution'; |
| 21 | 18 | |
| 22 | - const IMAGE_BACKGROUND_COLOR = 'background_color'; | |
| 23 | - | |
| 24 | 19 | const PROMPT = 'prompt'; |
| 25 | 20 | |
| 26 | 21 | public function get_title() { |
| 27 | 22 | return esc_html__( 'AI', 'elementor' ); |
| @@ -41,66 +36,10 @@ | ||
| 41 | 36 | ] |
| 42 | 37 | ); |
| 43 | 38 | } |
| 44 | 39 | |
| 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 | 40 | /** |
| 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 | - * | |
| 41 | + * get_file_payload | |
| 103 | 42 | * @param $filename |
| 104 | 43 | * @param $file_type |
| 105 | 44 | * @param $file_path |
| 106 | 45 | * @param $boundary |
| @@ -113,9 +52,9 @@ | ||
| 113 | 52 | $payload = ''; |
| 114 | 53 | // Upload the file |
| 115 | 54 | $payload .= '--' . $boundary; |
| 116 | 55 | $payload .= "\r\n"; |
| 117 | - $payload .= 'Content-Disposition: form-data; name="' . esc_attr( $name ) . '"; filename="' . esc_attr( $name ) . '"' . "\r\n"; | |
| 56 | + $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . "\r\n"; | |
| 118 | 57 | $payload .= 'Content-Type: ' . $mine_type . "\r\n"; |
| 119 | 58 | $payload .= "\r\n"; |
| 120 | 59 | $payload .= file_get_contents( $file_path ); |
| 121 | 60 | $payload .= "\r\n"; |
| @@ -128,9 +67,9 @@ | ||
| 128 | 67 | // add all body fields as standard POST fields: |
| 129 | 68 | foreach ( $body as $name => $value ) { |
| 130 | 69 | $payload .= '--' . $boundary; |
| 131 | 70 | $payload .= "\r\n"; |
| 132 | - $payload .= 'Content-Disposition: form-data; name="' . esc_attr( $name ) . '"' . "\r\n\r\n"; | |
| 71 | + $payload .= 'Content-Disposition: form-data; name="' . $name . '"' . "\r\n\r\n"; | |
| 133 | 72 | $payload .= $value; |
| 134 | 73 | $payload .= "\r\n"; |
| 135 | 74 | } |
| 136 | 75 | |
| @@ -152,9 +91,9 @@ | ||
| 152 | 91 | |
| 153 | 92 | return $payload; |
| 154 | 93 | } |
| 155 | 94 | |
| 156 | - private function ai_request( $method, $endpoint, $body, $file = false, $file_name = '', $format = 'default' ) { | |
| 95 | + private function ai_request( $method, $endpoint, $body, $file = false, $file_name = '' ) { | |
| 157 | 96 | $headers = [ |
| 158 | 97 | 'x-elementor-ai-version' => '2', |
| 159 | 98 | ]; |
| 160 | 99 | |
| @@ -162,11 +101,8 @@ | ||
| 162 | 101 | $boundary = wp_generate_password( 24, false ); |
| 163 | 102 | $body = $this->get_upload_request_body( $body, $file, $boundary, $file_name ); |
| 164 | 103 | // add content type header |
| 165 | 104 | $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 | 105 | } |
| 170 | 106 | |
| 171 | 107 | return $this->http_request( |
| 172 | 108 | $method, |
| @@ -171,16 +107,14 @@ | ||
| 171 | 107 | return $this->http_request( |
| 172 | 108 | $method, |
| 173 | 109 | $endpoint, |
| 174 | 110 | [ |
| 175 | - 'timeout' => 100, | |
| 111 | + 'timeout' => 50, | |
| 176 | 112 | 'headers' => $headers, |
| 177 | 113 | 'body' => $body, |
| 178 | - | |
| 179 | 114 | ], |
| 180 | 115 | [ |
| 181 | 116 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 182 | - 'with_error_data' => true, | |
| 183 | 117 | ] |
| 184 | 118 | ); |
| 185 | 119 | } |
| 186 | 120 | |
| @@ -216,59 +150,32 @@ | ||
| 216 | 150 | ] |
| 217 | 151 | ); |
| 218 | 152 | } |
| 219 | 153 | |
| 220 | - public function get_completion_text( $prompt, $context, $request_ids ) { | |
| 154 | + public function get_completion_text( $prompt ) { | |
| 221 | 155 | return $this->ai_request( |
| 222 | 156 | 'POST', |
| 223 | 157 | 'text/completion', |
| 224 | 158 | [ |
| 225 | 159 | 'prompt' => $prompt, |
| 226 | - 'context' => wp_json_encode( $context ), | |
| 227 | - 'ids' => $request_ids, | |
| 228 | 160 | 'api_version' => ELEMENTOR_VERSION, |
| 229 | 161 | 'site_lang' => get_bloginfo( 'language' ), |
| 230 | - ], | |
| 231 | - false, | |
| 232 | - '', | |
| 233 | - 'json' | |
| 162 | + ] | |
| 234 | 163 | ); |
| 235 | 164 | } |
| 236 | 165 | |
| 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 | 166 | /** |
| 257 | - * Get Image Prompt Enhanced get_image_prompt_enhanced | |
| 258 | - * | |
| 167 | + * get_image_prompt_enhanced | |
| 259 | 168 | * @param $prompt |
| 260 | 169 | * |
| 261 | 170 | * @return mixed|\WP_Error |
| 262 | 171 | */ |
| 263 | - public function get_image_prompt_enhanced( $prompt, $context, $request_ids ) { | |
| 172 | + public function get_image_prompt_enhanced( $prompt ) { | |
| 264 | 173 | return $this->ai_request( |
| 265 | 174 | 'POST', |
| 266 | 175 | 'text/enhance-image-prompt', |
| 267 | 176 | [ |
| 268 | 177 | 'prompt' => $prompt, |
| 269 | - 'context' => wp_json_encode( $context ), | |
| 270 | - 'ids' => $request_ids, | |
| 271 | 178 | 'api_version' => ELEMENTOR_VERSION, |
| 272 | 179 | 'site_lang' => get_bloginfo( 'language' ), |
| 273 | 180 | ] |
| 274 | 181 | ); |
| @@ -273,127 +180,77 @@ | ||
| 273 | 180 | ] |
| 274 | 181 | ); |
| 275 | 182 | } |
| 276 | 183 | |
| 277 | - public function get_edit_text( $data, $context, $request_ids ) { | |
| 184 | + public function get_edit_text( $input, $instruction ) { | |
| 278 | 185 | return $this->ai_request( |
| 279 | 186 | 'POST', |
| 280 | 187 | 'text/edit', |
| 281 | 188 | [ |
| 282 | - 'input' => $data['payload']['input'], | |
| 283 | - 'instruction' => $data['payload']['instruction'], | |
| 284 | - 'context' => wp_json_encode( $context ), | |
| 285 | - 'ids' => $request_ids, | |
| 189 | + 'input' => $input, | |
| 190 | + 'instruction' => $instruction, | |
| 286 | 191 | 'api_version' => ELEMENTOR_VERSION, |
| 287 | 192 | 'site_lang' => get_bloginfo( 'language' ), |
| 288 | - ], | |
| 289 | - false, | |
| 290 | - '', | |
| 291 | - 'json' | |
| 193 | + ] | |
| 292 | 194 | ); |
| 293 | 195 | } |
| 294 | 196 | |
| 295 | - public function get_custom_code( $data, $context, $request_ids ) { | |
| 197 | + public function get_custom_code( $prompt, $language ) { | |
| 296 | 198 | return $this->ai_request( |
| 297 | 199 | 'POST', |
| 298 | 200 | 'text/custom-code', |
| 299 | 201 | [ |
| 300 | - 'prompt' => $data['payload']['prompt'], | |
| 301 | - 'language' => $data['payload']['language'], | |
| 302 | - 'context' => wp_json_encode( $context ), | |
| 303 | - 'ids' => $request_ids, | |
| 202 | + 'prompt' => $prompt, | |
| 203 | + 'language' => $language, | |
| 304 | 204 | 'api_version' => ELEMENTOR_VERSION, |
| 305 | 205 | 'site_lang' => get_bloginfo( 'language' ), |
| 306 | - ], | |
| 307 | - false, | |
| 308 | - '', | |
| 309 | - 'json' | |
| 206 | + ] | |
| 310 | 207 | ); |
| 311 | 208 | } |
| 312 | 209 | |
| 313 | - public function get_custom_css( $data, $context, $request_ids ) { | |
| 210 | + public function get_custom_css( $prompt, $html_markup, $element_id ) { | |
| 314 | 211 | return $this->ai_request( |
| 315 | 212 | 'POST', |
| 316 | 213 | 'text/custom-css', |
| 317 | 214 | [ |
| 318 | - 'prompt' => $data['payload']['prompt'], | |
| 319 | - 'html_markup' => $data['payload']['html_markup'], | |
| 320 | - 'element_id' => $data['payload']['element_id'], | |
| 321 | - 'context' => wp_json_encode( $context ), | |
| 322 | - 'ids' => $request_ids, | |
| 215 | + 'prompt' => $prompt, | |
| 216 | + 'html_markup' => $html_markup, | |
| 217 | + 'element_id' => $element_id, | |
| 323 | 218 | 'api_version' => ELEMENTOR_VERSION, |
| 324 | 219 | 'site_lang' => get_bloginfo( 'language' ), |
| 325 | - ], | |
| 326 | - false, | |
| 327 | - '', | |
| 328 | - 'json' | |
| 220 | + ] | |
| 329 | 221 | ); |
| 330 | 222 | } |
| 331 | 223 | |
| 332 | 224 | /** |
| 333 | - * Get text to image get_text_to_image | |
| 334 | - * | |
| 225 | + * get_text_to_image | |
| 335 | 226 | * @param $prompt |
| 336 | 227 | * @param $prompt_settings |
| 337 | 228 | * |
| 338 | 229 | * @return mixed|\WP_Error |
| 339 | 230 | */ |
| 340 | - public function get_text_to_image( $data, $context, $request_ids ) { | |
| 231 | + public function get_text_to_image( $prompt, $prompt_settings ) { | |
| 341 | 232 | return $this->ai_request( |
| 342 | 233 | 'POST', |
| 343 | 234 | 'image/text-to-image', |
| 344 | 235 | [ |
| 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 ], | |
| 348 | - 'context' => wp_json_encode( $context ), | |
| 349 | - 'ids' => $request_ids, | |
| 236 | + self::PROMPT => $prompt, | |
| 237 | + self::IMAGE_TYPE => $prompt_settings[ self::IMAGE_TYPE ] . '/' . $prompt_settings[ self::STYLE_PRESET ], | |
| 238 | + self::ASPECT_RATIO => $prompt_settings[ self::ASPECT_RATIO ], | |
| 350 | 239 | 'api_version' => ELEMENTOR_VERSION, |
| 351 | 240 | 'site_lang' => get_bloginfo( 'language' ), |
| 352 | - ], | |
| 353 | - false, | |
| 354 | - '', | |
| 355 | - 'json' | |
| 241 | + ] | |
| 356 | 242 | ); |
| 357 | 243 | } |
| 358 | 244 | |
| 359 | 245 | /** |
| 360 | - * Get_Featured_Image get_featured_image | |
| 246 | + * get_image_to_image | |
| 247 | + * @param $image_data | |
| 361 | 248 | * |
| 362 | - * @param $data | |
| 363 | - * @param $context | |
| 364 | - * @param $request_ids | |
| 365 | 249 | * @return mixed|\WP_Error |
| 250 | + * @throws \Exception | |
| 366 | 251 | */ |
| 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 ) { | |
| 252 | + public function get_image_to_image( $image_data ) { | |
| 396 | 253 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 397 | 254 | |
| 398 | 255 | if ( ! $image_file ) { |
| 399 | 256 | throw new \Exception( 'Image file not found' ); |
| @@ -406,10 +263,8 @@ | ||
| 406 | 263 | self::PROMPT => $image_data[ self::PROMPT ], |
| 407 | 264 | self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ], |
| 408 | 265 | self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ], |
| 409 | 266 | self::ASPECT_RATIO => $image_data['promptSettings'][ self::ASPECT_RATIO ], |
| 410 | - 'context' => wp_json_encode( $context ), | |
| 411 | - 'ids' => $request_ids, | |
| 412 | 267 | 'api_version' => ELEMENTOR_VERSION, |
| 413 | 268 | 'site_lang' => get_bloginfo( 'language' ), |
| 414 | 269 | ], |
| 415 | 270 | $image_file, |
| @@ -418,87 +273,16 @@ | ||
| 418 | 273 | |
| 419 | 274 | return $result; |
| 420 | 275 | } |
| 421 | 276 | |
| 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 | 277 | /** |
| 492 | - * Get Image To Image Upscale get_image_to_image_upscale | |
| 278 | + * get_image_to_image_upscale | |
| 279 | + * @param $image_data | |
| 493 | 280 | * |
| 494 | - * @param $image_data | |
| 495 | - * @param $context | |
| 496 | - * @param $request_ids | |
| 497 | 281 | * @return mixed|\WP_Error |
| 498 | - * @throws \Exception If image file not found. | |
| 282 | + * @throws \Exception | |
| 499 | 283 | */ |
| 500 | - public function get_image_to_image_upscale( $image_data, $context, $request_ids ) { | |
| 284 | + public function get_image_to_image_upscale( $image_data ) { | |
| 501 | 285 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 502 | 286 | |
| 503 | 287 | if ( ! $image_file ) { |
| 504 | 288 | throw new \Exception( 'Image file not found' ); |
| @@ -508,10 +292,8 @@ | ||
| 508 | 292 | 'POST', |
| 509 | 293 | 'image/image-to-image/upscale', |
| 510 | 294 | [ |
| 511 | 295 | self::IMAGE_RESOLUTION => $image_data['promptSettings']['upscale_to'], |
| 512 | - 'context' => wp_json_encode( $context ), | |
| 513 | - 'ids' => $request_ids, | |
| 514 | 296 | 'api_version' => ELEMENTOR_VERSION, |
| 515 | 297 | 'site_lang' => get_bloginfo( 'language' ), |
| 516 | 298 | ], |
| 517 | 299 | $image_file, |
| @@ -521,76 +303,10 @@ | ||
| 521 | 303 | return $result; |
| 522 | 304 | } |
| 523 | 305 | |
| 524 | 306 | /** |
| 525 | - * Get Image To Image Remove Background get_image_to_image_remove_background | |
| 526 | - * | |
| 527 | - * @param $image_data | |
| 528 | - * @param $context | |
| 529 | - * @param $request_ids | |
| 530 | - * @return mixed|\WP_Error | |
| 531 | - * @throws \Exception If image file not found. | |
| 532 | - */ | |
| 533 | - public function get_image_to_image_remove_background( $image_data, $context, $request_ids ) { | |
| 534 | - $image_file = get_attached_file( $image_data['attachment_id'] ); | |
| 535 | - | |
| 536 | - if ( ! $image_file ) { | |
| 537 | - throw new \Exception( 'Image file not found' ); | |
| 538 | - } | |
| 539 | - | |
| 540 | - $result = $this->ai_request( | |
| 541 | - 'POST', | |
| 542 | - 'image/image-to-image/remove-background', | |
| 543 | - [ | |
| 544 | - 'context' => wp_json_encode( $context ), | |
| 545 | - 'ids' => $request_ids, | |
| 546 | - 'api_version' => ELEMENTOR_VERSION, | |
| 547 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 548 | - ], | |
| 549 | - $image_file, | |
| 550 | - 'image' | |
| 551 | - ); | |
| 552 | - | |
| 553 | - return $result; | |
| 554 | - } | |
| 555 | - | |
| 556 | - /** | |
| 557 | - * Get Image To Image Remove Text get_image_to_image_remove_text | |
| 558 | - * | |
| 559 | - * @param $image_data | |
| 560 | - * @param $context | |
| 561 | - * @param $request_ids | |
| 562 | - * @return mixed|\WP_Error | |
| 563 | - * @throws \Exception If image file not found. | |
| 564 | - */ | |
| 565 | - public function get_image_to_image_replace_background( $image_data, $context, $request_ids ) { | |
| 566 | - $image_file = get_attached_file( $image_data['attachment_id'] ); | |
| 567 | - | |
| 568 | - if ( ! $image_file ) { | |
| 569 | - throw new \Exception( 'Image file not found' ); | |
| 570 | - } | |
| 571 | - | |
| 572 | - $result = $this->ai_request( | |
| 573 | - 'POST', | |
| 574 | - 'image/image-to-image/replace-background', | |
| 575 | - [ | |
| 576 | - self::PROMPT => $image_data[ self::PROMPT ], | |
| 577 | - 'context' => wp_json_encode( $context ), | |
| 578 | - 'ids' => $request_ids, | |
| 579 | - 'api_version' => ELEMENTOR_VERSION, | |
| 580 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 581 | - ], | |
| 582 | - $image_file, | |
| 583 | - 'image' | |
| 584 | - ); | |
| 585 | - | |
| 586 | - return $result; | |
| 587 | - } | |
| 588 | - | |
| 589 | - /** | |
| 590 | - * Store Temp File store_temp_file | |
| 307 | + * store_temp_file | |
| 591 | 308 | * used to store a temp file for the AI request and deletes it once the request is done |
| 592 | - * | |
| 593 | 309 | * @param $file_content |
| 594 | 310 | * @param $file_ext |
| 595 | 311 | * |
| 596 | 312 | * @return string |
| @@ -595,29 +311,25 @@ | ||
| 595 | 311 | * |
| 596 | 312 | * @return string |
| 597 | 313 | */ |
| 598 | 314 | private function store_temp_file( $file_content, $file_ext = '' ) { |
| 599 | - $temp_file = str_replace( '.tmp', '', wp_tempnam() . $file_ext ); | |
| 315 | + $temp_file = tempnam( sys_get_temp_dir(), 'ai' ) . $file_ext; | |
| 600 | 316 | file_put_contents( $temp_file, $file_content ); |
| 601 | 317 | // make sure the temp file is deleted on shutdown |
| 602 | 318 | register_shutdown_function( function () use ( $temp_file ) { |
| 603 | - if ( file_exists( $temp_file ) ) { | |
| 604 | - unlink( $temp_file ); | |
| 605 | - } | |
| 319 | + @unlink( $temp_file ); | |
| 606 | 320 | } ); |
| 607 | 321 | return $temp_file; |
| 608 | 322 | } |
| 609 | 323 | |
| 610 | 324 | /** |
| 611 | - * Get Image To Image Out Painting get_image_to_image_out_painting | |
| 325 | + * get_image_to_image_out_painting | |
| 326 | + * @param $image_data | |
| 612 | 327 | * |
| 613 | - * @param $image_data | |
| 614 | - * @param $context | |
| 615 | - * @param $request_ids | |
| 616 | 328 | * @return mixed|\WP_Error |
| 617 | - * @throws \Exception If image file not found. | |
| 329 | + * @throws \Exception | |
| 618 | 330 | */ |
| 619 | - public function get_image_to_image_out_painting( $image_data, $context, $request_ids ) { | |
| 331 | + public function get_image_to_image_out_painting( $image_data ) { | |
| 620 | 332 | $img_content = str_replace( ' ', '+', $image_data['mask'] ); |
| 621 | 333 | $img_content = substr( $img_content, strpos( $img_content, ',' ) + 1 ); |
| 622 | 334 | $img_content = base64_decode( $img_content ); |
| 623 | 335 | $mask_file = $this->store_temp_file( $img_content, '.png' ); |
| @@ -629,16 +341,12 @@ | ||
| 629 | 341 | $result = $this->ai_request( |
| 630 | 342 | 'POST', |
| 631 | 343 | 'image/image-to-image/outpainting', |
| 632 | 344 | [ |
| 633 | - 'context' => wp_json_encode( $context ), | |
| 634 | - 'ids' => $request_ids, | |
| 345 | + self::PROMPT => $image_data[ self::PROMPT ], | |
| 346 | + self::IMAGE_TYPE => '', | |
| 635 | 347 | 'api_version' => ELEMENTOR_VERSION, |
| 636 | 348 | '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 | 349 | ], |
| 642 | 350 | [ |
| 643 | 351 | [ |
| 644 | 352 | 'name' => 'image', |
| @@ -651,17 +359,15 @@ | ||
| 651 | 359 | return $result; |
| 652 | 360 | } |
| 653 | 361 | |
| 654 | 362 | /** |
| 655 | - * Get Image To Image Mask get_image_to_image_mask | |
| 363 | + * get_image_to_image_mask | |
| 364 | + * @param $image_data | |
| 656 | 365 | * |
| 657 | - * @param $image_data | |
| 658 | - * @param $context | |
| 659 | - * @param $request_ids | |
| 660 | 366 | * @return mixed|\WP_Error |
| 661 | - * @throws \Exception If image file not found. | |
| 367 | + * @throws \Exception | |
| 662 | 368 | */ |
| 663 | - public function get_image_to_image_mask( $image_data, $context, $request_ids ) { | |
| 369 | + public function get_image_to_image_mask( $image_data ) { | |
| 664 | 370 | $image_file = get_attached_file( $image_data['attachment_id'] ); |
| 665 | 371 | $mask_file = $this->store_temp_file( $image_data['mask'], '.svg' ); |
| 666 | 372 | |
| 667 | 373 | if ( ! $image_file ) { |
| @@ -676,13 +382,12 @@ | ||
| 676 | 382 | 'POST', |
| 677 | 383 | 'image/image-to-image/inpainting', |
| 678 | 384 | [ |
| 679 | 385 | self::PROMPT => $image_data[ self::PROMPT ], |
| 680 | - 'context' => wp_json_encode( $context ), | |
| 681 | - 'ids' => $request_ids, | |
| 386 | + self::IMAGE_TYPE => $image_data['promptSettings'][ self::IMAGE_TYPE ] . '/' . $image_data['promptSettings'][ self::STYLE_PRESET ], | |
| 387 | + self::IMAGE_STRENGTH => $image_data['promptSettings'][ self::IMAGE_STRENGTH ], | |
| 682 | 388 | 'api_version' => ELEMENTOR_VERSION, |
| 683 | 389 | 'site_lang' => get_bloginfo( 'language' ), |
| 684 | - 'image_base64' => $image_data['image_base64'], | |
| 685 | 390 | ], |
| 686 | 391 | [ |
| 687 | 392 | [ |
| 688 | 393 | 'name' => 'image', |
| @@ -697,210 +402,8 @@ | ||
| 697 | 402 | ] |
| 698 | 403 | ); |
| 699 | 404 | |
| 700 | 405 | return $result; |
| 701 | - } | |
| 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 | - | |
| 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 | - 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 | - | |
| 818 | - return $this->ai_request( | |
| 819 | - '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 | - 'generate/enhance-prompt', | |
| 832 | - [ | |
| 833 | - 'prompt' => $prompt, | |
| 834 | - 'enhance_type' => $enhance_type, | |
| 835 | - 'context' => wp_json_encode( $context ), | |
| 836 | - 'api_version' => ELEMENTOR_VERSION, | |
| 837 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 838 | - ], | |
| 839 | - false, | |
| 840 | - '', | |
| 841 | - 'json' | |
| 842 | - ); | |
| 843 | - } | |
| 844 | - | |
| 845 | - public function get_history_by_type( $type, $page, $limit, $context = [] ) { | |
| 846 | - $endpoint = Module::HISTORY_TYPE_ALL === $type | |
| 847 | - ? 'history' | |
| 848 | - : add_query_arg( [ | |
| 849 | - 'page' => $page, | |
| 850 | - 'limit' => $limit, | |
| 851 | - ], "history/{$type}" ); | |
| 852 | - | |
| 853 | - return $this->ai_request( | |
| 854 | - 'POST', | |
| 855 | - $endpoint, | |
| 856 | - [ | |
| 857 | - 'context' => wp_json_encode( $context ), | |
| 858 | - 'api_version' => ELEMENTOR_VERSION, | |
| 859 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 860 | - ] | |
| 861 | - ); | |
| 862 | - } | |
| 863 | - | |
| 864 | - public function delete_history_item( $id, $context = [] ) { | |
| 865 | - return $this->ai_request( | |
| 866 | - 'DELETE', 'history/' . $id, | |
| 867 | - [ | |
| 868 | - 'context' => wp_json_encode( $context ), | |
| 869 | - 'api_version' => ELEMENTOR_VERSION, | |
| 870 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 871 | - ] | |
| 872 | - ); | |
| 873 | - } | |
| 874 | - | |
| 875 | - public function toggle_favorite_history_item( $id, $context = [] ) { | |
| 876 | - $sanitized_id = str_replace( '%', '%%', $id ); | |
| 877 | - return $this->ai_request( | |
| 878 | - 'POST', sprintf( 'history/%s/favorite', $sanitized_id ), | |
| 879 | - [ | |
| 880 | - 'context' => wp_json_encode( $context ), | |
| 881 | - 'api_version' => ELEMENTOR_VERSION, | |
| 882 | - 'site_lang' => get_bloginfo( 'language' ), | |
| 883 | - ] | |
| 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 | - ); | |
| 903 | 406 | } |
| 904 | 407 | |
| 905 | 408 | protected function init() {} |
| 906 | 409 | } |