Skip to content
Merged
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
78 changes: 51 additions & 27 deletions plugins/performance-lab/includes/admin/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@
* @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.
*/
function perflab_query_plugin_info( string $plugin_slug ) {
$plugin = get_transient( 'perflab_plugin_info_' . $plugin_slug );

if ( is_array( $plugin ) ) {
/**
* Validated (mostly) plugin data.
*
* @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
*/
return $plugin;
}

$fields = array(
$transient_key = 'perflab_plugins_info';
$plugins = get_transient( $transient_key );
$fields = array(
'name',
'slug',
'short_description',
Expand All @@ -41,36 +32,69 @@ function perflab_query_plugin_info( string $plugin_slug ) {
'version', // Needed by install_plugin_install_status().
);

$plugin = plugins_api(
'plugin_information',
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',
array(
'slug' => $plugin_slug,
'fields' => array_fill_keys( $fields, true ),
'author' => 'wordpressdotorg',
'tag' => 'performance',
'per_page' => 100,
'fields' => array_fill_keys( $fields, true ),
)
);

if ( is_wp_error( $plugin ) ) {
return $plugin;
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()
)
);
}

if ( is_object( $plugin ) ) {
$plugin = (array) $plugin;
// 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' ) );
}

// Only store what we need.
$plugin = wp_array_slice_assoc( $plugin, $fields );
$plugins = array();
foreach ( $response->plugins as $plugin_data ) {
$plugin_info = wp_array_slice_assoc( $plugin_data, $fields );

// Ensure the 'requires_plugins' is always an array.
if ( ! isset( $plugin_info['requires_plugins'] ) || ! is_array( $plugin_info['requires_plugins'] ) ) {
$plugin_info['requires_plugins'] = array();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this code needed? Right below the if $plugin_info['requires_plugins'] is not set, then it is set to false, which will then never occur. Apparently this if statement could be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// Make sure all fields default to false in case another plugin is modifying the response from WordPress.org via the plugins_api filter.
$plugin = array_merge( array_fill_keys( $fields, false ), $plugin );
// Ensure 'requires' and 'requires_php' are either strings or false.
$plugin_info['requires'] = isset( $plugin_info['requires'] ) ? $plugin_info['requires'] : false;
$plugin_info['requires_php'] = isset( $plugin_info['requires_php'] ) ? $plugin_info['requires_php'] : false;

set_transient( 'perflab_plugin_info_' . $plugin_slug, $plugin, HOUR_IN_SECONDS );
$plugins[ $plugin_data['slug'] ] = $plugin_info;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be the equivalent of the existing code:

	$plugins = array();
	foreach ( $response->plugins as $plugin_data ) {
		$plugins[ $plugin_data['slug'] ] = wp_array_slice_assoc( $plugin_data, $fields );
	}

I'm curious why the additional requires and requires_php checks are now present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


set_transient( $transient_key, $plugins, HOUR_IN_SECONDS );

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

/**
* Validated (mostly) plugin data.
*
* @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
* @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
*/
return $plugin;
return $plugins[ $plugin_slug ];
}

/**
Expand Down