|
1 | 1 | /** |
2 | | - * Routes Media Library grid uploads through the client-side media pipeline. |
| 2 | + * Routes media frame uploads through the client-side media pipeline. |
3 | 3 | * |
4 | | - * On wp-admin/upload.php (grid mode) WordPress uploads via wp.Uploader / |
5 | | - * plupload to async-upload.php. When the browser is cross-origin isolated |
6 | | - * and supports the client-side pipeline, this script intercepts the |
7 | | - * uploader's FilesAdded handler and hands files to wp.mediaUploadPipeline |
8 | | - * (media-upload-pipeline.js) instead: the original image is uploaded via |
9 | | - * the REST API and thumbnails are generated in the browser (wasm-vips), |
10 | | - * then sideloaded and finalized. |
| 4 | + * Every wp.media frame - the Media Library grid on wp-admin/upload.php and |
| 5 | + * the media modal the block editor, the site editor and the block widgets |
| 6 | + * screen open - uploads via wp.Uploader / plupload to async-upload.php. |
| 7 | + * When the browser is cross-origin isolated and supports the client-side |
| 8 | + * pipeline, this script intercepts the uploader's FilesAdded handler and |
| 9 | + * hands files to wp.mediaUploadPipeline (media-upload-pipeline.js) |
| 10 | + * instead: the original image is uploaded via the REST API and thumbnails |
| 11 | + * are generated in the browser (wasm-vips), then sideloaded and finalized. |
11 | 12 | * |
12 | | - * The grid's own UI is reused: the same placeholder tiles, progress bars, |
13 | | - * "Uploading n/m" status, and error sidebar that wp-plupload.js drives. |
| 13 | + * The frame's own UI is reused: the same placeholder tiles, progress bars, |
| 14 | + * "Uploading n/m" status, and error list that wp-plupload.js drives. |
14 | 15 | * |
15 | 16 | * When client-side support is unavailable the script cleanly no-ops and |
16 | 17 | * the classic plupload flow is left untouched. |
17 | 18 | * |
18 | | - * @output wp-admin/js/media-library-upload.js |
| 19 | + * @output wp-admin/js/media-frame-upload.js |
19 | 20 | */ |
20 | 21 |
|
21 | 22 | /* global plupload */ |
22 | 23 |
|
23 | 24 | ( function () { |
24 | 25 | // Guard against double execution (e.g. duplicate enqueues). |
25 | | - if ( window.__wpMediaLibraryUpload ) { |
| 26 | + if ( window.__wpMediaFrameUpload ) { |
26 | 27 | return; |
27 | 28 | } |
28 | 29 |
|
|
41 | 42 | return; |
42 | 43 | } |
43 | 44 |
|
44 | | - window.__wpMediaLibraryUpload = true; |
| 45 | + window.__wpMediaFrameUpload = true; |
45 | 46 |
|
46 | 47 | /** |
47 | 48 | * Resets the upload queue once every attachment has finished uploading. |
|
134 | 135 | wpUploader.error( message, {}, file ); |
135 | 136 | } |
136 | 137 |
|
| 138 | + /** |
| 139 | + * Whether a plupload uploader posts attachments the way wp.Uploader does |
| 140 | + * by default. |
| 141 | + * |
| 142 | + * The init wrapper below reaches every wp.Uploader built on the page, |
| 143 | + * and a plugin can build one for its own purposes. Core's defaults tell |
| 144 | + * an attachment upload apart: it posts to async-upload.php with the |
| 145 | + * `upload-attachment` action. Anything a plugin pointed elsewhere, or |
| 146 | + * gave another action, is uploading something else and is left alone. |
| 147 | + * |
| 148 | + * @param {plupload.Uploader} up The plupload uploader instance. |
| 149 | + * @return {boolean} True when the uploader posts attachments. |
| 150 | + */ |
| 151 | + function isAttachmentUploader( up ) { |
| 152 | + const settings = up.settings || {}; |
| 153 | + const params = settings.multipart_params || {}; |
| 154 | + const url = String( settings.url || '' ).split( '?' )[ 0 ]; |
| 155 | + |
| 156 | + return ( |
| 157 | + 'upload-attachment' === params.action && |
| 158 | + url.endsWith( 'async-upload.php' ) |
| 159 | + ); |
| 160 | + } |
| 161 | + |
137 | 162 | /** |
138 | 163 | * Intercepts files added to a plupload uploader. |
139 | 164 | * |
|
149 | 174 | * @return {boolean|undefined} False to suppress the built-in handler. |
150 | 175 | */ |
151 | 176 | function handleFilesAdded( wpUploader, up, files ) { |
152 | | - if ( ! pipeline.isReady() || ! pipeline.canHandleBatch( files ) ) { |
| 177 | + if ( |
| 178 | + ! isAttachmentUploader( up ) || |
| 179 | + ! pipeline.isReady() || |
| 180 | + ! pipeline.canHandleBatch( files ) |
| 181 | + ) { |
153 | 182 | return; |
154 | 183 | } |
155 | 184 |
|
|
236 | 265 |
|
237 | 266 | // Wrap wp.Uploader.prototype.init (an empty stub called once per instance |
238 | 267 | // after plupload is initialized) to bind a higher-priority FilesAdded |
239 | | - // handler on every uploader instance, including the Media Library grid's. |
| 268 | + // handler on every uploader instance: the grid's, and the one each media |
| 269 | + // modal creates when it opens. |
240 | 270 | const originalInit = wp.Uploader.prototype.init; |
241 | 271 | wp.Uploader.prototype.init = function () { |
242 | 272 | originalInit.apply( this, arguments ); |
243 | 273 |
|
244 | 274 | const wpUploader = /** @type {WPUploader} */ ( this ); |
245 | 275 | const up = /** @type {plupload.Uploader|undefined} */ ( this.uploader ); |
246 | 276 |
|
247 | | - if ( ! up || up.__wpMediaLibraryUploadBound ) { |
| 277 | + if ( ! up || up.__wpMediaFrameUploadBound ) { |
248 | 278 | return; |
249 | 279 | } |
250 | | - up.__wpMediaLibraryUploadBound = true; |
| 280 | + up.__wpMediaFrameUploadBound = true; |
251 | 281 |
|
252 | 282 | // plupload sorts handlers by priority (descending) and a `false` |
253 | 283 | // return breaks the chain, so priority 100 runs before and suppresses |
|
0 commit comments