-
Notifications
You must be signed in to change notification settings - Fork 138
Use a single WordPress.org API request to get information for all plugins #1562
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
Changes from 18 commits
6c3796a
6179a0c
d6424a6
23bd591
075cf49
0e669f2
9298221
fcfdc43
b4b4dfd
04ec261
c9aceb2
72bb1bd
d17c835
5dc56de
3490ae8
bf170bc
c9e287e
798de3e
b52f0e0
a082741
843b12d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,18 +19,9 @@ | |
| * @return array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string, version: 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 ); | ||
|
|
||
| if ( is_array( $plugin ) ) { | ||
| /** | ||
| * Validated (mostly) plugin data. | ||
| * | ||
| * @var array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string, version: string} $plugin | ||
| */ | ||
| return $plugin; | ||
| } | ||
|
|
||
| $fields = array( | ||
| $transient_key = 'perflab_plugins_info'; | ||
| $plugins = get_transient( $transient_key ); | ||
| $fields = array( | ||
| 'name', | ||
| 'slug', | ||
| 'short_description', | ||
|
|
@@ -41,36 +32,69 @@ function perflab_query_plugin_info( string $plugin_slug ) { | |
| 'version', // Needed by install_plugin_install_status(). | ||
| ); | ||
|
|
||
| $plugin = plugins_api( | ||
| 'plugin_information', | ||
| if ( is_array( $plugins ) ) { | ||
| /* If the specific plugin_slug is not in the cache, return an error. */ | ||
| if ( ! isset( $plugins[ $plugin_slug ] ) ) { | ||
| return new WP_Error( 'plugin_not_found', __( 'Plugin not found.', 'performance-lab' ) ); | ||
| } | ||
| return $plugins[ $plugin_slug ]; // Return cached plugin info if found. | ||
| } | ||
|
|
||
| // Proceed with API request since no cache hit. | ||
| $response = plugins_api( | ||
| 'query_plugins', | ||
| array( | ||
| 'slug' => $plugin_slug, | ||
| 'fields' => array_fill_keys( $fields, true ), | ||
| 'author' => 'wordpressdotorg', | ||
| 'tag' => 'performance', | ||
| 'per_page' => 100, | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'fields' => array_fill_keys( $fields, true ), | ||
| ) | ||
| ); | ||
|
|
||
| if ( is_wp_error( $plugin ) ) { | ||
| return $plugin; | ||
| if ( is_wp_error( $response ) ) { | ||
| return new WP_Error( | ||
| 'api_error', | ||
| sprintf( | ||
| /* translators: %s: API error message */ | ||
| __( 'Failed to retrieve plugins data from WordPress.org API: %s', 'performance-lab' ), | ||
| $response->get_error_message() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| if ( is_object( $plugin ) ) { | ||
| $plugin = (array) $plugin; | ||
| // Check if the response contains plugins. | ||
| if ( ! ( is_object( $response ) && property_exists( $response, 'plugins' ) ) ) { | ||
| return new WP_Error( 'no_plugins', __( 'No plugins found in the API response.', 'performance-lab' ) ); | ||
| } | ||
|
|
||
| // Only store what we need. | ||
| $plugin = wp_array_slice_assoc( $plugin, $fields ); | ||
| $plugins = array(); | ||
| foreach ( $response->plugins as $plugin_data ) { | ||
| $plugin_info = wp_array_slice_assoc( $plugin_data, $fields ); | ||
|
|
||
| // Ensure the 'requires_plugins' is always an array. | ||
| if ( ! isset( $plugin_info['requires_plugins'] ) || ! is_array( $plugin_info['requires_plugins'] ) ) { | ||
| $plugin_info['requires_plugins'] = array(); | ||
| } | ||
|
||
|
|
||
| // Make sure all fields default to false in case another plugin is modifying the response from WordPress.org via the plugins_api filter. | ||
| $plugin = array_merge( array_fill_keys( $fields, false ), $plugin ); | ||
| // Ensure 'requires' and 'requires_php' are either strings or false. | ||
| $plugin_info['requires'] = isset( $plugin_info['requires'] ) ? $plugin_info['requires'] : false; | ||
| $plugin_info['requires_php'] = isset( $plugin_info['requires_php'] ) ? $plugin_info['requires_php'] : false; | ||
narenin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| set_transient( 'perflab_plugin_info_' . $plugin_slug, $plugin, HOUR_IN_SECONDS ); | ||
| $plugins[ $plugin_data['slug'] ] = $plugin_info; | ||
| } | ||
|
Member
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. This would be the equivalent of the existing code: $plugins = array();
foreach ( $response->plugins as $plugin_data ) {
$plugins[ $plugin_data['slug'] ] = wp_array_slice_assoc( $plugin_data, $fields );
}I'm curious why the additional
Contributor
Author
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. Done |
||
|
|
||
| set_transient( $transient_key, $plugins, HOUR_IN_SECONDS ); | ||
|
|
||
| if ( ! isset( $plugins[ $plugin_slug ] ) ) { | ||
| return new WP_Error( 'plugin_not_found', __( 'Plugin not found.', 'performance-lab' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Validated (mostly) plugin data. | ||
| * | ||
| * @var array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string, version: string} $plugin | ||
| * @var array<string, array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string, version: string}> $plugins | ||
| */ | ||
| return $plugin; | ||
| return $plugins[ $plugin_slug ]; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.