| @@ -19,8 +19,9 @@ | ||
| 19 | 19 | use LearnPress\TemplateHooks\Admin\AI\AdminEditWithAITemplate; |
| 20 | 20 | use LP_Helper; |
| 21 | 21 | use LP_Request; |
| 22 | 22 | use LP_REST_Response; |
| 23 | +use LP_WP_Filesystem; | |
| 23 | 24 | use Throwable; |
| 24 | 25 | |
| 25 | 26 | /** |
| 26 | 27 | * class OpenAiAjax |
| @@ -205,10 +206,9 @@ | ||
| 205 | 206 | $course_edit_url = $coursePostModel->get_edit_link(); |
| 206 | 207 | if ( $is_course_builder ) { |
| 207 | 208 | $course_edit_url = \LearnPress\CourseBuilder\CourseBuilder::get_tab_link( |
| 208 | 209 | 'courses', |
| 209 | - $coursePostModel->get_id(), | |
| 210 | - 'overview' | |
| 210 | + $coursePostModel->get_id() | |
| 211 | 211 | ); |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | $response->data->edit_course_url = $course_edit_url; |
| @@ -371,9 +371,9 @@ | ||
| 371 | 371 | $prompt = $params['lp-openai-prompt-generated-field'] ?? ''; |
| 372 | 372 | $args = [ |
| 373 | 373 | 'prompt' => $prompt, |
| 374 | 374 | 'n' => intval( $params['outputs'] ?? 1 ), |
| 375 | - 'size' => $params['size'] ?? '', | |
| 375 | + 'size' => '1024x1024', | |
| 376 | 376 | ]; |
| 377 | 377 | |
| 378 | 378 | $result = OpenAiService::instance()->send_request_create_image( $args ); |
| 379 | 379 | $html_image = ''; |
| @@ -436,9 +436,9 @@ | ||
| 436 | 436 | * Apply image feature to post |
| 437 | 437 | * Upload image to media and set as feature image for post |
| 438 | 438 | * |
| 439 | 439 | * @since 4.3.0 |
| 440 | - * @version 1.0.1 | |
| 440 | + * @version 1.0.2 | |
| 441 | 441 | */ |
| 442 | 442 | public function openai_apply_image_feature() { |
| 443 | 443 | $response = new LP_REST_Response(); |
| 444 | 444 | |
| @@ -492,17 +492,26 @@ | ||
| 492 | 492 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 493 | 493 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | - if ( ! empty( $image_url ) ) { | |
| 497 | - $tmp = download_url( $image_url ); | |
| 498 | - $fileExt = pathinfo( parse_url( $image_url, PHP_URL_PATH ), PATHINFO_EXTENSION ); | |
| 499 | - $filename = sanitize_file_name( $post_slug . '-' . uniqid() . '.' . $fileExt ); | |
| 496 | + // model gpt-image-1 only return base64, so don't need to download from url | |
| 497 | + if ( ! empty( $image_base64 ) ) { | |
| 498 | + $decoded_image = base64_decode( $image_base64 ); | |
| 499 | + if ( false === $decoded_image || '' === $decoded_image ) { | |
| 500 | + throw new Exception( __( 'Invalid image data.', 'learnpress' ) ); | |
| 501 | + } | |
| 500 | 502 | |
| 501 | - } elseif ( ! empty( $image_base64 ) ) { | |
| 502 | - $decoded_image = base64_decode( $image_base64 ); | |
| 503 | - $tmp = wp_tempnam(); | |
| 504 | - file_put_contents( $tmp, $decoded_image ); | |
| 503 | + $image_info = @getimagesizefromstring( $decoded_image ); | |
| 504 | + if ( false === $image_info ) { | |
| 505 | + throw new Exception( __( 'Decoded data is not a valid image.', 'learnpress' ) ); | |
| 506 | + } | |
| 507 | + | |
| 508 | + $tmp = wp_tempnam(); | |
| 509 | + $lp_wp_filesystem = LP_WP_Filesystem::instance(); | |
| 510 | + $put_contents_result = $lp_wp_filesystem->put_contents( $tmp, $decoded_image ); | |
| 511 | + if ( ! $put_contents_result || ! is_readable( $tmp ) ) { | |
| 512 | + throw new Exception( __( 'Decoded image is not readable.', 'learnpress' ) ); | |
| 513 | + } | |
| 505 | 514 | $filename = sanitize_file_name( $post_slug . '-' . uniqid() . '.png' ); |
| 506 | 515 | } else { |
| 507 | 516 | throw new Exception( __( 'No image data provided.', 'learnpress' ) ); |
| 508 | 517 | } |