Make WordPress Core

Changeset 65748


Ignore:
Timestamp:
11/23/2026 11:28:40 PM (6 days ago)
Author:
adamsilverstein
Message:

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 ​https://github.com/WordPress/gutenberg/pull/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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/media-upload-pipeline.js

    r65356 r65748  
    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.
    … …  
    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 ) {
  • trunk/tests/e2e/specs/media-new-client-side-upload.test.js

    r65521 r65748  
     1/**
     2 * External dependencies
     3 */
     4import { randomUUID } from 'node:crypto';
     5
    16/**
    27 * Internal dependencies
    … …  
    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,
Note: See TracChangeset for help on using the changeset viewer.