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