Skip to content

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

Open
wants to merge 11 commits into
base: trunk
Choose a base branch
from

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

codecov bot commented Apr 9, 2025

Codecov Report

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

Project coverage is 72.58%. Comparing base (9ac71ce) to head (291f732).

Files with missing lines Patch % Lines
plugins/optimization-detective/optimization.php 60.00% 2 Missing ⚠️
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              
Flag Coverage Δ
multisite 72.58% <96.07%> (+0.07%) ⬆️
single 39.72% <0.00%> (-0.62%) ⬇️

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
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]>
*/
function od_can_optimize_response(): bool {
Copy link
Member

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

Copy link
Member

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.

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 9c097f6.

*
* @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. 😞

$disabled_flags['is_preview'] = true;
}

// Since injection of inline-editing controls interfere with breadcrumbs, while also just not necessary in this context.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// 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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// 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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// 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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// 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

Comment on lines +142 to +146
foreach ( $disabled_flags as $flag => $is_disabled ) {
if ( $is_disabled && isset( $reason_messages[ $flag ] ) ) {
$reasons[ $flag ] = $reason_messages[ $flag ];
}
}
Copy link
Member

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

Suggested change
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.
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.

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