-
Notifications
You must be signed in to change notification settings - Fork 94
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
Avoid passing incomplete data to perflab_render_plugin_card() and show error when plugin directory API query fails #1175
Merged
mukeshpanchal27
merged 11 commits into
trunk
from
fix/passing-incomplete-data-to-render-plugin-card
Apr 24, 2024
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
103161a
Avoid passing incomplete data to perflab_render_plugin_card()
westonruter 1e4c080
Improve readability of PHP templating and remove needless checks
westonruter e7db5ac
Show error when failed to query WordPress.org
westonruter 7067d9e
Only short-circuit if there are no plugins nor experimental plugins
westonruter c370906
Fix array shape for plugin API response
westonruter d2086f8
Request only the information needed from plugins API
westonruter c85a7e4
Fix showing error message when query fails in perflab_install_activat…
westonruter 61c26ed
Restore returning version from API request for install_plugin_install…
westonruter b1a2f02
Use Kses to sanitize WP_Error message from activate_plugin()
westonruter 2aa906b
Update translation string
westonruter 71a426d
Merge branch 'trunk' into fix/passing-incomplete-data-to-render-plugi…
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
* @since 2.8.0 | ||
* | ||
* @param string $plugin_slug The string identifier for the plugin in questions slug. | ||
* @return array Array of plugin data, or empty if none/error. | ||
* @return array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string}|WP_Error Array of plugin data or WP_Error if failed. | ||
*/ | ||
function perflab_query_plugin_info( string $plugin_slug ) { | ||
$plugin = get_transient( 'perflab_plugin_info_' . $plugin_slug ); | ||
|
@@ -29,14 +29,19 @@ function perflab_query_plugin_info( string $plugin_slug ) { | |
array( | ||
'slug' => $plugin_slug, | ||
'fields' => array( | ||
'name' => true, | ||
'slug' => true, | ||
'short_description' => true, | ||
'icons' => true, | ||
'requires' => true, | ||
'requires_php' => true, | ||
'requires_plugins' => true, | ||
'download_link' => true, | ||
), | ||
) | ||
); | ||
|
||
if ( is_wp_error( $plugin ) ) { | ||
return array(); | ||
return $plugin; | ||
} | ||
|
||
if ( is_object( $plugin ) ) { | ||
|
@@ -74,20 +79,41 @@ function perflab_render_plugins_ui() { | |
$experimental_plugins = 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( | ||
esc_html( | ||
sprintf( | ||
/* translators: %s is the plugin slug */ | ||
__( 'Failed to query WordPress.org Plugin Directory for plugin "%s".', 'performance-lab' ) . ' %s', | ||
$plugin_slug, | ||
$api_data->get_error_message() | ||
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
), | ||
array( 'type' => 'error' ) | ||
); | ||
continue; | ||
} | ||
|
||
$plugin_data = array_merge( | ||
array( | ||
'experimental' => false, | ||
), | ||
$plugin_data, // Data defined within Performance Lab. | ||
perflab_query_plugin_info( $plugin_slug ) // Data from wordpress.org. | ||
$api_data | ||
); | ||
|
||
// Separate experimental plugins so that they're displayed after non-experimental plugins. | ||
if ( isset( $plugin_data['experimental'] ) && $plugin_data['experimental'] ) { | ||
if ( $plugin_data['experimental'] ) { | ||
$experimental_plugins[ $plugin_slug ] = $plugin_data; | ||
} else { | ||
$plugins[ $plugin_slug ] = $plugin_data; | ||
} | ||
} | ||
|
||
if ( empty( $plugins ) ) { | ||
if ( ! $plugins && ! $experimental_plugins ) { | ||
return; | ||
} | ||
?> | ||
|
@@ -125,22 +151,15 @@ function perflab_render_plugins_ui() { | |
* @see WP_Plugin_Install_List_Table::display_rows() | ||
* @link https://github.com/WordPress/wordpress-develop/blob/0b8ca16ea3bd9722bd1a38f8ab68901506b1a0e7/src/wp-admin/includes/class-wp-plugin-install-list-table.php#L467-L830 | ||
* | ||
* @param array $plugin_data Plugin data from the WordPress.org API. | ||
* @param array{name: string, slug: string, short_description: string, requires_php: string|false, requires: string|false, requires_plugins: string[], experimental: bool} $plugin_data Plugin data augmenting data from the WordPress.org API. | ||
*/ | ||
function perflab_render_plugin_card( array $plugin_data ) { | ||
// If no plugin data is returned, return. | ||
if ( empty( $plugin_data ) ) { | ||
return; | ||
} | ||
|
||
// Remove any HTML from the description. | ||
$name = wp_strip_all_tags( $plugin_data['name'] ); | ||
$description = wp_strip_all_tags( $plugin_data['short_description'] ); | ||
$title = $plugin_data['name']; | ||
|
||
/** This filter is documented in wp-admin/includes/class-wp-plugin-install-list-table.php */ | ||
$description = apply_filters( 'plugin_install_description', $description, $plugin_data ); | ||
$version = $plugin_data['version']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't see a need for the version to be mentioned among the features, so I removed it. |
||
$name = wp_strip_all_tags( $title . ' ' . $version ); | ||
|
||
$requires_php = isset( $plugin_data['requires_php'] ) ? $plugin_data['requires_php'] : null; | ||
$requires_wp = isset( $plugin_data['requires'] ) ? $plugin_data['requires'] : null; | ||
|
@@ -193,7 +212,7 @@ function perflab_render_plugin_card( array $plugin_data ) { | |
); | ||
} | ||
|
||
if ( isset( $plugin_data['slug'] ) && current_user_can( 'install_plugins' ) ) { | ||
if ( current_user_can( 'install_plugins' ) ) { | ||
$title_link_attr = ' class="thickbox open-plugin-details-modal"'; | ||
$details_link = esc_url_raw( | ||
add_query_arg( | ||
|
@@ -297,25 +316,21 @@ function perflab_render_plugin_card( array $plugin_data ) { | |
<div class="name column-name"> | ||
<h3> | ||
<a href="<?php echo esc_url( $details_link ); ?>"<?php echo $title_link_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> | ||
<?php echo wp_kses_post( $title ); ?> | ||
<?php echo wp_kses_post( $name ); ?> | ||
</a> | ||
<?php | ||
if ( isset( $plugin_data['experimental'] ) && $plugin_data['experimental'] ) { | ||
?> | ||
<?php if ( $plugin_data['experimental'] ) : ?> | ||
<em class="perflab-plugin-experimental"> | ||
<?php echo esc_html( _x( '(experimental)', 'plugin suffix', 'performance-lab' ) ); ?> | ||
</em> | ||
<?php | ||
} | ||
?> | ||
<?php endif; ?> | ||
</h3> | ||
</div> | ||
<div class="action-links"> | ||
<?php | ||
if ( ! empty( $action_links ) ) { | ||
echo wp_kses_post( '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>' ); | ||
} | ||
?> | ||
<ul class="plugin-action-buttons"> | ||
<?php foreach ( $action_links as $action_link ) : ?> | ||
<li><?php echo wp_kses_post( $action_link ); ?></li> | ||
<?php endforeach; ?> | ||
</ul> | ||
</div> | ||
<div class="desc column-description"> | ||
<p><?php echo wp_kses_post( $description ); ?></p> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the specific error is shown rather than just the generic message.