From 943fe18d44729021d3ad954cabfa87c36e2c78c3 Mon Sep 17 00:00:00 2001 From: Aditya Dhade <76063440+b1ink0@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:22:59 +0530 Subject: [PATCH 1/3] REST API: Skip main file conversion for client side media uploads. With client side media processing and an `image_editor_output_format` filter, the server converted the main file of the upload, and the browser then replaced it with its own scaled or rotated image. The sideload recorded the converted copy as `original_image`, so the upload stayed on disk after the attachment was deleted permanently. The main file of these uploads is now left unconverted, and the browser supplies the converted main file through the sideload endpoint. The output format filter is turned off only once the upload is stored, so its name still reserves the names of the converted files. Follow-up to [62805], [63014]. Fixes #66763. Co-Authored-By: Claude Opus 5.5 --- .../class-wp-rest-attachments-controller.php | 5 +++ .../rest-api/rest-attachments-controller.php | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+) 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..7bf51326a9dc8 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 @@ -634,6 +634,11 @@ public function create_item( $request ) { require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; + // The client sideloads the converted main file. + if ( false === $request['generate_sub_sizes'] ) { + add_filter( 'image_editor_output_format', '__return_empty_array', 100 ); + } + /* * Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta. * At this point the server may run out of resources and post-processing of uploaded images may fail. diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index 56a62748a1c80..27eaf97bde95c 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -4490,6 +4490,43 @@ static function () { $this->assertSame( array(), array_values( $remaining ), 'Deleting the attachment should leave no files behind.' ); } + /** + * Tests that the main file of a client-side upload is not converted when an + * `image_editor_output_format` filter maps it to another format, since the + * client sideloads the converted main file. + * + * @ticket 66763 + * + * @covers WP_REST_Attachments_Controller::create_item + * @requires function imagejpeg + */ + public function test_client_side_upload_main_file_is_not_converted() { + if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { + $this->markTestSkipped( 'This test requires an image editor that can save WebP.' ); + } + + $this->enable_client_side_media_processing(); + + wp_set_current_user( self::$author_id ); + + add_filter( 'image_editor_output_format', array( $this, 'filter_output_format_jpeg_to_webp' ) ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); + $request->set_header( 'Content-Type', 'image/jpeg' ); + $request->set_header( 'Content-Disposition', 'attachment; filename=client-side-photo.jpg' ); + $request->set_param( 'generate_sub_sizes', false ); + $request->set_body( file_get_contents( self::$test_file ) ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( 201, $response->get_status(), 'Uploading the image should succeed.' ); + $this->assertSame( 'image/webp', $data['image_output_format'], 'The client should still be asked to convert to WebP.' ); + + $attached_file = get_attached_file( $data['id'] ); + $this->assertSame( 'client-side-photo.jpg', wp_basename( $attached_file ), 'The upload should stay the attached file.' ); + $this->assertFileDoesNotExist( dirname( $attached_file ) . '/client-side-photo.webp', 'The server should not write a converted copy.' ); + } + /** * Tests that sideloading scaled image requires authentication. * From 4e20dc6b589680fa6df5253333c8f34280c52b71 Mon Sep 17 00:00:00 2001 From: Aditya Dhade <76063440+b1ink0@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:00:29 +0530 Subject: [PATCH 2/3] REST API: Add a client side upload flow test and document the main file change. Adds a test for the full client side flow with an output format filter, for a `scaled` and an `original` main file sideload: upload, sideload, finalize and permanent delete. It checks that the upload is kept as `original_image` and that no files are left after the delete. Also documents that the main image is not converted when `generate_sub_sizes` is false, in the parameter description and in `create_item()`, and explains in the inline comment why the filter is added after the upload is stored. See #66763. Co-Authored-By: Claude Opus 5.5 --- .../class-wp-rest-attachments-controller.php | 8 +- .../rest-api/rest-attachments-controller.php | 92 +++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 2 +- 3 files changed, 99 insertions(+), 3 deletions(-) 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 7bf51326a9dc8..8a581a99d3be2 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 @@ -298,7 +298,7 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE $args['generate_sub_sizes'] = array( 'type' => 'boolean', 'default' => true, - 'description' => __( 'Whether to generate image sub sizes.' ), + 'description' => __( 'Whether to generate image sub sizes. When false, the main image is also not converted to the image output format.' ), ); $args['convert_format'] = array( @@ -503,6 +503,7 @@ public function create_item_permissions_check( $request ) { * * @since 4.7.0 * @since 7.1.0 Added the `generate_sub_sizes`, `convert_format`, and `url` parameters. + * @since 7.3.0 The main image is no longer converted to the image output format when `generate_sub_sizes` is false. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. @@ -634,7 +635,10 @@ public function create_item( $request ) { require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; - // The client sideloads the converted main file. + /* + * The client sideloads the converted main file. This is added after the upload is + * stored, so wp_unique_filename() still reserves the converted file names. + */ if ( false === $request['generate_sub_sizes'] ) { add_filter( 'image_editor_output_format', '__return_empty_array', 100 ); } diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index 27eaf97bde95c..c985bfa46a83a 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -4527,6 +4527,98 @@ public function test_client_side_upload_main_file_is_not_converted() { $this->assertFileDoesNotExist( dirname( $attached_file ) . '/client-side-photo.webp', 'The server should not write a converted copy.' ); } + /** + * Tests that a client-side upload whose main file the client converts keeps the + * upload as `original_image` and leaves no files behind after it is deleted. + * + * @ticket 66763 + * + * @dataProvider data_client_side_converted_main_file_flow + * + * @covers WP_REST_Attachments_Controller::create_item + * @covers WP_REST_Attachments_Controller::sideload_item + * @covers WP_REST_Attachments_Controller::finalize_item + * @requires function imagejpeg + * + * @param string $image_size The image size the client sideloads the main file as. + * @param string $source The test image to upload. + * @param string $main_file The expected name of the converted main file. + */ + public function test_client_side_converted_main_file_flow_leaves_no_files( string $image_size, string $source, string $main_file ) { + if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { + $this->markTestSkipped( 'This test requires an image editor that can save WebP.' ); + } + + $this->enable_client_side_media_processing(); + + wp_set_current_user( self::$author_id ); + + add_filter( 'image_editor_output_format', array( $this, 'filter_output_format_jpeg_to_webp' ) ); + + $upload_dir = wp_upload_dir(); + $files_before = (array) glob( $upload_dir['path'] . '/*' ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); + $request->set_header( 'Content-Type', 'image/jpeg' ); + $request->set_header( 'Content-Disposition', 'attachment; filename=flow-photo.jpg' ); + $request->set_param( 'generate_sub_sizes', false ); + $request->set_body( file_get_contents( DIR_TESTDATA . '/images/' . $source ) ); + $response = rest_get_server()->dispatch( $request ); + $attachment_id = $response->get_data()['id']; + + $this->assertSame( 201, $response->get_status(), 'Uploading the image should succeed.' ); + + // Convert the main file as the client does, scaled down for a 'scaled' sideload. + $editor = wp_get_image_editor( DIR_TESTDATA . '/images/' . $source ); + if ( 'scaled' === $image_size ) { + $editor->resize( 1000, 1000 ); + } + $converted = $editor->save( get_temp_dir() . 'flow-photo-converted.webp', 'image/webp' ); + + $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment_id}/sideload" ); + $request->set_header( 'Content-Type', 'image/webp' ); + $request->set_header( 'Content-Disposition', 'attachment; filename=' . $main_file ); + $request->set_param( 'image_size', $image_size ); + $request->set_param( 'convert_format', false ); + $request->set_body( file_get_contents( $converted['path'] ) ); + $response = rest_get_server()->dispatch( $request ); + $main_data = $response->get_data(); + unlink( $converted['path'] ); + + $this->assertSame( 200, $response->get_status(), 'Sideloading the converted main file should succeed.' ); + + $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment_id}/finalize" ); + $request->set_param( 'sub_sizes', array( $main_data ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status(), 'Finalize should succeed.' ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertSame( 'flow-photo.jpg', $metadata['original_image'], 'The untouched upload should be recorded as original_image.' ); + $this->assertSame( $main_file, wp_basename( $metadata['file'] ), 'The converted main file should become the attached file.' ); + + $written = array_map( 'wp_basename', array_diff( (array) glob( $upload_dir['path'] . '/*' ), $files_before ) ); + $this->assertEqualsCanonicalizing( array( 'flow-photo.jpg', $main_file ), $written, 'Only the upload and the converted main file should be written.' ); + + wp_delete_attachment( $attachment_id, true ); + + $remaining = array_diff( (array) glob( $upload_dir['path'] . '/*' ), $files_before ); + $this->assertSame( array(), array_values( $remaining ), 'Deleting the attachment should leave no files behind.' ); + } + + /** + * Data provider. + * + * @return array + */ + public static function data_client_side_converted_main_file_flow(): array { + return array( + 'scaled image' => array( 'scaled', '33772.jpg', 'flow-photo-scaled.webp' ), + 'original image' => array( 'original', 'canola.jpg', 'flow-photo.webp' ), + ); + } + /** * Tests that sideloading scaled image requires authentication. * diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 807eda9d2cf92..1b555559028a9 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -3152,7 +3152,7 @@ mockedApiResponse.Schema = { "generate_sub_sizes": { "type": "boolean", "default": true, - "description": "Whether to generate image sub sizes.", + "description": "Whether to generate image sub sizes. When false, the main image is also not converted to the image output format.", "required": false }, "convert_format": { From 5cef61a0ad573b17695bcee0e174e42620cecdb6 Mon Sep 17 00:00:00 2001 From: Aditya Dhade <76063440+b1ink0@users.noreply.github.com> Date: Fri, 27 Nov 2026 22:07:03 +0530 Subject: [PATCH 3/3] REST API: Update the attachment MIME type when a sideload replaces the main file. Co-Authored-By: Claude Opus 5.5 --- .../endpoints/class-wp-rest-attachments-controller.php | 10 ++++++++++ .../tests/rest-api/rest-attachments-controller.php | 2 ++ 2 files changed, 12 insertions(+) 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 8a581a99d3be2..45bcb2057f18c 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 @@ -3083,6 +3083,16 @@ public function sideload_item( WP_REST_Request $request ) { ); } + // Update the post mime type when the supplied image is in a different format. + if ( get_post_mime_type( $attachment_id ) !== $type ) { + wp_update_post( + array( + 'ID' => $attachment_id, + 'post_mime_type' => $type, + ) + ); + } + $sub_size_data['width'] = $size[0]; $sub_size_data['height'] = $size[1]; $sub_size_data['filesize'] = $filesize; diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index c985bfa46a83a..a4012ec773b59 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -4532,6 +4532,7 @@ public function test_client_side_upload_main_file_is_not_converted() { * upload as `original_image` and leaves no files behind after it is deleted. * * @ticket 66763 + * @ticket 53665 * * @dataProvider data_client_side_converted_main_file_flow * @@ -4597,6 +4598,7 @@ public function test_client_side_converted_main_file_flow_leaves_no_files( strin $this->assertSame( 'flow-photo.jpg', $metadata['original_image'], 'The untouched upload should be recorded as original_image.' ); $this->assertSame( $main_file, wp_basename( $metadata['file'] ), 'The converted main file should become the attached file.' ); + $this->assertSame( 'image/webp', get_post_mime_type( $attachment_id ), 'The attachment MIME type should match the converted main file.' ); $written = array_map( 'wp_basename', array_diff( (array) glob( $upload_dir['path'] . '/*' ), $files_before ) ); $this->assertEqualsCanonicalizing( array( 'flow-photo.jpg', $main_file ), $written, 'Only the upload and the converted main file should be written.' );