Skip to content
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

Only store info for relevant standalone plugins in the transient cache #1573

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions plugins/performance-lab/includes/admin/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
function perflab_query_plugin_info( string $plugin_slug ) {
$transient_key = 'perflab_plugins_info';
$plugins = get_transient( $transient_key );
$fields = array(

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.
}

$fields = array(
'name',
'slug',
'short_description',
Expand All @@ -32,14 +41,6 @@ function perflab_query_plugin_info( string $plugin_slug ) {
'version', // Needed by install_plugin_install_status().
);

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',
Expand Down Expand Up @@ -67,8 +68,12 @@ function perflab_query_plugin_info( string $plugin_slug ) {
return new WP_Error( 'no_plugins', __( 'No plugins found in the API response.', 'performance-lab' ) );
}

$plugins = array();
$plugins = array();
$standalone_plugins = array_flip( perflab_get_standalone_plugins() );
foreach ( $response->plugins as $plugin_data ) {
if ( ! isset( $standalone_plugins[ $plugin_data['slug'] ] ) ) {
continue;
}
$plugins[ $plugin_data['slug'] ] = wp_array_slice_assoc( $plugin_data, $fields );
}

Expand Down