1919 * @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.
2020 */
2121function perflab_query_plugin_info ( string $ plugin_slug ) {
22- $ plugin = get_transient ( 'perflab_plugin_info_ ' . $ plugin_slug );
23-
24- if ( is_array ( $ plugin ) ) {
25- /**
26- * Validated (mostly) plugin data.
27- *
28- * @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
29- */
30- return $ plugin ;
31- }
32-
33- $ fields = array (
22+ $ transient_key = 'perflab_plugins_info ' ;
23+ $ plugins = get_transient ( $ transient_key );
24+ $ fields = array (
3425 'name ' ,
3526 'slug ' ,
3627 'short_description ' ,
@@ -41,36 +32,58 @@ function perflab_query_plugin_info( string $plugin_slug ) {
4132 'version ' , // Needed by install_plugin_install_status().
4233 );
4334
44- $ plugin = plugins_api (
45- 'plugin_information ' ,
35+ if ( is_array ( $ plugins ) ) {
36+ // If the specific plugin_slug is not in the cache, return an error.
37+ if ( ! isset ( $ plugins [ $ plugin_slug ] ) ) {
38+ return new WP_Error ( 'plugin_not_found ' , __ ( 'Plugin not found. ' , 'performance-lab ' ) );
39+ }
40+ return $ plugins [ $ plugin_slug ]; // Return cached plugin info if found.
41+ }
42+
43+ // Proceed with API request since no cache hit.
44+ $ response = plugins_api (
45+ 'query_plugins ' ,
4646 array (
47- 'slug ' => $ plugin_slug ,
48- 'fields ' => array_fill_keys ( $ fields , true ),
47+ 'author ' => 'wordpressdotorg ' ,
48+ 'tag ' => 'performance ' ,
49+ 'per_page ' => 100 ,
50+ 'fields ' => array_fill_keys ( $ fields , true ),
4951 )
5052 );
5153
52- if ( is_wp_error ( $ plugin ) ) {
53- return $ plugin ;
54+ if ( is_wp_error ( $ response ) ) {
55+ return new WP_Error (
56+ 'api_error ' ,
57+ sprintf (
58+ /* translators: %s: API error message */
59+ __ ( 'Failed to retrieve plugins data from WordPress.org API: %s ' , 'performance-lab ' ),
60+ $ response ->get_error_message ()
61+ )
62+ );
5463 }
5564
56- if ( is_object ( $ plugin ) ) {
57- $ plugin = (array ) $ plugin ;
65+ // Check if the response contains plugins.
66+ if ( ! ( is_object ( $ response ) && property_exists ( $ response , 'plugins ' ) ) ) {
67+ return new WP_Error ( 'no_plugins ' , __ ( 'No plugins found in the API response. ' , 'performance-lab ' ) );
5868 }
5969
60- // Only store what we need.
61- $ plugin = wp_array_slice_assoc ( $ plugin , $ fields );
70+ $ plugins = array ();
71+ foreach ( $ response ->plugins as $ plugin_data ) {
72+ $ plugins [ $ plugin_data ['slug ' ] ] = wp_array_slice_assoc ( $ plugin_data , $ fields );
73+ }
6274
63- // Make sure all fields default to false in case another plugin is modifying the response from WordPress.org via the plugins_api filter.
64- $ plugin = array_merge ( array_fill_keys ( $ fields , false ), $ plugin );
75+ set_transient ( $ transient_key , $ plugins , HOUR_IN_SECONDS );
6576
66- set_transient ( 'perflab_plugin_info_ ' . $ plugin_slug , $ plugin , HOUR_IN_SECONDS );
77+ if ( ! isset ( $ plugins [ $ plugin_slug ] ) ) {
78+ return new WP_Error ( 'plugin_not_found ' , __ ( 'Plugin not found. ' , 'performance-lab ' ) );
79+ }
6780
6881 /**
6982 * Validated (mostly) plugin data.
7083 *
71- * @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
84+ * @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
7285 */
73- return $ plugin ;
86+ return $ plugins [ $ plugin_slug ] ;
7487}
7588
7689/**
0 commit comments