@@ -22,12 +22,12 @@ function perflab_query_plugin_info( string $plugin_slug ) {
22
22
$ transient_key = 'perflab_plugins_info ' ;
23
23
$ plugins = get_transient ( $ transient_key );
24
24
25
- if ( is_array ( $ plugins ) ) {
26
- // If the specific plugin_slug is not in the cache, return an error.
27
- if ( ! isset ( $ plugins [ $ plugin_slug ] ) ) {
25
+ if ( is_array ( $ plugins ) && isset ( $ plugins [ $ plugin_slug ] ) ) {
26
+ if ( isset ( $ plugins [ $ plugin_slug ][ ' error ' ] ) ) {
27
+ // Plugin was requested before but an error occurred for it.
28
28
return new WP_Error (
29
- ' plugin_not_found ' ,
30
- __ ( ' Plugin not found in cached API response. ' , ' performance-lab ' )
29
+ $ plugins [ $ plugin_slug ][ ' error ' ][ ' code ' ] ,
30
+ $ plugins [ $ plugin_slug ][ ' error ' ][ ' message ' ]
31
31
);
32
32
}
33
33
return $ plugins [ $ plugin_slug ]; // Return cached plugin info if found.
@@ -54,58 +54,94 @@ function perflab_query_plugin_info( string $plugin_slug ) {
54
54
)
55
55
);
56
56
57
+ $ has_errors = false ;
58
+ $ plugins = array ();
59
+
57
60
if ( is_wp_error ( $ response ) ) {
58
- return new WP_Error (
59
- 'api_error ' ,
60
- sprintf (
61
- /* translators: %s: API error message */
62
- __ ( 'Failed to retrieve plugins data from WordPress.org API: %s ' , 'performance-lab ' ),
63
- $ response ->get_error_message ()
64
- )
61
+ $ plugins [ $ plugin_slug ] = array (
62
+ 'error ' => array (
63
+ 'code ' => 'api_error ' ,
64
+ 'message ' => sprintf (
65
+ /* translators: %s: API error message */
66
+ __ ( 'Failed to retrieve plugins data from WordPress.org API: %s ' , 'performance-lab ' ),
67
+ $ response ->get_error_message ()
68
+ ),
69
+ ),
65
70
);
66
- }
67
71
68
- // Check if the response contains plugins.
69
- if ( ! ( is_object ( $ response ) && property_exists ( $ response , 'plugins ' ) ) ) {
70
- return new WP_Error ( 'no_plugins ' , __ ( 'No plugins found in the API response. ' , 'performance-lab ' ) );
71
- }
72
+ foreach ( perflab_get_standalone_plugins () as $ standalone_plugin ) {
73
+ $ plugins [ $ standalone_plugin ] = $ plugins [ $ plugin_slug ];
74
+ }
72
75
73
- $ plugins = array ();
74
- $ plugin_queue = perflab_get_standalone_plugins ();
76
+ $ has_errors = true ;
77
+ } elseif ( ! is_object ( $ response ) || ! property_exists ( $ response , 'plugins ' ) ) {
78
+ $ plugins [ $ plugin_slug ] = array (
79
+ 'error ' => array (
80
+ 'code ' => 'no_plugins ' ,
81
+ 'message ' => __ ( 'No plugins found in the API response. ' , 'performance-lab ' ),
82
+ ),
83
+ );
75
84
76
- // Index the plugins from the API response by their slug for efficient lookup.
77
- $ all_performance_plugins = array_column ( $ response ->plugins , null , 'slug ' );
85
+ foreach ( perflab_get_standalone_plugins () as $ standalone_plugin ) {
86
+ $ plugins [ $ standalone_plugin ] = $ plugins [ $ plugin_slug ];
87
+ }
78
88
79
- // Start processing the plugins using a queue-based approach.
80
- while ( count ( $ plugin_queue ) > 0 ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
81
- $ current_plugin_slug = array_shift ( $ plugin_queue );
89
+ $ has_errors = true ;
90
+ } else {
91
+ $ plugin_queue = perflab_get_standalone_plugins ( );
82
92
83
- if ( isset ( $ plugins [ $ current_plugin_slug ] ) ) {
84
- continue ;
85
- }
93
+ // Index the plugins from the API response by their slug for efficient lookup.
94
+ $ all_performance_plugins = array_column ( $ response ->plugins , null , 'slug ' );
86
95
87
- if ( ! isset ( $ all_performance_plugins [ $ current_plugin_slug ] ) ) {
88
- return new WP_Error (
89
- 'plugin_not_found ' ,
90
- __ ( 'Plugin not found in WordPress.org API response. ' , 'performance-lab ' )
91
- );
96
+ // Start processing the plugins using a queue-based approach.
97
+ while ( count ( $ plugin_queue ) > 0 ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
98
+ $ current_plugin_slug = array_shift ( $ plugin_queue );
99
+
100
+ // Skip already-processed plugins.
101
+ if ( isset ( $ plugins [ $ current_plugin_slug ] ) ) {
102
+ continue ;
103
+ }
104
+
105
+ if ( ! isset ( $ all_performance_plugins [ $ current_plugin_slug ] ) ) {
106
+ // Cache the fact that the plugin was not found.
107
+ $ plugins [ $ current_plugin_slug ] = array (
108
+ 'error ' => array (
109
+ 'code ' => 'plugin_not_found ' ,
110
+ 'message ' => __ ( 'Plugin not found in API response. ' , 'performance-lab ' ),
111
+ ),
112
+ );
113
+
114
+ $ has_errors = true ;
115
+ } else {
116
+ $ plugin_data = $ all_performance_plugins [ $ current_plugin_slug ];
117
+ $ plugins [ $ current_plugin_slug ] = wp_array_slice_assoc ( $ plugin_data , $ fields );
118
+
119
+ // Enqueue the required plugins slug by adding it to the queue.
120
+ if ( isset ( $ plugin_data ['requires_plugins ' ] ) && is_array ( $ plugin_data ['requires_plugins ' ] ) ) {
121
+ $ plugin_queue = array_merge ( $ plugin_queue , $ plugin_data ['requires_plugins ' ] );
122
+ }
123
+ }
92
124
}
93
125
94
- $ plugin_data = $ all_performance_plugins [ $ current_plugin_slug ];
95
- $ plugins [ $ current_plugin_slug ] = wp_array_slice_assoc ( $ plugin_data , $ fields );
126
+ if ( ! isset ( $ plugins [ $ plugin_slug ] ) ) {
127
+ // Cache the fact that the plugin was not found.
128
+ $ plugins [ $ plugin_slug ] = array (
129
+ 'error ' => array (
130
+ 'code ' => 'plugin_not_found ' ,
131
+ 'message ' => __ ( 'The requested plugin is not part of Performance Lab plugins. ' , 'performance-lab ' ),
132
+ ),
133
+ );
96
134
97
- // Enqueue the required plugins slug by adding it to the queue.
98
- if ( isset ( $ plugin_data ['requires_plugins ' ] ) && is_array ( $ plugin_data ['requires_plugins ' ] ) ) {
99
- $ plugin_queue = array_merge ( $ plugin_queue , $ plugin_data ['requires_plugins ' ] );
135
+ $ has_errors = true ;
100
136
}
101
137
}
102
138
103
- set_transient ( $ transient_key , $ plugins , HOUR_IN_SECONDS );
139
+ set_transient ( $ transient_key , $ plugins , $ has_errors ? MINUTE_IN_SECONDS : HOUR_IN_SECONDS );
104
140
105
- if ( ! isset ( $ plugins [ $ plugin_slug ] ) ) {
141
+ if ( isset ( $ plugins [ $ plugin_slug ][ ' error ' ] ) ) {
106
142
return new WP_Error (
107
- ' plugin_not_found ' ,
108
- __ ( ' Plugin not found in API response. ' , ' performance-lab ' )
143
+ $ plugins [ $ plugin_slug ][ ' error ' ][ ' code ' ] ,
144
+ $ plugins [ $ plugin_slug ][ ' error ' ][ ' message ' ]
109
145
);
110
146
}
111
147
0 commit comments