Skip to content

Commit 8ba0487

Browse files
Merge branch 'trunk' into fix/1557-convert-webp-to-avif
2 parents ab8d207 + 9661a3c commit 8ba0487

File tree

1 file changed

+77
-41
lines changed

1 file changed

+77
-41
lines changed

plugins/performance-lab/includes/admin/plugins.php

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ function perflab_query_plugin_info( string $plugin_slug ) {
2222
$transient_key = 'perflab_plugins_info';
2323
$plugins = get_transient( $transient_key );
2424

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.
2828
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']
3131
);
3232
}
3333
return $plugins[ $plugin_slug ]; // Return cached plugin info if found.
@@ -54,58 +54,94 @@ function perflab_query_plugin_info( string $plugin_slug ) {
5454
)
5555
);
5656

57+
$has_errors = false;
58+
$plugins = array();
59+
5760
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+
),
6570
);
66-
}
6771

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+
}
7275

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+
);
7584

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+
}
7888

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();
8292

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' );
8695

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+
}
92124
}
93125

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+
);
96134

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;
100136
}
101137
}
102138

103-
set_transient( $transient_key, $plugins, HOUR_IN_SECONDS );
139+
set_transient( $transient_key, $plugins, $has_errors ? MINUTE_IN_SECONDS : HOUR_IN_SECONDS );
104140

105-
if ( ! isset( $plugins[ $plugin_slug ] ) ) {
141+
if ( isset( $plugins[ $plugin_slug ]['error'] ) ) {
106142
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']
109145
);
110146
}
111147

0 commit comments

Comments
 (0)