diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index d4b40afc7bb6b..b03c2bd55eefe 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -222,6 +222,16 @@ function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me // Store the original image file name in image_meta. $image_meta['original_image'] = wp_basename( $original_file ); + // Update the post mime type when the new image was saved in a different format. + if ( isset( $saved_data['mime-type'] ) && get_post_mime_type( $attachment_id ) !== $saved_data['mime-type'] ) { + wp_update_post( + array( + 'ID' => $attachment_id, + 'post_mime_type' => $saved_data['mime-type'], + ) + ); + } + return $image_meta; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 2a14083968635..2ea3668c6473c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -2867,9 +2867,11 @@ public function sideload_item( WP_REST_Request $request ) { * The server writes sub-sizes in the attachment's output format without * checking it against the network's allowed upload file types, so a * sideload in that format is exempt from them too. Resolved before the - * output format filter below is added. + * output format filter below is added. When the filter leaves the format + * unchanged, as for a main file the server already converted, that is the + * attachment's own format. */ - $output_format = wp_attachment_is_image( $post ) ? $this->get_image_output_format( $post ) : null; + $output_format = wp_attachment_is_image( $post ) ? ( $this->get_image_output_format( $post ) ?? get_post_mime_type( $post ) ) : null; $output_extensions = null !== $output_format ? array_search( $output_format, wp_get_mime_types(), true ) : false; $filter_filetypes = null; diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index af84065113267..1c9a12dcbc61c 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -7657,6 +7657,7 @@ public function test_wp_img_tag_add_auto_sizes( string $input, string $expected * * @ticket 62305 * @ticket 62359 + * @ticket 53665 * * @dataProvider data_image_converted_to_other_format_has_correct_filename * @@ -7692,6 +7693,7 @@ public function test_heic_image_upload_is_converted_to_jpeg( bool $apply_big_ima $this->assertSame( "test-image{$scaled_suffix}.jpg", basename( $image_meta['file'] ), "The file name is expected to be test-image{$scaled_suffix}.jpg" ); $this->assertSame( 'test-image.heic', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' ); $this->assertSame( 'image/jpeg', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/jpeg.' ); + $this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ), 'The attachment mime type is expected to be image/jpeg.' ); } /** @@ -7699,6 +7701,7 @@ public function test_heic_image_upload_is_converted_to_jpeg( bool $apply_big_ima * * @ticket 62305 * @ticket 62359 + * @ticket 53665 * * @dataProvider data_image_converted_to_other_format_has_correct_filename * @@ -7737,6 +7740,7 @@ public function test_jpeg_image_converts_to_webp_when_filtered( bool $apply_big_ $this->assertSame( "33772{$scaled_suffix}.webp", basename( $image_meta['file'] ), "The file name is expected to be 33772{$scaled_suffix}.webp." ); $this->assertSame( '33772.jpg', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' ); $this->assertSame( 'image/webp', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/webp.' ); + $this->assertSame( 'image/webp', get_post_mime_type( $attachment_id ), 'The attachment mime type is expected to be image/webp.' ); } /**