Skip to content

Commit 5ae0af9

Browse files
Media: Create image sizes that share dimensions on upload.
When client-side media processing is active and two or more registered image sizes have the same dimensions, the Media Library upload pipeline sideloads one file for all of them and sends their names as an array in `image_size`. `flattenFormData()` in `media-upload-pipeline.js` only recursed into plain objects, so the array was sent as a comma-separated string. The sideload endpoint rejected it with a 400 and none of those sizes were created, while the upload itself appeared to succeed. Recurse into arrays as well as plain objects, matching `flattenFormData()` in the media-utils package since WordPress/gutenberg#82353. This affects only Media > Add New and the Media Library grid. The block editor already handled arrays correctly. Follow-up to [64588]. Reviewed by westonruter. Props aryanjasala, ozgursar, dhrupo, jyria, kirasong, westonruter. Fixes #66412. git-svn-id: https://develop.svn.wordpress.org/trunk@65748 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1ccce21 commit 5ae0af9

2 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎src/js/_enqueues/admin/media-upload-pipeline.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ window.wp = window.wp || {};
4646
const IMAGE_SUBTYPES = [ 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif' ];
4747

4848
/**
49-
* Recursively appends data to a FormData object, supporting nested objects.
49+
* Recursively appends data to a FormData object, supporting nested objects and arrays.
5050
*
5151
* Mirrors flattenFormData() in the media-utils package.
5252
*
@@ -56,9 +56,10 @@ window.wp = window.wp || {};
5656
*/
5757
function flattenFormData( formData, key, data ) {
5858
if (
59-
data !== null &&
60-
typeof data === 'object' &&
61-
Object.getPrototypeOf( data ) === Object.prototype
59+
Array.isArray( data ) ||
60+
( data !== null &&
61+
typeof data === 'object' &&
62+
Object.getPrototypeOf( data ) === Object.prototype )
6263
) {
6364
Object.keys( data ).forEach( function ( name ) {
6465
flattenFormData( formData, key + '[' + name + ']', data[ name ] );

‎tests/e2e/specs/media-new-client-side-upload.test.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { randomUUID } from 'node:crypto';
5+
16
/**
27
* Internal dependencies
38
*/
@@ -166,6 +171,70 @@ test.describe( 'Add New Media File client-side uploads', () => {
166171
expect( thumbnailResponse.status() ).toBe( 200 );
167172
} );
168173

174+
test( 'creates every image size when two sizes share dimensions', async ( {
175+
page,
176+
admin,
177+
attachments,
178+
requestUtils,
179+
muPlugin,
180+
baseURL,
181+
} ) => {
182+
// Sizes with the same dimensions are sideloaded as one file, with
183+
// their names sent as an array in `image_size`. Other workers share
184+
// the site, so only this test's requests see the extra sizes.
185+
const trigger = `e2e_${ randomUUID().replaceAll( '-', '' ) }`;
186+
await muPlugin.write(
187+
'shared-image-sizes.php',
188+
`<?php
189+
if ( isset( $_COOKIE['${ trigger }'] ) ) {
190+
add_action(
191+
'after_setup_theme',
192+
function () {
193+
add_image_size( 'e2e-shared-a', 320, 320, true );
194+
add_image_size( 'e2e-shared-b', 320, 320, true );
195+
}
196+
);
197+
}`
198+
);
199+
await page
200+
.context()
201+
.addCookies( [ { name: trigger, value: '1', url: baseURL } ] );
202+
203+
await admin.visitAdminPage( 'media-new.php' );
204+
205+
await expectCrossOriginIsolated( page );
206+
207+
const sideloadStatuses = [];
208+
page.on( 'response', ( response ) => {
209+
if (
210+
/\/wp\/v2\/media\/\d+\/sideload/.test(
211+
decodeURIComponent( response.url() )
212+
)
213+
) {
214+
sideloadStatuses.push( response.status() );
215+
}
216+
} );
217+
218+
const ids = trackUploads( page, attachments );
219+
const fileInput = page.locator( FILE_INPUT_SELECTOR ).first();
220+
await expect( fileInput ).toBeAttached( { timeout: 30_000 } );
221+
await fileInput.setInputFiles( TEST_IMAGE_PATH );
222+
223+
await expect( editLinks( page ) ).toBeVisible( { timeout: 60_000 } );
224+
225+
expect( sideloadStatuses.length ).toBeGreaterThanOrEqual( 1 );
226+
expect(
227+
sideloadStatuses.filter( ( status ) => status !== 200 )
228+
).toEqual( [] );
229+
230+
const attachment = await requestUtils.rest( {
231+
path: `/wp/v2/media/${ ids.at( -1 ) }`,
232+
} );
233+
expect( Object.keys( attachment.media_details.sizes || {} ) ).toEqual(
234+
expect.arrayContaining( [ 'e2e-shared-a', 'e2e-shared-b' ] )
235+
);
236+
} );
237+
169238
test( 'warns before leaving while a pipeline upload is in flight', async ( {
170239
page,
171240
admin,

0 commit comments

Comments
 (0)