Skip to content

Conversation

@ShyamGadde
Copy link
Contributor

@ShyamGadde ShyamGadde commented Apr 9, 2025

Summary

Fixes #1977

Relevant technical choices

  • Created a new helper function od_get_disabled_reasons() that centralizes the logic for determining why Optimization Detective is disabled
  • Updated the meta generator tag to include all disabled reason flags (can_optimize_response_falserest_api_unavailable, and query_param_disabled)
  • Refactored the existing code in od_maybe_add_template_output_buffer_filter() to use this helper function

@ShyamGadde ShyamGadde added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Apr 9, 2025
@codecov
Copy link

codecov bot commented Apr 9, 2025

Codecov Report

Attention: Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 72.54%. Comparing base (ff8b62b) to head (804d8ac).
Report is 42 commits behind head on trunk.

Files with missing lines Patch % Lines
plugins/optimization-detective/optimization.php 66.66% 2 Missing ⚠️
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     
Flag Coverage Δ
multisite 72.54% <96.00%> (+0.02%) ⬆️
single 40.05% <2.00%> (-0.29%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 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.

@ShyamGadde ShyamGadde marked this pull request as ready for review April 9, 2025 22:28
@ShyamGadde ShyamGadde requested a review from felixarntz as a code owner April 9, 2025 22:28
@ShyamGadde ShyamGadde requested a review from westonruter April 9, 2025 22:28
@github-actions
Copy link

github-actions bot commented Apr 9, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ShyamGadde <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

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(),
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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()

@ShyamGadde ShyamGadde force-pushed the update/od-meta-generator-tag branch from f3712cd to 9c097f6 Compare April 10, 2025 19:14
// > 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' );
Copy link
Member

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]>
@ShyamGadde ShyamGadde requested a review from westonruter April 10, 2025 20:23
Comment on lines 118 to 120
* @param bool $able Whether response can be optimized.
*/
$can_optimize_after_filter = (bool) apply_filters( 'od_can_optimize_response', $can_optimize_by_default );
Copy link
Member

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.

Copy link
Member

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;
} );

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented it in 28264af.

@felixarntz

This comment was marked as resolved.

@ShyamGadde

This comment was marked as resolved.

@felixarntz

This comment was marked as resolved.

Copy link
Member

@westonruter westonruter left a 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.
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9171d30.

@ShyamGadde ShyamGadde requested a review from westonruter April 21, 2025 12:50
Copy link
Member

@westonruter westonruter left a 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.

@ShyamGadde
Copy link
Contributor Author

Sorry for the delays in reviewing.

No worries at all — really appreciate you taking the time to go through it 🙂

* is_embed?: string,
* is_preview?: string,
* is_customize_preview?: string,
* non_get_request?: string,
Copy link
Member

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

Copy link
Member

@westonruter westonruter left a 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!

@westonruter westonruter merged commit 8a4987d into WordPress:trunk May 8, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimization Detective's meta generator tag should indicate more reasons for why it is disabled

3 participants