[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> plugin.php (source)

   1  <?php
   2  /**
   3   * WordPress Plugin Administration API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Parses the plugin contents to retrieve plugin's metadata.
  11   *
  12   * All plugin headers must be on their own line. Plugin description must not have
  13   * any newlines, otherwise only parts of the description will be displayed.
  14   * The below is formatted for printing.
  15   *
  16   *     /*
  17   *     Plugin Name: Name of the plugin.
  18   *     Plugin URI: The home page of the plugin.
  19   *     Description: Plugin description.
  20   *     Author: Plugin author's name.
  21   *     Author URI: Link to the author's website.
  22   *     Version: Plugin version.
  23   *     Text Domain: Optional. Unique identifier, should be same as the one used in
  24   *          load_plugin_textdomain().
  25   *     Domain Path: Optional. Only useful if the translations are located in a
  26   *          folder above the plugin's base path. For example, if .mo files are
  27   *          located in the locale folder then Domain Path will be "/locale/" and
  28   *          must have the first slash. Defaults to the base folder the plugin is
  29   *          located in.
  30   *     Network: Optional. Specify "Network: true" to require that a plugin is activated
  31   *          across all sites in an installation. This will prevent a plugin from being
  32   *          activated on a single site when Multisite is enabled.
  33   *     Requires at least: Optional. Specify the minimum required WordPress version.
  34   *     Requires PHP: Optional. Specify the minimum required PHP version.
  35   *     * / # Remove the space to close comment.
  36   *
  37   * The first 8 KB of the file will be pulled in and if the plugin data is not
  38   * within that first 8 KB, then the plugin author should correct their plugin
  39   * and move the plugin data headers to the top.
  40   *
  41   * The plugin file is assumed to have permissions to allow for scripts to read
  42   * the file. This is not checked however and the file is only opened for
  43   * reading.
  44   *
  45   * @since 1.5.0
  46   * @since 5.3.0 Added support for `Requires at least` and `Requires PHP` headers.
  47   * @since 5.8.0 Added support for `Update URI` header.
  48   * @since 6.5.0 Added support for `Requires Plugins` header.
  49   *
  50   * @param string $plugin_file Absolute path to the main plugin file.
  51   * @param bool   $markup      Optional. If the returned data should have HTML markup applied.
  52   *                            Default true.
  53   * @param bool   $translate   Optional. If the returned data should be translated. Default true.
  54   * @return array {
  55   *     Plugin data. Values will be empty if not supplied by the plugin.
  56   *
  57   *     @type string $Name            Name of the plugin. Should be unique.
  58   *     @type string $PluginURI       Plugin URI.
  59   *     @type string $Version         Plugin version.
  60   *     @type string $Description     Plugin description.
  61   *     @type string $Author          Plugin author's name.
  62   *     @type string $AuthorURI       Plugin author's website address (if set).
  63   *     @type string $TextDomain      Plugin textdomain.
  64   *     @type string $DomainPath      Plugin's relative directory path to .mo files.
  65   *     @type bool   $Network         Whether the plugin can only be activated network-wide.
  66   *     @type string $RequiresWP      Minimum required version of WordPress.
  67   *     @type string $RequiresPHP     Minimum required version of PHP.
  68   *     @type string $UpdateURI       ID of the plugin for update purposes, should be a URI.
  69   *     @type string $RequiresPlugins Comma separated list of dot org plugin slugs.
  70   *     @type string $Title           Title of the plugin and link to the plugin's site (if set).
  71   *     @type string $AuthorName      Plugin author's name.
  72   * }
  73   */
  74  function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
  75  
  76      $default_headers = array(
  77          'Name'            => 'Plugin Name',
  78          'PluginURI'       => 'Plugin URI',
  79          'Version'         => 'Version',
  80          'Description'     => 'Description',
  81          'Author'          => 'Author',
  82          'AuthorURI'       => 'Author URI',
  83          'TextDomain'      => 'Text Domain',
  84          'DomainPath'      => 'Domain Path',
  85          'Network'         => 'Network',
  86          'RequiresWP'      => 'Requires at least',
  87          'RequiresPHP'     => 'Requires PHP',
  88          'UpdateURI'       => 'Update URI',
  89          'RequiresPlugins' => 'Requires Plugins',
  90          // Site Wide Only is deprecated in favor of Network.
  91          '_sitewide'       => 'Site Wide Only',
  92      );
  93  
  94      $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
  95  
  96      // Site Wide Only is the old header for Network.
  97      if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
  98          /* translators: 1: Site Wide Only: true, 2: Network: true */
  99          _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s plugin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Network: true</code>' ) );
 100          $plugin_data['Network'] = $plugin_data['_sitewide'];
 101      }
 102      $plugin_data['Network'] = ( 'true' === strtolower( $plugin_data['Network'] ) );
 103      unset( $plugin_data['_sitewide'] );
 104  
 105      // If no text domain is defined fall back to the plugin slug.
 106      if ( ! $plugin_data['TextDomain'] ) {
 107          $plugin_slug = dirname( plugin_basename( $plugin_file ) );
 108          if ( '.' !== $plugin_slug && ! str_contains( $plugin_slug, '/' ) ) {
 109              $plugin_data['TextDomain'] = $plugin_slug;
 110          }
 111      }
 112  
 113      if ( $markup || $translate ) {
 114          $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
 115      } else {
 116          $plugin_data['Title']      = $plugin_data['Name'];
 117          $plugin_data['AuthorName'] = $plugin_data['Author'];
 118      }
 119  
 120      return $plugin_data;
 121  }
 122  
 123  /**
 124   * Sanitizes plugin data, optionally adds markup, optionally translates.
 125   *
 126   * @since 2.7.0
 127   *
 128   * @see get_plugin_data()
 129   *
 130   * @access private
 131   *
 132   * @param string $plugin_file Path to the main plugin file.
 133   * @param array  $plugin_data An array of plugin data. See get_plugin_data().
 134   * @param bool   $markup      Optional. If the returned data should have HTML markup applied.
 135   *                            Default true.
 136   * @param bool   $translate   Optional. If the returned data should be translated. Default true.
 137   * @return array Plugin data. Values will be empty if not supplied by the plugin.
 138   *               See get_plugin_data() for the list of possible values.
 139   */
 140  function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
 141  
 142      // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path.
 143      $plugin_file = plugin_basename( $plugin_file );
 144  
 145      // Translate fields.
 146      if ( $translate ) {
 147          $textdomain = $plugin_data['TextDomain'];
 148          if ( $textdomain ) {
 149              if ( ! is_textdomain_loaded( $textdomain ) ) {
 150                  if ( $plugin_data['DomainPath'] ) {
 151                      load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
 152                  } else {
 153                      load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
 154                  }
 155              }
 156          } elseif ( 'hello.php' === basename( $plugin_file ) ) {
 157              $textdomain = 'default';
 158          }
 159          if ( $textdomain ) {
 160              foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) {
 161                  if ( ! empty( $plugin_data[ $field ] ) ) {
 162                      // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
 163                      $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
 164                  }
 165              }
 166          }
 167      }
 168  
 169      // Sanitize fields.
 170      $allowed_tags_in_links = array(
 171          'abbr'    => array( 'title' => true ),
 172          'acronym' => array( 'title' => true ),
 173          'code'    => true,
 174          'em'      => true,
 175          'strong'  => true,
 176      );
 177  
 178      $allowed_tags      = $allowed_tags_in_links;
 179      $allowed_tags['a'] = array(
 180          'href'  => true,
 181          'title' => true,
 182      );
 183  
 184      /*
 185       * Name is marked up inside <a> tags. Don't allow these.
 186       * Author is too, but some plugins have used <a> here (omitting Author URI).
 187       */
 188      $plugin_data['Name']   = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
 189      $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
 190  
 191      $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
 192      $plugin_data['Version']     = wp_kses( $plugin_data['Version'], $allowed_tags );
 193  
 194      $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
 195      $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
 196  
 197      $plugin_data['Title']      = $plugin_data['Name'];
 198      $plugin_data['AuthorName'] = $plugin_data['Author'];
 199  
 200      // Apply markup.
 201      if ( $markup ) {
 202          if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) {
 203              $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
 204          }
 205  
 206          if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) {
 207              $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
 208          }
 209  
 210          $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
 211  
 212          if ( $plugin_data['Author'] ) {
 213              $plugin_data['Description'] .= sprintf(
 214                  /* translators: %s: Plugin author. */
 215                  ' <cite>' . __( 'By %s.' ) . '</cite>',
 216                  $plugin_data['Author']
 217              );
 218          }
 219      }
 220  
 221      return $plugin_data;
 222  }
 223  
 224  /**
 225   * Gets a list of a plugin's files.
 226   *
 227   * @since 2.8.0
 228   *
 229   * @param string $plugin Path to the plugin file relative to the plugins directory.
 230   * @return string[] Array of file names relative to the plugin root.
 231   */
 232  function get_plugin_files( $plugin ) {
 233      $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
 234      $dir         = dirname( $plugin_file );
 235  
 236      $plugin_files = array( plugin_basename( $plugin_file ) );
 237  
 238      if ( is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) {
 239  
 240          /**
 241           * Filters the array of excluded directories and files while scanning the folder.
 242           *
 243           * @since 4.9.0
 244           *
 245           * @param string[] $exclusions Array of excluded directories and files.
 246           */
 247          $exclusions = (array) apply_filters( 'plugin_files_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) );
 248  
 249          $list_files = list_files( $dir, 100, $exclusions );
 250          $list_files = array_map( 'plugin_basename', $list_files );
 251  
 252          $plugin_files = array_merge( $plugin_files, $list_files );
 253          $plugin_files = array_values( array_unique( $plugin_files ) );
 254      }
 255  
 256      return $plugin_files;
 257  }
 258  
 259  /**
 260   * Checks the plugins directory and retrieve all plugin files with plugin data.
 261   *
 262   * WordPress only supports plugin files in the base plugins directory
 263   * (wp-content/plugins) and in one directory above the plugins directory
 264   * (wp-content/plugins/my-plugin). The file it looks for has the plugin data
 265   * and must be found in those two locations. It is recommended to keep your
 266   * plugin files in their own directories.
 267   *
 268   * The file with the plugin data is the file that will be included and therefore
 269   * needs to have the main execution for the plugin. This does not mean
 270   * everything must be contained in the file and it is recommended that the file
 271   * be split for maintainability. Keep everything in one file for extreme
 272   * optimization purposes.
 273   *
 274   * @since 1.5.0
 275   *
 276   * @param string $plugin_folder Optional. Relative path to single plugin folder.
 277   * @return array[] Array of arrays of plugin data, keyed by plugin file name. See get_plugin_data().
 278   */
 279  function get_plugins( $plugin_folder = '' ) {
 280  
 281      $cache_plugins = wp_cache_get( 'plugins', 'plugins' );
 282      if ( ! $cache_plugins ) {
 283          $cache_plugins = array();
 284      }
 285  
 286      if ( isset( $cache_plugins[ $plugin_folder ] ) ) {
 287          return $cache_plugins[ $plugin_folder ];
 288      }
 289  
 290      $wp_plugins  = array();
 291      $plugin_root = WP_PLUGIN_DIR;
 292      if ( ! empty( $plugin_folder ) ) {
 293          $plugin_root .= $plugin_folder;
 294      }
 295  
 296      // Files in wp-content/plugins directory.
 297      $plugins_dir  = @opendir( $plugin_root );
 298      $plugin_files = array();
 299  
 300      if ( $plugins_dir ) {
 301          while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
 302              if ( str_starts_with( $file, '.' ) ) {
 303                  continue;
 304              }
 305  
 306              if ( is_dir( $plugin_root . '/' . $file ) ) {
 307                  $plugins_subdir = @opendir( $plugin_root . '/' . $file );
 308  
 309                  if ( $plugins_subdir ) {
 310                      while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) {
 311                          if ( str_starts_with( $subfile, '.' ) ) {
 312                              continue;
 313                          }
 314  
 315                          if ( str_ends_with( $subfile, '.php' ) ) {
 316                              $plugin_files[] = "$file/$subfile";
 317                          }
 318                      }
 319  
 320                      closedir( $plugins_subdir );
 321                  }
 322              } elseif ( str_ends_with( $file, '.php' ) ) {
 323                  $plugin_files[] = $file;
 324              }
 325          }
 326  
 327          closedir( $plugins_dir );
 328      }
 329  
 330      if ( empty( $plugin_files ) ) {
 331          return $wp_plugins;
 332      }
 333  
 334      foreach ( $plugin_files as $plugin_file ) {
 335          if ( ! is_readable( "$plugin_root/$plugin_file" ) ) {
 336              continue;
 337          }
 338  
 339          // Do not apply markup/translate as it will be cached.
 340          $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false );
 341  
 342          if ( empty( $plugin_data['Name'] ) ) {
 343              continue;
 344          }
 345  
 346          $wp_plugins[ plugin_basename( $plugin_file ) ] = $plugin_data;
 347      }
 348  
 349      uasort( $wp_plugins, '_sort_uname_callback' );
 350  
 351      $cache_plugins[ $plugin_folder ] = $wp_plugins;
 352      wp_cache_set( 'plugins', $cache_plugins, 'plugins' );
 353  
 354      return $wp_plugins;
 355  }
 356  
 357  /**
 358   * Checks the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
 359   *
 360   * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
 361   *
 362   * @since 3.0.0
 363   * @return array[] Array of arrays of mu-plugin data, keyed by plugin file name. See get_plugin_data().
 364   */
 365  function get_mu_plugins() {
 366      $wp_plugins   = array();
 367      $plugin_files = array();
 368  
 369      if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
 370          return $wp_plugins;
 371      }
 372  
 373      // Files in wp-content/mu-plugins directory.
 374      $plugins_dir = @opendir( WPMU_PLUGIN_DIR );
 375      if ( $plugins_dir ) {
 376          while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
 377              if ( str_ends_with( $file, '.php' ) ) {
 378                  $plugin_files[] = $file;
 379              }
 380          }
 381      } else {
 382          return $wp_plugins;
 383      }
 384  
 385      closedir( $plugins_dir );
 386  
 387      if ( empty( $plugin_files ) ) {
 388          return $wp_plugins;
 389      }
 390  
 391      foreach ( $plugin_files as $plugin_file ) {
 392          if ( ! is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) ) {
 393              continue;
 394          }
 395  
 396          // Do not apply markup/translate as it will be cached.
 397          $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false );
 398  
 399          if ( empty( $plugin_data['Name'] ) ) {
 400              $plugin_data['Name'] = $plugin_file;
 401          }
 402  
 403          $wp_plugins[ $plugin_file ] = $plugin_data;
 404      }
 405  
 406      if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php' ) <= 30 ) {
 407          // Silence is golden.
 408          unset( $wp_plugins['index.php'] );
 409      }
 410  
 411      uasort( $wp_plugins, '_sort_uname_callback' );
 412  
 413      return $wp_plugins;
 414  }
 415  
 416  /**
 417   * Declares a callback to sort array by a 'Name' key.
 418   *
 419   * @since 3.1.0
 420   *
 421   * @access private
 422   *
 423   * @param array $a array with 'Name' key.
 424   * @param array $b array with 'Name' key.
 425   * @return int Return 0 or 1 based on two string comparison.
 426   */
 427  function _sort_uname_callback( $a, $b ) {
 428      return strnatcasecmp( $a['Name'], $b['Name'] );
 429  }
 430  
 431  /**
 432   * Checks the wp-content directory and retrieve all drop-ins with any plugin data.
 433   *
 434   * @since 3.0.0
 435   * @return array[] Array of arrays of dropin plugin data, keyed by plugin file name. See get_plugin_data().
 436   */
 437  function get_dropins() {
 438      $dropins      = array();
 439      $plugin_files = array();
 440  
 441      $_dropins = _get_dropins();
 442  
 443      // Files in wp-content directory.
 444      $plugins_dir = @opendir( WP_CONTENT_DIR );
 445      if ( $plugins_dir ) {
 446          while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
 447              if ( isset( $_dropins[ $file ] ) ) {
 448                  $plugin_files[] = $file;
 449              }
 450          }
 451      } else {
 452          return $dropins;
 453      }
 454  
 455      closedir( $plugins_dir );
 456  
 457      if ( empty( $plugin_files ) ) {
 458          return $dropins;
 459      }
 460  
 461      foreach ( $plugin_files as $plugin_file ) {
 462          if ( ! is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) {
 463              continue;
 464          }
 465  
 466          // Do not apply markup/translate as it will be cached.
 467          $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false );
 468  
 469          if ( empty( $plugin_data['Name'] ) ) {
 470              $plugin_data['Name'] = $plugin_file;
 471          }
 472  
 473          $dropins[ $plugin_file ] = $plugin_data;
 474      }
 475  
 476      uksort( $dropins, 'strnatcasecmp' );
 477  
 478      return $dropins;
 479  }
 480  
 481  /**
 482   * Returns drop-in plugins that WordPress uses.
 483   *
 484   * Includes Multisite drop-ins only when is_multisite()
 485   *
 486   * @since 3.0.0
 487   *
 488   * @return array[] {
 489   *     Key is file name. The value is an array of data about the drop-in.
 490   *
 491   *     @type array ...$0 {
 492   *         Data about the drop-in.
 493   *
 494   *         @type string      $0 The purpose of the drop-in.
 495   *         @type string|true $1 Name of the constant that must be true for the drop-in
 496   *                              to be used, or true if no constant is required.
 497   *     }
 498   * }
 499   */
 500  function _get_dropins() {
 501      $dropins = array(
 502          'advanced-cache.php'      => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ),  // WP_CACHE
 503          'db.php'                  => array( __( 'Custom database class.' ), true ),          // Auto on load.
 504          'db-error.php'            => array( __( 'Custom database error message.' ), true ),  // Auto on error.
 505          'install.php'             => array( __( 'Custom installation script.' ), true ),     // Auto on installation.
 506          'maintenance.php'         => array( __( 'Custom maintenance message.' ), true ),     // Auto on maintenance.
 507          'object-cache.php'        => array( __( 'External object cache.' ), true ),          // Auto on load.
 508          'php-error.php'           => array( __( 'Custom PHP error message.' ), true ),       // Auto on error.
 509          'fatal-error-handler.php' => array( __( 'Custom PHP fatal error handler.' ), true ), // Auto on error.
 510      );
 511  
 512      if ( is_multisite() ) {
 513          $dropins['sunrise.php']        = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
 514          $dropins['blog-deleted.php']   = array( __( 'Custom site deleted message.' ), true );   // Auto on deleted blog.
 515          $dropins['blog-inactive.php']  = array( __( 'Custom site inactive message.' ), true );  // Auto on inactive blog.
 516          $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // Auto on archived or spammed blog.
 517      }
 518  
 519      return $dropins;
 520  }
 521  
 522  /**
 523   * Determines whether a plugin is active.
 524   *
 525   * Only plugins installed in the plugins/ folder can be active.
 526   *
 527   * Plugins in the mu-plugins/ folder can't be "activated," so this function will
 528   * return false for those plugins.
 529   *
 530   * For more information on this and similar theme functions, check out
 531   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 532   * Conditional Tags} article in the Theme Developer Handbook.
 533   *
 534   * @since 2.5.0
 535   *
 536   * @param string $plugin Path to the plugin file relative to the plugins directory.
 537   * @return bool True, if in the active plugins list. False, not in the list.
 538   */
 539  function is_plugin_active( $plugin ) {
 540      return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || is_plugin_active_for_network( $plugin );
 541  }
 542  
 543  /**
 544   * Determines whether the plugin is inactive.
 545   *
 546   * Reverse of is_plugin_active(). Used as a callback.
 547   *
 548   * For more information on this and similar theme functions, check out
 549   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 550   * Conditional Tags} article in the Theme Developer Handbook.
 551   *
 552   * @since 3.1.0
 553   *
 554   * @see is_plugin_active()
 555   *
 556   * @param string $plugin Path to the plugin file relative to the plugins directory.
 557   * @return bool True if inactive. False if active.
 558   */
 559  function is_plugin_inactive( $plugin ) {
 560      return ! is_plugin_active( $plugin );
 561  }
 562  
 563  /**
 564   * Determines whether the plugin is active for the entire network.
 565   *
 566   * Only plugins installed in the plugins/ folder can be active.
 567   *
 568   * Plugins in the mu-plugins/ folder can't be "activated," so this function will
 569   * return false for those plugins.
 570   *
 571   * For more information on this and similar theme functions, check out
 572   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 573   * Conditional Tags} article in the Theme Developer Handbook.
 574   *
 575   * @since 3.0.0
 576   *
 577   * @param string $plugin Path to the plugin file relative to the plugins directory.
 578   * @return bool True if active for the network, otherwise false.
 579   */
 580  function is_plugin_active_for_network( $plugin ) {
 581      if ( ! is_multisite() ) {
 582          return false;
 583      }
 584  
 585      $plugins = get_site_option( 'active_sitewide_plugins' );
 586      if ( isset( $plugins[ $plugin ] ) ) {
 587          return true;
 588      }
 589  
 590      return false;
 591  }
 592  
 593  /**
 594   * Checks for "Network: true" in the plugin header to see if this should
 595   * be activated only as a network wide plugin. The plugin would also work
 596   * when Multisite is not enabled.
 597   *
 598   * Checks for "Site Wide Only: true" for backward compatibility.
 599   *
 600   * @since 3.0.0
 601   * @since 7.1.1 The `$plugin` path is normalized with `plugin_basename()` and `trim()`,
 602   *              matching how `activate_plugin()` resolves it.
 603   *
 604   * @param string $plugin Path to the plugin file. Accepts a path relative to the plugins
 605   *                       directory, or an absolute path, with or without surrounding whitespace.
 606   * @return bool True if plugin is network only, false otherwise.
 607   */
 608  function is_network_only_plugin( $plugin ) {
 609      // Normalize the path the same way activate_plugin() does, so both agree on the file.
 610      $plugin = plugin_basename( trim( $plugin ) );
 611  
 612      $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
 613      if ( $plugin_data ) {
 614          return $plugin_data['Network'];
 615      }
 616      return false;
 617  }
 618  
 619  /**
 620   * Attempts activation of plugin in a "sandbox" and redirects on success.
 621   *
 622   * A plugin that is already activated will not attempt to be activated again.
 623   *
 624   * The way it works is by setting the redirection to the error before trying to
 625   * include the plugin file. If the plugin fails, then the redirection will not
 626   * be overwritten with the success message. Also, the options will not be
 627   * updated and the activation hook will not be called on plugin error.
 628   *
 629   * It should be noted that in no way the below code will actually prevent errors
 630   * within the file. The code should not be used elsewhere to replicate the
 631   * "sandbox", which uses redirection to work.
 632   * {@source 13 1}
 633   *
 634   * If any errors are found or text is outputted, then it will be captured to
 635   * ensure that the success redirection will update the error redirection.
 636   *
 637   * @since 2.5.0
 638   * @since 5.2.0 Test for WordPress version and PHP version compatibility.
 639   *
 640   * @param string $plugin       Path to the plugin file relative to the plugins directory.
 641   * @param string $redirect     Optional. URL to redirect to.
 642   * @param bool   $network_wide Optional. Whether to enable the plugin for all sites in the network
 643   *                             or just the current site. Multisite only. Default false.
 644   * @param bool   $silent       Optional. Whether to prevent calling activation hooks. Default false.
 645   * @return null|WP_Error Null on success, WP_Error on invalid file.
 646   */
 647  function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
 648      $plugin = plugin_basename( trim( $plugin ) );
 649  
 650      if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) {
 651          $network_wide        = true;
 652          $current             = get_site_option( 'active_sitewide_plugins', array() );
 653          $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
 654      } else {
 655          $current = get_option( 'active_plugins', array() );
 656      }
 657  
 658      $valid = validate_plugin( $plugin );
 659      if ( is_wp_error( $valid ) ) {
 660          return $valid;
 661      }
 662  
 663      $requirements = validate_plugin_requirements( $plugin );
 664      if ( is_wp_error( $requirements ) ) {
 665          return $requirements;
 666      }
 667  
 668      if ( $network_wide && ! isset( $current[ $plugin ] )
 669          || ! $network_wide && ! in_array( $plugin, $current, true )
 670      ) {
 671          if ( ! empty( $redirect ) ) {
 672              // We'll override this later if the plugin can be included without fatal error.
 673              wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) );
 674          }
 675  
 676          ob_start();
 677  
 678          // Load the plugin to test whether it throws any errors.
 679          plugin_sandbox_scrape( $plugin );
 680  
 681          if ( ! $silent ) {
 682              /**
 683               * Fires before a plugin is activated.
 684               *
 685               * If a plugin is silently activated (such as during an update),
 686               * this hook does not fire.
 687               *
 688               * @since 2.9.0
 689               *
 690               * @param string $plugin       Path to the plugin file relative to the plugins directory.
 691               * @param bool   $network_wide Whether to enable the plugin for all sites in the network
 692               *                             or just the current site. Multisite only. Default false.
 693               */
 694              do_action( 'activate_plugin', $plugin, $network_wide );
 695  
 696              /**
 697               * Fires as a specific plugin is being activated.
 698               *
 699               * This hook is the "activation" hook used internally by register_activation_hook().
 700               * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
 701               *
 702               * If a plugin is silently activated (such as during an update), this hook does not fire.
 703               *
 704               * @since 2.0.0
 705               *
 706               * @param bool $network_wide Whether to enable the plugin for all sites in the network
 707               *                           or just the current site. Multisite only. Default false.
 708               */
 709              do_action( "activate_{$plugin}", $network_wide );
 710          }
 711  
 712          if ( $network_wide ) {
 713              $current            = get_site_option( 'active_sitewide_plugins', array() );
 714              $current[ $plugin ] = time();
 715              update_site_option( 'active_sitewide_plugins', $current );
 716          } else {
 717              $current   = get_option( 'active_plugins', array() );
 718              $current[] = $plugin;
 719              sort( $current );
 720              update_option( 'active_plugins', $current );
 721          }
 722  
 723          if ( ! $silent ) {
 724              /**
 725               * Fires after a plugin has been activated.
 726               *
 727               * If a plugin is silently activated (such as during an update),
 728               * this hook does not fire.
 729               *
 730               * @since 2.9.0
 731               *
 732               * @param string $plugin       Path to the plugin file relative to the plugins directory.
 733               * @param bool   $network_wide Whether to enable the plugin for all sites in the network
 734               *                             or just the current site. Multisite only. Default false.
 735               */
 736              do_action( 'activated_plugin', $plugin, $network_wide );
 737          }
 738  
 739          if ( ob_get_length() > 0 ) {
 740              $output = ob_get_clean();
 741              return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
 742          }
 743  
 744          ob_end_clean();
 745      }
 746  
 747      return null;
 748  }
 749  
 750  /**
 751   * Deactivates a single plugin or multiple plugins.
 752   *
 753   * The deactivation hook is disabled by the plugin upgrader by using the $silent
 754   * parameter.
 755   *
 756   * @since 2.5.0
 757   *
 758   * @param string|string[] $plugins      Single plugin or list of plugins to deactivate.
 759   * @param bool            $silent       Prevent calling deactivation hooks. Default false.
 760   * @param bool|null       $network_wide Whether to deactivate the plugin for all sites in the network.
 761   *                                      A value of null will deactivate plugins for both the network
 762   *                                      and the current site. Multisite only. Default null.
 763   */
 764  function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
 765      if ( is_multisite() ) {
 766          $network_current = get_site_option( 'active_sitewide_plugins', array() );
 767      }
 768      $current    = get_option( 'active_plugins', array() );
 769      $do_blog    = false;
 770      $do_network = false;
 771  
 772      foreach ( (array) $plugins as $plugin ) {
 773          $plugin = plugin_basename( trim( $plugin ) );
 774          if ( ! is_plugin_active( $plugin ) ) {
 775              continue;
 776          }
 777  
 778          $network_deactivating = ( false !== $network_wide ) && is_plugin_active_for_network( $plugin );
 779  
 780          if ( ! $silent ) {
 781              /**
 782               * Fires before a plugin is deactivated.
 783               *
 784               * If a plugin is silently deactivated (such as during an update),
 785               * this hook does not fire.
 786               *
 787               * @since 2.9.0
 788               *
 789               * @param string $plugin               Path to the plugin file relative to the plugins directory.
 790               * @param bool   $network_deactivating Whether the plugin is deactivated for all sites in the network
 791               *                                     or just the current site. Multisite only. Default false.
 792               */
 793              do_action( 'deactivate_plugin', $plugin, $network_deactivating );
 794          }
 795  
 796          if ( false !== $network_wide ) {
 797              if ( is_plugin_active_for_network( $plugin ) ) {
 798                  $do_network = true;
 799                  unset( $network_current[ $plugin ] );
 800              } elseif ( $network_wide ) {
 801                  continue;
 802              }
 803          }
 804  
 805          if ( true !== $network_wide ) {
 806              $key = array_search( $plugin, $current, true );
 807              if ( false !== $key ) {
 808                  $do_blog = true;
 809                  unset( $current[ $key ] );
 810              }
 811          }
 812  
 813          if ( $do_blog && wp_is_recovery_mode() ) {
 814              list( $extension ) = explode( '/', $plugin );
 815              wp_paused_plugins()->delete( $extension );
 816          }
 817  
 818          if ( ! $silent ) {
 819              /**
 820               * Fires as a specific plugin is being deactivated.
 821               *
 822               * This hook is the "deactivation" hook used internally by register_deactivation_hook().
 823               * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
 824               *
 825               * If a plugin is silently deactivated (such as during an update), this hook does not fire.
 826               *
 827               * @since 2.0.0
 828               *
 829               * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
 830               *                                   or just the current site. Multisite only. Default false.
 831               */
 832              do_action( "deactivate_{$plugin}", $network_deactivating );
 833  
 834              /**
 835               * Fires after a plugin is deactivated.
 836               *
 837               * If a plugin is silently deactivated (such as during an update),
 838               * this hook does not fire.
 839               *
 840               * @since 2.9.0
 841               *
 842               * @param string $plugin               Path to the plugin file relative to the plugins directory.
 843               * @param bool   $network_deactivating Whether the plugin is deactivated for all sites in the network
 844               *                                     or just the current site. Multisite only. Default false.
 845               */
 846              do_action( 'deactivated_plugin', $plugin, $network_deactivating );
 847          }
 848      }
 849  
 850      if ( $do_blog ) {
 851          update_option( 'active_plugins', $current );
 852      }
 853      if ( $do_network ) {
 854          update_site_option( 'active_sitewide_plugins', $network_current );
 855      }
 856  }
 857  
 858  /**
 859   * Activates multiple plugins.
 860   *
 861   * When WP_Error is returned, it does not mean that one of the plugins had
 862   * errors. It means that one or more of the plugin file paths were invalid.
 863   *
 864   * The execution will be halted as soon as one of the plugins has an error.
 865   *
 866   * @since 2.6.0
 867   *
 868   * @param string|string[] $plugins      Single plugin or list of plugins to activate.
 869   * @param string          $redirect     Redirect to page after successful activation.
 870   * @param bool            $network_wide Whether to enable the plugin for all sites in the network.
 871   *                                      Default false.
 872   * @param bool            $silent       Prevent calling activation hooks. Default false.
 873   * @return true|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
 874   */
 875  function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
 876      if ( ! is_array( $plugins ) ) {
 877          $plugins = array( $plugins );
 878      }
 879  
 880      $errors = array();
 881      foreach ( $plugins as $plugin ) {
 882          if ( ! empty( $redirect ) ) {
 883              $redirect = add_query_arg( 'plugin', $plugin, $redirect );
 884          }
 885          $result = activate_plugin( $plugin, $redirect, $network_wide, $silent );
 886          if ( is_wp_error( $result ) ) {
 887              $errors[ $plugin ] = $result;
 888          }
 889      }
 890  
 891      if ( ! empty( $errors ) ) {
 892          return new WP_Error( 'plugins_invalid', __( 'One of the plugins is invalid.' ), $errors );
 893      }
 894  
 895      return true;
 896  }
 897  
 898  /**
 899   * Removes directory and files of a plugin for a list of plugins.
 900   *
 901   * @since 2.6.0
 902   *
 903   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 904   *
 905   * @param string[] $plugins    List of plugin paths to delete, relative to the plugins directory.
 906   * @param string   $deprecated Not used.
 907   * @return bool|null|WP_Error True on success, false if `$plugins` is empty, `WP_Error` on failure.
 908   *                            `null` if filesystem credentials are required to proceed.
 909   *
 910   * @phpstan-param '' $deprecated
 911   * @phpstan-return ( $plugins is empty ? false : true|null|WP_Error )
 912   */
 913  function delete_plugins( $plugins, $deprecated = '' ) {
 914      global $wp_filesystem;
 915  
 916      if ( empty( $plugins ) ) {
 917          return false;
 918      }
 919  
 920      $checked = array();
 921      foreach ( $plugins as $plugin ) {
 922          $checked[] = 'checked[]=' . $plugin;
 923      }
 924  
 925      $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&' . implode( '&', $checked ), 'bulk-plugins' );
 926  
 927      ob_start();
 928      $credentials = request_filesystem_credentials( $url );
 929      $data        = ob_get_clean();
 930  
 931      if ( false === $credentials ) {
 932          if ( ! empty( $data ) ) {
 933              require_once  ABSPATH . 'wp-admin/admin-header.php';
 934              echo $data;
 935              require_once  ABSPATH . 'wp-admin/admin-footer.php';
 936              exit;
 937          }
 938          return null;
 939      }
 940  
 941      if ( ! WP_Filesystem( $credentials ) ) {
 942          ob_start();
 943          // Failed to connect. Error and request again.
 944          request_filesystem_credentials( $url, '', true );
 945          $data = ob_get_clean();
 946  
 947          if ( ! empty( $data ) ) {
 948              require_once  ABSPATH . 'wp-admin/admin-header.php';
 949              echo $data;
 950              require_once  ABSPATH . 'wp-admin/admin-footer.php';
 951              exit;
 952          }
 953          return null;
 954      }
 955  
 956      if ( ! is_object( $wp_filesystem ) ) {
 957          return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
 958      }
 959  
 960      if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
 961          return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
 962      }
 963  
 964      // Get the base plugin folder.
 965      $plugins_dir = $wp_filesystem->wp_plugins_dir();
 966      if ( empty( $plugins_dir ) ) {
 967          return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) );
 968      }
 969  
 970      $plugins_dir = trailingslashit( $plugins_dir );
 971  
 972      $plugin_translations = wp_get_installed_translations( 'plugins' );
 973  
 974      $errors = array();
 975  
 976      foreach ( $plugins as $plugin_file ) {
 977          // Run Uninstall hook.
 978          if ( is_uninstallable_plugin( $plugin_file ) ) {
 979              uninstall_plugin( $plugin_file );
 980          }
 981  
 982          /**
 983           * Fires immediately before a plugin deletion attempt.
 984           *
 985           * @since 4.4.0
 986           *
 987           * @param string $plugin_file Path to the plugin file relative to the plugins directory.
 988           */
 989          do_action( 'delete_plugin', $plugin_file );
 990  
 991          $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
 992  
 993          /*
 994           * If plugin is in its own directory, recursively delete the directory.
 995           * Base check on if plugin includes directory separator AND that it's not the root plugin folder.
 996           */
 997          if ( strpos( $plugin_file, '/' ) && $this_plugin_dir !== $plugins_dir ) {
 998              $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
 999          } else {
1000              $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
1001          }
1002  
1003          /**
1004           * Fires immediately after a plugin deletion attempt.
1005           *
1006           * @since 4.4.0
1007           *
1008           * @param string $plugin_file Path to the plugin file relative to the plugins directory.
1009           * @param bool   $deleted     Whether the plugin deletion was successful.
1010           */
1011          do_action( 'deleted_plugin', $plugin_file, $deleted );
1012  
1013          if ( ! $deleted ) {
1014              $errors[] = $plugin_file;
1015              continue;
1016          }
1017  
1018          $plugin_slug = dirname( $plugin_file );
1019  
1020          if ( 'hello.php' === $plugin_file ) {
1021              $plugin_slug = 'hello-dolly';
1022          }
1023  
1024          // Remove language files, silently.
1025          if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
1026              $translations = $plugin_translations[ $plugin_slug ];
1027  
1028              foreach ( $translations as $translation => $data ) {
1029                  $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
1030                  $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
1031                  $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.l10n.php' );
1032  
1033                  $json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
1034                  if ( $json_translation_files ) {
1035                      array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
1036                  }
1037              }
1038          }
1039      }
1040  
1041      // Remove deleted plugins from the plugin updates list.
1042      $current = get_site_transient( 'update_plugins' );
1043      if ( $current ) {
1044          // Don't remove the plugins that weren't deleted.
1045          $deleted = array_diff( $plugins, $errors );
1046  
1047          foreach ( $deleted as $plugin_file ) {
1048              unset( $current->response[ $plugin_file ] );
1049          }
1050  
1051          set_site_transient( 'update_plugins', $current );
1052      }
1053  
1054      if ( ! empty( $errors ) ) {
1055          if ( 1 === count( $errors ) ) {
1056              /* translators: %s: Plugin filename. */
1057              $message = __( 'Could not fully remove the plugin %s.' );
1058          } else {
1059              /* translators: %s: Comma-separated list of plugin filenames. */
1060              $message = __( 'Could not fully remove the plugins %s.' );
1061          }
1062  
1063          return new WP_Error( 'could_not_remove_plugin', sprintf( $message, implode( ', ', $errors ) ) );
1064      }
1065  
1066      return true;
1067  }
1068  
1069  /**
1070   * Validates active plugins.
1071   *
1072   * Validate all active plugins, deactivates invalid and
1073   * returns an array of deactivated ones.
1074   *
1075   * @since 2.5.0
1076   * @return WP_Error[] Array of plugin errors keyed by plugin file name.
1077   */
1078  function validate_active_plugins() {
1079      $plugins = get_option( 'active_plugins', array() );
1080      // Validate vartype: array.
1081      if ( ! is_array( $plugins ) ) {
1082          update_option( 'active_plugins', array() );
1083          $plugins = array();
1084      }
1085  
1086      if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
1087          $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
1088          $plugins         = array_merge( $plugins, array_keys( $network_plugins ) );
1089      }
1090  
1091      if ( empty( $plugins ) ) {
1092          return array();
1093      }
1094  
1095      $invalid = array();
1096  
1097      // Invalid plugins get deactivated.
1098      foreach ( $plugins as $plugin ) {
1099          $result = validate_plugin( $plugin );
1100          if ( is_wp_error( $result ) ) {
1101              $invalid[ $plugin ] = $result;
1102              deactivate_plugins( $plugin, true );
1103          }
1104      }
1105      return $invalid;
1106  }
1107  
1108  /**
1109   * Validates the plugin path.
1110   *
1111   * Checks that the main plugin file exists and is a valid plugin. See validate_file().
1112   *
1113   * @since 2.5.0
1114   *
1115   * @param string $plugin Path to the plugin file relative to the plugins directory.
1116   * @return int|WP_Error 0 on success, WP_Error on failure.
1117   *
1118   * @phpstan-return ( $plugin is empty ? WP_Error : 0|WP_Error )
1119   */
1120  function validate_plugin( $plugin ) {
1121      if ( validate_file( $plugin ) ) {
1122          return new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) );
1123      }
1124      if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
1125          return new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) );
1126      }
1127  
1128      $installed_plugins = get_plugins();
1129      if ( ! isset( $installed_plugins[ $plugin ] ) ) {
1130          return new WP_Error( 'no_plugin_header', __( 'The plugin does not have a valid header.' ) );
1131      }
1132      return 0;
1133  }
1134  
1135  /**
1136   * Validates the plugin requirements for WordPress version and PHP version.
1137   *
1138   * Uses the information from `Requires at least`, `Requires PHP` and `Requires Plugins` headers
1139   * defined in the plugin's main PHP file.
1140   *
1141   * @since 5.2.0
1142   * @since 5.3.0 Added support for reading the headers from the plugin's
1143   *              main PHP file, with `readme.txt` as a fallback.
1144   * @since 5.8.0 Removed support for using `readme.txt` as a fallback.
1145   * @since 6.5.0 Added support for the 'Requires Plugins' header.
1146   *
1147   * @param string $plugin Path to the plugin file relative to the plugins directory.
1148   * @return true|WP_Error True if requirements are met, WP_Error on failure.
1149   */
1150  function validate_plugin_requirements( $plugin ) {
1151      $plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
1152  
1153      $requirements = array(
1154          'requires'         => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '',
1155          'requires_php'     => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '',
1156          'requires_plugins' => ! empty( $plugin_headers['RequiresPlugins'] ) ? $plugin_headers['RequiresPlugins'] : '',
1157      );
1158  
1159      $compatible_wp  = is_wp_version_compatible( $requirements['requires'] );
1160      $compatible_php = is_php_version_compatible( $requirements['requires_php'] );
1161  
1162      $php_update_message = '</p><p>' . sprintf(
1163          /* translators: %s: URL to Update PHP page. */
1164          __( '<a href="%s">Learn more about updating PHP</a>.' ),
1165          esc_url( wp_get_update_php_url() )
1166      );
1167  
1168      $annotation = wp_get_update_php_annotation();
1169  
1170      if ( $annotation ) {
1171          $php_update_message .= '</p><p><em>' . $annotation . '</em>';
1172      }
1173  
1174      if ( ! $compatible_wp && ! $compatible_php ) {
1175          return new WP_Error(
1176              'plugin_wp_php_incompatible',
1177              '<p>' . sprintf(
1178                  /* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */
1179                  _x( '<strong>Error:</strong> Current versions of WordPress (%1$s) and PHP (%2$s) do not meet minimum requirements for %3$s. The plugin requires WordPress %4$s and PHP %5$s.', 'plugin' ),
1180                  get_bloginfo( 'version' ),
1181                  PHP_VERSION,
1182                  $plugin_headers['Name'],
1183                  $requirements['requires'],
1184                  $requirements['requires_php']
1185              ) . $php_update_message . '</p>'
1186          );
1187      } elseif ( ! $compatible_php ) {
1188          return new WP_Error(
1189              'plugin_php_incompatible',
1190              '<p>' . sprintf(
1191                  /* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */
1192                  _x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'plugin' ),
1193                  PHP_VERSION,
1194                  $plugin_headers['Name'],
1195                  $requirements['requires_php']
1196              ) . $php_update_message . '</p>'
1197          );
1198      } elseif ( ! $compatible_wp ) {
1199          return new WP_Error(
1200              'plugin_wp_incompatible',
1201              '<p>' . sprintf(
1202                  /* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */
1203                  _x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'plugin' ),
1204                  get_bloginfo( 'version' ),
1205                  $plugin_headers['Name'],
1206                  $requirements['requires']
1207              ) . '</p>'
1208          );
1209      }
1210  
1211      WP_Plugin_Dependencies::initialize();
1212  
1213      if ( WP_Plugin_Dependencies::has_unmet_dependencies( $plugin ) ) {
1214          $dependency_names       = WP_Plugin_Dependencies::get_dependency_names( $plugin );
1215          $unmet_dependencies     = array();
1216          $unmet_dependency_names = array();
1217  
1218          foreach ( $dependency_names as $dependency => $dependency_name ) {
1219              $dependency_file = WP_Plugin_Dependencies::get_dependency_filepath( $dependency );
1220  
1221              if ( false === $dependency_file ) {
1222                  $unmet_dependencies['not_installed'][ $dependency ] = $dependency_name;
1223                  $unmet_dependency_names[]                           = $dependency_name;
1224              } elseif ( is_plugin_inactive( $dependency_file ) ) {
1225                  $unmet_dependencies['inactive'][ $dependency ] = $dependency_name;
1226                  $unmet_dependency_names[]                      = $dependency_name;
1227              }
1228          }
1229  
1230          $error_message = sprintf(
1231              /* translators: 1: Plugin name, 2: Number of plugins, 3: A comma-separated list of plugin names. */
1232              _n(
1233                  '<strong>Error:</strong> %1$s requires %2$d plugin to be installed and activated: %3$s.',
1234                  '<strong>Error:</strong> %1$s requires %2$d plugins to be installed and activated: %3$s.',
1235                  count( $unmet_dependency_names )
1236              ),
1237              $plugin_headers['Name'],
1238              count( $unmet_dependency_names ),
1239              implode( wp_get_list_item_separator(), $unmet_dependency_names )
1240          );
1241  
1242          if ( is_multisite() ) {
1243              if ( current_user_can( 'manage_network_plugins' ) ) {
1244                  $error_message .= ' ' . sprintf(
1245                      /* translators: %s: Link to the plugins page. */
1246                      __( '<a href="%s">Manage plugins</a>.' ),
1247                      esc_url( network_admin_url( 'plugins.php' ) )
1248                  );
1249              } else {
1250                  $error_message .= ' ' . __( 'Please contact your network administrator.' );
1251              }
1252          } else {
1253              $error_message .= ' ' . sprintf(
1254                  /* translators: %s: Link to the plugins page. */
1255                  __( '<a href="%s">Manage plugins</a>.' ),
1256                  esc_url( admin_url( 'plugins.php' ) )
1257              );
1258          }
1259  
1260          return new WP_Error(
1261              'plugin_missing_dependencies',
1262              "<p>{$error_message}</p>",
1263              $unmet_dependencies
1264          );
1265      }
1266  
1267      /**
1268       * Filters the plugin requirement validation response.
1269       *
1270       * If a plugin fails due to a Core-provided validation (incompatible WP, PHP versions), this
1271       * filter will not fire. A WP_Error response will already be returned.
1272       *
1273       * This filter is intended to add additional validation steps by site administrators.
1274       *
1275       * @since 6.9.0
1276       *
1277       * @param bool|WP_Error $met_requirements True if the plugin meets requirements, WP_Error if not.
1278       * @param string        $plugin           Path to the plugin file relative to the plugins directory.
1279       */
1280      return apply_filters( 'validate_plugin_requirements', true, $plugin );
1281  }
1282  
1283  /**
1284   * Determines whether the plugin can be uninstalled.
1285   *
1286   * @since 2.7.0
1287   *
1288   * @param string $plugin Path to the plugin file relative to the plugins directory.
1289   * @return bool Whether plugin can be uninstalled.
1290   */
1291  function is_uninstallable_plugin( $plugin ) {
1292      $file = plugin_basename( $plugin );
1293  
1294      $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
1295      if ( isset( $uninstallable_plugins[ $file ] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) {
1296          return true;
1297      }
1298  
1299      return false;
1300  }
1301  
1302  /**
1303   * Uninstalls a single plugin.
1304   *
1305   * Calls the uninstall hook, if it is available.
1306   *
1307   * @since 2.7.0
1308   *
1309   * @param string $plugin Path to the plugin file relative to the plugins directory.
1310   * @return true|null True if a plugin's uninstall.php file has been found and included.
1311   *                   Null otherwise.
1312   */
1313  function uninstall_plugin( $plugin ) {
1314      $file = plugin_basename( $plugin );
1315  
1316      $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
1317  
1318      /**
1319       * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
1320       *
1321       * @since 4.5.0
1322       *
1323       * @param string $plugin                Path to the plugin file relative to the plugins directory.
1324       * @param array  $uninstallable_plugins Uninstallable plugins.
1325       */
1326      do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
1327  
1328      if ( file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) {
1329          if ( isset( $uninstallable_plugins[ $file ] ) ) {
1330              unset( $uninstallable_plugins[ $file ] );
1331              update_option( 'uninstall_plugins', $uninstallable_plugins );
1332          }
1333          unset( $uninstallable_plugins );
1334  
1335          define( 'WP_UNINSTALL_PLUGIN', $file );
1336  
1337          wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
1338          include_once WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php';
1339  
1340          return true;
1341      }
1342  
1343      if ( isset( $uninstallable_plugins[ $file ] ) ) {
1344          $callable = $uninstallable_plugins[ $file ];
1345          unset( $uninstallable_plugins[ $file ] );
1346          update_option( 'uninstall_plugins', $uninstallable_plugins );
1347          unset( $uninstallable_plugins );
1348  
1349          wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
1350          include_once WP_PLUGIN_DIR . '/' . $file;
1351  
1352          add_action( "uninstall_{$file}", $callable );
1353  
1354          /**
1355           * Fires in uninstall_plugin() once the plugin has been uninstalled.
1356           *
1357           * The action concatenates the 'uninstall_' prefix with the basename of the
1358           * plugin passed to uninstall_plugin() to create a dynamically-named action.
1359           *
1360           * @since 2.7.0
1361           */
1362          do_action( "uninstall_{$file}" );
1363      }
1364      return null;
1365  }
1366  
1367  //
1368  // Menu.
1369  //
1370  
1371  /**
1372   * Adds a top-level menu page.
1373   *
1374   * This function takes a capability which will be used to determine whether
1375   * or not a page is included in the menu.
1376   *
1377   * The function which is hooked in to handle the output of the page must check
1378   * that the user has the required capability as well.
1379   *
1380   * @since 1.5.0
1381   *
1382   * @global array $menu
1383   * @global array $admin_page_hooks
1384   * @global array $_registered_pages
1385   * @global array $_parent_pages
1386   *
1387   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1388   * @param string               $menu_title The text to be used for the menu.
1389   * @param string               $capability The capability required for this menu to be displayed to the user.
1390   * @param string               $menu_slug  The slug name to refer to this menu by. Should be unique for this menu page and only
1391   *                                         include lowercase alphanumeric, dashes, and underscores characters to be compatible
1392   *                                         with sanitize_key().
1393   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1394   * @param string               $icon_url   Optional. The URL to the icon to be used for this menu.
1395   *                                         * Pass a base64-encoded SVG using a data URI, which will be colored to match
1396   *                                           the color scheme. This should begin with 'data:image/svg+xml;base64,'.
1397   *                                         * Pass the name of a Dashicons helper class to use a font icon,
1398   *                                           e.g. 'dashicons-chart-pie'.
1399   *                                         * Pass the namespaced name of a registered icon to render it as an inline SVG,
1400   *                                           e.g. 'core-admin/pin'.
1401   *                                         * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
1402   * @param int|float            $position   Optional. The position in the menu order this item should appear.
1403   * @return string The resulting page's hook_suffix.
1404   *
1405   * @phpstan-param non-falsy-string            $menu_slug
1406   * @phpstan-param ''|(callable(): mixed)|null $callback
1407   */
1408  function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '', $position = null ) {
1409      global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
1410  
1411      $menu_slug = plugin_basename( $menu_slug );
1412  
1413      $admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title );
1414  
1415      $hookname = get_plugin_page_hookname( $menu_slug, '' );
1416  
1417      if ( ! empty( $callback ) && ! empty( $hookname ) && current_user_can( $capability ) ) {
1418          add_action( $hookname, $callback );
1419      }
1420  
1421      if ( empty( $icon_url ) ) {
1422          $icon_url   = 'dashicons-admin-generic';
1423          $icon_class = 'menu-icon-generic ';
1424      } else {
1425          $icon_url   = set_url_scheme( $icon_url );
1426          $icon_class = '';
1427      }
1428  
1429      $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
1430  
1431      if ( null !== $position && ! is_numeric( $position ) ) {
1432          _doing_it_wrong(
1433              __FUNCTION__,
1434              sprintf(
1435                  /* translators: %s: add_menu_page() */
1436                  __( 'The seventh parameter passed to %s should be numeric representing menu position.' ),
1437                  '<code>add_menu_page()</code>'
1438              ),
1439              '6.0.0'
1440          );
1441          $position = null;
1442      }
1443  
1444      if ( null === $position || ! is_numeric( $position ) ) {
1445          $menu[] = $new_menu;
1446      } elseif ( isset( $menu[ (string) $position ] ) ) {
1447          $collision_avoider = (int) base_convert( substr( md5( $menu_slug . $menu_title ), -4 ), 16, 10 ) * 0.00001;
1448          $position          = (string) ( $position + $collision_avoider );
1449          $menu[ $position ] = $new_menu;
1450      } else {
1451          /*
1452           * Cast menu position to a string.
1453           *
1454           * This allows for floats to be passed as the position. PHP will normally cast a float to an
1455           * integer value, this ensures the float retains its mantissa (positive fractional part).
1456           *
1457           * A string containing an integer value, eg "10", is treated as a numeric index.
1458           */
1459          $position          = (string) $position;
1460          $menu[ $position ] = $new_menu;
1461      }
1462  
1463      $_registered_pages[ $hookname ] = true;
1464  
1465      // No parent as top level.
1466      $_parent_pages[ $menu_slug ] = false;
1467  
1468      return $hookname;
1469  }
1470  
1471  /**
1472   * Adds a submenu page.
1473   *
1474   * This function takes a capability which will be used to determine whether
1475   * or not a page is included in the menu.
1476   *
1477   * The function which is hooked in to handle the output of the page must check
1478   * that the user has the required capability as well.
1479   *
1480   * @since 1.5.0
1481   * @since 5.3.0 Added the `$position` parameter.
1482   *
1483   * @global array                              $submenu
1484   * @global array                              $menu
1485   * @global array                              $_wp_real_parent_file
1486   * @global array<string, array<string, true>> $_wp_submenu_nopriv
1487   * @global array<string, true>                $_wp_nopriv_pages
1488   * @global array<string, true>                $_registered_pages
1489   * @global array                              $_parent_pages
1490   *
1491   * @param string               $parent_slug The slug name for the parent menu (or the file name of a standard
1492   *                                          WordPress admin page).
1493   * @param string               $page_title  The text to be displayed in the title tags of the page when the menu
1494   *                                          is selected.
1495   * @param string               $menu_title  The text to be used for the menu.
1496   * @param string               $capability  The capability required for this menu to be displayed to the user.
1497   * @param string               $menu_slug   The slug name to refer to this menu by. Should be unique for this menu
1498   *                                          and only include lowercase alphanumeric, dashes, and underscores characters
1499   *                                          to be compatible with sanitize_key().
1500   * @param callable|string|null $callback    Optional. The function to be called to output the content for this page.
1501   * @param int|float            $position    Optional. The position in the menu order this item should appear.
1502   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1503   *
1504   * @phpstan-param non-falsy-string            $menu_slug
1505   * @phpstan-param ''|(callable(): mixed)|null $callback
1506   */
1507  function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1508      global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
1509          $_wp_nopriv_pages, $_registered_pages, $_parent_pages;
1510  
1511      $menu_slug   = plugin_basename( $menu_slug );
1512      $parent_slug = plugin_basename( $parent_slug );
1513  
1514      if ( isset( $_wp_real_parent_file[ $parent_slug ] ) ) {
1515          $parent_slug = $_wp_real_parent_file[ $parent_slug ];
1516      }
1517  
1518      if ( ! current_user_can( $capability ) ) {
1519          $_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true;
1520          $_wp_nopriv_pages[ $menu_slug ]                   = true;
1521          return false;
1522      }
1523  
1524      /*
1525       * If the parent doesn't already have a submenu, add a link to the parent
1526       * as the first item in the submenu. If the submenu file is the same as the
1527       * parent file someone is trying to link back to the parent manually. In
1528       * this case, don't automatically add a link back to avoid duplication.
1529       */
1530      if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug !== $parent_slug ) {
1531          foreach ( (array) $menu as $parent_menu ) {
1532              if ( $parent_menu[2] === $parent_slug && current_user_can( $parent_menu[1] ) ) {
1533                  $submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 );
1534              }
1535          }
1536      }
1537  
1538      $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title );
1539  
1540      if ( null !== $position && ! is_numeric( $position ) ) {
1541          _doing_it_wrong(
1542              __FUNCTION__,
1543              sprintf(
1544                  /* translators: %s: add_submenu_page() */
1545                  __( 'The seventh parameter passed to %s should be numeric representing menu position.' ),
1546                  '<code>add_submenu_page()</code>'
1547              ),
1548              '5.3.0'
1549          );
1550          $position = null;
1551      }
1552  
1553      if (
1554          null === $position ||
1555          ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) )
1556      ) {
1557          $submenu[ $parent_slug ][] = $new_sub_menu;
1558      } else {
1559          // Test for a negative position.
1560          $position = max( $position, 0 );
1561          if ( 0 === $position ) {
1562              // For negative or `0` positions, prepend the submenu.
1563              array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
1564          } else {
1565              $position = absint( $position );
1566              // Grab all of the items before the insertion point.
1567              $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
1568              // Grab all of the items after the insertion point.
1569              $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
1570              // Add the new item.
1571              $before_items[] = $new_sub_menu;
1572              // Merge the items.
1573              $submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
1574          }
1575      }
1576  
1577      // Sort the parent array.
1578      ksort( $submenu[ $parent_slug ] );
1579  
1580      $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
1581      if ( ! empty( $callback ) && ! empty( $hookname ) ) {
1582          add_action( $hookname, $callback );
1583      }
1584  
1585      $_registered_pages[ $hookname ] = true;
1586  
1587      /*
1588       * Backward-compatibility for plugins using add_management_page().
1589       * See wp-admin/admin.php for redirect from edit.php to tools.php.
1590       */
1591      if ( 'tools.php' === $parent_slug ) {
1592          $_registered_pages[ get_plugin_page_hookname( $menu_slug, 'edit.php' ) ] = true;
1593      }
1594  
1595      // No parent as top level.
1596      $_parent_pages[ $menu_slug ] = $parent_slug;
1597  
1598      return $hookname;
1599  }
1600  
1601  /**
1602   * Adds a submenu page to the Tools main menu.
1603   *
1604   * This function takes a capability which will be used to determine whether
1605   * or not a page is included in the menu.
1606   *
1607   * The function which is hooked in to handle the output of the page must check
1608   * that the user has the required capability as well.
1609   *
1610   * @since 1.5.0
1611   * @since 5.3.0 Added the `$position` parameter.
1612   *
1613   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1614   * @param string               $menu_title The text to be used for the menu.
1615   * @param string               $capability The capability required for this menu to be displayed to the user.
1616   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1617   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1618   * @param int                  $position   Optional. The position in the menu order this item should appear.
1619   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1620   *
1621   * @phpstan-param non-falsy-string            $menu_slug
1622   * @phpstan-param ''|(callable(): mixed)|null $callback
1623   */
1624  function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1625      return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1626  }
1627  
1628  /**
1629   * Adds a submenu page to the Settings main menu.
1630   *
1631   * This function takes a capability which will be used to determine whether
1632   * or not a page is included in the menu.
1633   *
1634   * The function which is hooked in to handle the output of the page must check
1635   * that the user has the required capability as well.
1636   *
1637   * @since 1.5.0
1638   * @since 5.3.0 Added the `$position` parameter.
1639   *
1640   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1641   * @param string               $menu_title The text to be used for the menu.
1642   * @param string               $capability The capability required for this menu to be displayed to the user.
1643   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1644   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1645   * @param int                  $position   Optional. The position in the menu order this item should appear.
1646   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1647   *
1648   * @phpstan-param non-falsy-string            $menu_slug
1649   * @phpstan-param ''|(callable(): mixed)|null $callback
1650   */
1651  function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1652      return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1653  }
1654  
1655  /**
1656   * Adds a submenu page to the Appearance main menu.
1657   *
1658   * This function takes a capability which will be used to determine whether
1659   * or not a page is included in the menu.
1660   *
1661   * The function which is hooked in to handle the output of the page must check
1662   * that the user has the required capability as well.
1663   *
1664   * @since 2.0.0
1665   * @since 5.3.0 Added the `$position` parameter.
1666   *
1667   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1668   * @param string               $menu_title The text to be used for the menu.
1669   * @param string               $capability The capability required for this menu to be displayed to the user.
1670   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1671   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1672   * @param int                  $position   Optional. The position in the menu order this item should appear.
1673   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1674   *
1675   * @phpstan-param non-falsy-string            $menu_slug
1676   * @phpstan-param ''|(callable(): mixed)|null $callback
1677   */
1678  function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1679      return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1680  }
1681  
1682  /**
1683   * Adds a submenu page to the Plugins main menu.
1684   *
1685   * This function takes a capability which will be used to determine whether
1686   * or not a page is included in the menu.
1687   *
1688   * The function which is hooked in to handle the output of the page must check
1689   * that the user has the required capability as well.
1690   *
1691   * @since 3.0.0
1692   * @since 5.3.0 Added the `$position` parameter.
1693   *
1694   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1695   * @param string               $menu_title The text to be used for the menu.
1696   * @param string               $capability The capability required for this menu to be displayed to the user.
1697   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1698   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1699   * @param int                  $position   Optional. The position in the menu order this item should appear.
1700   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1701   *
1702   * @phpstan-param non-falsy-string            $menu_slug
1703   * @phpstan-param ''|(callable(): mixed)|null $callback
1704   */
1705  function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1706      return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1707  }
1708  
1709  /**
1710   * Adds a submenu page to the Users/Profile main menu.
1711   *
1712   * This function takes a capability which will be used to determine whether
1713   * or not a page is included in the menu.
1714   *
1715   * The function which is hooked in to handle the output of the page must check
1716   * that the user has the required capability as well.
1717   *
1718   * @since 2.1.3
1719   * @since 5.3.0 Added the `$position` parameter.
1720   *
1721   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1722   * @param string               $menu_title The text to be used for the menu.
1723   * @param string               $capability The capability required for this menu to be displayed to the user.
1724   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1725   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1726   * @param int                  $position   Optional. The position in the menu order this item should appear.
1727   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1728   *
1729   * @phpstan-param non-falsy-string            $menu_slug
1730   * @phpstan-param ''|(callable(): mixed)|null $callback
1731   */
1732  function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1733      if ( current_user_can( 'edit_users' ) ) {
1734          $parent = 'users.php';
1735      } else {
1736          $parent = 'profile.php';
1737      }
1738      return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1739  }
1740  
1741  /**
1742   * Adds a submenu page to the Dashboard main menu.
1743   *
1744   * This function takes a capability which will be used to determine whether
1745   * or not a page is included in the menu.
1746   *
1747   * The function which is hooked in to handle the output of the page must check
1748   * that the user has the required capability as well.
1749   *
1750   * @since 2.7.0
1751   * @since 5.3.0 Added the `$position` parameter.
1752   *
1753   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1754   * @param string               $menu_title The text to be used for the menu.
1755   * @param string               $capability The capability required for this menu to be displayed to the user.
1756   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1757   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1758   * @param int                  $position   Optional. The position in the menu order this item should appear.
1759   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1760   *
1761   * @phpstan-param non-falsy-string            $menu_slug
1762   * @phpstan-param ''|(callable(): mixed)|null $callback
1763   */
1764  function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1765      return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1766  }
1767  
1768  /**
1769   * Adds a submenu page to the Posts main menu.
1770   *
1771   * This function takes a capability which will be used to determine whether
1772   * or not a page is included in the menu.
1773   *
1774   * The function which is hooked in to handle the output of the page must check
1775   * that the user has the required capability as well.
1776   *
1777   * @since 2.7.0
1778   * @since 5.3.0 Added the `$position` parameter.
1779   *
1780   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1781   * @param string               $menu_title The text to be used for the menu.
1782   * @param string               $capability The capability required for this menu to be displayed to the user.
1783   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1784   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1785   * @param int                  $position   Optional. The position in the menu order this item should appear.
1786   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1787   *
1788   * @phpstan-param non-falsy-string            $menu_slug
1789   * @phpstan-param ''|(callable(): mixed)|null $callback
1790   */
1791  function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1792      return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1793  }
1794  
1795  /**
1796   * Adds a submenu page to the Media main menu.
1797   *
1798   * This function takes a capability which will be used to determine whether
1799   * or not a page is included in the menu.
1800   *
1801   * The function which is hooked in to handle the output of the page must check
1802   * that the user has the required capability as well.
1803   *
1804   * @since 2.7.0
1805   * @since 5.3.0 Added the `$position` parameter.
1806   *
1807   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1808   * @param string               $menu_title The text to be used for the menu.
1809   * @param string               $capability The capability required for this menu to be displayed to the user.
1810   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1811   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1812   * @param int                  $position   Optional. The position in the menu order this item should appear.
1813   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1814   *
1815   * @phpstan-param non-falsy-string            $menu_slug
1816   * @phpstan-param ''|(callable(): mixed)|null $callback
1817   */
1818  function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1819      return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1820  }
1821  
1822  /**
1823   * Adds a submenu page to the Links main menu.
1824   *
1825   * This function takes a capability which will be used to determine whether
1826   * or not a page is included in the menu.
1827   *
1828   * The function which is hooked in to handle the output of the page must check
1829   * that the user has the required capability as well.
1830   *
1831   * @since 2.7.0
1832   * @since 5.3.0 Added the `$position` parameter.
1833   *
1834   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1835   * @param string               $menu_title The text to be used for the menu.
1836   * @param string               $capability The capability required for this menu to be displayed to the user.
1837   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1838   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1839   * @param int                  $position   Optional. The position in the menu order this item should appear.
1840   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1841   *
1842   * @phpstan-param non-falsy-string            $menu_slug
1843   * @phpstan-param ''|(callable(): mixed)|null $callback
1844   */
1845  function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1846      return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1847  }
1848  
1849  /**
1850   * Adds a submenu page to the Pages main menu.
1851   *
1852   * This function takes a capability which will be used to determine whether
1853   * or not a page is included in the menu.
1854   *
1855   * The function which is hooked in to handle the output of the page must check
1856   * that the user has the required capability as well.
1857   *
1858   * @since 2.7.0
1859   * @since 5.3.0 Added the `$position` parameter.
1860   *
1861   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1862   * @param string               $menu_title The text to be used for the menu.
1863   * @param string               $capability The capability required for this menu to be displayed to the user.
1864   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1865   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1866   * @param int                  $position   Optional. The position in the menu order this item should appear.
1867   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1868   *
1869   * @phpstan-param non-falsy-string            $menu_slug
1870   * @phpstan-param ''|(callable(): mixed)|null $callback
1871   */
1872  function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1873      return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1874  }
1875  
1876  /**
1877   * Adds a submenu page to the Comments main menu.
1878   *
1879   * This function takes a capability which will be used to determine whether
1880   * or not a page is included in the menu.
1881   *
1882   * The function which is hooked in to handle the output of the page must check
1883   * that the user has the required capability as well.
1884   *
1885   * @since 2.7.0
1886   * @since 5.3.0 Added the `$position` parameter.
1887   *
1888   * @param string               $page_title The text to be displayed in the title tags of the page when the menu is selected.
1889   * @param string               $menu_title The text to be used for the menu.
1890   * @param string               $capability The capability required for this menu to be displayed to the user.
1891   * @param string               $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
1892   * @param callable|string|null $callback   Optional. The function to be called to output the content for this page.
1893   * @param int                  $position   Optional. The position in the menu order this item should appear.
1894   * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
1895   *
1896   * @phpstan-param non-falsy-string            $menu_slug
1897   * @phpstan-param ''|(callable(): mixed)|null $callback
1898   */
1899  function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
1900      return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
1901  }
1902  
1903  /**
1904   * Removes a top-level admin menu.
1905   *
1906   * Example usage:
1907   *
1908   *  - `remove_menu_page( 'tools.php' )`
1909   *  - `remove_menu_page( 'plugin_menu_slug' )`
1910   *
1911   * @since 3.1.0
1912   *
1913   * @global array $menu
1914   *
1915   * @param string $menu_slug The slug of the menu.
1916   * @return array|false The removed menu on success, false if not found.
1917   */
1918  function remove_menu_page( $menu_slug ) {
1919      global $menu;
1920  
1921      foreach ( $menu as $i => $item ) {
1922          if ( $menu_slug === $item[2] ) {
1923              unset( $menu[ $i ] );
1924              return $item;
1925          }
1926      }
1927  
1928      return false;
1929  }
1930  
1931  /**
1932   * Removes an admin submenu.
1933   *
1934   * Example usage:
1935   *
1936   *  - `remove_submenu_page( 'themes.php', 'nav-menus.php' )`
1937   *  - `remove_submenu_page( 'tools.php', 'plugin_submenu_slug' )`
1938   *  - `remove_submenu_page( 'plugin_menu_slug', 'plugin_submenu_slug' )`
1939   *
1940   * @since 3.1.0
1941   *
1942   * @global array $submenu
1943   *
1944   * @param string $menu_slug    The slug for the parent menu.
1945   * @param string $submenu_slug The slug of the submenu.
1946   * @return array|false The removed submenu on success, false if not found.
1947   */
1948  function remove_submenu_page( $menu_slug, $submenu_slug ) {
1949      global $submenu;
1950  
1951      if ( ! isset( $submenu[ $menu_slug ] ) ) {
1952          return false;
1953      }
1954  
1955      foreach ( $submenu[ $menu_slug ] as $i => $item ) {
1956          if ( $submenu_slug === $item[2] ) {
1957              unset( $submenu[ $menu_slug ][ $i ] );
1958              return $item;
1959          }
1960      }
1961  
1962      return false;
1963  }
1964  
1965  /**
1966   * Gets the URL to access a particular menu page based on the slug it was registered with.
1967   *
1968   * If the slug hasn't been registered properly, no URL will be returned.
1969   *
1970   * @since 3.0.0
1971   *
1972   * @global array $_parent_pages
1973   *
1974   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1975   * @param bool   $display   Optional. Whether or not to display the URL. Default true.
1976   * @return string The menu page URL.
1977   */
1978  function menu_page_url( $menu_slug, $display = true ) {
1979      global $_parent_pages;
1980  
1981      if ( isset( $_parent_pages[ $menu_slug ] ) ) {
1982          $parent_slug = $_parent_pages[ $menu_slug ];
1983  
1984          if ( $parent_slug && ! isset( $_parent_pages[ $parent_slug ] ) ) {
1985              $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1986          } else {
1987              $url = admin_url( 'admin.php?page=' . $menu_slug );
1988          }
1989      } else {
1990          $url = '';
1991      }
1992  
1993      $url = esc_url( $url );
1994  
1995      if ( $display ) {
1996          echo $url;
1997      }
1998  
1999      return $url;
2000  }
2001  
2002  //
2003  // Pluggable Menu Support -- Private.
2004  //
2005  /**
2006   * Gets the parent file of the current admin page.
2007   *
2008   * @since 1.5.0
2009   *
2010   * @global string $parent_file
2011   * @global array  $menu
2012   * @global array  $submenu
2013   * @global string $pagenow              The filename of the current screen.
2014   * @global string $typenow              The post type of the current screen.
2015   * @global string $plugin_page
2016   * @global array  $_wp_real_parent_file
2017   * @global array  $_wp_menu_nopriv
2018   * @global array  $_wp_submenu_nopriv
2019   *
2020   * @param string $parent_page Optional. The slug name for the parent menu (or the file name
2021   *                            of a standard WordPress admin page). Default empty string.
2022   * @return string The parent file of the current admin page.
2023   */
2024  function get_admin_page_parent( $parent_page = '' ) {
2025      global $parent_file, $menu, $submenu, $pagenow, $typenow,
2026          $plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;
2027  
2028      if ( ! empty( $parent_page ) && 'admin.php' !== $parent_page ) {
2029          return $_wp_real_parent_file[ $parent_page ] ?? $parent_page;
2030      }
2031  
2032      if ( 'admin.php' === $pagenow && isset( $plugin_page ) ) {
2033          foreach ( (array) $menu as $parent_menu ) {
2034              if ( $parent_menu[2] === $plugin_page ) {
2035                  $parent_file = $plugin_page;
2036                  return $_wp_real_parent_file[ $parent_file ] ?? $parent_file;
2037              }
2038          }
2039          if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) ) {
2040              $parent_file = $plugin_page;
2041              return $_wp_real_parent_file[ $parent_file ] ?? $parent_file;
2042          }
2043      }
2044  
2045      if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) {
2046          $parent_file = $pagenow;
2047          return $_wp_real_parent_file[ $parent_file ] ?? $parent_file;
2048      }
2049  
2050      foreach ( array_keys( (array) $submenu ) as $parent_page ) {
2051          foreach ( $submenu[ $parent_page ] as $submenu_array ) {
2052              if ( isset( $_wp_real_parent_file[ $parent_page ] ) ) {
2053                  $parent_page = $_wp_real_parent_file[ $parent_page ];
2054              }
2055  
2056              if ( ! empty( $typenow ) && "$pagenow?post_type=$typenow" === $submenu_array[2] ) {
2057                  $parent_file = $parent_page;
2058                  return $parent_page;
2059              } elseif ( empty( $typenow ) && $pagenow === $submenu_array[2]
2060                  && ( empty( $parent_file ) || ! str_contains( $parent_file, '?' ) )
2061              ) {
2062                  $parent_file = $parent_page;
2063                  return $parent_page;
2064              } elseif ( isset( $plugin_page ) && $plugin_page === $submenu_array[2] ) {
2065                  $parent_file = $parent_page;
2066                  return $parent_page;
2067              }
2068          }
2069      }
2070  
2071      if ( empty( $parent_file ) ) {
2072          $parent_file = '';
2073      }
2074      return '';
2075  }
2076  
2077  /**
2078   * Gets the title of the current admin page.
2079   *
2080   * @since 1.5.0
2081   *
2082   * @global string $title       The title of the current screen.
2083   * @global array  $menu
2084   * @global array  $submenu
2085   * @global string $pagenow     The filename of the current screen.
2086   * @global string $typenow     The post type of the current screen.
2087   * @global string $plugin_page
2088   *
2089   * @return string The title of the current admin page.
2090   */
2091  function get_admin_page_title() {
2092      global $title, $menu, $submenu, $pagenow, $typenow, $plugin_page;
2093  
2094      if ( ! empty( $title ) ) {
2095          return $title;
2096      }
2097  
2098      $hook = get_plugin_page_hook( $plugin_page, $pagenow );
2099  
2100      $parent  = get_admin_page_parent();
2101      $parent1 = $parent;
2102  
2103      if ( empty( $parent ) ) {
2104          foreach ( (array) $menu as $menu_array ) {
2105              if ( isset( $menu_array[3] ) ) {
2106                  if ( $menu_array[2] === $pagenow ) {
2107                      $title = $menu_array[3];
2108                      return $menu_array[3];
2109                  } elseif ( isset( $plugin_page ) && $plugin_page === $menu_array[2] && $hook === $menu_array[5] ) {
2110                      $title = $menu_array[3];
2111                      return $menu_array[3];
2112                  }
2113              } else {
2114                  $title = $menu_array[0];
2115                  return $title;
2116              }
2117          }
2118      } else {
2119          foreach ( array_keys( $submenu ) as $parent ) {
2120              foreach ( $submenu[ $parent ] as $submenu_array ) {
2121                  if ( isset( $plugin_page )
2122                      && $plugin_page === $submenu_array[2]
2123                      && ( $pagenow === $parent
2124                          || $plugin_page === $parent
2125                          || $plugin_page === $hook
2126                          || 'admin.php' === $pagenow && $parent1 !== $submenu_array[2]
2127                          || ! empty( $typenow ) && "$pagenow?post_type=$typenow" === $parent )
2128                      ) {
2129                          $title = $submenu_array[3];
2130                          return $submenu_array[3];
2131                  }
2132  
2133                  if ( $submenu_array[2] !== $pagenow || isset( $_GET['page'] ) ) { // Not the current page.
2134                      continue;
2135                  }
2136  
2137                  if ( isset( $submenu_array[3] ) ) {
2138                      $title = $submenu_array[3];
2139                      return $submenu_array[3];
2140                  } else {
2141                      $title = $submenu_array[0];
2142                      return $title;
2143                  }
2144              }
2145          }
2146          if ( empty( $title ) ) {
2147              foreach ( $menu as $menu_array ) {
2148                  if ( isset( $plugin_page )
2149                      && $plugin_page === $menu_array[2]
2150                      && 'admin.php' === $pagenow
2151                      && $parent1 === $menu_array[2]
2152                  ) {
2153                          $title = $menu_array[3];
2154                          return $menu_array[3];
2155                  }
2156              }
2157          }
2158      }
2159  
2160      return $title;
2161  }
2162  
2163  /**
2164   * Gets the hook attached to the administrative page of a plugin.
2165   *
2166   * @since 1.5.0
2167   *
2168   * @param string $plugin_page The slug name of the plugin page.
2169   * @param string $parent_page The slug name for the parent menu (or the file name of a standard
2170   *                            WordPress admin page).
2171   * @return string|null Hook attached to the plugin page, null otherwise.
2172   */
2173  function get_plugin_page_hook( $plugin_page, $parent_page ) {
2174      $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
2175      if ( has_action( $hook ) ) {
2176          return $hook;
2177      } else {
2178          return null;
2179      }
2180  }
2181  
2182  /**
2183   * Determines whether the requested admin page is registered.
2184   *
2185   * This does not check whether the current user has the capability to access
2186   * the page, only whether the page itself exists. Pages registered for a
2187   * capability the current user lacks are recorded in the no-privilege
2188   * registries rather than in $_registered_pages, and are still considered
2189   * to exist so that the caller can report a permission error instead of a
2190   * "page not found" error.
2191   *
2192   * @since 7.2.0
2193   *
2194   * @global string|null                        $plugin_page        The plugin page slug being loaded.
2195   * @global array<string, true>                $_registered_pages  Array of all registered admin page hooks.
2196   * @global array<string, true>                $_wp_menu_nopriv    Array of top-level menu slugs the current user cannot access.
2197   * @global array<string, array<string, true>> $_wp_submenu_nopriv Array of submenu slugs the current user cannot access, keyed by parent slug.
2198   * @global array<string, true>                $_wp_nopriv_pages   Array of page slugs registered for a capability the current user lacks.
2199   *
2200   * @return bool True if the admin page exists, false otherwise.
2201   */
2202  function wp_admin_page_exists(): bool {
2203      global $plugin_page, $_registered_pages, $_wp_menu_nopriv, $_wp_submenu_nopriv,
2204          $_wp_nopriv_pages;
2205  
2206      if ( ! isset( $plugin_page ) ) {
2207          return true;
2208      }
2209  
2210      $hookname = get_plugin_page_hookname( $plugin_page, get_admin_page_parent() );
2211  
2212      if ( isset( $_registered_pages[ $hookname ] ) ) {
2213          return true;
2214      }
2215  
2216      // The page may be registered for a capability the current user lacks.
2217      if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) || isset( $_wp_nopriv_pages[ $plugin_page ] ) ) {
2218          return true;
2219      }
2220  
2221      return array_any(
2222          (array) $_wp_submenu_nopriv,
2223          fn( $nopriv_submenus ) => isset( $nopriv_submenus[ $plugin_page ] )
2224      );
2225  }
2226  
2227  /**
2228   * Gets the hook name for the administrative page of a plugin.
2229   *
2230   * @since 1.5.0
2231   *
2232   * @global array $admin_page_hooks
2233   *
2234   * @param string $plugin_page The slug name of the plugin page.
2235   * @param string $parent_page The slug name for the parent menu (or the file name of a standard
2236   *                            WordPress admin page).
2237   * @return string Hook name for the plugin page.
2238   *
2239   * @phpstan-return non-falsy-string
2240   */
2241  function get_plugin_page_hookname( $plugin_page, $parent_page ) {
2242      global $admin_page_hooks;
2243  
2244      $parent = get_admin_page_parent( $parent_page );
2245  
2246      $page_type = 'admin';
2247      if ( empty( $parent_page ) || 'admin.php' === $parent_page || isset( $admin_page_hooks[ $plugin_page ] ) ) {
2248          if ( isset( $admin_page_hooks[ $plugin_page ] ) ) {
2249              $page_type = 'toplevel';
2250          } elseif ( isset( $admin_page_hooks[ $parent ] ) ) {
2251              $page_type = $admin_page_hooks[ $parent ];
2252          }
2253      } elseif ( isset( $admin_page_hooks[ $parent ] ) ) {
2254          $page_type = $admin_page_hooks[ $parent ];
2255      }
2256  
2257      $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
2258  
2259      return $page_type . '_page_' . $plugin_name;
2260  }
2261  
2262  /**
2263   * Determines whether the current user can access the current admin page.
2264   *
2265   * @since 1.5.0
2266   *
2267   * @global string                             $pagenow            The filename of the current screen.
2268   * @global array                              $menu               The top-level admin menu items.
2269   * @global array                              $submenu            The admin submenu items, keyed by parent slug.
2270   * @global array<string, true>                $_wp_menu_nopriv    Array of top-level menu slugs the current user cannot access.
2271   * @global array<string, array<string, true>> $_wp_submenu_nopriv Array of submenu slugs the current user cannot access, keyed by parent slug.
2272   * @global string|null                        $plugin_page        The plugin page slug being loaded.
2273   * @global array<string, true>                $_registered_pages  Array of all registered admin page hooks.
2274   *
2275   * @return bool True if the current user can access the admin page, false otherwise.
2276   */
2277  function user_can_access_admin_page(): bool {
2278      global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv,
2279          $plugin_page, $_registered_pages;
2280  
2281      $parent = get_admin_page_parent();
2282  
2283      if ( ! isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $parent ][ $pagenow ] ) ) {
2284          return false;
2285      }
2286  
2287      if ( isset( $plugin_page ) ) {
2288          if ( isset( $_wp_submenu_nopriv[ $parent ][ $plugin_page ] ) ) {
2289              return false;
2290          }
2291  
2292          $hookname = get_plugin_page_hookname( $plugin_page, $parent );
2293  
2294          if ( ! isset( $_registered_pages[ $hookname ] ) ) {
2295              return false;
2296          }
2297      }
2298  
2299      if ( empty( $parent ) ) {
2300          if ( isset( $_wp_menu_nopriv[ $pagenow ] ) ) {
2301              return false;
2302          }
2303          if ( isset( $_wp_submenu_nopriv[ $pagenow ][ $pagenow ] ) ) {
2304              return false;
2305          }
2306          if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) {
2307              return false;
2308          }
2309          if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[ $plugin_page ] ) ) {
2310              return false;
2311          }
2312  
2313          foreach ( array_keys( $_wp_submenu_nopriv ) as $key ) {
2314              if ( isset( $_wp_submenu_nopriv[ $key ][ $pagenow ] ) ) {
2315                  return false;
2316              }
2317              if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $key ][ $plugin_page ] ) ) {
2318                  return false;
2319              }
2320          }
2321  
2322          return true;
2323      }
2324  
2325      if ( isset( $plugin_page ) && $plugin_page === $parent && isset( $_wp_menu_nopriv[ $plugin_page ] ) ) {
2326          return false;
2327      }
2328  
2329      if ( isset( $submenu[ $parent ] ) ) {
2330          foreach ( $submenu[ $parent ] as $submenu_array ) {
2331              if ( isset( $plugin_page ) && $submenu_array[2] === $plugin_page ) {
2332                  return current_user_can( $submenu_array[1] );
2333              } elseif ( $submenu_array[2] === $pagenow ) {
2334                  return current_user_can( $submenu_array[1] );
2335              }
2336          }
2337      }
2338  
2339      foreach ( $menu as $menu_array ) {
2340          if ( $menu_array[2] === $parent ) {
2341              return current_user_can( $menu_array[1] );
2342          }
2343      }
2344  
2345      return true;
2346  }
2347  
2348  /* Allowed list functions */
2349  
2350  /**
2351   * Refreshes the value of the allowed options list available via the 'allowed_options' hook.
2352   *
2353   * See the {@see 'allowed_options'} filter.
2354   *
2355   * @since 2.7.0
2356   * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`.
2357   *              Please consider writing more inclusive code.
2358   *
2359   * @global array $new_allowed_options
2360   *
2361   * @param array $options
2362   * @return array Updated allowed options.
2363   */
2364  function option_update_filter( $options ) {
2365      global $new_allowed_options;
2366  
2367      if ( is_array( $new_allowed_options ) ) {
2368          $options = add_allowed_options( $new_allowed_options, $options );
2369      }
2370  
2371      return $options;
2372  }
2373  
2374  /**
2375   * Adds an array of options to the list of allowed options.
2376   *
2377   * @since 5.5.0
2378   *
2379   * @global array $allowed_options
2380   *
2381   * @param array        $new_options
2382   * @param string|array $options
2383   * @return array Updated allowed options.
2384   */
2385  function add_allowed_options( $new_options, $options = '' ) {
2386      if ( '' === $options ) {
2387          global $allowed_options;
2388      } else {
2389          $allowed_options = $options;
2390      }
2391  
2392      foreach ( $new_options as $page => $keys ) {
2393          foreach ( $keys as $key ) {
2394              if ( ! isset( $allowed_options[ $page ] ) || ! is_array( $allowed_options[ $page ] ) ) {
2395                  $allowed_options[ $page ]   = array();
2396                  $allowed_options[ $page ][] = $key;
2397              } else {
2398                  $pos = array_search( $key, $allowed_options[ $page ], true );
2399                  if ( false === $pos ) {
2400                      $allowed_options[ $page ][] = $key;
2401                  }
2402              }
2403          }
2404      }
2405  
2406      return $allowed_options;
2407  }
2408  
2409  /**
2410   * Removes a list of options from the allowed options list.
2411   *
2412   * @since 5.5.0
2413   *
2414   * @global array $allowed_options
2415   *
2416   * @param array        $del_options
2417   * @param string|array $options
2418   * @return array Updated allowed options.
2419   */
2420  function remove_allowed_options( $del_options, $options = '' ) {
2421      if ( '' === $options ) {
2422          global $allowed_options;
2423      } else {
2424          $allowed_options = $options;
2425      }
2426  
2427      foreach ( $del_options as $page => $keys ) {
2428          foreach ( $keys as $key ) {
2429              if ( isset( $allowed_options[ $page ] ) && is_array( $allowed_options[ $page ] ) ) {
2430                  $pos = array_search( $key, $allowed_options[ $page ], true );
2431                  if ( false !== $pos ) {
2432                      unset( $allowed_options[ $page ][ $pos ] );
2433                  }
2434              }
2435          }
2436      }
2437  
2438      return $allowed_options;
2439  }
2440  
2441  /**
2442   * Outputs nonce, action, and option_page fields for a settings page.
2443   *
2444   * @since 2.7.0
2445   *
2446   * @param string $option_group A settings group name. This should match the group name
2447   *                             used in register_setting().
2448   */
2449  function settings_fields( $option_group ) {
2450      echo "<input type='hidden' name='option_page' value='" . esc_attr( $option_group ) . "' />";
2451      echo '<input type="hidden" name="action" value="update" />';
2452      wp_nonce_field( "$option_group-options" );
2453  }
2454  
2455  /**
2456   * Clears the plugins cache used by get_plugins() and by default, the plugin updates cache.
2457   *
2458   * @since 3.7.0
2459   *
2460   * @param bool $clear_update_cache Whether to clear the plugin updates cache. Default true.
2461   */
2462  function wp_clean_plugins_cache( $clear_update_cache = true ) {
2463      if ( $clear_update_cache ) {
2464          delete_site_transient( 'update_plugins' );
2465      }
2466      wp_cache_delete( 'plugins', 'plugins' );
2467  }
2468  
2469  /**
2470   * Loads a given plugin attempt to generate errors.
2471   *
2472   * @since 3.0.0
2473   * @since 4.4.0 Function was moved into the `wp-admin/includes/plugin.php` file.
2474   *
2475   * @param string $plugin Path to the plugin file relative to the plugins directory.
2476   */
2477  function plugin_sandbox_scrape( $plugin ) {
2478      if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
2479          define( 'WP_SANDBOX_SCRAPING', true );
2480      }
2481  
2482      wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
2483      include_once WP_PLUGIN_DIR . '/' . $plugin;
2484  }
2485  
2486  /**
2487   * Declares a helper function for adding content to the Privacy Policy Guide.
2488   *
2489   * Plugins and themes should suggest text for inclusion in the site's privacy policy.
2490   * The suggested text should contain information about any functionality that affects user privacy,
2491   * and will be shown on the Privacy Policy Guide screen.
2492   *
2493   * A plugin or theme can use this function multiple times as long as it will help to better present
2494   * the suggested policy content. For example modular plugins such as WooCommerse or Jetpack
2495   * can add or remove suggested content depending on the modules/extensions that are enabled.
2496   * For more information see the Plugin Handbook:
2497   * https://developer.wordpress.org/plugins/privacy/suggesting-text-for-the-site-privacy-policy/.
2498   *
2499   * The HTML contents of the `$policy_text` supports use of a specialized `.privacy-policy-tutorial`
2500   * CSS class which can be used to provide supplemental information. Any content contained within
2501   * HTML elements that have the `.privacy-policy-tutorial` CSS class applied will be omitted
2502   * from the clipboard when the section content is copied.
2503   *
2504   * Intended for use with the `'admin_init'` action.
2505   *
2506   * @since 4.9.6
2507   *
2508   * @param string $plugin_name The name of the plugin or theme that is suggesting content
2509   *                            for the site's privacy policy.
2510   * @param string $policy_text The suggested content for inclusion in the policy.
2511   */
2512  function wp_add_privacy_policy_content( $plugin_name, $policy_text ) {
2513      if ( ! is_admin() ) {
2514          _doing_it_wrong(
2515              __FUNCTION__,
2516              sprintf(
2517                  /* translators: %s: admin_init */
2518                  __( 'The suggested privacy policy content should be added only in wp-admin by using the %s (or later) action.' ),
2519                  '<code>admin_init</code>'
2520              ),
2521              '4.9.7'
2522          );
2523          return;
2524      } elseif ( ! doing_action( 'admin_init' ) && ! did_action( 'admin_init' ) ) {
2525          _doing_it_wrong(
2526              __FUNCTION__,
2527              sprintf(
2528                  /* translators: %s: admin_init */
2529                  __( 'The suggested privacy policy content should be added by using the %s (or later) action. Please see the inline documentation.' ),
2530                  '<code>admin_init</code>'
2531              ),
2532              '4.9.7'
2533          );
2534          return;
2535      }
2536  
2537      if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
2538          require_once  ABSPATH . 'wp-admin/includes/class-wp-privacy-policy-content.php';
2539      }
2540  
2541      WP_Privacy_Policy_Content::add( $plugin_name, $policy_text );
2542  }
2543  
2544  /**
2545   * Determines whether a plugin is technically active but was paused while
2546   * loading.
2547   *
2548   * For more information on this and similar theme functions, check out
2549   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
2550   * Conditional Tags} article in the Theme Developer Handbook.
2551   *
2552   * @since 5.2.0
2553   *
2554   * @global WP_Paused_Extensions_Storage $_paused_plugins
2555   *
2556   * @param string $plugin Path to the plugin file relative to the plugins directory.
2557   * @return bool True, if in the list of paused plugins. False, if not in the list.
2558   */
2559  function is_plugin_paused( $plugin ) {
2560      if ( ! isset( $GLOBALS['_paused_plugins'] ) ) {
2561          return false;
2562      }
2563  
2564      if ( ! is_plugin_active( $plugin ) ) {
2565          return false;
2566      }
2567  
2568      list( $plugin ) = explode( '/', $plugin );
2569  
2570      return array_key_exists( $plugin, $GLOBALS['_paused_plugins'] );
2571  }
2572  
2573  /**
2574   * Gets the error that was recorded for a paused plugin.
2575   *
2576   * @since 5.2.0
2577   *
2578   * @global WP_Paused_Extensions_Storage $_paused_plugins
2579   *
2580   * @param string $plugin Path to the plugin file relative to the plugins directory.
2581   * @return array|false Array of error information as returned by `error_get_last()`,
2582   *                     or false if none was recorded.
2583   */
2584  function wp_get_plugin_error( $plugin ) {
2585      if ( ! isset( $GLOBALS['_paused_plugins'] ) ) {
2586          return false;
2587      }
2588  
2589      list( $plugin ) = explode( '/', $plugin );
2590  
2591      if ( ! array_key_exists( $plugin, $GLOBALS['_paused_plugins'] ) ) {
2592          return false;
2593      }
2594  
2595      return $GLOBALS['_paused_plugins'][ $plugin ];
2596  }
2597  
2598  /**
2599   * Tries to resume a single plugin.
2600   *
2601   * If a redirect was provided, we first ensure the plugin does not throw fatal
2602   * errors anymore.
2603   *
2604   * The way it works is by setting the redirection to the error before trying to
2605   * include the plugin file. If the plugin fails, then the redirection will not
2606   * be overwritten with the success message and the plugin will not be resumed.
2607   *
2608   * @since 5.2.0
2609   *
2610   * @param string $plugin   Single plugin to resume.
2611   * @param string $redirect Optional. URL to redirect to. Default empty string.
2612   * @return true|WP_Error True on success, false if `$plugin` was not paused,
2613   *                       `WP_Error` on failure.
2614   */
2615  function resume_plugin( $plugin, $redirect = '' ) {
2616      /*
2617       * We'll override this later if the plugin could be resumed without
2618       * creating a fatal error.
2619       */
2620      if ( ! empty( $redirect ) ) {
2621          wp_redirect(
2622              add_query_arg(
2623                  '_error_nonce',
2624                  wp_create_nonce( 'plugin-resume-error_' . $plugin ),
2625                  $redirect
2626              )
2627          );
2628  
2629          // Load the plugin to test whether it throws a fatal error.
2630          ob_start();
2631          plugin_sandbox_scrape( $plugin );
2632          ob_clean();
2633      }
2634  
2635      list( $extension ) = explode( '/', $plugin );
2636  
2637      $result = wp_paused_plugins()->delete( $extension );
2638  
2639      if ( ! $result ) {
2640          return new WP_Error(
2641              'could_not_resume_plugin',
2642              __( 'Could not resume the plugin.' )
2643          );
2644      }
2645  
2646      return true;
2647  }
2648  
2649  /**
2650   * Renders an admin notice in case some plugins have been paused due to errors.
2651   *
2652   * @since 5.2.0
2653   *
2654   * @global string                       $pagenow         The filename of the current screen.
2655   * @global WP_Paused_Extensions_Storage $_paused_plugins
2656   */
2657  function paused_plugins_notice() {
2658      if ( 'plugins.php' === $GLOBALS['pagenow'] ) {
2659          return;
2660      }
2661  
2662      if ( ! current_user_can( 'resume_plugins' ) ) {
2663          return;
2664      }
2665  
2666      if ( ! isset( $GLOBALS['_paused_plugins'] ) || empty( $GLOBALS['_paused_plugins'] ) ) {
2667          return;
2668      }
2669  
2670      $message = sprintf(
2671          '<strong>%s</strong><br>%s</p><p><a href="%s">%s</a>',
2672          __( 'One or more plugins failed to load properly.' ),
2673          __( 'You can find more details and make changes on the Plugins screen.' ),
2674          esc_url( admin_url( 'plugins.php?plugin_status=paused' ) ),
2675          __( 'Go to the Plugins screen' )
2676      );
2677      wp_admin_notice(
2678          $message,
2679          array( 'type' => 'error' )
2680      );
2681  }
2682  
2683  /**
2684   * Renders an admin notice when a plugin was deactivated during an update.
2685   *
2686   * Displays an admin notice in case a plugin has been deactivated during an
2687   * upgrade due to incompatibility with the current version of WordPress.
2688   *
2689   * @since 5.8.0
2690   * @access private
2691   *
2692   * @global string $pagenow    The filename of the current screen.
2693   * @global string $wp_version The WordPress version string.
2694   */
2695  function deactivated_plugins_notice() {
2696      if ( 'plugins.php' === $GLOBALS['pagenow'] ) {
2697          return;
2698      }
2699  
2700      if ( ! current_user_can( 'activate_plugins' ) ) {
2701          return;
2702      }
2703  
2704      $blog_deactivated_plugins = get_option( 'wp_force_deactivated_plugins' );
2705      $site_deactivated_plugins = array();
2706  
2707      if ( false === $blog_deactivated_plugins ) {
2708          // Option not in database, add an empty array to avoid extra DB queries on subsequent loads.
2709          update_option( 'wp_force_deactivated_plugins', array(), false );
2710      }
2711  
2712      if ( is_multisite() ) {
2713          $site_deactivated_plugins = get_site_option( 'wp_force_deactivated_plugins' );
2714          if ( false === $site_deactivated_plugins ) {
2715              // Option not in database, add an empty array to avoid extra DB queries on subsequent loads.
2716              update_site_option( 'wp_force_deactivated_plugins', array() );
2717          }
2718      }
2719  
2720      if ( empty( $blog_deactivated_plugins ) && empty( $site_deactivated_plugins ) ) {
2721          // No deactivated plugins.
2722          return;
2723      }
2724  
2725      $deactivated_plugins = array_merge( $blog_deactivated_plugins, $site_deactivated_plugins );
2726  
2727      foreach ( $deactivated_plugins as $plugin ) {
2728          if ( ! empty( $plugin['version_compatible'] ) && ! empty( $plugin['version_deactivated'] ) ) {
2729              $explanation = sprintf(
2730                  /* translators: 1: Name of deactivated plugin, 2: Plugin version deactivated, 3: Current WP version, 4: Compatible plugin version. */
2731                  __( '%1$s %2$s was deactivated due to incompatibility with WordPress %3$s, please upgrade to %1$s %4$s or later.' ),
2732                  $plugin['plugin_name'],
2733                  $plugin['version_deactivated'],
2734                  $GLOBALS['wp_version'],
2735                  $plugin['version_compatible']
2736              );
2737          } else {
2738              $explanation = sprintf(
2739                  /* translators: 1: Name of deactivated plugin, 2: Plugin version deactivated, 3: Current WP version. */
2740                  __( '%1$s %2$s was deactivated due to incompatibility with WordPress %3$s.' ),
2741                  $plugin['plugin_name'],
2742                  ! empty( $plugin['version_deactivated'] ) ? $plugin['version_deactivated'] : '',
2743                  $GLOBALS['wp_version'],
2744                  $plugin['version_compatible']
2745              );
2746          }
2747  
2748          $message = sprintf(
2749              '<strong>%s</strong><br>%s</p><p><a href="%s">%s</a>',
2750              sprintf(
2751                  /* translators: %s: Name of deactivated plugin. */
2752                  __( '%s plugin deactivated during WordPress upgrade.' ),
2753                  $plugin['plugin_name']
2754              ),
2755              $explanation,
2756              esc_url( admin_url( 'plugins.php?plugin_status=inactive' ) ),
2757              __( 'Go to the Plugins screen' )
2758          );
2759          wp_admin_notice( $message, array( 'type' => 'warning' ) );
2760      }
2761  
2762      // Empty the options.
2763      update_option( 'wp_force_deactivated_plugins', array(), false );
2764      if ( is_multisite() ) {
2765          update_site_option( 'wp_force_deactivated_plugins', array() );
2766      }
2767  }


Generated : Thu Nov 5 08:20:37 2026 Cross-referenced by PHPXref