[ 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   * Parse 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   * <code>
  18   * /*
  19   * Plugin Name: Name of Plugin
  20   * Plugin URI: Link to plugin information
  21   * Description: Plugin Description
  22   * Author: Plugin author's name
  23   * Author URI: Link to the author's web site
  24   * Version: Must be set in the plugin for WordPress 2.3+
  25   * Text Domain: Optional. Unique identifier, should be same as the one used in
  26   *        plugin_text_domain()
  27   * Domain Path: Optional. Only useful if the translations are located in a
  28   *        folder above the plugin's base path. For example, if .mo files are
  29   *        located in the locale folder then Domain Path will be "/locale/" and
  30   *        must have the first slash. Defaults to the base folder the plugin is
  31   *        located in.
  32   * Network: Optional. Specify "Network: true" to require that a plugin is activated
  33   *        across all sites in an installation. This will prevent a plugin from being
  34   *        activated on a single site when Multisite is enabled.
  35   *  * / # Remove the space to close comment
  36   * </code>
  37   *
  38   * Plugin data returned array contains the following:
  39   *        'Name' - Name of the plugin, must be unique.
  40   *        'Title' - Title of the plugin and the link to the plugin's web site.
  41   *        'Description' - Description of what the plugin does and/or notes
  42   *        from the author.
  43   *        'Author' - The author's name
  44   *        'AuthorURI' - The authors web site address.
  45   *        'Version' - The plugin version number.
  46   *        'PluginURI' - Plugin web site address.
  47   *        'TextDomain' - Plugin's text domain for localization.
  48   *        'DomainPath' - Plugin's relative directory path to .mo files.
  49   *        'Network' - Boolean. Whether the plugin can only be activated network wide.
  50   *
  51   * Some users have issues with opening large files and manipulating the contents
  52   * for want is usually the first 1kiB or 2kiB. This function stops pulling in
  53   * the plugin contents when it has all of the required plugin data.
  54   *
  55   * The first 8kiB of the file will be pulled in and if the plugin data is not
  56   * within that first 8kiB, then the plugin author should correct their plugin
  57   * and move the plugin data headers to the top.
  58   *
  59   * The plugin file is assumed to have permissions to allow for scripts to read
  60   * the file. This is not checked however and the file is only opened for
  61   * reading.
  62   *
  63   * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
  64   * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
  65   * @since 1.5.0
  66   *
  67   * @param string $plugin_file Path to the plugin file
  68   * @param bool $markup Optional. If the returned data should have HTML markup applied. Defaults to true.
  69   * @param bool $translate Optional. If the returned data should be translated. Defaults to true.
  70   * @return array See above for description.
  71   */
  72  function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
  73  
  74      $default_headers = array(
  75          'Name' => 'Plugin Name',
  76          'PluginURI' => 'Plugin URI',
  77          'Version' => 'Version',
  78          'Description' => 'Description',
  79          'Author' => 'Author',
  80          'AuthorURI' => 'Author URI',
  81          'TextDomain' => 'Text Domain',
  82          'DomainPath' => 'Domain Path',
  83          'Network' => 'Network',
  84          // Site Wide Only is deprecated in favor of Network.
  85          '_sitewide' => 'Site Wide Only',
  86      );
  87  
  88      $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
  89  
  90      // Site Wide Only is the old header for Network
  91      if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
  92          _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The <code>%1$s</code> plugin header is deprecated. Use <code>%2$s</code> instead.' ), 'Site Wide Only: true', 'Network: true' ) );
  93          $plugin_data['Network'] = $plugin_data['_sitewide'];
  94      }
  95      $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
  96      unset( $plugin_data['_sitewide'] );
  97  
  98      if ( $markup || $translate ) {
  99          $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
 100      } else {
 101          $plugin_data['Title']      = $plugin_data['Name'];
 102          $plugin_data['AuthorName'] = $plugin_data['Author'];
 103      }
 104  
 105      return $plugin_data;
 106  }
 107  
 108  /**
 109   * Sanitizes plugin data, optionally adds markup, optionally translates.
 110   *
 111   * @since 2.7.0
 112   * @access private
 113   * @see get_plugin_data()
 114   */
 115  function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
 116  
 117      // Translate fields
 118      if ( $translate ) {
 119          if ( $textdomain = $plugin_data['TextDomain'] ) {
 120              if ( $plugin_data['DomainPath'] )
 121                  load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
 122              else
 123                  load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
 124          } elseif ( in_array( basename( $plugin_file ), array( 'hello.php', 'akismet.php' ) ) ) {
 125              $textdomain = 'default';
 126          }
 127          if ( $textdomain ) {
 128              foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
 129                  $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
 130          }
 131      }
 132  
 133      // Sanitize fields
 134      $allowed_tags = $allowed_tags_in_links = array(
 135          'abbr'    => array( 'title' => true ),
 136          'acronym' => array( 'title' => true ),
 137          'code'    => true,
 138          'em'      => true,
 139          'strong'  => true,
 140      );
 141      $allowed_tags['a'] = array( 'href' => true, 'title' => true );
 142  
 143      // Name is marked up inside <a> tags. Don't allow these.
 144      // Author is too, but some plugins have used <a> here (omitting Author URI).
 145      $plugin_data['Name']        = wp_kses( $plugin_data['Name'],        $allowed_tags_in_links );
 146      $plugin_data['Author']      = wp_kses( $plugin_data['Author'],      $allowed_tags );
 147  
 148      $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
 149      $plugin_data['Version']     = wp_kses( $plugin_data['Version'],     $allowed_tags );
 150  
 151      $plugin_data['PluginURI']   = esc_url( $plugin_data['PluginURI'] );
 152      $plugin_data['AuthorURI']   = esc_url( $plugin_data['AuthorURI'] );
 153  
 154      $plugin_data['Title']      = $plugin_data['Name'];
 155      $plugin_data['AuthorName'] = $plugin_data['Author'];
 156  
 157      // Apply markup
 158      if ( $markup ) {
 159          if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
 160              $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
 161  
 162          if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
 163              $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
 164  
 165          $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
 166  
 167          if ( $plugin_data['Author'] )
 168              $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
 169      }
 170  
 171      return $plugin_data;
 172  }
 173  
 174  /**
 175   * Get a list of a plugin's files.
 176   *
 177   * @since 2.8.0
 178   *
 179   * @param string $plugin Plugin ID
 180   * @return array List of files relative to the plugin root.
 181   */
 182  function get_plugin_files($plugin) {
 183      $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
 184      $dir = dirname($plugin_file);
 185      $plugin_files = array($plugin);
 186      if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
 187          $plugins_dir = @ opendir( $dir );
 188          if ( $plugins_dir ) {
 189              while (($file = readdir( $plugins_dir ) ) !== false ) {
 190                  if ( substr($file, 0, 1) == '.' )
 191                      continue;
 192                  if ( is_dir( $dir . '/' . $file ) ) {
 193                      $plugins_subdir = @ opendir( $dir . '/' . $file );
 194                      if ( $plugins_subdir ) {
 195                          while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
 196                              if ( substr($subfile, 0, 1) == '.' )
 197                                  continue;
 198                              $plugin_files[] = plugin_basename("$dir/$file/$subfile");
 199                          }
 200                          @closedir( $plugins_subdir );
 201                      }
 202                  } else {
 203                      if ( plugin_basename("$dir/$file") != $plugin )
 204                          $plugin_files[] = plugin_basename("$dir/$file");
 205                  }
 206              }
 207              @closedir( $plugins_dir );
 208          }
 209      }
 210  
 211      return $plugin_files;
 212  }
 213  
 214  /**
 215   * Check the plugins directory and retrieve all plugin files with plugin data.
 216   *
 217   * WordPress only supports plugin files in the base plugins directory
 218   * (wp-content/plugins) and in one directory above the plugins directory
 219   * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
 220   * must be found in those two locations. It is recommended that do keep your
 221   * plugin files in directories.
 222   *
 223   * The file with the plugin data is the file that will be included and therefore
 224   * needs to have the main execution for the plugin. This does not mean
 225   * everything must be contained in the file and it is recommended that the file
 226   * be split for maintainability. Keep everything in one file for extreme
 227   * optimization purposes.
 228   *
 229   * @since 1.5.0
 230   *
 231   * @param string $plugin_folder Optional. Relative path to single plugin folder.
 232   * @return array Key is the plugin file path and the value is an array of the plugin data.
 233   */
 234  function get_plugins($plugin_folder = '') {
 235  
 236      if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
 237          $cache_plugins = array();
 238  
 239      if ( isset($cache_plugins[ $plugin_folder ]) )
 240          return $cache_plugins[ $plugin_folder ];
 241  
 242      $wp_plugins = array ();
 243      $plugin_root = WP_PLUGIN_DIR;
 244      if ( !empty($plugin_folder) )
 245          $plugin_root .= $plugin_folder;
 246  
 247      // Files in wp-content/plugins directory
 248      $plugins_dir = @ opendir( $plugin_root);
 249      $plugin_files = array();
 250      if ( $plugins_dir ) {
 251          while (($file = readdir( $plugins_dir ) ) !== false ) {
 252              if ( substr($file, 0, 1) == '.' )
 253                  continue;
 254              if ( is_dir( $plugin_root.'/'.$file ) ) {
 255                  $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
 256                  if ( $plugins_subdir ) {
 257                      while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
 258                          if ( substr($subfile, 0, 1) == '.' )
 259                              continue;
 260                          if ( substr($subfile, -4) == '.php' )
 261                              $plugin_files[] = "$file/$subfile";
 262                      }
 263                      closedir( $plugins_subdir );
 264                  }
 265              } else {
 266                  if ( substr($file, -4) == '.php' )
 267                      $plugin_files[] = $file;
 268              }
 269          }
 270          closedir( $plugins_dir );
 271      }
 272  
 273      if ( empty($plugin_files) )
 274          return $wp_plugins;
 275  
 276      foreach ( $plugin_files as $plugin_file ) {
 277          if ( !is_readable( "$plugin_root/$plugin_file" ) )
 278              continue;
 279  
 280          $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
 281  
 282          if ( empty ( $plugin_data['Name'] ) )
 283              continue;
 284  
 285          $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
 286      }
 287  
 288      uasort( $wp_plugins, '_sort_uname_callback' );
 289  
 290      $cache_plugins[ $plugin_folder ] = $wp_plugins;
 291      wp_cache_set('plugins', $cache_plugins, 'plugins');
 292  
 293      return $wp_plugins;
 294  }
 295  
 296  /**
 297   * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
 298   *
 299   * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
 300   *
 301   * @since 3.0.0
 302   * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
 303   */
 304  function get_mu_plugins() {
 305      $wp_plugins = array();
 306      // Files in wp-content/mu-plugins directory
 307      $plugin_files = array();
 308  
 309      if ( ! is_dir( WPMU_PLUGIN_DIR ) )
 310          return $wp_plugins;
 311      if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
 312          while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
 313              if ( substr( $file, -4 ) == '.php' )
 314                  $plugin_files[] = $file;
 315          }
 316      } else {
 317          return $wp_plugins;
 318      }
 319  
 320      @closedir( $plugins_dir );
 321  
 322      if ( empty($plugin_files) )
 323          return $wp_plugins;
 324  
 325      foreach ( $plugin_files as $plugin_file ) {
 326          if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
 327              continue;
 328  
 329          $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
 330  
 331          if ( empty ( $plugin_data['Name'] ) )
 332              $plugin_data['Name'] = $plugin_file;
 333  
 334          $wp_plugins[ $plugin_file ] = $plugin_data;
 335      }
 336  
 337      if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
 338          unset( $wp_plugins['index.php'] );
 339  
 340      uasort( $wp_plugins, '_sort_uname_callback' );
 341  
 342      return $wp_plugins;
 343  }
 344  
 345  /**
 346   * Callback to sort array by a 'Name' key.
 347   *
 348   * @since 3.1.0
 349   * @access private
 350   */
 351  function _sort_uname_callback( $a, $b ) {
 352      return strnatcasecmp( $a['Name'], $b['Name'] );
 353  }
 354  
 355  /**
 356   * Check the wp-content directory and retrieve all drop-ins with any plugin data.
 357   *
 358   * @since 3.0.0
 359   * @return array Key is the file path and the value is an array of the plugin data.
 360   */
 361  function get_dropins() {
 362      $dropins = array();
 363      $plugin_files = array();
 364  
 365      $_dropins = _get_dropins();
 366  
 367      // These exist in the wp-content directory
 368      if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
 369          while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
 370              if ( isset( $_dropins[ $file ] ) )
 371                  $plugin_files[] = $file;
 372          }
 373      } else {
 374          return $dropins;
 375      }
 376  
 377      @closedir( $plugins_dir );
 378  
 379      if ( empty($plugin_files) )
 380          return $dropins;
 381  
 382      foreach ( $plugin_files as $plugin_file ) {
 383          if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
 384              continue;
 385          $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
 386          if ( empty( $plugin_data['Name'] ) )
 387              $plugin_data['Name'] = $plugin_file;
 388          $dropins[ $plugin_file ] = $plugin_data;
 389      }
 390  
 391      uksort( $dropins, 'strnatcasecmp' );
 392  
 393      return $dropins;
 394  }
 395  
 396  /**
 397   * Returns drop-ins that WordPress uses.
 398   *
 399   * Includes Multisite drop-ins only when is_multisite()
 400   *
 401   * @since 3.0.0
 402   * @return array Key is file name. The value is an array, with the first value the
 403   *    purpose of the drop-in and the second value the name of the constant that must be
 404   *    true for the drop-in to be used, or true if no constant is required.
 405   */
 406  function _get_dropins() {
 407      $dropins = array(
 408          'advanced-cache.php' => array( __( 'Advanced caching plugin.'       ), 'WP_CACHE' ), // WP_CACHE
 409          'db.php'             => array( __( 'Custom database class.'         ), true ), // auto on load
 410          'db-error.php'       => array( __( 'Custom database error message.' ), true ), // auto on error
 411          'install.php'        => array( __( 'Custom install script.'         ), true ), // auto on install
 412          'maintenance.php'    => array( __( 'Custom maintenance message.'    ), true ), // auto on maintenance
 413          'object-cache.php'   => array( __( 'External object cache.'         ), true ), // auto on load
 414      );
 415  
 416      if ( is_multisite() ) {
 417          $dropins['sunrise.php'       ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
 418          $dropins['blog-deleted.php'  ] = array( __( 'Custom site deleted message.'   ), true ); // auto on deleted blog
 419          $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.'  ), true ); // auto on inactive blog
 420          $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
 421      }
 422  
 423      return $dropins;
 424  }
 425  
 426  /**
 427   * Check whether the plugin is active by checking the active_plugins list.
 428   *
 429   * @since 2.5.0
 430   *
 431   * @param string $plugin Base plugin path from plugins directory.
 432   * @return bool True, if in the active plugins list. False, not in the list.
 433   */
 434  function is_plugin_active( $plugin ) {
 435      return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
 436  }
 437  
 438  /**
 439   * Check whether the plugin is inactive.
 440   *
 441   * Reverse of is_plugin_active(). Used as a callback.
 442   *
 443   * @since 3.1.0
 444   * @see is_plugin_active()
 445   *
 446   * @param string $plugin Base plugin path from plugins directory.
 447   * @return bool True if inactive. False if active.
 448   */
 449  function is_plugin_inactive( $plugin ) {
 450      return ! is_plugin_active( $plugin );
 451  }
 452  
 453  /**
 454   * Check whether the plugin is active for the entire network.
 455   *
 456   * @since 3.0.0
 457   *
 458   * @param string $plugin Base plugin path from plugins directory.
 459   * @return bool True, if active for the network, otherwise false.
 460   */
 461  function is_plugin_active_for_network( $plugin ) {
 462      if ( !is_multisite() )
 463          return false;
 464  
 465      $plugins = get_site_option( 'active_sitewide_plugins');
 466      if ( isset($plugins[$plugin]) )
 467          return true;
 468  
 469      return false;
 470  }
 471  
 472  /**
 473   * Checks for "Network: true" in the plugin header to see if this should
 474   * be activated only as a network wide plugin. The plugin would also work
 475   * when Multisite is not enabled.
 476   *
 477   * Checks for "Site Wide Only: true" for backwards compatibility.
 478   *
 479   * @since 3.0.0
 480   *
 481   * @param string $plugin Plugin to check
 482   * @return bool True if plugin is network only, false otherwise.
 483   */
 484  function is_network_only_plugin( $plugin ) {
 485      $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
 486      if ( $plugin_data )
 487          return $plugin_data['Network'];
 488      return false;
 489  }
 490  
 491  /**
 492   * Attempts activation of plugin in a "sandbox" and redirects on success.
 493   *
 494   * A plugin that is already activated will not attempt to be activated again.
 495   *
 496   * The way it works is by setting the redirection to the error before trying to
 497   * include the plugin file. If the plugin fails, then the redirection will not
 498   * be overwritten with the success message. Also, the options will not be
 499   * updated and the activation hook will not be called on plugin error.
 500   *
 501   * It should be noted that in no way the below code will actually prevent errors
 502   * within the file. The code should not be used elsewhere to replicate the
 503   * "sandbox", which uses redirection to work.
 504   * {@source 13 1}
 505   *
 506   * If any errors are found or text is outputted, then it will be captured to
 507   * ensure that the success redirection will update the error redirection.
 508   *
 509   * @since 2.5.0
 510   *
 511   * @param string $plugin Plugin path to main plugin file with plugin data.
 512   * @param string $redirect Optional. URL to redirect to.
 513   * @param bool $network_wide Whether to enable the plugin for all sites in the
 514   *   network or just the current site. Multisite only. Default is false.
 515   * @param bool $silent Prevent calling activation hooks. Optional, default is false.
 516   * @return WP_Error|null WP_Error on invalid file or null on success.
 517   */
 518  function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
 519      $plugin = plugin_basename( trim( $plugin ) );
 520  
 521      if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
 522          $network_wide = true;
 523          $current = get_site_option( 'active_sitewide_plugins', array() );
 524      } else {
 525          $current = get_option( 'active_plugins', array() );
 526      }
 527  
 528      $valid = validate_plugin($plugin);
 529      if ( is_wp_error($valid) )
 530          return $valid;
 531  
 532      if ( !in_array($plugin, $current) ) {
 533          if ( !empty($redirect) )
 534              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
 535          ob_start();
 536          include_once(WP_PLUGIN_DIR . '/' . $plugin);
 537  
 538          if ( ! $silent ) {
 539              do_action( 'activate_plugin', $plugin, $network_wide );
 540              do_action( 'activate_' . $plugin, $network_wide );
 541          }
 542  
 543          if ( $network_wide ) {
 544              $current[$plugin] = time();
 545              update_site_option( 'active_sitewide_plugins', $current );
 546          } else {
 547              $current[] = $plugin;
 548              sort($current);
 549              update_option('active_plugins', $current);
 550          }
 551  
 552          if ( ! $silent ) {
 553              do_action( 'activated_plugin', $plugin, $network_wide );
 554          }
 555  
 556          if ( ob_get_length() > 0 ) {
 557              $output = ob_get_clean();
 558              return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
 559          }
 560          ob_end_clean();
 561      }
 562  
 563      return null;
 564  }
 565  
 566  /**
 567   * Deactivate a single plugin or multiple plugins.
 568   *
 569   * The deactivation hook is disabled by the plugin upgrader by using the $silent
 570   * parameter.
 571   *
 572   * @since 2.5.0
 573   *
 574   * @param string|array $plugins Single plugin or list of plugins to deactivate.
 575   * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
 576   *     A value of null (the default) will deactivate plugins for both the site and the network.
 577   * @param bool $silent Prevent calling deactivation hooks. Default is false.
 578   */
 579  function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
 580      if ( is_multisite() )
 581          $network_current = get_site_option( 'active_sitewide_plugins', array() );
 582      $current = get_option( 'active_plugins', array() );
 583      $do_blog = $do_network = false;
 584  
 585      foreach ( (array) $plugins as $plugin ) {
 586          $plugin = plugin_basename( trim( $plugin ) );
 587          if ( ! is_plugin_active($plugin) )
 588              continue;
 589  
 590          $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
 591  
 592          if ( ! $silent )
 593              do_action( 'deactivate_plugin', $plugin, $network_deactivating );
 594  
 595          if ( false !== $network_wide ) {
 596              if ( is_plugin_active_for_network( $plugin ) ) {
 597                  $do_network = true;
 598                  unset( $network_current[ $plugin ] );
 599              } elseif ( $network_wide ) {
 600                  continue;
 601              }
 602          }
 603  
 604          if ( true !== $network_wide ) {
 605              $key = array_search( $plugin, $current );
 606              if ( false !== $key ) {
 607                  $do_blog = true;
 608                  array_splice( $current, $key, 1 );
 609              }
 610          }
 611  
 612          if ( ! $silent ) {
 613              do_action( 'deactivate_' . $plugin, $network_deactivating );
 614              do_action( 'deactivated_plugin', $plugin, $network_deactivating );
 615          }
 616      }
 617  
 618      if ( $do_blog )
 619          update_option('active_plugins', $current);
 620      if ( $do_network )
 621          update_site_option( 'active_sitewide_plugins', $network_current );
 622  }
 623  
 624  /**
 625   * Activate multiple plugins.
 626   *
 627   * When WP_Error is returned, it does not mean that one of the plugins had
 628   * errors. It means that one or more of the plugins file path was invalid.
 629   *
 630   * The execution will be halted as soon as one of the plugins has an error.
 631   *
 632   * @since 2.6.0
 633   *
 634   * @param string|array $plugins
 635   * @param string $redirect Redirect to page after successful activation.
 636   * @param bool $network_wide Whether to enable the plugin for all sites in the network.
 637   * @param bool $silent Prevent calling activation hooks. Default is false.
 638   * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
 639   */
 640  function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
 641      if ( !is_array($plugins) )
 642          $plugins = array($plugins);
 643  
 644      $errors = array();
 645      foreach ( $plugins as $plugin ) {
 646          if ( !empty($redirect) )
 647              $redirect = add_query_arg('plugin', $plugin, $redirect);
 648          $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
 649          if ( is_wp_error($result) )
 650              $errors[$plugin] = $result;
 651      }
 652  
 653      if ( !empty($errors) )
 654          return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
 655  
 656      return true;
 657  }
 658  
 659  /**
 660   * Remove directory and files of a plugin for a single or list of plugin(s).
 661   *
 662   * If the plugins parameter list is empty, false will be returned. True when
 663   * completed.
 664   *
 665   * @since 2.6.0
 666   *
 667   * @param array $plugins List of plugin
 668   * @param string $redirect Redirect to page when complete.
 669   * @return mixed
 670   */
 671  function delete_plugins($plugins, $redirect = '' ) {
 672      global $wp_filesystem;
 673  
 674      if ( empty($plugins) )
 675          return false;
 676  
 677      $checked = array();
 678      foreach( $plugins as $plugin )
 679          $checked[] = 'checked[]=' . $plugin;
 680  
 681      ob_start();
 682      $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
 683      if ( false === ($credentials = request_filesystem_credentials($url)) ) {
 684          $data = ob_get_contents();
 685          ob_end_clean();
 686          if ( ! empty($data) ){
 687              include_once ( ABSPATH . 'wp-admin/admin-header.php');
 688              echo $data;
 689              include ( ABSPATH . 'wp-admin/admin-footer.php');
 690              exit;
 691          }
 692          return;
 693      }
 694  
 695      if ( ! WP_Filesystem($credentials) ) {
 696          request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
 697          $data = ob_get_contents();
 698          ob_end_clean();
 699          if ( ! empty($data) ){
 700              include_once ( ABSPATH . 'wp-admin/admin-header.php');
 701              echo $data;
 702              include ( ABSPATH . 'wp-admin/admin-footer.php');
 703              exit;
 704          }
 705          return;
 706      }
 707  
 708      if ( ! is_object($wp_filesystem) )
 709          return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
 710  
 711      if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
 712          return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
 713  
 714      //Get the base plugin folder
 715      $plugins_dir = $wp_filesystem->wp_plugins_dir();
 716      if ( empty($plugins_dir) )
 717          return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
 718  
 719      $plugins_dir = trailingslashit( $plugins_dir );
 720  
 721      $errors = array();
 722  
 723      foreach( $plugins as $plugin_file ) {
 724          // Run Uninstall hook
 725          if ( is_uninstallable_plugin( $plugin_file ) )
 726              uninstall_plugin($plugin_file);
 727  
 728          $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
 729          // If plugin is in its own directory, recursively delete the directory.
 730          if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that its not the root plugin folder
 731              $deleted = $wp_filesystem->delete($this_plugin_dir, true);
 732          else
 733              $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
 734  
 735          if ( ! $deleted )
 736              $errors[] = $plugin_file;
 737      }
 738  
 739      if ( ! empty($errors) )
 740          return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
 741  
 742      // Force refresh of plugin update information
 743      if ( $current = get_site_transient('update_plugins') ) {
 744          unset( $current->response[ $plugin_file ] );
 745          set_site_transient('update_plugins', $current);
 746      }
 747  
 748      return true;
 749  }
 750  
 751  /**
 752   * Validate active plugins
 753   *
 754   * Validate all active plugins, deactivates invalid and
 755   * returns an array of deactivated ones.
 756   *
 757   * @since 2.5.0
 758   * @return array invalid plugins, plugin as key, error as value
 759   */
 760  function validate_active_plugins() {
 761      $plugins = get_option( 'active_plugins', array() );
 762      // validate vartype: array
 763      if ( ! is_array( $plugins ) ) {
 764          update_option( 'active_plugins', array() );
 765          $plugins = array();
 766      }
 767  
 768      if ( is_multisite() && is_super_admin() ) {
 769          $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
 770          $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
 771      }
 772  
 773      if ( empty( $plugins ) )
 774          return;
 775  
 776      $invalid = array();
 777  
 778      // invalid plugins get deactivated
 779      foreach ( $plugins as $plugin ) {
 780          $result = validate_plugin( $plugin );
 781          if ( is_wp_error( $result ) ) {
 782              $invalid[$plugin] = $result;
 783              deactivate_plugins( $plugin, true );
 784          }
 785      }
 786      return $invalid;
 787  }
 788  
 789  /**
 790   * Validate the plugin path.
 791   *
 792   * Checks that the file exists and {@link validate_file() is valid file}.
 793   *
 794   * @since 2.5.0
 795   *
 796   * @param string $plugin Plugin Path
 797   * @return WP_Error|int 0 on success, WP_Error on failure.
 798   */
 799  function validate_plugin($plugin) {
 800      if ( validate_file($plugin) )
 801          return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
 802      if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
 803          return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
 804  
 805      $installed_plugins = get_plugins();
 806      if ( ! isset($installed_plugins[$plugin]) )
 807          return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
 808      return 0;
 809  }
 810  
 811  /**
 812   * Whether the plugin can be uninstalled.
 813   *
 814   * @since 2.7.0
 815   *
 816   * @param string $plugin Plugin path to check.
 817   * @return bool Whether plugin can be uninstalled.
 818   */
 819  function is_uninstallable_plugin($plugin) {
 820      $file = plugin_basename($plugin);
 821  
 822      $uninstallable_plugins = (array) get_option('uninstall_plugins');
 823      if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
 824          return true;
 825  
 826      return false;
 827  }
 828  
 829  /**
 830   * Uninstall a single plugin.
 831   *
 832   * Calls the uninstall hook, if it is available.
 833   *
 834   * @since 2.7.0
 835   *
 836   * @param string $plugin Relative plugin path from Plugin Directory.
 837   */
 838  function uninstall_plugin($plugin) {
 839      $file = plugin_basename($plugin);
 840  
 841      $uninstallable_plugins = (array) get_option('uninstall_plugins');
 842      if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
 843          if ( isset( $uninstallable_plugins[$file] ) ) {
 844              unset($uninstallable_plugins[$file]);
 845              update_option('uninstall_plugins', $uninstallable_plugins);
 846          }
 847          unset($uninstallable_plugins);
 848  
 849          define('WP_UNINSTALL_PLUGIN', $file);
 850          include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
 851  
 852          return true;
 853      }
 854  
 855      if ( isset( $uninstallable_plugins[$file] ) ) {
 856          $callable = $uninstallable_plugins[$file];
 857          unset($uninstallable_plugins[$file]);
 858          update_option('uninstall_plugins', $uninstallable_plugins);
 859          unset($uninstallable_plugins);
 860  
 861          include WP_PLUGIN_DIR . '/' . $file;
 862  
 863          add_action( 'uninstall_' . $file, $callable );
 864          do_action( 'uninstall_' . $file );
 865      }
 866  }
 867  
 868  //
 869  // Menu
 870  //
 871  
 872  /**
 873   * Add a top level menu page
 874   *
 875   * This function takes a capability which will be used to determine whether
 876   * or not a page is included in the menu.
 877   *
 878   * The function which is hooked in to handle the output of the page must check
 879   * that the user has the required capability as well.
 880   *
 881   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
 882   * @param string $menu_title The text to be used for the menu
 883   * @param string $capability The capability required for this menu to be displayed to the user.
 884   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
 885   * @param callback $function The function to be called to output the content for this page.
 886   * @param string $icon_url The url to the icon to be used for this menu
 887   * @param int $position The position in the menu order this one should appear
 888   *
 889   * @return string The resulting page's hook_suffix
 890   */
 891  function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
 892      global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
 893  
 894      $menu_slug = plugin_basename( $menu_slug );
 895  
 896      $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
 897  
 898      $hookname = get_plugin_page_hookname( $menu_slug, '' );
 899  
 900      if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
 901          add_action( $hookname, $function );
 902  
 903      if ( empty($icon_url) )
 904          $icon_url = esc_url( admin_url( 'images/generic.png' ) );
 905      elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
 906          $icon_url = 'https://' . substr($icon_url, 7);
 907  
 908      $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
 909  
 910      if ( null === $position )
 911          $menu[] = $new_menu;
 912      else
 913          $menu[$position] = $new_menu;
 914  
 915      $_registered_pages[$hookname] = true;
 916  
 917      // No parent as top level
 918      $_parent_pages[$menu_slug] = false;
 919  
 920      return $hookname;
 921  }
 922  
 923  /**
 924   * Add a top level menu page in the 'objects' section
 925   *
 926   * This function takes a capability which will be used to determine whether
 927   * or not a page is included in the menu.
 928   *
 929   * The function which is hooked in to handle the output of the page must check
 930   * that the user has the required capability as well.
 931   *
 932   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
 933   * @param string $menu_title The text to be used for the menu
 934   * @param string $capability The capability required for this menu to be displayed to the user.
 935   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
 936   * @param callback $function The function to be called to output the content for this page.
 937   * @param string $icon_url The url to the icon to be used for this menu
 938   *
 939   * @return string The resulting page's hook_suffix
 940   */
 941  function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
 942      global $_wp_last_object_menu;
 943  
 944      $_wp_last_object_menu++;
 945  
 946      return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
 947  }
 948  
 949  /**
 950   * Add a top level menu page in the 'utility' section
 951   *
 952   * This function takes a capability which will be used to determine whether
 953   * or not a page is included in the menu.
 954   *
 955   * The function which is hooked in to handle the output of the page must check
 956   * that the user has the required capability as well.
 957   *
 958   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
 959   * @param string $menu_title The text to be used for the menu
 960   * @param string $capability The capability required for this menu to be displayed to the user.
 961   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
 962   * @param callback $function The function to be called to output the content for this page.
 963   * @param string $icon_url The url to the icon to be used for this menu
 964   *
 965   * @return string The resulting page's hook_suffix
 966   */
 967  function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
 968      global $_wp_last_utility_menu;
 969  
 970      $_wp_last_utility_menu++;
 971  
 972      return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
 973  }
 974  
 975  /**
 976   * Add a sub menu page
 977   *
 978   * This function takes a capability which will be used to determine whether
 979   * or not a page is included in the menu.
 980   *
 981   * The function which is hooked in to handle the output of the page must check
 982   * that the user has the required capability as well.
 983   *
 984   * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
 985   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
 986   * @param string $menu_title The text to be used for the menu
 987   * @param string $capability The capability required for this menu to be displayed to the user.
 988   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
 989   * @param callback $function The function to be called to output the content for this page.
 990   *
 991   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
 992   */
 993  function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
 994      global $submenu;
 995      global $menu;
 996      global $_wp_real_parent_file;
 997      global $_wp_submenu_nopriv;
 998      global $_registered_pages;
 999      global $_parent_pages;
1000  
1001      $menu_slug = plugin_basename( $menu_slug );
1002      $parent_slug = plugin_basename( $parent_slug);
1003  
1004      if ( isset( $_wp_real_parent_file[$parent_slug] ) )
1005          $parent_slug = $_wp_real_parent_file[$parent_slug];
1006  
1007      if ( !current_user_can( $capability ) ) {
1008          $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
1009          return false;
1010      }
1011  
1012      // If the parent doesn't already have a submenu, add a link to the parent
1013      // as the first item in the submenu. If the submenu file is the same as the
1014      // parent file someone is trying to link back to the parent manually. In
1015      // this case, don't automatically add a link back to avoid duplication.
1016      if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
1017          foreach ( (array)$menu as $parent_menu ) {
1018              if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
1019                  $submenu[$parent_slug][] = $parent_menu;
1020          }
1021      }
1022  
1023      $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1024  
1025      $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1026      if (!empty ( $function ) && !empty ( $hookname ))
1027          add_action( $hookname, $function );
1028  
1029      $_registered_pages[$hookname] = true;
1030      // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
1031      if ( 'tools.php' == $parent_slug )
1032          $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1033  
1034      // No parent as top level
1035      $_parent_pages[$menu_slug] = $parent_slug;
1036  
1037      return $hookname;
1038  }
1039  
1040  /**
1041   * Add sub menu page to the tools main menu.
1042   *
1043   * This function takes a capability which will be used to determine whether
1044   * or not a page is included in the menu.
1045   *
1046   * The function which is hooked in to handle the output of the page must check
1047   * that the user has the required capability as well.
1048   *
1049   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1050   * @param string $menu_title The text to be used for the menu
1051   * @param string $capability The capability required for this menu to be displayed to the user.
1052   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1053   * @param callback $function The function to be called to output the content for this page.
1054   *
1055   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1056   */
1057  function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1058      return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1059  }
1060  
1061  /**
1062   * Add sub menu page to the options main menu.
1063   *
1064   * This function takes a capability which will be used to determine whether
1065   * or not a page is included in the menu.
1066   *
1067   * The function which is hooked in to handle the output of the page must check
1068   * that the user has the required capability as well.
1069   *
1070   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1071   * @param string $menu_title The text to be used for the menu
1072   * @param string $capability The capability required for this menu to be displayed to the user.
1073   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1074   * @param callback $function The function to be called to output the content for this page.
1075   *
1076   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1077   */
1078  function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1079      return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1080  }
1081  
1082  /**
1083   * Add sub menu page to the themes main menu.
1084   *
1085   * This function takes a capability which will be used to determine whether
1086   * or not a page is included in the menu.
1087   *
1088   * The function which is hooked in to handle the output of the page must check
1089   * that the user has the required capability as well.
1090   *
1091   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1092   * @param string $menu_title The text to be used for the menu
1093   * @param string $capability The capability required for this menu to be displayed to the user.
1094   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1095   * @param callback $function The function to be called to output the content for this page.
1096   *
1097   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1098   */
1099  function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1100      return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1101  }
1102  
1103  /**
1104   * Add sub menu page to the plugins main menu.
1105   *
1106   * This function takes a capability which will be used to determine whether
1107   * or not a page is included in the menu.
1108   *
1109   * The function which is hooked in to handle the output of the page must check
1110   * that the user has the required capability as well.
1111   *
1112   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1113   * @param string $menu_title The text to be used for the menu
1114   * @param string $capability The capability required for this menu to be displayed to the user.
1115   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1116   * @param callback $function The function to be called to output the content for this page.
1117   *
1118   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1119   */
1120  function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1121      return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1122  }
1123  
1124  /**
1125   * Add sub menu page to the Users/Profile main menu.
1126   *
1127   * This function takes a capability which will be used to determine whether
1128   * or not a page is included in the menu.
1129   *
1130   * The function which is hooked in to handle the output of the page must check
1131   * that the user has the required capability as well.
1132   *
1133   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1134   * @param string $menu_title The text to be used for the menu
1135   * @param string $capability The capability required for this menu to be displayed to the user.
1136   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1137   * @param callback $function The function to be called to output the content for this page.
1138   *
1139   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1140   */
1141  function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1142      if ( current_user_can('edit_users') )
1143          $parent = 'users.php';
1144      else
1145          $parent = 'profile.php';
1146      return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1147  }
1148  /**
1149   * Add sub menu page to the Dashboard main menu.
1150   *
1151   * This function takes a capability which will be used to determine whether
1152   * or not a page is included in the menu.
1153   *
1154   * The function which is hooked in to handle the output of the page must check
1155   * that the user has the required capability as well.
1156   *
1157   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1158   * @param string $menu_title The text to be used for the menu
1159   * @param string $capability The capability required for this menu to be displayed to the user.
1160   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1161   * @param callback $function The function to be called to output the content for this page.
1162   *
1163   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1164   */
1165  function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1166      return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1167  }
1168  
1169  /**
1170   * Add sub menu page to the posts main menu.
1171   *
1172   * This function takes a capability which will be used to determine whether
1173   * or not a page is included in the menu.
1174   *
1175   * The function which is hooked in to handle the output of the page must check
1176   * that the user has the required capability as well.
1177   *
1178   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1179   * @param string $menu_title The text to be used for the menu
1180   * @param string $capability The capability required for this menu to be displayed to the user.
1181   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1182   * @param callback $function The function to be called to output the content for this page.
1183   *
1184   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1185   */
1186  function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1187      return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1188  }
1189  
1190  /**
1191   * Add sub menu page to the media main menu.
1192   *
1193   * This function takes a capability which will be used to determine whether
1194   * or not a page is included in the menu.
1195   *
1196   * The function which is hooked in to handle the output of the page must check
1197   * that the user has the required capability as well.
1198   *
1199   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1200   * @param string $menu_title The text to be used for the menu
1201   * @param string $capability The capability required for this menu to be displayed to the user.
1202   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1203   * @param callback $function The function to be called to output the content for this page.
1204   *
1205   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1206   */
1207  function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1208      return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1209  }
1210  
1211  /**
1212   * Add sub menu page to the links main menu.
1213   *
1214   * This function takes a capability which will be used to determine whether
1215   * or not a page is included in the menu.
1216   *
1217   * The function which is hooked in to handle the output of the page must check
1218   * that the user has the required capability as well.
1219   *
1220   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1221   * @param string $menu_title The text to be used for the menu
1222   * @param string $capability The capability required for this menu to be displayed to the user.
1223   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1224   * @param callback $function The function to be called to output the content for this page.
1225   *
1226   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1227   */
1228  function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1229      return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1230  }
1231  
1232  /**
1233   * Add sub menu page to the pages main menu.
1234   *
1235   * This function takes a capability which will be used to determine whether
1236   * or not a page is included in the menu.
1237   *
1238   * The function which is hooked in to handle the output of the page must check
1239   * that the user has the required capability as well.
1240   *
1241   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1242   * @param string $menu_title The text to be used for the menu
1243   * @param string $capability The capability required for this menu to be displayed to the user.
1244   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1245   * @param callback $function The function to be called to output the content for this page.
1246   *
1247   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1248  */
1249  function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1250      return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1251  }
1252  
1253  /**
1254   * Add sub menu page to the comments main menu.
1255   *
1256   * This function takes a capability which will be used to determine whether
1257   * or not a page is included in the menu.
1258   *
1259   * The function which is hooked in to handle the output of the page must check
1260   * that the user has the required capability as well.
1261   *
1262   * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1263   * @param string $menu_title The text to be used for the menu
1264   * @param string $capability The capability required for this menu to be displayed to the user.
1265   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1266   * @param callback $function The function to be called to output the content for this page.
1267   *
1268   * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1269  */
1270  function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1271      return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1272  }
1273  
1274  /**
1275   * Remove a top level admin menu
1276   *
1277   * @since 3.1.0
1278   *
1279   * @param string $menu_slug The slug of the menu
1280   * @return array|bool The removed menu on success, False if not found
1281   */
1282  function remove_menu_page( $menu_slug ) {
1283      global $menu;
1284  
1285      foreach ( $menu as $i => $item ) {
1286          if ( $menu_slug == $item[2] ) {
1287              unset( $menu[$i] );
1288              return $item;
1289          }
1290      }
1291  
1292      return false;
1293  }
1294  
1295  /**
1296   * Remove an admin submenu
1297   *
1298   * @since 3.1.0
1299   *
1300   * @param string $menu_slug The slug for the parent menu
1301   * @param string $submenu_slug The slug of the submenu
1302   * @return array|bool The removed submenu on success, False if not found
1303   */
1304  function remove_submenu_page( $menu_slug, $submenu_slug ) {
1305      global $submenu;
1306  
1307      if ( !isset( $submenu[$menu_slug] ) )
1308          return false;
1309  
1310      foreach ( $submenu[$menu_slug] as $i => $item ) {
1311          if ( $submenu_slug == $item[2] ) {
1312              unset( $submenu[$menu_slug][$i] );
1313              return $item;
1314          }
1315      }
1316  
1317      return false;
1318  }
1319  
1320  /**
1321   * Get the url to access a particular menu page based on the slug it was registered with.
1322   *
1323   * If the slug hasn't been registered properly no url will be returned
1324   *
1325   * @since 3.0
1326   *
1327   * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1328   * @param bool $echo Whether or not to echo the url - default is true
1329   * @return string the url
1330   */
1331  function menu_page_url($menu_slug, $echo = true) {
1332      global $_parent_pages;
1333  
1334      if ( isset( $_parent_pages[$menu_slug] ) ) {
1335          $parent_slug = $_parent_pages[$menu_slug];
1336          if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1337              $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1338          } else {
1339              $url = admin_url( 'admin.php?page=' . $menu_slug );
1340          }
1341      } else {
1342          $url = '';
1343      }
1344  
1345      $url = esc_url($url);
1346  
1347      if ( $echo )
1348          echo $url;
1349  
1350      return $url;
1351  }
1352  
1353  //
1354  // Pluggable Menu Support -- Private
1355  //
1356  
1357  function get_admin_page_parent( $parent = '' ) {
1358      global $parent_file;
1359      global $menu;
1360      global $submenu;
1361      global $pagenow;
1362      global $typenow;
1363      global $plugin_page;
1364      global $_wp_real_parent_file;
1365      global $_wp_menu_nopriv;
1366      global $_wp_submenu_nopriv;
1367  
1368      if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1369          if ( isset( $_wp_real_parent_file[$parent] ) )
1370              $parent = $_wp_real_parent_file[$parent];
1371          return $parent;
1372      }
1373  
1374      /*
1375      if ( !empty ( $parent_file ) ) {
1376          if ( isset( $_wp_real_parent_file[$parent_file] ) )
1377              $parent_file = $_wp_real_parent_file[$parent_file];
1378  
1379          return $parent_file;
1380      }
1381      */
1382  
1383      if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1384          foreach ( (array)$menu as $parent_menu ) {
1385              if ( $parent_menu[2] == $plugin_page ) {
1386                  $parent_file = $plugin_page;
1387                  if ( isset( $_wp_real_parent_file[$parent_file] ) )
1388                      $parent_file = $_wp_real_parent_file[$parent_file];
1389                  return $parent_file;
1390              }
1391          }
1392          if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1393              $parent_file = $plugin_page;
1394              if ( isset( $_wp_real_parent_file[$parent_file] ) )
1395                      $parent_file = $_wp_real_parent_file[$parent_file];
1396              return $parent_file;
1397          }
1398      }
1399  
1400      if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1401          $parent_file = $pagenow;
1402          if ( isset( $_wp_real_parent_file[$parent_file] ) )
1403              $parent_file = $_wp_real_parent_file[$parent_file];
1404          return $parent_file;
1405      }
1406  
1407      foreach (array_keys( (array)$submenu ) as $parent) {
1408          foreach ( $submenu[$parent] as $submenu_array ) {
1409              if ( isset( $_wp_real_parent_file[$parent] ) )
1410                  $parent = $_wp_real_parent_file[$parent];
1411              if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1412                  $parent_file = $parent;
1413                  return $parent;
1414              } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1415                  $parent_file = $parent;
1416                  return $parent;
1417              } else
1418                  if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1419                      $parent_file = $parent;
1420                      return $parent;
1421                  }
1422          }
1423      }
1424  
1425      if ( empty($parent_file) )
1426          $parent_file = '';
1427      return '';
1428  }
1429  
1430  function get_admin_page_title() {
1431      global $title;
1432      global $menu;
1433      global $submenu;
1434      global $pagenow;
1435      global $plugin_page;
1436      global $typenow;
1437  
1438      if ( ! empty ( $title ) )
1439          return $title;
1440  
1441      $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1442  
1443      $parent = $parent1 = get_admin_page_parent();
1444  
1445      if ( empty ( $parent) ) {
1446          foreach ( (array)$menu as $menu_array ) {
1447              if ( isset( $menu_array[3] ) ) {
1448                  if ( $menu_array[2] == $pagenow ) {
1449                      $title = $menu_array[3];
1450                      return $menu_array[3];
1451                  } else
1452                      if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1453                          $title = $menu_array[3];
1454                          return $menu_array[3];
1455                      }
1456              } else {
1457                  $title = $menu_array[0];
1458                  return $title;
1459              }
1460          }
1461      } else {
1462          foreach ( array_keys( $submenu ) as $parent ) {
1463              foreach ( $submenu[$parent] as $submenu_array ) {
1464                  if ( isset( $plugin_page ) &&
1465                      ( $plugin_page == $submenu_array[2] ) &&
1466                      (
1467                          ( $parent == $pagenow ) ||
1468                          ( $parent == $plugin_page ) ||
1469                          ( $plugin_page == $hook ) ||
1470                          ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1471                          ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1472                      )
1473                      ) {
1474                          $title = $submenu_array[3];
1475                          return $submenu_array[3];
1476                      }
1477  
1478                  if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1479                      continue;
1480  
1481                  if ( isset( $submenu_array[3] ) ) {
1482                      $title = $submenu_array[3];
1483                      return $submenu_array[3];
1484                  } else {
1485                      $title = $submenu_array[0];
1486                      return $title;
1487                  }
1488              }
1489          }
1490          if ( empty ( $title ) ) {
1491              foreach ( $menu as $menu_array ) {
1492                  if ( isset( $plugin_page ) &&
1493                      ( $plugin_page == $menu_array[2] ) &&
1494                      ( $pagenow == 'admin.php' ) &&
1495                      ( $parent1 == $menu_array[2] ) )
1496                      {
1497                          $title = $menu_array[3];
1498                          return $menu_array[3];
1499                      }
1500              }
1501          }
1502      }
1503  
1504      return $title;
1505  }
1506  
1507  function get_plugin_page_hook( $plugin_page, $parent_page ) {
1508      $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1509      if ( has_action($hook) )
1510          return $hook;
1511      else
1512          return null;
1513  }
1514  
1515  function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1516      global $admin_page_hooks;
1517  
1518      $parent = get_admin_page_parent( $parent_page );
1519  
1520      $page_type = 'admin';
1521      if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1522          if ( isset( $admin_page_hooks[$plugin_page] ) )
1523              $page_type = 'toplevel';
1524          else
1525              if ( isset( $admin_page_hooks[$parent] ))
1526                  $page_type = $admin_page_hooks[$parent];
1527      } else if ( isset( $admin_page_hooks[$parent] ) ) {
1528          $page_type = $admin_page_hooks[$parent];
1529      }
1530  
1531      $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1532  
1533      return $page_type . '_page_' . $plugin_name;
1534  }
1535  
1536  function user_can_access_admin_page() {
1537      global $pagenow;
1538      global $menu;
1539      global $submenu;
1540      global $_wp_menu_nopriv;
1541      global $_wp_submenu_nopriv;
1542      global $plugin_page;
1543      global $_registered_pages;
1544  
1545      $parent = get_admin_page_parent();
1546  
1547      if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1548          return false;
1549  
1550      if ( isset( $plugin_page ) ) {
1551          if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1552              return false;
1553  
1554          $hookname = get_plugin_page_hookname($plugin_page, $parent);
1555  
1556          if ( !isset($_registered_pages[$hookname]) )
1557              return false;
1558      }
1559  
1560      if ( empty( $parent) ) {
1561          if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1562              return false;
1563          if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1564              return false;
1565          if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1566              return false;
1567          if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1568              return false;
1569          foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1570              if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1571                  return false;
1572              if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1573              return false;
1574          }
1575          return true;
1576      }
1577  
1578      if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1579          return false;
1580  
1581      if ( isset( $submenu[$parent] ) ) {
1582          foreach ( $submenu[$parent] as $submenu_array ) {
1583              if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1584                  if ( current_user_can( $submenu_array[1] ))
1585                      return true;
1586                  else
1587                      return false;
1588              } else if ( $submenu_array[2] == $pagenow ) {
1589                  if ( current_user_can( $submenu_array[1] ))
1590                      return true;
1591                  else
1592                      return false;
1593              }
1594          }
1595      }
1596  
1597      foreach ( $menu as $menu_array ) {
1598          if ( $menu_array[2] == $parent) {
1599              if ( current_user_can( $menu_array[1] ))
1600                  return true;
1601              else
1602                  return false;
1603          }
1604      }
1605  
1606      return true;
1607  }
1608  
1609  /* Whitelist functions */
1610  
1611  /**
1612   * Register a setting and its sanitization callback
1613   *
1614   * @since 2.7.0
1615   *
1616   * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1617   *     Default whitelisted option key names include "general," "discussion," and "reading," among others.
1618   * @param string $option_name The name of an option to sanitize and save.
1619   * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
1620   * @return unknown
1621   */
1622  function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1623      global $new_whitelist_options;
1624  
1625      if ( 'misc' == $option_group ) {
1626          _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1627          $option_group = 'general';
1628      }
1629  
1630      $new_whitelist_options[ $option_group ][] = $option_name;
1631      if ( $sanitize_callback != '' )
1632          add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1633  }
1634  
1635  /**
1636   * Unregister a setting
1637   *
1638   * @since 2.7.0
1639   *
1640   * @param unknown_type $option_group
1641   * @param unknown_type $option_name
1642   * @param unknown_type $sanitize_callback
1643   * @return unknown
1644   */
1645  function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1646      global $new_whitelist_options;
1647  
1648      if ( 'misc' == $option_group ) {
1649          _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1650          $option_group = 'general';
1651      }
1652  
1653      $pos = array_search( $option_name, (array) $new_whitelist_options );
1654      if ( $pos !== false )
1655          unset( $new_whitelist_options[ $option_group ][ $pos ] );
1656      if ( $sanitize_callback != '' )
1657          remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1658  }
1659  
1660  /**
1661   * {@internal Missing Short Description}}
1662   *
1663   * @since 2.7.0
1664   *
1665   * @param unknown_type $options
1666   * @return unknown
1667   */
1668  function option_update_filter( $options ) {
1669      global $new_whitelist_options;
1670  
1671      if ( is_array( $new_whitelist_options ) )
1672          $options = add_option_whitelist( $new_whitelist_options, $options );
1673  
1674      return $options;
1675  }
1676  add_filter( 'whitelist_options', 'option_update_filter' );
1677  
1678  /**
1679   * {@internal Missing Short Description}}
1680   *
1681   * @since 2.7.0
1682   *
1683   * @param unknown_type $new_options
1684   * @param unknown_type $options
1685   * @return unknown
1686   */
1687  function add_option_whitelist( $new_options, $options = '' ) {
1688      if ( $options == '' )
1689          global $whitelist_options;
1690      else
1691          $whitelist_options = $options;
1692  
1693      foreach ( $new_options as $page => $keys ) {
1694          foreach ( $keys as $key ) {
1695              if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1696                  $whitelist_options[ $page ] = array();
1697                  $whitelist_options[ $page ][] = $key;
1698              } else {
1699                  $pos = array_search( $key, $whitelist_options[ $page ] );
1700                  if ( $pos === false )
1701                      $whitelist_options[ $page ][] = $key;
1702              }
1703          }
1704      }
1705  
1706      return $whitelist_options;
1707  }
1708  
1709  /**
1710   * {@internal Missing Short Description}}
1711   *
1712   * @since 2.7.0
1713   *
1714   * @param unknown_type $del_options
1715   * @param unknown_type $options
1716   * @return unknown
1717   */
1718  function remove_option_whitelist( $del_options, $options = '' ) {
1719      if ( $options == '' )
1720          global $whitelist_options;
1721      else
1722          $whitelist_options = $options;
1723  
1724      foreach ( $del_options as $page => $keys ) {
1725          foreach ( $keys as $key ) {
1726              if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1727                  $pos = array_search( $key, $whitelist_options[ $page ] );
1728                  if ( $pos !== false )
1729                      unset( $whitelist_options[ $page ][ $pos ] );
1730              }
1731          }
1732      }
1733  
1734      return $whitelist_options;
1735  }
1736  
1737  /**
1738   * Output nonce, action, and option_page fields for a settings page.
1739   *
1740   * @since 2.7.0
1741   *
1742   * @param string $option_group A settings group name. This should match the group name used in register_setting().
1743   */
1744  function settings_fields($option_group) {
1745      echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1746      echo '<input type="hidden" name="action" value="update" />';
1747      wp_nonce_field("$option_group-options");
1748  }


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.