Skip to content

Commit

Permalink
Merge branch 'trunk' into update/incorporate-page-state-for-etag
Browse files Browse the repository at this point in the history
  • Loading branch information
ShyamGadde authored Dec 10, 2024
2 parents 3c3a1a3 + 9661a3c commit 65dc803
Showing 1 changed file with 77 additions and 41 deletions.
118 changes: 77 additions & 41 deletions plugins/performance-lab/includes/admin/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function perflab_query_plugin_info( string $plugin_slug ) {
$transient_key = 'perflab_plugins_info';
$plugins = get_transient( $transient_key );

if ( is_array( $plugins ) ) {
// If the specific plugin_slug is not in the cache, return an error.
if ( ! isset( $plugins[ $plugin_slug ] ) ) {
if ( is_array( $plugins ) && isset( $plugins[ $plugin_slug ] ) ) {
if ( isset( $plugins[ $plugin_slug ]['error'] ) ) {
// Plugin was requested before but an error occurred for it.
return new WP_Error(
'plugin_not_found',
__( 'Plugin not found in cached API response.', 'performance-lab' )
$plugins[ $plugin_slug ]['error']['code'],
$plugins[ $plugin_slug ]['error']['message']
);
}
return $plugins[ $plugin_slug ]; // Return cached plugin info if found.
Expand All @@ -54,58 +54,94 @@ function perflab_query_plugin_info( string $plugin_slug ) {
)
);

$has_errors = false;
$plugins = array();

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()
)
$plugins[ $plugin_slug ] = array(
'error' => array(
'code' => 'api_error',
'message' => sprintf(
/* translators: %s: API error message */
__( 'Failed to retrieve plugins data from WordPress.org API: %s', 'performance-lab' ),
$response->get_error_message()
),
),
);
}

// 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' ) );
}
foreach ( perflab_get_standalone_plugins() as $standalone_plugin ) {
$plugins[ $standalone_plugin ] = $plugins[ $plugin_slug ];
}

$plugins = array();
$plugin_queue = perflab_get_standalone_plugins();
$has_errors = true;
} elseif ( ! is_object( $response ) || ! property_exists( $response, 'plugins' ) ) {
$plugins[ $plugin_slug ] = array(
'error' => array(
'code' => 'no_plugins',
'message' => __( 'No plugins found in the API response.', 'performance-lab' ),
),
);

// Index the plugins from the API response by their slug for efficient lookup.
$all_performance_plugins = array_column( $response->plugins, null, 'slug' );
foreach ( perflab_get_standalone_plugins() as $standalone_plugin ) {
$plugins[ $standalone_plugin ] = $plugins[ $plugin_slug ];
}

// Start processing the plugins using a queue-based approach.
while ( count( $plugin_queue ) > 0 ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
$current_plugin_slug = array_shift( $plugin_queue );
$has_errors = true;
} else {
$plugin_queue = perflab_get_standalone_plugins();

if ( isset( $plugins[ $current_plugin_slug ] ) ) {
continue;
}
// Index the plugins from the API response by their slug for efficient lookup.
$all_performance_plugins = array_column( $response->plugins, null, 'slug' );

if ( ! isset( $all_performance_plugins[ $current_plugin_slug ] ) ) {
return new WP_Error(
'plugin_not_found',
__( 'Plugin not found in WordPress.org API response.', 'performance-lab' )
);
// Start processing the plugins using a queue-based approach.
while ( count( $plugin_queue ) > 0 ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
$current_plugin_slug = array_shift( $plugin_queue );

// Skip already-processed plugins.
if ( isset( $plugins[ $current_plugin_slug ] ) ) {
continue;
}

if ( ! isset( $all_performance_plugins[ $current_plugin_slug ] ) ) {
// Cache the fact that the plugin was not found.
$plugins[ $current_plugin_slug ] = array(
'error' => array(
'code' => 'plugin_not_found',
'message' => __( 'Plugin not found in API response.', 'performance-lab' ),
),
);

$has_errors = true;
} else {
$plugin_data = $all_performance_plugins[ $current_plugin_slug ];
$plugins[ $current_plugin_slug ] = wp_array_slice_assoc( $plugin_data, $fields );

// Enqueue the required plugins slug by adding it to the queue.
if ( isset( $plugin_data['requires_plugins'] ) && is_array( $plugin_data['requires_plugins'] ) ) {
$plugin_queue = array_merge( $plugin_queue, $plugin_data['requires_plugins'] );
}
}
}

$plugin_data = $all_performance_plugins[ $current_plugin_slug ];
$plugins[ $current_plugin_slug ] = wp_array_slice_assoc( $plugin_data, $fields );
if ( ! isset( $plugins[ $plugin_slug ] ) ) {
// Cache the fact that the plugin was not found.
$plugins[ $plugin_slug ] = array(
'error' => array(
'code' => 'plugin_not_found',
'message' => __( 'The requested plugin is not part of Performance Lab plugins.', 'performance-lab' ),
),
);

// Enqueue the required plugins slug by adding it to the queue.
if ( isset( $plugin_data['requires_plugins'] ) && is_array( $plugin_data['requires_plugins'] ) ) {
$plugin_queue = array_merge( $plugin_queue, $plugin_data['requires_plugins'] );
$has_errors = true;
}
}

set_transient( $transient_key, $plugins, HOUR_IN_SECONDS );
set_transient( $transient_key, $plugins, $has_errors ? MINUTE_IN_SECONDS : HOUR_IN_SECONDS );

if ( ! isset( $plugins[ $plugin_slug ] ) ) {
if ( isset( $plugins[ $plugin_slug ]['error'] ) ) {
return new WP_Error(
'plugin_not_found',
__( 'Plugin not found in API response.', 'performance-lab' )
$plugins[ $plugin_slug ]['error']['code'],
$plugins[ $plugin_slug ]['error']['message']
);
}

Expand Down

0 comments on commit 65dc803

Please sign in to comment.