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