-
Notifications
You must be signed in to change notification settings - Fork 110
Enhance Optimization Detective meta generator tag with all disabled reasons #1979
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
base: trunk
Are you sure you want to change the base?
Enhance Optimization Detective meta generator tag with all disabled reasons #1979
Conversation
Signed-off-by: Shyamsundar Gadde <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #1979 +/- ##
==========================================
+ Coverage 72.51% 72.58% +0.07%
==========================================
Files 85 85
Lines 7023 7041 +18
==========================================
+ Hits 5093 5111 +18
Misses 1930 1930
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:
|
Co-authored-by: Weston Ruter <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
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. |
function od_get_disabled_reasons(): array { | ||
$conditions = array( | ||
'can_optimize_response_false' => array( | ||
'test' => od_can_optimize_response(), |
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 just realized that the disabled reasons are somewhat nested here. There are 7 different possible reasons for OD being disabled in od_can_optimize_response()
. Should the logic in that function be similarly structured in an array in another function so that its reasons can be exposed as well? Maybe this is overkill. But I just wanted to mention it since it came to mind.
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 implemented this in bae1198. Please let me know if this matches what you had in mind.
…tion Detective disabled state Signed-off-by: Shyamsundar Gadde <[email protected]>
*/ | ||
function od_can_optimize_response(): bool { |
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.
We should add this function back as a wrapper for the new function, like:
function od_can_optimize_response(): bool {
return count( od_get_cannot_optimize_reasons() ) === 0;
}
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.
Granted, the function is private, but still... we're technically in beta so we shouldn't break any back-compat.
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.
Done in 9c097f6.
* | ||
* @covers ::od_can_optimize_response | ||
* @covers ::od_get_cannot_optimize_reasons |
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.
This test could then test both od_get_cannot_optimize_reasons()
and od_can_optimize_response()
…ptimize_reasons Signed-off-by: Shyamsundar Gadde <[email protected]>
f3712cd
to
9c097f6
Compare
…unction Signed-off-by: Shyamsundar Gadde <[email protected]>
// > Access to script at '.../detect.js?ver=0.4.1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. | ||
// So it's better to just avoid attempting to optimize Post Embed responses (which don't need optimization anyway). | ||
if ( is_embed() ) { | ||
$reasons['is_embed'] = __( 'Page is not optimized because it is an embed.', 'optimization-detective' ); |
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.
Let's add a test for this for good measure.
Signed-off-by: Shyamsundar Gadde <[email protected]>
* @param bool $able Whether response can be optimized. | ||
*/ | ||
$can_optimize_after_filter = (bool) apply_filters( 'od_can_optimize_response', $can_optimize_by_default ); |
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.
Idea: How about we pass the $reasons
into the od_can_optimize_response
filter as the second argument? This would allow od_can_optimize_response
filter callbacks to see any existing reasons for why OD was disabled, and then add their own considerations on top.
So for example, the above $reasons
array could instead be an $disabled_flags
(or maybe there is a better name) which is array<string, bool>
, this could then be passed as the second argument to the filter.
The array of booleans can then be passed through array_filter
to remove the non-disabled flags, and then the resulting can be intersected with associative array array<string, string>
that has the disabled reasons provided as the string array values. This resulting intersection can then be returned as $reasons
.
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 would this be useful to add? Consider the case where you want to selectively enable the search template to be optimized. In order to do this, you'd have to re-compute all of the conditions to flip the false
value of $can_optimize_by_default
back to true
. But if all of the flags were passed in, then they could be examined before flipping that boolean. For example:
add_filter( 'od_can_optimize_response', static function ( $can_optimize, $disabled_flags ) {
if ( ! $can_optimize && is_search() ) {
unset( $disabled_flags['is_search'] );
$can_optimize = count( array_filter( $disabled_flags ) ) === 0;
}
return $can_optimize;
} );
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.
So in this way, the $disabled_flags
would not just be array<string, bool>
but rather it should be an array shape that defines all of the keys.
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.
Implemented it in 28264af.
Signed-off-by: Shyamsundar Gadde <[email protected]>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
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.
Sorry for the delay in adding another review. 😞
$disabled_flags['is_preview'] = true; | ||
} | ||
|
||
// Since injection of inline-editing controls interfere with breadcrumbs, while also just not necessary in this context. |
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.
// Since injection of inline-editing controls interfere with breadcrumbs, while also just not necessary in this context. | |
// Disable in Customizer preview since injection of inline-editing controls can interfere with XPath. Optimization is also not necessary in this context. |
'no_cache_purge_post_id' => false, | ||
); | ||
|
||
// Since there is no predictability in whether posts in the loop will have featured images assigned or not. If a |
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.
// Since there is no predictability in whether posts in the loop will have featured images assigned or not. If a | |
// Disable the search template since there is no predictability in whether posts in the loop will have featured images assigned or not. If a |
$disabled_flags['is_customize_preview'] = true; | ||
} | ||
|
||
// Since the images detected in the response body of a POST request cannot, by definition, be cached. |
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.
// Since the images detected in the response body of a POST request cannot, by definition, be cached. | |
// Disable for POST responses since since they cannot, by definition, be cached. |
$disabled_flags['not_get_request'] = true; | ||
} | ||
|
||
// Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger |
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.
// Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger | |
// Disable when there is no post ID available for cache purging. Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger |
foreach ( $disabled_flags as $flag => $is_disabled ) { | ||
if ( $is_disabled && isset( $reason_messages[ $flag ] ) ) { | ||
$reasons[ $flag ] = $reason_messages[ $flag ]; | ||
} | ||
} |
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.
Alternative which uses wp_array_slice_assoc()
:
foreach ( $disabled_flags as $flag => $is_disabled ) { | |
if ( $is_disabled && isset( $reason_messages[ $flag ] ) ) { | |
$reasons[ $flag ] = $reason_messages[ $flag ]; | |
} | |
} | |
$reasons = wp_array_slice_assoc( $reason_messages, array_keys( array_filter( $disabled_flags ) ) ); |
* @since n.e.x.t | ||
* @access private | ||
* | ||
* @return array<string, string> Array of disabled reason codes and their messages. |
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.
How about adding an explicit array shape for the return value here, defining all of the possible (nullable) keys mapping to boolean values?
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.
Maybe this is overkill since we're never . If we do it here, then it should also be done in the param for the od_can_optimize_response
filter.
Summary
Fixes #1977
Relevant technical choices
od_get_disabled_reasons()
that centralizes the logic for determining why Optimization Detective is disabledcan_optimize_response_false
,rest_api_unavailable
, andquery_param_disabled
)od_maybe_add_template_output_buffer_filter()
to use this helper function