From 83e6e242bba706de624c1514cc50af4b95839f00 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 14 Aug 2024 13:38:08 -0700 Subject: [PATCH 1/9] Disable Optimization Detective by default on the embed template --- plugins/optimization-detective/optimization.php | 5 +++++ plugins/optimization-detective/readme.txt | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/plugins/optimization-detective/optimization.php b/plugins/optimization-detective/optimization.php index e47d7813e2..66a97591f2 100644 --- a/plugins/optimization-detective/optimization.php +++ b/plugins/optimization-detective/optimization.php @@ -107,6 +107,11 @@ function od_can_optimize_response(): bool { // 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() || + // Avoid optimizing embed responses because the Post Embed iframes include a sandbox attribute with the value of + // "allow-scripts" but without "allow-same-origin". This can result in an error in the console: + // > 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). + is_embed() || // 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. diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index 5bf5a01c9c..0bb7601ec5 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -48,10 +48,11 @@ The default breakpoints are reused from Gutenberg which appear to be used the mo Filters whether the current response can be optimized. By default, detection and optimization are only performed when: -1. It’s not a search template (i.e. `is_search()`). -2. It’s not the Customizer preview. -3. It’s not the response to a `POST` request. -4. The user is not an administrator (i.e. the `customize` capability). +1. It’s not a search template (`is_search()`). +2. It’s not a post embed template (`is_embed()`). +3. It’s not the Customizer preview (`is_customize_preview()`) +4. It’s not the response to a `POST` request. +5. The user is not an administrator (`current_user_can( 'customize' )`). During development, you may want to force this to always be enabled: From 20ee00070e6c12f907f05a4b15795db40bf09b59 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 15 Aug 2024 11:40:10 -0700 Subject: [PATCH 2/9] Fix escaping link in error mesage --- plugins/performance-lab/includes/admin/plugins.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index a94c4d10bf..8ff8cd549f 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -103,12 +103,15 @@ function perflab_render_plugins_ui(): void { // Skip if the plugin is not on WordPress.org or there was a network error. if ( $api_data instanceof WP_Error ) { wp_admin_notice( - esc_html( + wp_kses( sprintf( /* translators: 1: plugin slug. 2: error message. */ - __( 'Failed to query WordPress.org Plugin Directory for plugin "%1$s". %2$s', 'performance-lab' ), + esc_html__( 'Failed to query WordPress.org Plugin Directory for plugin "%1$s". %2$s', 'performance-lab' ), $plugin_slug, $api_data->get_error_message() + ), + array( + 'a' => array( 'href' => true ), ) ), array( 'type' => 'error' ) From 6f4418a5755a0d9a0fdc77bf3f82c541c99a8518 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 15 Aug 2024 11:55:29 -0700 Subject: [PATCH 3/9] Merge error messages --- .../includes/admin/plugins.php | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index 8ff8cd549f..f8f0bd23a5 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -96,26 +96,14 @@ function perflab_render_plugins_ui(): void { require_once ABSPATH . 'wp-admin/includes/plugin.php'; $plugins = array(); + $errors = array(); foreach ( perflab_get_standalone_plugin_data() as $plugin_slug => $plugin_data ) { $api_data = perflab_query_plugin_info( $plugin_slug ); // Data from wordpress.org. // Skip if the plugin is not on WordPress.org or there was a network error. if ( $api_data instanceof WP_Error ) { - wp_admin_notice( - wp_kses( - sprintf( - /* translators: 1: plugin slug. 2: error message. */ - esc_html__( 'Failed to query WordPress.org Plugin Directory for plugin "%1$s". %2$s', 'performance-lab' ), - $plugin_slug, - $api_data->get_error_message() - ), - array( - 'a' => array( 'href' => true ), - ) - ), - array( 'type' => 'error' ) - ); + $errors[ $plugin_slug ] = $api_data; } else { $plugins[ $plugin_slug ] = array_merge( array( @@ -127,6 +115,27 @@ function perflab_render_plugins_ui(): void { } } + if ( count( $errors ) > 0 ) { + $list = '
    '; + foreach ( $errors as $plugin_slug => $error ) { + $list .= sprintf( + '
  • %s: %s
  • ', + esc_url( trailingslashit( __( 'https://wordpress.org/plugins/', 'default' ) . $plugin_slug ) ), + esc_html( $plugin_slug ), + wp_kses( $error->get_error_message(), array( 'a' => array( 'href' => true ) ) ) + ); + } + $list .= '
'; + wp_admin_notice( + esc_html( + _n( 'Failed to query WordPress.org Plugin Directory for the following plugin:', 'Failed to query WordPress.org Plugin Directory for the following plugins:', count( $errors ), 'performance-lab' ) + ) . + $list . + esc_html__( 'Please consider manual installation.', 'performance-lab' ), + array( 'type' => 'error' ) + ); + } + /* * Sort plugins alphabetically, with experimental ones coming last. * Even though `experimental` is a boolean flag, the underlying From 24b7d62c40a9e4690d91eb57344f95a89c28f1c6 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 15 Aug 2024 12:17:40 -0700 Subject: [PATCH 4/9] Eliminate duplicated error messages --- .../includes/admin/plugins.php | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index f8f0bd23a5..5b0435b9b9 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -116,21 +116,34 @@ function perflab_render_plugins_ui(): void { } if ( count( $errors ) > 0 ) { - $list = '
    '; + $active_plugins = array_map( + static function ( string $file ) { + return strtok( $file, '/' ); + }, + array_keys( get_plugins() ) + ); + $plugin_list = '
      '; + $error_messages = array(); foreach ( $errors as $plugin_slug => $error ) { - $list .= sprintf( - '
    • %s: %s
    • ', + $plugin_list .= sprintf( + '
    • %s%s
    • ', esc_url( trailingslashit( __( 'https://wordpress.org/plugins/', 'default' ) . $plugin_slug ) ), esc_html( $plugin_slug ), - wp_kses( $error->get_error_message(), array( 'a' => array( 'href' => true ) ) ) + in_array( $plugin_slug, $active_plugins, true ) ? ' ' . esc_html__( '(already installed)', 'performance-lab' ) : '' ); + $error_messages[] = $error->get_error_message(); } - $list .= '
    '; + $plugin_list .= '
'; + + $error_messages = array_unique( $error_messages ); + wp_admin_notice( esc_html( _n( 'Failed to query WordPress.org Plugin Directory for the following plugin:', 'Failed to query WordPress.org Plugin Directory for the following plugins:', count( $errors ), 'performance-lab' ) ) . - $list . + $plugin_list . + '

' . esc_html( _n( 'The following error occurred:', 'The following errors occurred:', count( $error_messages ), 'performance-lab' ) ) . '

' . + '
  • ' . join( '
  • ', $error_messages ) . '
' . esc_html__( 'Please consider manual installation.', 'performance-lab' ), array( 'type' => 'error' ) ); From edcc4bfbba0be3eb198f9330a9ec4dfec284660d Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 15 Aug 2024 12:21:31 -0700 Subject: [PATCH 5/9] Indicate whether a plugin is installed or active --- .../performance-lab/includes/admin/plugins.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index 5b0435b9b9..77b51454bf 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -98,7 +98,8 @@ function perflab_render_plugins_ui(): void { $plugins = array(); $errors = array(); - foreach ( perflab_get_standalone_plugin_data() as $plugin_slug => $plugin_data ) { + $standalone_plugin_data = perflab_get_standalone_plugin_data(); + foreach ( $standalone_plugin_data as $plugin_slug => $plugin_data ) { $api_data = perflab_query_plugin_info( $plugin_slug ); // Data from wordpress.org. // Skip if the plugin is not on WordPress.org or there was a network error. @@ -125,11 +126,19 @@ static function ( string $file ) { $plugin_list = '
    '; $error_messages = array(); foreach ( $errors as $plugin_slug => $error ) { + if ( defined( $standalone_plugin_data[ $plugin_slug ]['constant'] ) ) { + $status = __( '(active)', 'performance-lab' ); + } elseif ( in_array( $plugin_slug, $active_plugins, true ) ) { + $status = __( '(installed)', 'performance-lab' ); + } else { + $status = ''; + } + $plugin_list .= sprintf( - '
  • %s%s
  • ', + '
  • %s %s
  • ', esc_url( trailingslashit( __( 'https://wordpress.org/plugins/', 'default' ) . $plugin_slug ) ), esc_html( $plugin_slug ), - in_array( $plugin_slug, $active_plugins, true ) ? ' ' . esc_html__( '(already installed)', 'performance-lab' ) : '' + esc_html( $status ) ); $error_messages[] = $error->get_error_message(); } @@ -144,7 +153,7 @@ static function ( string $file ) { $plugin_list . '

    ' . esc_html( _n( 'The following error occurred:', 'The following errors occurred:', count( $error_messages ), 'performance-lab' ) ) . '

    ' . '
    • ' . join( '
    • ', $error_messages ) . '
    ' . - esc_html__( 'Please consider manual installation.', 'performance-lab' ), + esc_html__( 'Please consider manual plugin installation and activation.', 'performance-lab' ), array( 'type' => 'error' ) ); } From 96a03b26a3041f621e1b2ff71be503c2281be7a5 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 15 Aug 2024 12:33:18 -0700 Subject: [PATCH 6/9] Add note about where to access settings --- plugins/performance-lab/includes/admin/plugins.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index 77b51454bf..3bf8404646 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -153,7 +153,7 @@ static function ( string $file ) { $plugin_list . '

    ' . esc_html( _n( 'The following error occurred:', 'The following errors occurred:', count( $error_messages ), 'performance-lab' ) ) . '

    ' . '
    • ' . join( '
    • ', $error_messages ) . '
    ' . - esc_html__( 'Please consider manual plugin installation and activation.', 'performance-lab' ), + esc_html__( 'Please consider manual plugin installation and activation. You can then access each plugin\'s settings via its respective "Settings" link on the Plugins screen.', 'performance-lab' ), array( 'type' => 'error' ) ); } From 8f5726d3bf72b2684b70f9984aab4f6ae81f5456 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 15 Aug 2024 13:14:31 -0700 Subject: [PATCH 7/9] Add use of wp_kses() --- plugins/performance-lab/includes/admin/plugins.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index 3bf8404646..04714481a1 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -152,7 +152,17 @@ static function ( string $file ) { ) . $plugin_list . '

    ' . esc_html( _n( 'The following error occurred:', 'The following errors occurred:', count( $error_messages ), 'performance-lab' ) ) . '

    ' . - '
    • ' . join( '
    • ', $error_messages ) . '
    ' . + '
    • ' . + join( + '
    • ', + array_map( + static function ( string $error_message ): string { + return wp_kses( $error_message, array( 'a' => array( 'href' => true ) ) ); + }, + $error_messages + ) + ) + . '
    ' . esc_html__( 'Please consider manual plugin installation and activation. You can then access each plugin\'s settings via its respective "Settings" link on the Plugins screen.', 'performance-lab' ), array( 'type' => 'error' ) ); From 96ab82bbdd78a51d0792427593d9b693371b0845 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 16 Aug 2024 10:47:11 +0530 Subject: [PATCH 8/9] Remove empty p tag --- plugins/performance-lab/includes/admin/plugins.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index 04714481a1..f2f2dd6798 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -163,8 +163,11 @@ static function ( string $error_message ): string { ) ) . '
' . - esc_html__( 'Please consider manual plugin installation and activation. You can then access each plugin\'s settings via its respective "Settings" link on the Plugins screen.', 'performance-lab' ), - array( 'type' => 'error' ) + '

' . esc_html__( 'Please consider manual plugin installation and activation. You can then access each plugin\'s settings via its respective "Settings" link on the Plugins screen.', 'performance-lab' ) . '

', + array( + 'type' => 'error', + 'paragraph_wrap' => false, + ) ); } From 5ecd4c2acfb3b61d044d5203a5f5e0cd706e8995 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 16 Aug 2024 10:53:08 +0530 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: Weston Ruter --- plugins/performance-lab/includes/admin/plugins.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index f2f2dd6798..eb69aca044 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -147,9 +147,9 @@ static function ( string $file ) { $error_messages = array_unique( $error_messages ); wp_admin_notice( - esc_html( + '

' . esc_html( _n( 'Failed to query WordPress.org Plugin Directory for the following plugin:', 'Failed to query WordPress.org Plugin Directory for the following plugins:', count( $errors ), 'performance-lab' ) - ) . + ) . '

' . $plugin_list . '

' . esc_html( _n( 'The following error occurred:', 'The following errors occurred:', count( $error_messages ), 'performance-lab' ) ) . '

' . '
  • ' .