Skip to content

Commit 291e1b2

Browse files
authored
Merge pull request #1680 from b1ink0/add/automatic-plugin-dependancies-discovery
Automatically discover plugin dependencies when obtaining Performance feature plugins from WordPress.org
2 parents b0007c6 + 451a736 commit 291e1b2

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,34 @@ function perflab_query_plugin_info( string $plugin_slug ) {
7171
return new WP_Error( 'no_plugins', __( 'No plugins found in the API response.', 'performance-lab' ) );
7272
}
7373

74-
$plugins = array();
75-
$standalone_plugins = array_merge(
76-
array_flip( perflab_get_standalone_plugins() ),
77-
array( 'optimization-detective' => array() ) // TODO: Programmatically discover the plugin dependencies and add them here. See <https://github.com/WordPress/performance/issues/1616>.
78-
);
79-
foreach ( $response->plugins as $plugin_data ) {
80-
if ( ! isset( $standalone_plugins[ $plugin_data['slug'] ] ) ) {
74+
$plugins = array();
75+
$plugin_queue = perflab_get_standalone_plugins();
76+
77+
// Index the plugins from the API response by their slug for efficient lookup.
78+
$all_performance_plugins = array_column( $response->plugins, null, 'slug' );
79+
80+
// Start processing the plugins using a queue-based approach.
81+
while ( count( $plugin_queue ) > 0 ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
82+
$current_plugin_slug = array_shift( $plugin_queue );
83+
84+
if ( isset( $plugins[ $current_plugin_slug ] ) ) {
8185
continue;
8286
}
83-
$plugins[ $plugin_data['slug'] ] = wp_array_slice_assoc( $plugin_data, $fields );
87+
88+
if ( ! isset( $all_performance_plugins[ $current_plugin_slug ] ) ) {
89+
return new WP_Error(
90+
'plugin_not_found',
91+
__( 'Plugin not found in WordPress.org API response.', 'performance-lab' )
92+
);
93+
}
94+
95+
$plugin_data = $all_performance_plugins[ $current_plugin_slug ];
96+
$plugins[ $current_plugin_slug ] = wp_array_slice_assoc( $plugin_data, $fields );
97+
98+
// Enqueue the required plugins slug by adding it to the queue.
99+
if ( isset( $plugin_data['requires_plugins'] ) && is_array( $plugin_data['requires_plugins'] ) ) {
100+
$plugin_queue = array_merge( $plugin_queue, $plugin_data['requires_plugins'] );
101+
}
84102
}
85103

86104
set_transient( $transient_key, $plugins, HOUR_IN_SECONDS );

0 commit comments

Comments
 (0)