[ Index ]

PHP Cross Reference of WordPress

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


Generated: Fri Jul 19 01:00:04 2019 Cross-referenced by PHPXref 0.7.1