Skip to content

Commit 7754d69

Browse files
Copilotswissspidy
andcommitted
Use WP_Plugin_Dependencies class for getting plugin dependencies
Co-authored-by: swissspidy <[email protected]>
1 parent aef88f0 commit 7754d69

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

src/Plugin_Command.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,40 @@ private function collect_dependencies( $slug, &$all_to_install, &$installed_trac
11651165
}
11661166

11671167
/**
1168-
* Gets the dependencies for a plugin from WordPress.org API.
1168+
* Gets the dependencies for a plugin.
1169+
*
1170+
* Uses WP_Plugin_Dependencies class if available (WordPress 6.5+),
1171+
* otherwise falls back to WordPress.org API.
11691172
*
11701173
* @param string $slug Plugin slug.
11711174
* @return array Array of dependency slugs.
11721175
*/
11731176
private function get_plugin_dependencies( $slug ) {
1177+
// Try to use WP_Plugin_Dependencies class if available (WordPress 6.5+)
1178+
if ( class_exists( 'WP_Plugin_Dependencies' ) ) {
1179+
// Find the plugin file for this slug
1180+
$plugins = get_plugins();
1181+
foreach ( $plugins as $plugin_file => $plugin_data ) {
1182+
$plugin_slug = dirname( $plugin_file );
1183+
if ( '.' === $plugin_slug ) {
1184+
$plugin_slug = basename( $plugin_file, '.php' );
1185+
}
1186+
1187+
if ( $plugin_slug === $slug ) {
1188+
// Initialize WP_Plugin_Dependencies if needed
1189+
if ( method_exists( 'WP_Plugin_Dependencies', 'initialize' ) ) {
1190+
WP_Plugin_Dependencies::initialize();
1191+
}
1192+
1193+
// Get dependencies for this plugin file
1194+
if ( method_exists( 'WP_Plugin_Dependencies', 'get_dependencies' ) ) {
1195+
return WP_Plugin_Dependencies::get_dependencies( $plugin_file );
1196+
}
1197+
}
1198+
}
1199+
}
1200+
1201+
// Fallback to WordPress.org API for plugins not yet installed
11741202
$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) );
11751203

11761204
if ( is_wp_error( $api ) ) {
@@ -1513,13 +1541,27 @@ public function install_dependencies( $args, $assoc_args ) {
15131541
$plugin = $this->fetcher->get_check( $args[0] );
15141542
$file = $plugin->file;
15151543

1516-
// Get dependencies from plugin header
1517-
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
1544+
// Get dependencies using WP_Plugin_Dependencies if available (WordPress 6.5+)
15181545
$dependencies = [];
1519-
1520-
if ( ! empty( $plugin_data['RequiresPlugins'] ) ) {
1521-
// Parse the comma-separated list
1522-
$dependencies = array_map( 'trim', explode( ',', $plugin_data['RequiresPlugins'] ) );
1546+
1547+
if ( class_exists( 'WP_Plugin_Dependencies' ) ) {
1548+
// Initialize WP_Plugin_Dependencies
1549+
if ( method_exists( 'WP_Plugin_Dependencies', 'initialize' ) ) {
1550+
WP_Plugin_Dependencies::initialize();
1551+
}
1552+
1553+
// Get dependencies for this plugin
1554+
if ( method_exists( 'WP_Plugin_Dependencies', 'get_dependencies' ) ) {
1555+
$dependencies = WP_Plugin_Dependencies::get_dependencies( $file );
1556+
}
1557+
} else {
1558+
// Fallback: Get dependencies from plugin header manually
1559+
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
1560+
1561+
if ( ! empty( $plugin_data['RequiresPlugins'] ) ) {
1562+
// Parse the comma-separated list
1563+
$dependencies = array_map( 'trim', explode( ',', $plugin_data['RequiresPlugins'] ) );
1564+
}
15231565
}
15241566

15251567
if ( empty( $dependencies ) ) {

0 commit comments

Comments
 (0)