-
Notifications
You must be signed in to change notification settings - Fork 139
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
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.54% +0.02%
==========================================
Files 85 88 +3
Lines 7023 7116 +93
==========================================
+ Hits 5093 5162 +69
- Misses 1930 1954 +24
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]>
| * Test od_get_cannot_optimize_reasons(). | ||
| * | ||
| * @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.
westonruter
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.
Sorry for the delay in adding another review. 😞
| * @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.
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 9171d30.
Co-authored-by: Weston Ruter <[email protected]>
…view Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
westonruter
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.
Sorry for the delays in reviewing.
No worries at all — really appreciate you taking the time to go through it 🙂 |
Co-authored-by: Weston Ruter <[email protected]>
| * is_embed?: string, | ||
| * is_preview?: string, | ||
| * is_customize_preview?: string, | ||
| * non_get_request?: string, |
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.
When I was typing out these keys in a test case, I caught myself typing non_get_request first. This made me wonder if it would be better than not_get_request and Gemini seems to think so: https://g.co/gemini/share/53a822060e73
This addresses issues detected by PhpStorm.
westonruter
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.
So sorry for the extended delay with finalizing the review of this PR!
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