Skip to content
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

Optimization Detective should be enabled by default for admins #1425

Open
westonruter opened this issue Jul 31, 2024 · 0 comments
Open

Optimization Detective should be enabled by default for admins #1425

westonruter opened this issue Jul 31, 2024 · 0 comments
Labels
Needs Discussion Anything that needs a discussion/agreement [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature

Comments

@westonruter
Copy link
Member

Optimization Detective currently only applies its optimizations by default when the user is not an admin. The logic is here:

$able = ! (
// Since there is no predictability in whether posts in the loop will have featured images assigned or not. If a
// theme template for search results doesn't even show featured images, then this wouldn't be an issue.
is_search() ||
// Since injection of inline-editing controls interfere with breadcrumbs, while also just not necessary in this context.
is_customize_preview() ||
// Since the images detected in the response body of a POST request cannot, by definition, be cached.
( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) ||
// The aim is to optimize pages for the majority of site visitors, not those who administer the site. For admin
// users, additional elements will be present like the script from wp_customize_support_script() which will
// interfere with the XPath indices. Note that od_get_normalized_query_vars() is varied by is_user_logged_in()
// so membership sites and e-commerce sites will still be able to be optimized for their normal visitors.
current_user_can( 'customize' )
);
/**
* Filters whether the current response can be optimized.
*
* @since 0.1.0
*
* @param bool $able Whether response can be optimized.
*/
return (bool) apply_filters( 'od_can_optimize_response', $able );

This can be easily overridden via the following, which is super helpful during development (but see #1423):

add_filter( 'od_can_optimize_response', '__return_true' );

However, it can be confusing to be required to do this. The README includes this bit:

URL metrics are not collected for administrators because it is likely that additional elements will be present on the page which are not also shown to non-administrators, meaning the URL metrics could not reliably be reused between them.

Additional elements include Customizer scripts, edit post links, and so on.

If we included admins, then we'd need to modify od_get_normalized_query_vars() as follows to vary the URL Metrics by whether the user is an admin (due to the additional elements present for admins):

--- a/plugins/optimization-detective/storage/data.php
+++ b/plugins/optimization-detective/storage/data.php
@@ -82,6 +82,10 @@ function od_get_normalized_query_vars(): array {
 		$normalized_query_vars['user_logged_in'] = true;
 	}
 
+	if ( current_user_can( 'customize' ) ) {
+		$normalized_query_vars['admin'] = true;
+	}
+
 	return $normalized_query_vars;
 }

This means that for every URL, there would potentially be separate od_url_metrics posts for:

  1. Unauthenticated users.
  2. Logged-in users who aren't admins.
  3. Admin users.

Maybe this still isn't worthwhile to do and the existing logic is correct. But I wanted to open an issue to discuss further.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature Needs Discussion Anything that needs a discussion/agreement [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Discussion Anything that needs a discussion/agreement [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant