Make WordPress Core

Opened 7 weeks ago

Closed 4 weeks ago

Last modified 4 weeks ago

#66220 closed enhancement (duplicate)

Media: route media modal uploads through the client-side pipeline

Reported by: adamsilverstein Owned by: adamsilverstein
Priority: normal Milestone:
Component: Media Version: trunk
Severity: normal Keywords: has-patch has-unit-tests commit
Cc: Focuses: javascript

Description

Follow up to #65661, which routes uploads on the Media Library grid and the "Add New Media File" screen through the client-side media processing pipeline. This ticket covers the media modal: the wp.media frame the block editor, the site editor and the block widgets screen open from an Image block's "Media Library" button, the featured image panel, and so on.

Uploads the block editor starts itself (dropping a file on a block, the inserter, the Upload button) already go through the client-side pipeline. The modal's uploader is wp.Uploader/plupload, which nothing intercepts, so it still posts the original bytes to async-upload.php. That is why the same HEIC file converts and uploads on an Image block but fails in the modal on a server with no HEIC support, and why anything uploaded through the modal skips browser-generated sub-sizes, the big image threshold and animated GIF handling.

Gutenberg currently works around this in the plugin, see ​https://github.com/WordPress/gutenberg/pull/82473 (​https://github.com/WordPress/gutenberg/issues/82409). Core is the right home: the block editor screens are already cross-origin isolated by wp_set_up_cross_origin_isolation(), and the grid script from #65661 already binds to every wp.Uploader instance. The delta is mostly wiring:

  • Rename the grid script to media-frame-upload and enqueue it from wp_enqueue_media(), so it loads on every screen that opens a media frame, instead of only on upload.php.
  • Only enqueue it on requests wp_set_up_cross_origin_isolation() actually isolated (new wp_is_cross_origin_isolated_request()), so screens that are not isolated, such as the Customizer and the Site Icon picker on Settings > General, do not download the pipeline bundles for a script that could only no-op. Uploads there stay on the classic path for now.
  • Let the shared pipeline script adopt the upload-media store when the block editor has already configured it, rather than rendering a second provider into the same store.
  • Only intercept uploaders that post attachments (core's defaults: async-upload.php with the upload-attachment action), so a plugin's own wp.Uploader pointed at another endpoint is left alone. This mirrors a review finding on the Gutenberg PR.

Not covered here, worth their own tickets once this lands: isolating the Customizer (its preview iframe needs same-origin access that Document-Isolation-Policy would block), the Site Icon picker, and the classic editor's media modal.

Patch incoming.

Attachments (1)

php-unit-test-status.png​ (64.5 KB ) - added by logicrays 7 weeks ago.

Download all attachments as: .zip

Change History (9)

This ticket was mentioned in ​PR #13875 on ​WordPress/wordpress-develop by ​@adamsilverstein.


7 weeks ago
#1

Stacked on ​https://github.com/WordPress/wordpress-develop/pull/12585 - only the last commit is new here, everything before it is that PR. Once #12585 lands this rebases down to the one commit.

Description

#12585 routes uploads on the Media Library grid and the "Add New Media File" screen through the client-side media processing pipeline. The media modal - the wp.media frame the block editor, the site editor and the block widgets screen open from an Image block's "Media Library" button, the featured image panel and so on - still uploads through wp.Uploader/plupload to async-upload.php. That is why a HEIC file converts and uploads when dropped on an Image block but fails in the modal on a server with no HEIC support, and why anything uploaded through the modal skips browser-generated sub-sizes, the big image threshold and animated GIF handling. Gutenberg currently works around this in the plugin (​https://github.com/WordPress/gutenberg/pull/82473); this PR gives it a home in core.

The block editor screens are already cross-origin isolated by wp_set_up_cross_origin_isolation(), and the grid script from #12585 already binds to every wp.Uploader instance, so the delta is mostly wiring:

  • Rename media-library-upload to media-frame-upload and enqueue it from wp_enqueue_media(), so it loads on every screen that opens a media frame instead of only on upload.php. The grid keeps working through the same call.
  • Only enqueue it on requests wp_set_up_cross_origin_isolation() actually isolated, via a new wp_is_cross_origin_isolated_request(). Screens that are not isolated - the Customizer, the Site Icon picker on Settings > General, the classic editor - load the media frame too, but they would download the whole wp-upload-media chain for a script that can only no-op there. Their uploads stay on the classic path for now.
  • Let the shared pipeline script adopt the upload-media store when the block editor has already configured it, rather than rendering a second provider into the same store. Readiness is now "a provider's settings landed" (mediaSideload and mediaFinalize have no defaults) instead of "this script's own settings landed", since the block editor's provider can render after ours and replace them with equivalent ones.
  • Only intercept uploaders that post attachments - core's defaults, async-upload.php with the upload-attachment action - so a plugin's own wp.Uploader pointed at another endpoint is left alone. This mirrors a review finding from @andrewserong on the Gutenberg PR.

Not covered here, worth their own tickets once this lands: isolating the Customizer (its preview iframe needs same-origin access that Document-Isolation-Policy would block), the Site Icon picker, and the classic editor's media modal.

Testing Instructions

​[[Image(https://img.shields.io/badge/Test_in-WP_Playground-blue?style=for-the-badge&logo=wordpress)]]

_Test in Chrome 137 or newer on a secure origin (https or localhost)._

  1. Open a post in the block editor, add an Image block and click "Media Library".
  2. Drop an image on the "Upload files" tab with the Network tab open. You should see a POST to wp/v2/media, then sideload and finalize requests, and no POST to async-upload.php. The tile's progress bar advances, the finished tile is selectable, and "Select" inserts it into the block as usual.
  3. Try a HEIC or AVIF on a server whose image editor cannot process them: before this change the modal reports an error, after it the file uploads (HEIC as a converted JPEG).
  4. Drop a file straight on the block, outside the modal: still goes through the pipeline as before. Both paths now share one configured store.
  5. Repeat step 2 in Firefox or Safari, or in Chrome with the Document-Isolation-Policy header blocked in DevTools: the upload goes through async-upload.php as before.
  6. Open the Customizer and pick a site logo, or Settings > General and pick a Site Icon: the modal opens, uploads go through async-upload.php, and media-frame-upload.js is not loaded on the page.
  7. Media > Library in grid view still uploads through the pipeline exactly as on #12585.

Automated coverage, all green locally:

  • npm run test:php -- --filter 'wpEnqueueMediaFrameUpload|wpEnqueueMediaNewUpload|CrossOriginIsolation' - 76 tests, including four new ones for the isolated-request gate and the wp_enqueue_media() enqueue.
  • npm run test:e2e -- media-modal-client-side-upload media-library-client-side-upload media-new-client-side-upload - 31 tests. The new modal spec checks the script loads in the block editor, that an upload from the modal goes create, sideload, finalize with nothing hitting async-upload.php and the finished image lands in the block, and that stripping the isolation header falls back to the classic uploader.

AI Use

Claude Code did the typing here, I did the asking. I will review and test.

🤖 Generated with ​Claude Code

​https://claude.ai/code/session_015q272S5jKgWCuQYrbz2SKb

#2 @adamsilverstein
7 weeks ago

  • Focuses javascript added

#3 @logicrays
7 weeks ago

## Test Report
### Environment

  • OS: Linux
  • Browser: Chrome (version)
  • Local: wordpress-develop @ PR 13875

### Results

  • [x] PHPUnit filter: OK (70 tests, 108 assertions)
  • [x] Media modal upload via Image block -> Network shows wp/v2/media include withNo image "[[Image(" attached to Ticket #66220)]] sideload/finalize, no async-upload.php
  • [x] crossOriginIsolated === true

#4 follow-up: @im3dabasia1
5 weeks ago

  • Milestone 7.2 → 7.3

As per today’s pre-beta 1 scrub: this is stacked on #65661, which hasn’t landed yet, and with beta 1 close, I’m moving this to 7.3.

@adamsilverstein, feel free to move it back if it needs to be in this milestone.

#5 @adamsilverstein
4 weeks ago

  • Keywords commit added
  • Owner set to adamsilverstein
  • Status new → assigned

#6 in reply to: ↑ 4 @adamsilverstein
4 weeks ago

  • Milestone 7.3
  • Resolution → duplicate
  • Status assigned → closed

Replying to im3dabasia1:

As per today’s pre-beta 1 scrub: this is stacked on #65661, which hasn’t landed yet, and with beta 1 close, I’m moving this to 7.3.

@adamsilverstein, feel free to move it back if it needs to be in this milestone.

@im3dabasia1 - turns out I can just squash this down to a single commit and trac ticket, no changes are required on the Gutenberg side so splitting this out isn't really needed.

I'm going to close this trac ticket in favor of https://core.trac.wordpress.org/ticket/65661 and continue the work there.

​@adamsilverstein commented on ​PR #13875:


4 weeks ago
#7

Folded into #12585, which now carries the media modal work as well, so closing this one in favor of it.

#8 @adamsilverstein
4 weeks ago

In 64588:

Media: Enable client-side uploads in the Media Library and modal.

Enable client-side media processing for Uploads from the Media Library grid, the Add New Media File screen and the media modal in the block editor (in supported browsers).

These screens now use the same pipeline as block editor uploads: the original is uploaded through the REST API, sub-sizes are generated in the browser and sideloaded, then the attachment is finalized. wp_set_up_cross_origin_isolation() now also isolates the Media Library grid and the Add New Media File screen using Document Isolation Policy. Uploads huse the client-side processing still use the existing progress, tile and error UI.

When client-side processing is disabled, uploads fall back to the classic flow unchanged. Audio files, uploaders that do not post attachments, and JPEG/PNG files while plupload resizing is enabled also stay on the classic path, and a HEIC the browser cannot decode is handed to the server when its image editor supports HEIC. The Customizer, Site Icon picker and classic editor are not isolated and keep classic uploads.

See ​https://github.com/WordPress/gutenberg/issues/82409.

Props adamsilverstein, westonruter, andrewserong, sanayasir, arkaprabhachowdhury, logicrays.
Fixes #65661. See #66220.

Note: See TracTickets for help on using tickets.