Automattic\WooCommerce\Blueprint\Exporters
ExportInstallPluginSteps::sort_plugins_by_dep
Sort plugins by dependencies -- put the dependencies at the top.
Method of the class: ExportInstallPluginSteps{}
No Hooks.
Returns
Array.
Usage
$ExportInstallPluginSteps = new ExportInstallPluginSteps(); $ExportInstallPluginSteps->sort_plugins_by_dep( $plugins );
- $plugins(array) (required)
- List of plugins to sort (from wp_get_plugins function).
ExportInstallPluginSteps::sort_plugins_by_dep() ExportInstallPluginSteps::sort plugins by dep code WC 10.7.0
public function sort_plugins_by_dep( array $plugins ) {
$sorted = array();
$visited = array();
// Create a mapping of lowercase titles to plugin keys for quick lookups.
$title_map = array_reduce(
array_keys( $plugins ),
function ( $carry, $key ) use ( $plugins ) {
$title = strtolower( $plugins[ $key ]['Title'] ?? '' );
if ( $title ) {
$carry[ $title ] = $key;
}
return $carry;
},
array()
);
// Recursive function for topological sort.
$visit = function ( $plugin_key ) use ( &$visit, &$sorted, &$visited, $plugins, $title_map ) {
if ( isset( $visited[ $plugin_key ] ) ) {
return;
}
$visited[ $plugin_key ] = true;
$requires = $plugins[ $plugin_key ]['RequiresPlugins'] ?? array();
foreach ( (array) $requires as $dependency ) {
$dependency_key = $title_map[ strtolower( $dependency ) ] ?? null;
if ( $dependency_key ) {
$visit( $dependency_key );
}
}
$sorted[ $plugin_key ] = $plugins[ $plugin_key ];
};
// Perform sort for each plugin.
foreach ( array_keys( $plugins ) as $plugin_key ) {
$visit( $plugin_key );
}
return $sorted;
}