Skip to content

Upload Media: Smallest encoding test plugin replaces a core operation - #83333

Draft
swissspidy wants to merge 4 commits into
try/82035-upload-operation-apifrom
try/82035-upload-operation-smallest-encoding
Draft

swissspidy wants to merge 4 commits into
try/82035-upload-operation-apifrom
try/82035-upload-operation-smallest-encoding

Conversation

@swissspidy

@swissspidy swissspidy commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

What?

See #82035. Second of the step-3 proofs of concept for the upload operation API: a plugin that replaces a core step under its own name.

Stacked on #83331 (which is stacked on #82474); sibling of #83332 and #83334. The diff here is only the test plugin and its spec.

Why?

The proposal's escape hatch is "unregister a core step, register your own under the same name". This exercises it end to end from outside the package, and with it the consequence settled in #82474: a replacement of a core operation runs with a plugin's context, not core's, so everything it does has to fit the public contract.

It also surfaced two facts about replacing a core step, both now documented in #83331's README:

  • Core only plans core/transcode-image for an original image when the site asks for a format conversion (image_editor_output_format). A replacement that expects to run on every upload also has to plan itself in — which the API supports, since the replacement has a plan() of its own and sees the pipeline as planned so far.
  • Wherever core did place the step — the original when the site asks for a conversion, and every sub-size — the replacement arrives with core's arguments (UploadOperationArgs['core/transcode-image']: outputFormat, outputQuality, interlaced). A replacement has to honour them: the site asked for WebP, so WebP is the answer.

How?

packages/e2e-tests/plugins/upload-operation-smallest-encoding/index.js:

  • unregisterUploadOperation( 'core/transcode-image' ) then registerUploadOperation( 'core/transcode-image', … ).
  • plan() places the step { before: 'core/upload' } for JPEG, PNG, WebP and AVIF originals, unless the pipeline already carries a core/transcode-image step. Animated GIFs are left alone: createImageBitmap decodes the first frame and re-encoding that would upload a still.
  • The handler decodes the file onto an OffscreenCanvas. With core's args (outputFormat set), it encodes exactly that format at the requested quality and returns it. Without, it encodes WebP, PNG and — when the image is opaque — JPEG at one quality so they compete on size alone, and returns the smallest, the original included, so the file never gets bigger. Either way it records { type, size, candidates, requested? } in additionalData.smallest_encoding.
  • It returns a preview as attachment.url through context.createBlobURL() (added in Upload Media: Public API for registering upload operations #83331), so the editor shows the chosen file until the server's URL replaces it, and the queue revokes the URL when the item leaves.

packages/e2e-tests/plugins/upload-operation-smallest-encoding.php accepts smallest_encoding as a REST field on attachments, keeps it as post meta, and exposes it on the attachment.

test/e2e/specs/editor/various/upload-operation-smallest-encoding.spec.js uploads 1024x768_e2e_test_image.png and asserts the invariant rather than a winner (which format wins depends on the browser's encoders): the attachment's mime_type is the type the step picked, that type's size is the minimum of the candidates, and fetching source_url returns exactly that many bytes. A second test checks getUploadOperation( 'core/transcode-image' ) now carries the plugin's label.

Testing Instructions

npm run test:e2e -- test/e2e/specs/editor/various/upload-operation-smallest-encoding.spec.js

The spec skips itself where client-side media processing is inactive. I could not run it locally (no Docker in this environment), so CI is the first run; the plugin JS and the spec pass eslint, the PHP passes phpcs.

Testing Instructions for Keyboard

N/A — no UI changes.

Screenshots or screencast

N/A.

Use of AI Tools

Authored with Claude Code: the plugin, the spec and this description.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ

An e2e test plugin that takes the escape hatch of the operation API:
it unregisters core/transcode-image, registers its own step under the
same name, and plans that step into every still image upload. The step
encodes the image with the browser's own encoders in every format that
would not lose anything the image has, and uploads whichever came out
smallest, the original included. It records what it compared as a
field of the upload request, which the plugin's PHP side keeps.

The spec checks the invariant rather than a winner, since which format
wins depends on the browser's encoders: the file the server holds is
the one the step picked, that one is the smallest of the candidates,
and the bytes served back are exactly that many.

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ
@github-actions github-actions Bot added the [Package] E2E Tests /packages/e2e-tests label Sep 22, 2026
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

📦 Bundle size

Size Change: 0 B

Total Size: 8.21 MB

d0086ec Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 0c7a0aa try/82035-upload-operation-api % Change
timeToFirstByte 56.65 ms +10.06% -4.06% 56.9 ms +10.46% -3.43% -0.44%
largestContentfulPaint 92 ms +8.7% -4.35% 92 ms +8.7% -4.35% 0%
lcpMinusTtfb 34 ms +5.29% -2.06% 34 ms +7.65% -3.68% 0%
wpBeforeTemplate 27.76 ms +20.53% -1.33% 28.2 ms +14.57% -1.7% -1.56%
wpTemplate 23.56 ms +5.56% -2.42% 24.39 ms +1.89% -4.84% -3.4%
wpTotal 53.12 ms +9.98% -4.39% 53.26 ms +9.41% -3.25% -0.26%
wpMemoryUsage 7.59 MB +0% -0% 7.58 MB +0% -0% 0.08%
wpDbQueries 17 +0% -0% 17 +0% -0% 0%

front-end-classic-theme

Metric 0c7a0aa try/82035-upload-operation-api % Change
timeToFirstByte 46.75 ms +9.63% -1.6% 46.7 ms +5.46% -1.61% 0.11%
largestContentfulPaint 102 ms +3.92% -1.96% 100 ms +4% -0% 2%
lcpMinusTtfb 54.1 ms +5.45% -1.57% 53.9 ms +2.78% -1.3% 0.37%
wpBeforeTemplate 25.88 ms +13.64% -1.12% 25.9 ms +7.18% -2.28% -0.08%
wpTemplate 17.62 ms +3.69% -1.53% 17.63 ms +4.25% -1.02% -0.06%
wpTotal 43.86 ms +8.76% -1.44% 43.89 ms +4.99% -1.85% -0.07%
wpMemoryUsage 6.21 MB +0% -0% 6.20 MB +0% -0% 0.1%
wpDbQueries 14 +0% -0% 14 +0% -0% 0%

media-processing

Metric 0c7a0aa try/82035-upload-operation-api % Change
mediaProcessingJpeg 394.24 ms +0.21% -0.42% 392.96 ms +0.92% -0.87% 0.33%
mediaProcessingAvif 6031.06 ms +0.18% -0.36% 6001.51 ms +0.65% -0.16% 0.49%
mediaProcessingJpegToAvif 4160.07 ms +0.09% -0.73% 4145.04 ms +0.1% -0.32% 0.36%

media-upload

Metric 0c7a0aa try/82035-upload-operation-api % Change
jpegUploadProcessing 1425.66 ms +4.03% -2.11% 1417.09 ms +2.75% -1.12% 0.6%
pngUploadProcessing 210.41 ms +4.9% -2.87% 209.61 ms +6.31% -7.13% 0.38%
largeJpegUploadProcessing 1401.18 ms +1.9% -0.52% 1391.97 ms +1.19% -0.82% 0.66%
multipleImageUploadProcessing 1547.09 ms +0.5% -0.95% 1531.37 ms +2.07% -0.59% 1.03%

post-editor

Metric 0c7a0aa try/82035-upload-operation-api % Change
serverResponse 426.12 ms +5.69% -7.62% 419.89 ms +4.63% -4.74% 1.48%
firstPaint 242.7 ms +11.14% -19.53% 238.86 ms +28.39% -16.48% 1.61%
domContentLoaded 1138.76 ms +3.89% -1.15% 1126.1 ms +0.99% -2.09% 1.12%
loaded 1140.21 ms +3.91% -1.16% 1127.52 ms +1% -2.09% 1.13%
firstContentfulPaint 487.65 ms +0.67% -6.65% 460.02 ms +3.78% -1.48% 6.01%
firstBlock 3286.96 ms +1.36% -0.61% 3246.82 ms +1.51% -0.67% 1.24%
type 20.91 ms +2.92% -2.3% 21.88 ms +6.72% -3.93% -4.43%
typeWithoutInspector 21.68 ms +3.74% -1.34% 19.45 ms +0.93% -1.95% 11.47%
typeWithTopToolbar 24.8 ms +3.15% -2.58% 25.98 ms +2.73% -2.81% -4.54%
typeContainer 9.36 ms +6.52% -4.49% 9.01 ms +4.88% -11.1% 3.88%
focus 76.85 ms +4.23% -8.03% 85.14 ms +3.44% -5.81% -9.74%
firstFocus 199.35 ms +0% -0% 208.6 ms +0% -0% -4.43%
selectAll 476.05 ms +1.91% -1.62% 507.13 ms +3.49% -4.91% -6.13%
listViewOpen 68.56 ms +2.79% -4.77% 73.1 ms +5.55% -6.89% -6.21%
inserterOpen 24.6 ms +3.41% -3.62% 27.18 ms +4.05% -4.75% -9.49%
inserterHover 3.67 ms +8.17% -8.99% 3.36 ms +8.04% -13.1% 9.23%
inserterSearch 8.66 ms +3% -4.04% 8.98 ms +12.14% -3.79% -3.56%
loadPatterns 679.08 ms +4.29% -4.79% 678.8 ms +3.87% -3.24% 0.04%
wpTotal 414.87 ms +5.44% -7.68% 408.84 ms +4.77% -4.98% 1.47%
wpMemoryUsage 13.13 MB +0% -0% 13.12 MB +0% -0% 0.07%
wpDbQueries 54 +0% -1.85% 54 +0% -1.85% 0%

site-editor

Metric 0c7a0aa try/82035-upload-operation-api % Change
serverResponse 470.99 ms +4.82% -5.58% 491.09 ms +4.39% -3.53% -4.09%
firstPaint 278.31 ms +21.17% -18.69% 295.64 ms +51.28% -21.19% -5.86%
domContentLoaded 1122.64 ms +3.1% -1.62% 1109.98 ms +1.03% -1.02% 1.14%
loaded 1123.86 ms +3.08% -1.63% 1111.39 ms +1.01% -1.03% 1.12%
firstContentfulPaint 442.48 ms +2.66% -0.27% 446.3 ms +3.45% -4.18% -0.86%
firstBlock 4169.87 ms +1.16% -0.69% 4174.19 ms +1.52% -1.08% -0.1%
type 20.31 ms +2.71% -9.26% 19.9 ms +5.93% -1.76% 2.06%
navigate 121.69 ms +10.62% -10.63% 113.73 ms +8.45% -9.68% 7%
loadPatterns 1301.45 ms +14.19% -6.4% 1455.09 ms +11.18% -15.11% -10.56%
loadPages 1054.09 ms +3.28% -0.37% 1049.73 ms +3.91% -0.68% 0.42%
wpTotal 461.43 ms +4.85% -5.82% 481.46 ms +4.26% -3.67% -4.16%
wpMemoryUsage 12.11 MB +0% -0% 12.10 MB +0% -0% 0.05%
wpDbQueries 43.5 +1.15% -1.15% 43 +2.33% -0% 1.16%

d0086ec Run

claude and others added 2 commits September 22, 2026 09:22
Where core placed core/transcode-image — the original when the site
asks for a conversion, and every sub-size — the replacement arrives with
core's arguments, and until now ignored them: the site asked for WebP
and could get whatever came out smallest, sub-sizes included. Now a
requested format is the answer and the comparison only runs when
nothing was asked.

The step also returns a preview through context.createBlobURL(), which
the queue revokes when the item leaves, so the editor shows the chosen
file rather than the original until the server's URL replaces it.

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ
@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement. [Feature] Client Side Media Media processing in the browser with WASM labels Sep 22, 2026
The REST field was declared as an object while the plugin sent it as a
JSON string. Additional fields become arguments of the create endpoint
and are validated against their schema before the attachment is made,
so the mismatch refused every upload the step touched, and the spec saw
an image block with no attachment. The record now travels as nested
form fields, which the server reads back as an object, and the schema
says what is in it so the sizes arrive as integers.

The spec watches the create request and reports its status and body
when it is not 201, so a refused upload reads as the refusal rather than
as a missing ID.

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Client Side Media Media processing in the browser with WASM [Package] E2E Tests /packages/e2e-tests [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants