* WordPress Plugin Administration API: WP_Plugin_Dependencies class
* @subpackage Administration
* Core class for installing plugin dependencies.
* It is designed to add plugin dependencies as designated in the
* `Requires Plugins` header to a new view in the plugins install page.
class WP_Plugin_Dependencies {
protected static $plugins;
* Holds plugin directory names to compare with cache.
protected static $plugin_dirnames;
* Holds sanitized plugin dependency slugs.
* Keyed on the dependent plugin's filepath,
* relative to the plugins directory.
protected static $dependencies;
* Holds an array of sanitized plugin dependency slugs.
protected static $dependency_slugs;
* Holds an array of dependent plugin slugs.
* Keyed on the dependent plugin's filepath,
* relative to the plugins directory.
protected static $dependent_slugs;
* Holds 'plugins_api()' data for plugin dependencies.
protected static $dependency_api_data;
* Holds plugin dependency filepaths, relative to the plugins directory.
* Keyed on the dependency's slug.
protected static $dependency_filepaths;
* An array of circular dependency pairings.
protected static $circular_dependencies_pairs;
* An array of circular dependency slugs.
protected static $circular_dependencies_slugs;
* Whether Plugin Dependencies have been initialized.
protected static $initialized = false;
* Initializes by fetching plugin header and plugin API data.
public static function initialize() {
if ( false === self::$initialized ) {
self::read_dependencies_from_plugin_headers();
self::get_dependency_api_data();
self::$initialized = true;
* Determines whether the plugin has plugins that depend on it.
* @param string $plugin_file The plugin's filepath, relative to the plugins directory.
* @return bool Whether the plugin has plugins that depend on it.
public static function has_dependents( $plugin_file ) {
return in_array( self::convert_to_slug( $plugin_file ), (array) self::$dependency_slugs, true );
* Determines whether the plugin has plugin dependencies.
* @param string $plugin_file The plugin's filepath, relative to the plugins directory.
* @return bool Whether a plugin has plugin dependencies.
public static function has_dependencies( $plugin_file ) {
return isset( self::$dependencies[ $plugin_file ] );
* Determines whether the plugin has active dependents.
* @param string $plugin_file The plugin's filepath, relative to the plugins directory.
* @return bool Whether the plugin has active dependents.
public static function has_active_dependents( $plugin_file ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$dependents = self::get_dependents( self::convert_to_slug( $plugin_file ) );
foreach ( $dependents as $dependent ) {
if ( is_plugin_active( $dependent ) ) {
* Gets filepaths of plugins that require the dependency.
* @param string $slug The dependency's slug.
* @return array An array of dependent plugin filepaths, relative to the plugins directory.
public static function get_dependents( $slug ) {
foreach ( (array) self::$dependencies as $dependent => $dependencies ) {
if ( in_array( $slug, $dependencies, true ) ) {
$dependents[] = $dependent;
* Gets the slugs of plugins that the dependent requires.
* @param string $plugin_file The dependent plugin's filepath, relative to the plugins directory.
* @return array An array of dependency plugin slugs.
public static function get_dependencies( $plugin_file ) {
if ( isset( self::$dependencies[ $plugin_file ] ) ) {
return self::$dependencies[ $plugin_file ];
* Gets a dependent plugin's filepath.
* @param string $slug The dependent plugin's slug.
* @return string|false The dependent plugin's filepath, relative to the plugins directory,
* or false if the plugin has no dependencies.
public static function get_dependent_filepath( $slug ) {
$filepath = array_search( $slug, self::$dependent_slugs, true );
return $filepath ? $filepath : false;
* Determines whether the plugin has unmet dependencies.
* @param string $plugin_file The plugin's filepath, relative to the plugins directory.
* @return bool Whether the plugin has unmet dependencies.
public static function has_unmet_dependencies( $plugin_file ) {
if ( ! isset( self::$dependencies[ $plugin_file ] ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
foreach ( self::$dependencies[ $plugin_file ] as $dependency ) {
$dependency_filepath = self::get_dependency_filepath( $dependency );
if ( false === $dependency_filepath || is_plugin_inactive( $dependency_filepath ) ) {
* Determines whether the plugin has a circular dependency.
* @param string $plugin_file The plugin's filepath, relative to the plugins directory.
* @return bool Whether the plugin has a circular dependency.
public static function has_circular_dependency( $plugin_file ) {
if ( ! is_array( self::$circular_dependencies_slugs ) ) {
self::get_circular_dependencies();
if ( ! empty( self::$circular_dependencies_slugs ) ) {
$slug = self::convert_to_slug( $plugin_file );
if ( in_array( $slug, self::$circular_dependencies_slugs, true ) ) {
* Gets the names of plugins that require the plugin.
* @param string $plugin_file The plugin's filepath, relative to the plugins directory.
* @return array An array of dependent names.
public static function get_dependent_names( $plugin_file ) {
$dependent_names = array();
$plugins = self::get_plugins();
$slug = self::convert_to_slug( $plugin_file );
foreach ( self::get_dependents( $slug ) as $dependent ) {
$dependent_names[ $dependent ] = $plugins[ $dependent ]['Name'];
sort( $dependent_names );
* Gets the names of plugins required by the plugin.
* @param string $plugin_file The dependent plugin's filepath, relative to the plugins directory.
* @return array An array of dependency names.
public static function get_dependency_names( $plugin_file ) {
$dependency_api_data = self::get_dependency_api_data();
$dependencies = self::get_dependencies( $plugin_file );
$plugins = self::get_plugins();
$dependency_names = array();
foreach ( $dependencies as $dependency ) {
// Use the name if it's available, otherwise fall back to the slug.
if ( isset( $dependency_api_data[ $dependency ]['name'] ) ) {
$name = $dependency_api_data[ $dependency ]['name'];
$dependency_filepath = self::get_dependency_filepath( $dependency );
if ( false !== $dependency_filepath ) {
$name = $plugins[ $dependency_filepath ]['Name'];
$dependency_names[ $dependency ] = $name;
return $dependency_names;
* Gets the filepath for a dependency, relative to the plugin's directory.
* @param string $slug The dependency's slug.
* @return string|false If installed, the dependency's filepath relative to the plugins directory, otherwise false.
public static function get_dependency_filepath( $slug ) {
$dependency_filepaths = self::get_dependency_filepaths();
if ( ! isset( $dependency_filepaths[ $slug ] ) ) {
return $dependency_filepaths[ $slug ];
* Returns API data for the dependency.
* @param string $slug The dependency's slug.
* @return array|false The dependency's API data on success, otherwise false.
public static function get_dependency_data( $slug ) {
$dependency_api_data = self::get_dependency_api_data();
if ( isset( $dependency_api_data[ $slug ] ) ) {
return $dependency_api_data[ $slug ];
* Displays an admin notice if dependencies are not installed.
public static function display_admin_notice_for_unmet_dependencies() {
if ( in_array( false, self::get_dependency_filepaths(), true ) ) {
$error_message = __( 'Some required plugins are missing or inactive.' );
if ( current_user_can( 'manage_network_plugins' ) ) {
$error_message .= ' ' . sprintf(
/* translators: %s: Link to the network plugins page. */
__( '<a href="%s">Manage plugins</a>.' ),
esc_url( network_admin_url( 'plugins.php' ) )
$error_message .= ' ' . __( 'Please contact your network administrator.' );
} elseif ( 'plugins' !== get_current_screen()->base ) {
$error_message .= ' ' . sprintf(
/* translators: %s: Link to the plugins page. */
__( '<a href="%s">Manage plugins</a>.' ),
esc_url( admin_url( 'plugins.php' ) )
* Displays an admin notice if circular dependencies are installed.
public static function display_admin_notice_for_circular_dependencies() {
$circular_dependencies = self::get_circular_dependencies();
if ( ! empty( $circular_dependencies ) && count( $circular_dependencies ) > 1 ) {
$circular_dependencies = array_unique( $circular_dependencies, SORT_REGULAR );
$plugins = self::get_plugins();
$plugin_dirnames = self::get_plugin_dirnames();
$circular_dependency_lines = '';
foreach ( $circular_dependencies as $circular_dependency ) {
$first_filepath = $plugin_dirnames[ $circular_dependency[0] ];
$second_filepath = $plugin_dirnames[ $circular_dependency[1] ];
$circular_dependency_lines .= sprintf(
/* translators: 1: First plugin name, 2: Second plugin name. */
'<li>' . _x( '%1$s requires %2$s', 'The first plugin requires the second plugin.' ) . '</li>',
'<strong>' . esc_html( $plugins[ $first_filepath ]['Name'] ) . '</strong>',
'<strong>' . esc_html( $plugins[ $second_filepath ]['Name'] ) . '</strong>'
'<p>%1$s</p><ul>%2$s</ul><p>%3$s</p>',
__( 'These plugins cannot be activated because their requirements are invalid.' ),
$circular_dependency_lines,
__( 'Please contact the plugin authors for more information.' )
'paragraph_wrap' => false,
* Checks plugin dependencies after a plugin is installed via AJAX.
public static function check_plugin_dependencies_during_ajax() {
check_ajax_referer( 'updates' );
if ( empty( $_POST['slug'] ) ) {
'errorCode' => 'no_plugin_specified',
'errorMessage' => __( 'No plugin specified.' ),
$slug = sanitize_key( wp_unslash( $_POST['slug'] ) );
$status = array( 'slug' => $slug );
self::get_plugin_dirnames();
if ( ! isset( self::$plugin_dirnames[ $slug ] ) ) {
$status['errorCode'] = 'plugin_not_installed';
$status['errorMessage'] = __( 'The plugin is not installed.' );
wp_send_json_error( $status );
$plugin_file = self::$plugin_dirnames[ $slug ];
$status['pluginName'] = self::$plugins[ $plugin_file ]['Name'];
$status['plugin'] = $plugin_file;
if ( current_user_can( 'activate_plugin', $plugin_file ) && is_plugin_inactive( $plugin_file ) ) {
$status['activateUrl'] = add_query_arg(
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_file ),
'plugin' => $plugin_file,
is_multisite() ? network_admin_url( 'plugins.php' ) : admin_url( 'plugins.php' )
if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
$status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] );
$dependencies = self::get_dependencies( $plugin_file );
if ( empty( $dependencies ) ) {
$status['message'] = __( 'The plugin has no required plugins.' );
wp_send_json_success( $status );
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$inactive_dependencies = array();