Skip to content

Allow Embed Optimizer to work with Classic Editor and Classic block embeds - #2685

Draft
sarthak-19 wants to merge 1 commit into
WordPress:trunkfrom
sarthak-19:fix/embed-optimizer-classic-editor-support
Draft

sarthak-19 wants to merge 1 commit into
WordPress:trunkfrom
sarthak-19:fix/embed-optimizer-classic-editor-support

Conversation

@sarthak-19

Copy link
Copy Markdown
Contributor

Summary

Fixes #2226.

Embed Optimizer's optimizations (dns-prefetch, layout-shift prevention, and Optimization Detective-aware lazy loading) previously only applied to embeds added via the block editor's Embed block, since Embed_Optimizer_Tag_Visitor only recognizes the FIGURE.wp-block-embed > DIV.wp-block-embed__wrapper markup that block saves.

Embeds added via the Classic Editor (auto-embedded URLs, the [embed] shortcode) or the Classic block render as bare oEmbed HTML with no such wrapper, so they were invisible to the tag visitor and missed out on these optimizations.

Both cases resolve through WP_Embed::shortcode() and fire the same embed_oembed_html filter — including the bare URL saved inside the Embed block's own wrapper, which is resolved by WP_Embed::autoembed() before the block is even parsed (autoembed() runs at the_content priority 8, do_blocks() at priority 9). This PR hooks that filter to wrap the resolved HTML in the same FIGURE/DIV markup the Embed block uses, so classic embeds become recognizable to the tag visitor just like Embed-block ones.

To avoid double-wrapping the Embed block's own markup (since its bare URL is resolved through the exact same filter), wrapping is skipped when the URL is already found inside an existing wp-block-embed__wrapper in the post's stored content.

  • plugins/embed-optimizer/helper.php: adds the wrapping filter, a helper to detect an existing Embed-block wrapper, and a best-effort URL-to-provider-class mapping (for the wp-block-embed-{provider} class used by dns-prefetch matching).
  • plugins/embed-optimizer/tests/test-hooks.php: unit coverage for the new functions plus an integration test exercising the real WP_Embed class for both the classic and Embed-block cases.
  • plugins/embed-optimizer/readme.txt: drops the "not inside a Classic Block" caveat.

Trade-off to note

Wrapping previously-unwrapped classic embed HTML in FIGURE.wp-block-embed pulls in the block library's default .wp-block-embed spacing/CSS for those embeds (most themes already enqueue block styles site-wide). This is a minor, expected visual change and is the intended fix, but worth a second look from reviewers. Widget content (widget_text_content/widget_block_content) is not covered by the double-wrap guard, since there's no post ID to check against there; this is a known limitation left for follow-up since the issue is specifically about post content.

Test plan

  • composer phpstan — 0 errors
  • composer lint -- --standard=plugins/embed-optimizer/phpcs.xml.dist — 0 errors
  • plugins/embed-optimizer PHPUnit suite (86 tests) passing against a real wp-env WordPress install
  • optimization-detective PHPUnit suite (359 tests) passing, confirming no regressions in the tag-visitor pipeline

…mbeds

Embed Optimizer's optimizations (dns-prefetch, layout-shift prevention,
and Optimization Detective-aware lazy loading) previously only applied to
embeds added via the block editor's Embed block, since Embed_Optimizer_Tag_Visitor
only recognizes the FIGURE.wp-block-embed > DIV.wp-block-embed__wrapper markup
that block saves.

Embeds added via the Classic Editor (auto-embedded URLs, the [embed]
shortcode) or the Classic block render as bare oEmbed HTML with no such
wrapper, so they were invisible to the tag visitor.

Both cases resolve through WP_Embed::shortcode() and fire the same
embed_oembed_html filter, including the bare URL saved inside the Embed
block's own wrapper (resolved by WP_Embed::autoembed() before the block is
even parsed). Wrap the resolved HTML in the same FIGURE/DIV markup the
Embed block uses, skipping wrapping when the URL is already inside an
existing wrapper in the post's stored content to avoid nesting it twice.

Fixes WordPress#2226
@github-actions github-actions Bot added the [Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes) label Sep 24, 2026
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.67442% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.65%. Comparing base (1647b94) to head (08cd395).

Files with missing lines Patch % Lines
plugins/embed-optimizer/helper.php 97.67% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2685      +/-   ##
==========================================
+ Coverage   70.50%   70.65%   +0.14%     
==========================================
  Files          91       91              
  Lines        7869     7912      +43     
==========================================
+ Hits         5548     5590      +42     
- Misses       2321     2322       +1     
Flag Coverage Δ
multisite 70.65% <97.67%> (+0.14%) ⬆️
single 35.57% <79.06%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

[Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow Embed Optimizer to work in Classic block and with Classic Editor

1 participant