-
Notifications
You must be signed in to change notification settings - Fork 138
Account for OPTIMIZATION_DETECTIVE_VERSION not always being defined for Embed Optimizer
#1908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…or Embed Optimizer
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## trunk #1908 +/- ##
=======================================
Coverage 69.63% 69.63%
=======================================
Files 86 86
Lines 6991 6991
=======================================
Hits 4868 4868
Misses 2123 2123
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| // As of 1.0.0-beta3, next_tag() allows $query and is beginning to migrate to skip tag closers by default. | ||
| // In versions prior to this, the method always visited closers and passing a $query actually threw an exception. | ||
| $tag_query = version_compare( OPTIMIZATION_DETECTIVE_VERSION, '1.0.0-beta3', '>=' ) | ||
| $tag_query = ! defined( 'OPTIMIZATION_DETECTIVE_VERSION' ) || version_compare( OPTIMIZATION_DETECTIVE_VERSION, '1.0.0-beta3', '>=' ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this function's logic running even when Optimization Detective is not active? Is it not something that relies on that plugin?
Also, looking at this again: Why does the Optimization Detective matter for this, given it's passed to the WP_HTML_Tag_Processor instance, which should depend on Core, not OD? Maybe I'm missing something here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Embed Optimizer does not require Optimization Detective. Without Optimization Detective, it lazy-loads all embeds.
When OD is not active, a WP_HTML_Tag_Processor instance is passed. When OD is active, then an OD_HTML_Tag_Processor instance is passed (a subclass of WP_HTML_Tag_Processor).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks.
I didn't know Embed Optimizer was doing this already on its own (albeit less reliably of course).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just realizing that this change and #1872 probably fix Embed Optimizer to work properly when OD is not active, actually. Because the default behavior of next_tag() is to not visit tag closers, and yet the OD subclass was visiting tag closers by default... since no query was being passed in, when OD is not active then it would be skipping all tag closers and the logic that checks $processor->is_tag_closer() would always return false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind. The only part of the code that uses is_tag_closer() is for the non-OD context:
performance/plugins/embed-optimizer/hooks.php
Lines 212 to 234 in 20fea77
| // This condition ensures that when iterating over an embed inside a larger document that we stop once we reach | |
| // closing </figure> tag. The $processor is an OD_HTML_Tag_Processor when Optimization Detective is iterating | |
| // over all tags in the document, and this embed_optimizer_update_markup() is used as part of the tag visitor | |
| // from Embed Optimizer. On the other hand, if $html_processor is not an OD_HTML_Tag_Processor then this is | |
| // iterating over the tags of the embed markup alone as is passed into the embed_oembed_html filter. | |
| if ( ! $is_isolated ) { | |
| if ( 'FIGURE' === $html_processor->get_tag() ) { | |
| if ( $html_processor->is_tag_closer() ) { | |
| --$figure_depth; | |
| if ( $figure_depth <= 0 ) { | |
| // We reached the end of the embed. | |
| break; | |
| } | |
| } else { | |
| ++$figure_depth; | |
| // Move to next element to start looking for IFRAME or SCRIPT tag. | |
| continue; | |
| } | |
| } | |
| if ( 0 === $figure_depth ) { | |
| continue; | |
| } | |
| } |
So there wouldn't have been a bug.
felixarntz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM!
I was doing some testing with Optimization Detective deactivated but with Embed Optimizer active. I started getting a fatal error:
This issue was introduced in #1872. It is an issue unique to Embed Optimizer because it does not require Optimization Detective but merely suggests it. This means the
OPTIMIZATION_DETECTIVE_VERSIONconstant is not guaranteed to be defined. It's not needed by Image Prioritizer since it initializes at theod_initaction so we guaranteed that the constant will be available.