Skip to content

Commit

Permalink
Merge branch 'trunk' into update/auto-sizes-code-reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 authored Aug 16, 2024
2 parents 7e8cebb + 4d22998 commit fa8d99b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
5 changes: 5 additions & 0 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions plugins/optimization-detective/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
71 changes: 59 additions & 12 deletions plugins/performance-lab/includes/admin/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,15 @@ 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 ) {
$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.
if ( $api_data instanceof WP_Error ) {
wp_admin_notice(
esc_html(
sprintf(
/* translators: 1: plugin slug. 2: error message. */
__( 'Failed to query WordPress.org Plugin Directory for plugin "%1$s". %2$s', 'performance-lab' ),
$plugin_slug,
$api_data->get_error_message()
)
),
array( 'type' => 'error' )
);
$errors[ $plugin_slug ] = $api_data;
} else {
$plugins[ $plugin_slug ] = array_merge(
array(
Expand All @@ -124,6 +116,61 @@ function perflab_render_plugins_ui(): void {
}
}

if ( count( $errors ) > 0 ) {
$active_plugins = array_map(
static function ( string $file ) {
return strtok( $file, '/' );
},
array_keys( get_plugins() )
);
$plugin_list = '<ul>';
$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(
'<li><a target="_blank" href="%s"><code>%s</code></a> %s</li>',
esc_url( trailingslashit( __( 'https://wordpress.org/plugins/', 'default' ) . $plugin_slug ) ),
esc_html( $plugin_slug ),
esc_html( $status )
);
$error_messages[] = $error->get_error_message();
}
$plugin_list .= '</ul>';

$error_messages = array_unique( $error_messages );

wp_admin_notice(
'<p>' . 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' )
) . '</p>' .
$plugin_list .
'<p>' . esc_html( _n( 'The following error occurred:', 'The following errors occurred:', count( $error_messages ), 'performance-lab' ) ) . '</p>' .
'<ul><li>' .
join(
'</li><li>',
array_map(
static function ( string $error_message ): string {
return wp_kses( $error_message, array( 'a' => array( 'href' => true ) ) );
},
$error_messages
)
)
. '</li></ul>' .
'<p>' . 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' ) . '</p>',
array(
'type' => 'error',
'paragraph_wrap' => false,
)
);
}

/*
* Sort plugins alphabetically, with experimental ones coming last.
* Even though `experimental` is a boolean flag, the underlying
Expand Down

0 comments on commit fa8d99b

Please sign in to comment.