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

Improve Performance screen when external requests to WordPress.org fail #1474

Merged
merged 8 commits into from
Aug 16, 2024
58 changes: 46 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,48 @@ 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(
esc_html(
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
_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' )
) .
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$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>', $error_messages ) . '</li></ul>' .
westonruter marked this conversation as resolved.
Show resolved Hide resolved
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' )
);
}

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