Allow Embed Optimizer to work with Classic Editor and Classic block embeds - #2685
Draft
sarthak-19 wants to merge 1 commit into
Draft
sarthak-19 wants to merge 1 commit into
sarthak-19 wants to merge 1 commit into
Conversation
…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
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_Visitoronly recognizes theFIGURE.wp-block-embed > DIV.wp-block-embed__wrappermarkup 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 sameembed_oembed_htmlfilter — including the bare URL saved inside the Embed block's own wrapper, which is resolved byWP_Embed::autoembed()before the block is even parsed (autoembed()runs atthe_contentpriority 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__wrapperin 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 thewp-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 realWP_Embedclass 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-embedpulls in the block library's default.wp-block-embedspacing/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 errorscomposer lint -- --standard=plugins/embed-optimizer/phpcs.xml.dist— 0 errorsplugins/embed-optimizerPHPUnit suite (86 tests) passing against a realwp-envWordPress installoptimization-detectivePHPUnit suite (359 tests) passing, confirming no regressions in the tag-visitor pipeline