[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-plugin-dependencies.php (source)

   1  <?php
   2  /**
   3   * WordPress Plugin Administration API: WP_Plugin_Dependencies class
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   * @since 6.5.0
   8   */
   9  
  10  /**
  11   * Core class for installing plugin dependencies.
  12   *
  13   * It is designed to add plugin dependencies as designated in the
  14   * `Requires Plugins` header to a new view in the plugins install page.
  15   */
  16  class WP_Plugin_Dependencies {
  17  
  18      /**
  19       * Holds 'get_plugins()'.
  20       *
  21       * @since 6.5.0
  22       *
  23       * @var array
  24       */
  25      protected static $plugins;
  26  
  27      /**
  28       * Holds plugin directory names to compare with cache.
  29       *
  30       * @since 6.5.0
  31       *
  32       * @var array
  33       */
  34      protected static $plugin_dirnames;
  35  
  36      /**
  37       * Holds sanitized plugin dependency slugs.
  38       *
  39       * Keyed on the dependent plugin's filepath,
  40       * relative to the plugins directory.
  41       *
  42       * @since 6.5.0
  43       *
  44       * @var array
  45       */
  46      protected static $dependencies;
  47  
  48      /**
  49       * Holds an array of sanitized plugin dependency slugs.
  50       *
  51       * @since 6.5.0
  52       *
  53       * @var array
  54       */
  55      protected static $dependency_slugs;
  56  
  57      /**
  58       * Holds an array of dependent plugin slugs.
  59       *
  60       * Keyed on the dependent plugin's filepath,
  61       * relative to the plugins directory.
  62       *
  63       * @since 6.5.0
  64       *
  65       * @var array
  66       */
  67      protected static $dependent_slugs;
  68  
  69      /**
  70       * Holds 'plugins_api()' data for plugin dependencies.
  71       *
  72       * @since 6.5.0
  73       *
  74       * @var array
  75       */
  76      protected static $dependency_api_data;
  77  
  78      /**
  79       * Holds plugin dependency filepaths, relative to the plugins directory.
  80       *
  81       * Keyed on the dependency's slug.
  82       *
  83       * @since 6.5.0
  84       *
  85       * @var string[]
  86       */
  87      protected static $dependency_filepaths;
  88  
  89      /**
  90       * An array of circular dependency pairings.
  91       *
  92       * @since 6.5.0
  93       *
  94       * @var array[]
  95       */
  96      protected static $circular_dependencies_pairs;
  97  
  98      /**
  99       * An array of circular dependency slugs.
 100       *
 101       * @since 6.5.0
 102       *
 103       * @var string[]
 104       */
 105      protected static $circular_dependencies_slugs;
 106  
 107      /**
 108       * Whether Plugin Dependencies have been initialized.
 109       *
 110       * @since 6.5.0
 111       *
 112       * @var bool
 113       */
 114      protected static $initialized = false;
 115  
 116      /**
 117       * Initializes by fetching plugin header and plugin API data.
 118       *
 119       * @since 6.5.0
 120       */
 121  	public static function initialize() {
 122          if ( false === self::$initialized ) {
 123              self::read_dependencies_from_plugin_headers();
 124              self::get_dependency_api_data();
 125              self::$initialized = true;
 126          }
 127      }
 128  
 129      /**
 130       * Determines whether the plugin has plugins that depend on it.
 131       *
 132       * @since 6.5.0
 133       *
 134       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 135       * @return bool Whether the plugin has plugins that depend on it.
 136       */
 137  	public static function has_dependents( $plugin_file ) {
 138          return in_array( self::convert_to_slug( $plugin_file ), (array) self::$dependency_slugs, true );
 139      }
 140  
 141      /**
 142       * Determines whether the plugin has plugin dependencies.
 143       *
 144       * @since 6.5.0
 145       *
 146       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 147       * @return bool Whether a plugin has plugin dependencies.
 148       */
 149  	public static function has_dependencies( $plugin_file ) {
 150          return isset( self::$dependencies[ $plugin_file ] );
 151      }
 152  
 153      /**
 154       * Determines whether the plugin has active dependents.
 155       *
 156       * @since 6.5.0
 157       *
 158       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 159       * @return bool Whether the plugin has active dependents.
 160       */
 161  	public static function has_active_dependents( $plugin_file ) {
 162          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 163  
 164          $dependents = self::get_dependents( self::convert_to_slug( $plugin_file ) );
 165          foreach ( $dependents as $dependent ) {
 166              if ( is_plugin_active( $dependent ) ) {
 167                  return true;
 168              }
 169          }
 170  
 171          return false;
 172      }
 173  
 174      /**
 175       * Gets filepaths of plugins that require the dependency.
 176       *
 177       * @since 6.5.0
 178       *
 179       * @param string $slug The dependency's slug.
 180       * @return array An array of dependent plugin filepaths, relative to the plugins directory.
 181       */
 182  	public static function get_dependents( $slug ) {
 183          $dependents = array();
 184  
 185          foreach ( (array) self::$dependencies as $dependent => $dependencies ) {
 186              if ( in_array( $slug, $dependencies, true ) ) {
 187                  $dependents[] = $dependent;
 188              }
 189          }
 190  
 191          return $dependents;
 192      }
 193  
 194      /**
 195       * Gets the slugs of plugins that the dependent requires.
 196       *
 197       * @since 6.5.0
 198       *
 199       * @param string $plugin_file The dependent plugin's filepath, relative to the plugins directory.
 200       * @return array An array of dependency plugin slugs.
 201       */
 202  	public static function get_dependencies( $plugin_file ) {
 203          return self::$dependencies[ $plugin_file ] ?? array();
 204      }
 205  
 206      /**
 207       * Gets a dependent plugin's filepath.
 208       *
 209       * @since 6.5.0
 210       *
 211       * @param string $slug  The dependent plugin's slug.
 212       * @return string|false The dependent plugin's filepath, relative to the plugins directory,
 213       *                      or false if the plugin has no dependencies.
 214       */
 215  	public static function get_dependent_filepath( $slug ) {
 216          $filepath = array_search( $slug, self::$dependent_slugs, true );
 217  
 218          return $filepath ? $filepath : false;
 219      }
 220  
 221      /**
 222       * Determines whether the plugin has unmet dependencies.
 223       *
 224       * @since 6.5.0
 225       *
 226       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 227       * @return bool Whether the plugin has unmet dependencies.
 228       */
 229  	public static function has_unmet_dependencies( $plugin_file ) {
 230          if ( ! isset( self::$dependencies[ $plugin_file ] ) ) {
 231              return false;
 232          }
 233  
 234          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 235  
 236          foreach ( self::$dependencies[ $plugin_file ] as $dependency ) {
 237              $dependency_filepath = self::get_dependency_filepath( $dependency );
 238  
 239              if ( false === $dependency_filepath || is_plugin_inactive( $dependency_filepath ) ) {
 240                  return true;
 241              }
 242          }
 243  
 244          return false;
 245      }
 246  
 247      /**
 248       * Determines whether the plugin has a circular dependency.
 249       *
 250       * @since 6.5.0
 251       *
 252       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 253       * @return bool Whether the plugin has a circular dependency.
 254       */
 255  	public static function has_circular_dependency( $plugin_file ) {
 256          if ( ! is_array( self::$circular_dependencies_slugs ) ) {
 257              self::get_circular_dependencies();
 258          }
 259  
 260          if ( ! empty( self::$circular_dependencies_slugs ) ) {
 261              $slug = self::convert_to_slug( $plugin_file );
 262  
 263              if ( in_array( $slug, self::$circular_dependencies_slugs, true ) ) {
 264                  return true;
 265              }
 266          }
 267  
 268          return false;
 269      }
 270  
 271      /**
 272       * Gets the names of plugins that require the plugin.
 273       *
 274       * @since 6.5.0
 275       *
 276       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 277       * @return array An array of dependent names.
 278       */
 279  	public static function get_dependent_names( $plugin_file ) {
 280          $dependent_names = array();
 281          $plugins         = self::get_plugins();
 282          $slug            = self::convert_to_slug( $plugin_file );
 283  
 284          foreach ( self::get_dependents( $slug ) as $dependent ) {
 285              $dependent_names[ $dependent ] = $plugins[ $dependent ]['Name'];
 286          }
 287          sort( $dependent_names );
 288  
 289          return $dependent_names;
 290      }
 291  
 292      /**
 293       * Gets the names of plugins required by the plugin.
 294       *
 295       * @since 6.5.0
 296       *
 297       * @param string $plugin_file The dependent plugin's filepath, relative to the plugins directory.
 298       * @return array An array of dependency names.
 299       */
 300  	public static function get_dependency_names( $plugin_file ) {
 301          $dependency_api_data = self::get_dependency_api_data();
 302          $dependencies        = self::get_dependencies( $plugin_file );
 303          $plugins             = self::get_plugins();
 304  
 305          $dependency_names = array();
 306          foreach ( $dependencies as $dependency ) {
 307              // Use the name if it's available, otherwise fall back to the slug.
 308              if ( isset( $dependency_api_data[ $dependency ]['name'] ) ) {
 309                  $name = $dependency_api_data[ $dependency ]['name'];
 310              } else {
 311                  $dependency_filepath = self::get_dependency_filepath( $dependency );
 312                  if ( false !== $dependency_filepath ) {
 313                      $name = $plugins[ $dependency_filepath ]['Name'];
 314                  } else {
 315                      $name = $dependency;
 316                  }
 317              }
 318  
 319              $dependency_names[ $dependency ] = $name;
 320          }
 321  
 322          return $dependency_names;
 323      }
 324  
 325      /**
 326       * Gets the filepath for a dependency, relative to the plugin's directory.
 327       *
 328       * @since 6.5.0
 329       *
 330       * @param string $slug The dependency's slug.
 331       * @return string|false If installed, the dependency's filepath relative to the plugins directory, otherwise false.
 332       */
 333  	public static function get_dependency_filepath( $slug ) {
 334          $dependency_filepaths = self::get_dependency_filepaths();
 335  
 336          if ( ! isset( $dependency_filepaths[ $slug ] ) ) {
 337              return false;
 338          }
 339  
 340          return $dependency_filepaths[ $slug ];
 341      }
 342  
 343      /**
 344       * Returns API data for the dependency.
 345       *
 346       * @since 6.5.0
 347       *
 348       * @param string $slug The dependency's slug.
 349       * @return array|false The dependency's API data on success, otherwise false.
 350       */
 351  	public static function get_dependency_data( $slug ) {
 352          $dependency_api_data = self::get_dependency_api_data();
 353          return $dependency_api_data[ $slug ] ?? false;
 354      }
 355  
 356      /**
 357       * Displays an admin notice if dependencies are not installed.
 358       *
 359       * @since 6.5.0
 360       */
 361  	public static function display_admin_notice_for_unmet_dependencies() {
 362          if ( in_array( false, self::get_dependency_filepaths(), true ) ) {
 363              $error_message = __( 'Some required plugins are missing or inactive.' );
 364  
 365              if ( is_multisite() ) {
 366                  if ( current_user_can( 'manage_network_plugins' ) ) {
 367                      $error_message .= ' ' . sprintf(
 368                          /* translators: %s: Link to the network plugins page. */
 369                          __( '<a href="%s">Manage plugins</a>.' ),
 370                          esc_url( network_admin_url( 'plugins.php' ) )
 371                      );
 372                  } else {
 373                      $error_message .= ' ' . __( 'Please contact your network administrator.' );
 374                  }
 375              } elseif ( 'plugins' !== get_current_screen()->base ) {
 376                  $error_message .= ' ' . sprintf(
 377                      /* translators: %s: Link to the plugins page. */
 378                      __( '<a href="%s">Manage plugins</a>.' ),
 379                      esc_url( admin_url( 'plugins.php' ) )
 380                  );
 381              }
 382  
 383              wp_admin_notice(
 384                  $error_message,
 385                  array(
 386                      'type' => 'warning',
 387                  )
 388              );
 389          }
 390      }
 391  
 392      /**
 393       * Displays an admin notice if circular dependencies are installed.
 394       *
 395       * @since 6.5.0
 396       */
 397      public static function display_admin_notice_for_circular_dependencies() {
 398          $circular_dependencies = self::get_circular_dependencies();
 399          if ( ! empty( $circular_dependencies ) && count( $circular_dependencies ) > 1 ) {
 400              $circular_dependencies = array_unique( $circular_dependencies, SORT_REGULAR );
 401              $plugins               = self::get_plugins();
 402              $plugin_dirnames       = self::get_plugin_dirnames();
 403  
 404              // Build output lines.
 405              $circular_dependency_lines = '';
 406              foreach ( $circular_dependencies as $circular_dependency ) {
 407                  $first_filepath             = $plugin_dirnames[ $circular_dependency[0] ];
 408                  $second_filepath            = $plugin_dirnames[ $circular_dependency[1] ];
 409                  $circular_dependency_lines .= sprintf(
 410                      /* translators: 1: First plugin name, 2: Second plugin name. */
 411                      '<li>' . _x( '%1$s requires %2$s', 'The first plugin requires the second plugin.' ) . '</li>',
 412                      '<strong>' . esc_html( $plugins[ $first_filepath ]['Name'] ) . '</strong>',
 413                      '<strong>' . esc_html( $plugins[ $second_filepath ]['Name'] ) . '</strong>'
 414                  );
 415              }
 416  
 417              wp_admin_notice(
 418                  sprintf(
 419                      '<p>%1$s</p><ul>%2$s</ul><p>%3$s</p>',
 420                      __( 'These plugins cannot be activated because their requirements are invalid.' ),
 421                      $circular_dependency_lines,
 422                      __( 'Please contact the plugin authors for more information.' )
 423                  ),
 424                  array(
 425                      'type'           => 'warning',
 426                      'paragraph_wrap' => false,
 427                  )
 428              );
 429          }
 430      }
 431  
 432      /**
 433       * Checks plugin dependencies after a plugin is installed via AJAX.
 434       *
 435       * @since 6.5.0
 436       *
 437       * @return never
 438       */
 439  	public static function check_plugin_dependencies_during_ajax() {
 440          check_ajax_referer( 'updates' );
 441  
 442          if ( empty( $_POST['slug'] ) ) {
 443              wp_send_json_error(
 444                  array(
 445                      'slug'         => '',
 446                      'pluginName'   => '',
 447                      'errorCode'    => 'no_plugin_specified',
 448                      'errorMessage' => __( 'No plugin specified.' ),
 449                  )
 450              );
 451          }
 452  
 453          $slug   = sanitize_key( wp_unslash( $_POST['slug'] ) );
 454          $status = array( 'slug' => $slug );
 455  
 456          self::get_plugins();
 457          self::get_plugin_dirnames();
 458  
 459          if ( ! isset( self::$plugin_dirnames[ $slug ] ) ) {
 460              $status['errorCode']    = 'plugin_not_installed';
 461              $status['errorMessage'] = __( 'The plugin is not installed.' );
 462              wp_send_json_error( $status );
 463          }
 464  
 465          $plugin_file          = self::$plugin_dirnames[ $slug ];
 466          $status['pluginName'] = self::$plugins[ $plugin_file ]['Name'];
 467          $status['plugin']     = $plugin_file;
 468  
 469          if ( current_user_can( 'activate_plugin', $plugin_file ) && is_plugin_inactive( $plugin_file ) ) {
 470              $status['activateUrl'] = add_query_arg(
 471                  array(
 472                      '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_file ),
 473                      'action'   => 'activate',
 474                      'plugin'   => $plugin_file,
 475                  ),
 476                  is_multisite() ? network_admin_url( 'plugins.php' ) : admin_url( 'plugins.php' )
 477              );
 478          }
 479  
 480          if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
 481              $status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] );
 482          }
 483  
 484          self::initialize();
 485          $dependencies = self::get_dependencies( $plugin_file );
 486          if ( empty( $dependencies ) ) {
 487              $status['message'] = __( 'The plugin has no required plugins.' );
 488              wp_send_json_success( $status );
 489          }
 490  
 491          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 492  
 493          $inactive_dependencies = array();
 494          foreach ( $dependencies as $dependency ) {
 495              if ( false === self::$plugin_dirnames[ $dependency ] || is_plugin_inactive( self::$plugin_dirnames[ $dependency ] ) ) {
 496                  $inactive_dependencies[] = $dependency;
 497              }
 498          }
 499  
 500          if ( ! empty( $inactive_dependencies ) ) {
 501              $inactive_dependency_names = array_map(
 502                  function ( $dependency ) {
 503                      if ( isset( self::$dependency_api_data[ $dependency ]['Name'] ) ) {
 504                          $inactive_dependency_name = self::$dependency_api_data[ $dependency ]['Name'];
 505                      } else {
 506                          $inactive_dependency_name = $dependency;
 507                      }
 508                      return $inactive_dependency_name;
 509                  },
 510                  $inactive_dependencies
 511              );
 512  
 513              $status['errorCode']    = 'inactive_dependencies';
 514              $status['errorMessage'] = sprintf(
 515                  /* translators: %s: A list of inactive dependency plugin names. */
 516                  __( 'The following plugins must be activated first: %s.' ),
 517                  implode( ', ', $inactive_dependency_names )
 518              );
 519              $status['errorData'] = array_combine( $inactive_dependencies, $inactive_dependency_names );
 520  
 521              wp_send_json_error( $status );
 522          }
 523  
 524          $status['message'] = __( 'All required plugins are installed and activated.' );
 525          wp_send_json_success( $status );
 526      }
 527  
 528      /**
 529       * Gets data for installed plugins.
 530       *
 531       * @since 6.5.0
 532       *
 533       * @return array An array of plugin data.
 534       */
 535  	protected static function get_plugins() {
 536          if ( is_array( self::$plugins ) ) {
 537              return self::$plugins;
 538          }
 539  
 540          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 541          self::$plugins = get_plugins();
 542  
 543          return self::$plugins;
 544      }
 545  
 546      /**
 547       * Reads and stores dependency slugs from a plugin's 'Requires Plugins' header.
 548       *
 549       * @since 6.5.0
 550       */
 551  	protected static function read_dependencies_from_plugin_headers() {
 552          self::$dependencies     = array();
 553          self::$dependency_slugs = array();
 554          self::$dependent_slugs  = array();
 555          $plugins                = self::get_plugins();
 556          foreach ( $plugins as $plugin => $header ) {
 557              if ( '' === $header['RequiresPlugins'] ) {
 558                  continue;
 559              }
 560  
 561              $dependency_slugs              = self::sanitize_dependency_slugs( $header['RequiresPlugins'] );
 562              self::$dependencies[ $plugin ] = $dependency_slugs;
 563              self::$dependency_slugs        = array_merge( self::$dependency_slugs, $dependency_slugs );
 564  
 565              $dependent_slug                   = self::convert_to_slug( $plugin );
 566              self::$dependent_slugs[ $plugin ] = $dependent_slug;
 567          }
 568          self::$dependency_slugs = array_unique( self::$dependency_slugs );
 569      }
 570  
 571      /**
 572       * Sanitizes slugs.
 573       *
 574       * @since 6.5.0
 575       *
 576       * @param string $slugs A comma-separated string of plugin dependency slugs.
 577       * @return array An array of sanitized plugin dependency slugs.
 578       */
 579  	protected static function sanitize_dependency_slugs( $slugs ) {
 580          $sanitized_slugs = array();
 581          $slugs           = explode( ',', $slugs );
 582  
 583          foreach ( $slugs as $slug ) {
 584              $slug = trim( $slug );
 585  
 586              /**
 587               * Filters a plugin dependency's slug before matching to
 588               * the WordPress.org slug format.
 589               *
 590               * Can be used to switch between free and premium plugin slugs, for example.
 591               *
 592               * @since 6.5.0
 593               *
 594               * @param string $slug The slug.
 595               */
 596              $slug = apply_filters( 'wp_plugin_dependencies_slug', $slug );
 597  
 598              // Match to WordPress.org slug format.
 599              if ( preg_match( '/^[a-z0-9]+(-[a-z0-9]+)*$/mu', $slug ) ) {
 600                  $sanitized_slugs[] = $slug;
 601              }
 602          }
 603          $sanitized_slugs = array_unique( $sanitized_slugs );
 604          sort( $sanitized_slugs );
 605  
 606          return $sanitized_slugs;
 607      }
 608  
 609      /**
 610       * Gets the filepath of installed dependencies.
 611       * If a dependency is not installed, the filepath defaults to false.
 612       *
 613       * @since 6.5.0
 614       *
 615       * @return array An array of install dependencies filepaths, relative to the plugins directory.
 616       */
 617  	protected static function get_dependency_filepaths() {
 618          if ( is_array( self::$dependency_filepaths ) ) {
 619              return self::$dependency_filepaths;
 620          }
 621  
 622          if ( null === self::$dependency_slugs ) {
 623              return array();
 624          }
 625  
 626          self::$dependency_filepaths = array();
 627  
 628          $plugin_dirnames = self::get_plugin_dirnames();
 629          foreach ( self::$dependency_slugs as $slug ) {
 630              if ( isset( $plugin_dirnames[ $slug ] ) ) {
 631                  self::$dependency_filepaths[ $slug ] = $plugin_dirnames[ $slug ];
 632                  continue;
 633              }
 634  
 635              self::$dependency_filepaths[ $slug ] = false;
 636          }
 637  
 638          return self::$dependency_filepaths;
 639      }
 640  
 641      /**
 642       * Retrieves and stores dependency plugin data from the WordPress.org Plugin API.
 643       *
 644       * @since 6.5.0
 645       *
 646       * @global string $pagenow The filename of the current screen.
 647       *
 648       * @return array|null An array of dependency API data, or null on early exit.
 649       */
 650  	protected static function get_dependency_api_data() {
 651          global $pagenow;
 652  
 653          if ( ! is_admin() || ( 'plugins.php' !== $pagenow && 'plugin-install.php' !== $pagenow ) ) {
 654              return null;
 655          }
 656  
 657          if ( is_array( self::$dependency_api_data ) ) {
 658              return self::$dependency_api_data;
 659          }
 660  
 661          $plugins                   = self::get_plugins();
 662          self::$dependency_api_data = (array) get_site_transient( 'wp_plugin_dependencies_plugin_data' );
 663          foreach ( self::$dependency_slugs as $slug ) {
 664              // Set transient for individual data, remove from self::$dependency_api_data if transient expired.
 665              if ( ! get_site_transient( "wp_plugin_dependencies_plugin_timeout_{$slug}" ) ) {
 666                  unset( self::$dependency_api_data[ $slug ] );
 667                  set_site_transient( "wp_plugin_dependencies_plugin_timeout_{$slug}", true, 12 * HOUR_IN_SECONDS );
 668              }
 669  
 670              if ( isset( self::$dependency_api_data[ $slug ] ) ) {
 671                  if ( false === self::$dependency_api_data[ $slug ] ) {
 672                      $dependency_file = self::get_dependency_filepath( $slug );
 673  
 674                      if ( false === $dependency_file ) {
 675                          self::$dependency_api_data[ $slug ] = array( 'Name' => $slug );
 676                      } else {
 677                          self::$dependency_api_data[ $slug ] = array( 'Name' => $plugins[ $dependency_file ]['Name'] );
 678                      }
 679                      continue;
 680                  }
 681  
 682                  // Don't hit the Plugin API if data exists.
 683                  if ( ! empty( self::$dependency_api_data[ $slug ]['last_updated'] ) ) {
 684                      continue;
 685                  }
 686              }
 687  
 688              if ( ! function_exists( 'plugins_api' ) ) {
 689                  require_once  ABSPATH . 'wp-admin/includes/plugin-install.php';
 690              }
 691  
 692              $information = plugins_api(
 693                  'plugin_information',
 694                  array(
 695                      'slug'   => $slug,
 696                      'fields' => array(
 697                          'short_description' => true,
 698                          'icons'             => true,
 699                      ),
 700                  )
 701              );
 702  
 703              if ( is_wp_error( $information ) ) {
 704                  continue;
 705              }
 706  
 707              self::$dependency_api_data[ $slug ] = (array) $information;
 708              // plugins_api() returns 'name' not 'Name'.
 709              self::$dependency_api_data[ $slug ]['Name'] = self::$dependency_api_data[ $slug ]['name'];
 710              set_site_transient( 'wp_plugin_dependencies_plugin_data', self::$dependency_api_data, 0 );
 711          }
 712  
 713          // Remove from self::$dependency_api_data if slug no longer a dependency.
 714          $differences = array_diff( array_keys( self::$dependency_api_data ), self::$dependency_slugs );
 715          foreach ( $differences as $difference ) {
 716              unset( self::$dependency_api_data[ $difference ] );
 717          }
 718  
 719          ksort( self::$dependency_api_data );
 720          // Remove empty elements.
 721          self::$dependency_api_data = array_filter( self::$dependency_api_data );
 722          set_site_transient( 'wp_plugin_dependencies_plugin_data', self::$dependency_api_data, 0 );
 723  
 724          return self::$dependency_api_data;
 725      }
 726  
 727      /**
 728       * Gets plugin directory names.
 729       *
 730       * @since 6.5.0
 731       *
 732       * @return array An array of plugin directory names.
 733       */
 734  	protected static function get_plugin_dirnames() {
 735          if ( is_array( self::$plugin_dirnames ) ) {
 736              return self::$plugin_dirnames;
 737          }
 738  
 739          self::$plugin_dirnames = array();
 740  
 741          $plugin_files = array_keys( self::get_plugins() );
 742          foreach ( $plugin_files as $plugin_file ) {
 743              $slug                           = self::convert_to_slug( $plugin_file );
 744              self::$plugin_dirnames[ $slug ] = $plugin_file;
 745          }
 746  
 747          return self::$plugin_dirnames;
 748      }
 749  
 750      /**
 751       * Gets circular dependency data.
 752       *
 753       * @since 6.5.0
 754       *
 755       * @return array[] An array of circular dependency pairings.
 756       */
 757  	protected static function get_circular_dependencies() {
 758          if ( is_array( self::$circular_dependencies_pairs ) ) {
 759              return self::$circular_dependencies_pairs;
 760          }
 761  
 762          if ( null === self::$dependencies ) {
 763              return array();
 764          }
 765  
 766          self::$circular_dependencies_slugs = array();
 767  
 768          self::$circular_dependencies_pairs = array();
 769          foreach ( self::$dependencies as $dependent => $dependencies ) {
 770              /*
 771               * $dependent is in 'a/a.php' format. Dependencies are stored as slugs, i.e. 'a'.
 772               *
 773               * Convert $dependent to slug format for checking.
 774               */
 775              $dependent_slug = self::convert_to_slug( $dependent );
 776  
 777              self::$circular_dependencies_pairs = array_merge(
 778                  self::$circular_dependencies_pairs,
 779                  self::check_for_circular_dependencies( array( $dependent_slug ), $dependencies )
 780              );
 781          }
 782  
 783          return self::$circular_dependencies_pairs;
 784      }
 785  
 786      /**
 787       * Checks for circular dependencies.
 788       *
 789       * @since 6.5.0
 790       *
 791       * @param array $dependents   Array of dependent plugins.
 792       * @param array $dependencies Array of plugins dependencies.
 793       * @return array A circular dependency pairing, or an empty array if none exists.
 794       */
 795  	protected static function check_for_circular_dependencies( $dependents, $dependencies ) {
 796          $circular_dependencies_pairs = array();
 797  
 798          // Check for a self-dependency.
 799          $dependents_location_in_its_own_dependencies = array_intersect( $dependents, $dependencies );
 800          if ( ! empty( $dependents_location_in_its_own_dependencies ) ) {
 801              foreach ( $dependents_location_in_its_own_dependencies as $self_dependency ) {
 802                  self::$circular_dependencies_slugs[] = $self_dependency;
 803                  $circular_dependencies_pairs[]       = array( $self_dependency, $self_dependency );
 804  
 805                  // No need to check for itself again.
 806                  unset( $dependencies[ array_search( $self_dependency, $dependencies, true ) ] );
 807              }
 808          }
 809  
 810          /*
 811           * Check each dependency to see:
 812           * 1. If it has dependencies.
 813           * 2. If its list of dependencies includes one of its own dependents.
 814           */
 815          foreach ( $dependencies as $dependency ) {
 816              // Check if the dependency is also a dependent.
 817              $dependency_location_in_dependents = array_search( $dependency, self::$dependent_slugs, true );
 818  
 819              if ( false !== $dependency_location_in_dependents ) {
 820                  $dependencies_of_the_dependency = self::$dependencies[ $dependency_location_in_dependents ];
 821  
 822                  foreach ( $dependents as $dependent ) {
 823                      // Check if its dependencies includes one of its own dependents.
 824                      $dependent_location_in_dependency_dependencies = array_search(
 825                          $dependent,
 826                          $dependencies_of_the_dependency,
 827                          true
 828                      );
 829  
 830                      if ( false !== $dependent_location_in_dependency_dependencies ) {
 831                          self::$circular_dependencies_slugs[] = $dependent;
 832                          self::$circular_dependencies_slugs[] = $dependency;
 833                          $circular_dependencies_pairs[]       = array( $dependent, $dependency );
 834  
 835                          // Remove the dependent from its dependency's dependencies.
 836                          unset( $dependencies_of_the_dependency[ $dependent_location_in_dependency_dependencies ] );
 837                      }
 838                  }
 839  
 840                  $dependents[] = $dependency;
 841  
 842                  /*
 843                   * Now check the dependencies of the dependency's dependencies for the dependent.
 844                   *
 845                   * Yes, that does make sense.
 846                   */
 847                  $circular_dependencies_pairs = array_merge(
 848                      $circular_dependencies_pairs,
 849                      self::check_for_circular_dependencies( $dependents, array_unique( $dependencies_of_the_dependency ) )
 850                  );
 851              }
 852          }
 853  
 854          return $circular_dependencies_pairs;
 855      }
 856  
 857      /**
 858       * Converts a plugin filepath to a slug.
 859       *
 860       * @since 6.5.0
 861       *
 862       * @param string $plugin_file The plugin's filepath, relative to the plugins directory.
 863       * @return string The plugin's slug.
 864       */
 865  	protected static function convert_to_slug( $plugin_file ) {
 866          if ( 'hello.php' === $plugin_file ) {
 867              return 'hello-dolly';
 868          }
 869          return str_contains( $plugin_file, '/' ) ? dirname( $plugin_file ) : str_replace( '.php', '', $plugin_file );
 870      }
 871  }


Generated : Sat Jul 18 08:20:16 2026 Cross-referenced by PHPXref