3 * WordPress Plugin Administration API
6 * @subpackage Administration
10 * Parse the plugin contents to retrieve plugin's metadata.
12 * The metadata of the plugin's data searches for the following in the plugin's
13 * header. All plugin data must be on its own line. For plugin description, it
14 * must not have any newlines or only parts of the description will be displayed
15 * and the same goes for the plugin data. The below is formatted for printing.
19 * Plugin Name: Name of Plugin
20 * Plugin URI: Link to plugin information
21 * Description: Plugin Description
22 * Author: Plugin author's name
23 * Author URI: Link to the author's web site
24 * Version: Must be set in the plugin for WordPress 2.3+
25 * Text Domain: Optional. Unique identifier, should be same as the one used in
26 * plugin_text_domain()
27 * Domain Path: Optional. Only useful if the translations are located in a
28 * folder above the plugin's base path. For example, if .mo files are
29 * located in the locale folder then Domain Path will be "/locale/" and
30 * must have the first slash. Defaults to the base folder the plugin is
32 * Network: Optional. Specify "Network: true" to require that a plugin is activated
33 * across all sites in an installation. This will prevent a plugin from being
34 * activated on a single site when Multisite is enabled.
35 * * / # Remove the space to close comment
38 * Plugin data returned array contains the following:
39 * 'Name' - Name of the plugin, must be unique.
40 * 'Title' - Title of the plugin and the link to the plugin's web site.
41 * 'Description' - Description of what the plugin does and/or notes
43 * 'Author' - The author's name
44 * 'AuthorURI' - The authors web site address.
45 * 'Version' - The plugin version number.
46 * 'PluginURI' - Plugin web site address.
47 * 'TextDomain' - Plugin's text domain for localization.
48 * 'DomainPath' - Plugin's relative directory path to .mo files.
49 * 'Network' - Boolean. Whether the plugin can only be activated network wide.
51 * Some users have issues with opening large files and manipulating the contents
52 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
53 * the plugin contents when it has all of the required plugin data.
55 * The first 8kiB of the file will be pulled in and if the plugin data is not
56 * within that first 8kiB, then the plugin author should correct their plugin
57 * and move the plugin data headers to the top.
59 * The plugin file is assumed to have permissions to allow for scripts to read
60 * the file. This is not checked however and the file is only opened for
63 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
64 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
67 * @param string $plugin_file Path to the plugin file
68 * @param bool $markup Optional. If the returned data should have HTML markup applied. Defaults to true.
69 * @param bool $translate Optional. If the returned data should be translated. Defaults to true.
70 * @return array See above for description.
72 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
74 $default_headers = array(
75 'Name' => 'Plugin Name',
76 'PluginURI' => 'Plugin URI',
77 'Version' => 'Version',
78 'Description' => 'Description',
80 'AuthorURI' => 'Author URI',
81 'TextDomain' => 'Text Domain',
82 'DomainPath' => 'Domain Path',
83 'Network' => 'Network',
84 // Site Wide Only is deprecated in favor of Network.
85 '_sitewide' => 'Site Wide Only',
88 $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
90 // Site Wide Only is the old header for Network
91 if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
92 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The <code>%1$s</code> plugin header is deprecated. Use <code>%2$s</code> instead.' ), 'Site Wide Only: true', 'Network: true' ) );
93 $plugin_data['Network'] = $plugin_data['_sitewide'];
95 $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
96 unset( $plugin_data['_sitewide'] );
98 if ( $markup || $translate ) {
99 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
101 $plugin_data['Title'] = $plugin_data['Name'];
102 $plugin_data['AuthorName'] = $plugin_data['Author'];
109 * Sanitizes plugin data, optionally adds markup, optionally translates.
113 * @see get_plugin_data()
115 function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
117 // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path
118 $plugin_file = plugin_basename( $plugin_file );
122 if ( $textdomain = $plugin_data['TextDomain'] ) {
123 if ( $plugin_data['DomainPath'] )
124 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
126 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
127 } elseif ( in_array( basename( $plugin_file ), array( 'hello.php', 'akismet.php' ) ) ) {
128 $textdomain = 'default';
131 foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
132 $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
137 $allowed_tags = $allowed_tags_in_links = array(
138 'abbr' => array( 'title' => true ),
139 'acronym' => array( 'title' => true ),
144 $allowed_tags['a'] = array( 'href' => true, 'title' => true );
146 // Name is marked up inside <a> tags. Don't allow these.
147 // Author is too, but some plugins have used <a> here (omitting Author URI).
148 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
149 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
151 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
152 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
154 $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
155 $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
157 $plugin_data['Title'] = $plugin_data['Name'];
158 $plugin_data['AuthorName'] = $plugin_data['Author'];
162 if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
163 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
165 if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
166 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
168 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
170 if ( $plugin_data['Author'] )
171 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
178 * Get a list of a plugin's files.
182 * @param string $plugin Plugin ID
183 * @return array List of files relative to the plugin root.
185 function get_plugin_files($plugin) {
186 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
187 $dir = dirname($plugin_file);
188 $plugin_files = array($plugin);
189 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
190 $plugins_dir = @ opendir( $dir );
191 if ( $plugins_dir ) {
192 while (($file = readdir( $plugins_dir ) ) !== false ) {
193 if ( substr($file, 0, 1) == '.' )
195 if ( is_dir( $dir . '/' . $file ) ) {
196 $plugins_subdir = @ opendir( $dir . '/' . $file );
197 if ( $plugins_subdir ) {
198 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
199 if ( substr($subfile, 0, 1) == '.' )
201 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
203 @closedir( $plugins_subdir );
206 if ( plugin_basename("$dir/$file") != $plugin )
207 $plugin_files[] = plugin_basename("$dir/$file");
210 @closedir( $plugins_dir );
214 return $plugin_files;
218 * Check the plugins directory and retrieve all plugin files with plugin data.
220 * WordPress only supports plugin files in the base plugins directory
221 * (wp-content/plugins) and in one directory above the plugins directory
222 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
223 * must be found in those two locations. It is recommended that do keep your
224 * plugin files in directories.
226 * The file with the plugin data is the file that will be included and therefore
227 * needs to have the main execution for the plugin. This does not mean
228 * everything must be contained in the file and it is recommended that the file
229 * be split for maintainability. Keep everything in one file for extreme
230 * optimization purposes.
234 * @param string $plugin_folder Optional. Relative path to single plugin folder.
235 * @return array Key is the plugin file path and the value is an array of the plugin data.
237 function get_plugins($plugin_folder = '') {
239 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
240 $cache_plugins = array();
242 if ( isset($cache_plugins[ $plugin_folder ]) )
243 return $cache_plugins[ $plugin_folder ];
245 $wp_plugins = array ();
246 $plugin_root = WP_PLUGIN_DIR;
247 if ( !empty($plugin_folder) )
248 $plugin_root .= $plugin_folder;
250 // Files in wp-content/plugins directory
251 $plugins_dir = @ opendir( $plugin_root);
252 $plugin_files = array();
253 if ( $plugins_dir ) {
254 while (($file = readdir( $plugins_dir ) ) !== false ) {
255 if ( substr($file, 0, 1) == '.' )
257 if ( is_dir( $plugin_root.'/'.$file ) ) {
258 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
259 if ( $plugins_subdir ) {
260 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
261 if ( substr($subfile, 0, 1) == '.' )
263 if ( substr($subfile, -4) == '.php' )
264 $plugin_files[] = "$file/$subfile";
266 closedir( $plugins_subdir );
269 if ( substr($file, -4) == '.php' )
270 $plugin_files[] = $file;
273 closedir( $plugins_dir );
276 if ( empty($plugin_files) )
279 foreach ( $plugin_files as $plugin_file ) {
280 if ( !is_readable( "$plugin_root/$plugin_file" ) )
283 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
285 if ( empty ( $plugin_data['Name'] ) )
288 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
291 uasort( $wp_plugins, '_sort_uname_callback' );
293 $cache_plugins[ $plugin_folder ] = $wp_plugins;
294 wp_cache_set('plugins', $cache_plugins, 'plugins');
300 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
302 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
305 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
307 function get_mu_plugins() {
308 $wp_plugins = array();
309 // Files in wp-content/mu-plugins directory
310 $plugin_files = array();
312 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
314 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
315 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
316 if ( substr( $file, -4 ) == '.php' )
317 $plugin_files[] = $file;
323 @closedir( $plugins_dir );
325 if ( empty($plugin_files) )
328 foreach ( $plugin_files as $plugin_file ) {
329 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
332 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
334 if ( empty ( $plugin_data['Name'] ) )
335 $plugin_data['Name'] = $plugin_file;
337 $wp_plugins[ $plugin_file ] = $plugin_data;
340 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
341 unset( $wp_plugins['index.php'] );
343 uasort( $wp_plugins, '_sort_uname_callback' );
349 * Callback to sort array by a 'Name' key.
354 function _sort_uname_callback( $a, $b ) {
355 return strnatcasecmp( $a['Name'], $b['Name'] );
359 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
362 * @return array Key is the file path and the value is an array of the plugin data.
364 function get_dropins() {
366 $plugin_files = array();
368 $_dropins = _get_dropins();
370 // These exist in the wp-content directory
371 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
372 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
373 if ( isset( $_dropins[ $file ] ) )
374 $plugin_files[] = $file;
380 @closedir( $plugins_dir );
382 if ( empty($plugin_files) )
385 foreach ( $plugin_files as $plugin_file ) {
386 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
388 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
389 if ( empty( $plugin_data['Name'] ) )
390 $plugin_data['Name'] = $plugin_file;
391 $dropins[ $plugin_file ] = $plugin_data;
394 uksort( $dropins, 'strnatcasecmp' );
400 * Returns drop-ins that WordPress uses.
402 * Includes Multisite drop-ins only when is_multisite()
405 * @return array Key is file name. The value is an array, with the first value the
406 * purpose of the drop-in and the second value the name of the constant that must be
407 * true for the drop-in to be used, or true if no constant is required.
409 function _get_dropins() {
411 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
412 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
413 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
414 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
415 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
416 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
419 if ( is_multisite() ) {
420 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
421 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
422 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
423 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
430 * Check whether the plugin is active by checking the active_plugins list.
434 * @param string $plugin Base plugin path from plugins directory.
435 * @return bool True, if in the active plugins list. False, not in the list.
437 function is_plugin_active( $plugin ) {
438 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
442 * Check whether the plugin is inactive.
444 * Reverse of is_plugin_active(). Used as a callback.
447 * @see is_plugin_active()
449 * @param string $plugin Base plugin path from plugins directory.
450 * @return bool True if inactive. False if active.
452 function is_plugin_inactive( $plugin ) {
453 return ! is_plugin_active( $plugin );
457 * Check whether the plugin is active for the entire network.
461 * @param string $plugin Base plugin path from plugins directory.
462 * @return bool True, if active for the network, otherwise false.
464 function is_plugin_active_for_network( $plugin ) {
465 if ( !is_multisite() )
468 $plugins = get_site_option( 'active_sitewide_plugins');
469 if ( isset($plugins[$plugin]) )
476 * Checks for "Network: true" in the plugin header to see if this should
477 * be activated only as a network wide plugin. The plugin would also work
478 * when Multisite is not enabled.
480 * Checks for "Site Wide Only: true" for backwards compatibility.
484 * @param string $plugin Plugin to check
485 * @return bool True if plugin is network only, false otherwise.
487 function is_network_only_plugin( $plugin ) {
488 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
490 return $plugin_data['Network'];
495 * Attempts activation of plugin in a "sandbox" and redirects on success.
497 * A plugin that is already activated will not attempt to be activated again.
499 * The way it works is by setting the redirection to the error before trying to
500 * include the plugin file. If the plugin fails, then the redirection will not
501 * be overwritten with the success message. Also, the options will not be
502 * updated and the activation hook will not be called on plugin error.
504 * It should be noted that in no way the below code will actually prevent errors
505 * within the file. The code should not be used elsewhere to replicate the
506 * "sandbox", which uses redirection to work.
509 * If any errors are found or text is outputted, then it will be captured to
510 * ensure that the success redirection will update the error redirection.
514 * @param string $plugin Plugin path to main plugin file with plugin data.
515 * @param string $redirect Optional. URL to redirect to.
516 * @param bool $network_wide Whether to enable the plugin for all sites in the
517 * network or just the current site. Multisite only. Default is false.
518 * @param bool $silent Prevent calling activation hooks. Optional, default is false.
519 * @return WP_Error|null WP_Error on invalid file or null on success.
521 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
522 $plugin = plugin_basename( trim( $plugin ) );
524 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
525 $network_wide = true;
526 $current = get_site_option( 'active_sitewide_plugins', array() );
527 $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
529 $current = get_option( 'active_plugins', array() );
532 $valid = validate_plugin($plugin);
533 if ( is_wp_error($valid) )
536 if ( !in_array($plugin, $current) ) {
537 if ( !empty($redirect) )
538 wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
540 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
541 include_once( WP_PLUGIN_DIR . '/' . $plugin );
545 * Fires before a plugin is activated.
547 * If a plugin is silently activated (such as during an update),
548 * this hook does not fire.
552 * @param string $plugin Plugin path to main plugin file with plugin data.
553 * @param bool $network_wide Whether to enable the plugin for all sites in the network
554 * or just the current site. Multisite only. Default is false.
556 do_action( 'activate_plugin', $plugin, $network_wide );
559 * Fires as a specific plugin is being deactivated.
561 * This hook is the "deactivation" hook used internally by
562 * register_deactivation_hook(). The dynamic portion of the
563 * hook name, $plugin. refers to the plugin basename.
565 * If a plugin is silently activated (such as during an update),
566 * this hook does not fire.
570 * @param bool $network_wide Whether to enable the plugin for all sites in the network
571 * or just the current site. Multisite only. Default is false.
573 do_action( 'activate_' . $plugin, $network_wide );
576 if ( $network_wide ) {
577 $current[$plugin] = time();
578 update_site_option( 'active_sitewide_plugins', $current );
580 $current[] = $plugin;
582 update_option('active_plugins', $current);
587 * Fires after a plugin has been activated.
589 * If a plugin is silently activated (such as during an update),
590 * this hook does not fire.
594 * @param string $plugin Plugin path to main plugin file with plugin data.
595 * @param bool $network_wide Whether to enable the plugin for all sites in the network
596 * or just the current site. Multisite only. Default is false.
598 do_action( 'activated_plugin', $plugin, $network_wide );
601 if ( ob_get_length() > 0 ) {
602 $output = ob_get_clean();
603 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
612 * Deactivate a single plugin or multiple plugins.
614 * The deactivation hook is disabled by the plugin upgrader by using the $silent
619 * @param string|array $plugins Single plugin or list of plugins to deactivate.
620 * @param bool $silent Prevent calling deactivation hooks. Default is false.
621 * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
622 * A value of null (the default) will deactivate plugins for both the site and the network.
624 function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
625 if ( is_multisite() )
626 $network_current = get_site_option( 'active_sitewide_plugins', array() );
627 $current = get_option( 'active_plugins', array() );
628 $do_blog = $do_network = false;
630 foreach ( (array) $plugins as $plugin ) {
631 $plugin = plugin_basename( trim( $plugin ) );
632 if ( ! is_plugin_active($plugin) )
635 $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
639 * Fires before a plugin is deactivated.
641 * If a plugin is silently deactivated (such as during an update),
642 * this hook does not fire.
646 * @param string $plugin Plugin path to main plugin file with plugin data.
647 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
648 * or just the current site. Multisite only. Default is false.
650 do_action( 'deactivate_plugin', $plugin, $network_deactivating );
653 if ( false !== $network_wide ) {
654 if ( is_plugin_active_for_network( $plugin ) ) {
656 unset( $network_current[ $plugin ] );
657 } elseif ( $network_wide ) {
662 if ( true !== $network_wide ) {
663 $key = array_search( $plugin, $current );
664 if ( false !== $key ) {
666 unset( $current[ $key ] );
672 * Fires as a specific plugin is being deactivated.
674 * This hook is the "deactivation" hook used internally by
675 * register_deactivation_hook(). The dynamic portion of the
676 * hook name, $plugin. refers to the plugin basename.
678 * If a plugin is silently deactivated (such as during an update),
679 * this hook does not fire.
683 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
684 * or just the current site. Multisite only. Default is false.
686 do_action( 'deactivate_' . $plugin, $network_deactivating );
689 * Fires after a plugin is deactivated.
691 * If a plugin is silently deactivated (such as during an update),
692 * this hook does not fire.
696 * @param string $plugin Plugin basename.
697 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
698 * or just the current site. Multisite only. Default false.
700 do_action( 'deactivated_plugin', $plugin, $network_deactivating );
705 update_option('active_plugins', $current);
707 update_site_option( 'active_sitewide_plugins', $network_current );
711 * Activate multiple plugins.
713 * When WP_Error is returned, it does not mean that one of the plugins had
714 * errors. It means that one or more of the plugins file path was invalid.
716 * The execution will be halted as soon as one of the plugins has an error.
720 * @param string|array $plugins
721 * @param string $redirect Redirect to page after successful activation.
722 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
723 * @param bool $silent Prevent calling activation hooks. Default is false.
724 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
726 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
727 if ( !is_array($plugins) )
728 $plugins = array($plugins);
731 foreach ( $plugins as $plugin ) {
732 if ( !empty($redirect) )
733 $redirect = add_query_arg('plugin', $plugin, $redirect);
734 $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
735 if ( is_wp_error($result) )
736 $errors[$plugin] = $result;
739 if ( !empty($errors) )
740 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
746 * Remove directory and files of a plugin for a single or list of plugin(s).
748 * If the plugins parameter list is empty, false will be returned. True when
753 * @param array $plugins List of plugin
754 * @param string $redirect Redirect to page when complete.
757 function delete_plugins($plugins, $redirect = '' ) {
758 global $wp_filesystem;
760 if ( empty($plugins) )
764 foreach( $plugins as $plugin )
765 $checked[] = 'checked[]=' . $plugin;
768 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
769 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
770 $data = ob_get_contents();
772 if ( ! empty($data) ){
773 include_once( ABSPATH . 'wp-admin/admin-header.php');
775 include( ABSPATH . 'wp-admin/admin-footer.php');
781 if ( ! WP_Filesystem($credentials) ) {
782 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
783 $data = ob_get_contents();
785 if ( ! empty($data) ){
786 include_once( ABSPATH . 'wp-admin/admin-header.php');
788 include( ABSPATH . 'wp-admin/admin-footer.php');
794 if ( ! is_object($wp_filesystem) )
795 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
797 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
798 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
800 //Get the base plugin folder
801 $plugins_dir = $wp_filesystem->wp_plugins_dir();
802 if ( empty($plugins_dir) )
803 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
805 $plugins_dir = trailingslashit( $plugins_dir );
809 foreach( $plugins as $plugin_file ) {
810 // Run Uninstall hook
811 if ( is_uninstallable_plugin( $plugin_file ) )
812 uninstall_plugin($plugin_file);
814 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
815 // If plugin is in its own directory, recursively delete the directory.
816 if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder
817 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
819 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
822 $errors[] = $plugin_file;
825 // Remove deleted plugins from the plugin updates list.
826 if ( $current = get_site_transient('update_plugins') ) {
827 // Don't remove the plugins that weren't deleted.
828 $deleted = array_diff( $plugins, $errors );
830 foreach ( $deleted as $plugin_file ) {
831 unset( $current->response[ $plugin_file ] );
834 set_site_transient( 'update_plugins', $current );
837 if ( ! empty($errors) )
838 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
844 * Validate active plugins
846 * Validate all active plugins, deactivates invalid and
847 * returns an array of deactivated ones.
850 * @return array invalid plugins, plugin as key, error as value
852 function validate_active_plugins() {
853 $plugins = get_option( 'active_plugins', array() );
854 // validate vartype: array
855 if ( ! is_array( $plugins ) ) {
856 update_option( 'active_plugins', array() );
860 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
861 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
862 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
865 if ( empty( $plugins ) )
870 // invalid plugins get deactivated
871 foreach ( $plugins as $plugin ) {
872 $result = validate_plugin( $plugin );
873 if ( is_wp_error( $result ) ) {
874 $invalid[$plugin] = $result;
875 deactivate_plugins( $plugin, true );
882 * Validate the plugin path.
884 * Checks that the file exists and {@link validate_file() is valid file}.
888 * @param string $plugin Plugin Path
889 * @return WP_Error|int 0 on success, WP_Error on failure.
891 function validate_plugin($plugin) {
892 if ( validate_file($plugin) )
893 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
894 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
895 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
897 $installed_plugins = get_plugins();
898 if ( ! isset($installed_plugins[$plugin]) )
899 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
904 * Whether the plugin can be uninstalled.
908 * @param string $plugin Plugin path to check.
909 * @return bool Whether plugin can be uninstalled.
911 function is_uninstallable_plugin($plugin) {
912 $file = plugin_basename($plugin);
914 $uninstallable_plugins = (array) get_option('uninstall_plugins');
915 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
922 * Uninstall a single plugin.
924 * Calls the uninstall hook, if it is available.
928 * @param string $plugin Relative plugin path from Plugin Directory.
930 function uninstall_plugin($plugin) {
931 $file = plugin_basename($plugin);
933 $uninstallable_plugins = (array) get_option('uninstall_plugins');
934 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
935 if ( isset( $uninstallable_plugins[$file] ) ) {
936 unset($uninstallable_plugins[$file]);
937 update_option('uninstall_plugins', $uninstallable_plugins);
939 unset($uninstallable_plugins);
941 define('WP_UNINSTALL_PLUGIN', $file);
942 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . dirname( $file ) );
943 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
948 if ( isset( $uninstallable_plugins[$file] ) ) {
949 $callable = $uninstallable_plugins[$file];
950 unset($uninstallable_plugins[$file]);
951 update_option('uninstall_plugins', $uninstallable_plugins);
952 unset($uninstallable_plugins);
954 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
955 include WP_PLUGIN_DIR . '/' . $file;
957 add_action( 'uninstall_' . $file, $callable );
960 * Fires in uninstall_plugin() once the plugin has been uninstalled.
962 * The action concatenates the 'uninstall_' prefix with the basename of the
963 * plugin passed to {@see uninstall_plugin()} to create a dynamically-named action.
967 do_action( 'uninstall_' . $file );
976 * Add a top level menu page
978 * This function takes a capability which will be used to determine whether
979 * or not a page is included in the menu.
981 * The function which is hooked in to handle the output of the page must check
982 * that the user has the required capability as well.
984 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
985 * @param string $menu_title The text to be used for the menu
986 * @param string $capability The capability required for this menu to be displayed to the user.
987 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
988 * @param callback $function The function to be called to output the content for this page.
989 * @param string $icon_url The url to the icon to be used for this menu.
990 * * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
991 * This should begin with 'data:image/svg+xml;base64,'.
992 * * Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'.
993 * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
994 * @param int $position The position in the menu order this one should appear
996 * @return string The resulting page's hook_suffix
998 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
999 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
1001 $menu_slug = plugin_basename( $menu_slug );
1003 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
1005 $hookname = get_plugin_page_hookname( $menu_slug, '' );
1007 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
1008 add_action( $hookname, $function );
1010 if ( empty($icon_url) ) {
1011 $icon_url = 'dashicons-admin-generic';
1012 $icon_class = 'menu-icon-generic ';
1014 $icon_url = set_url_scheme( $icon_url );
1018 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
1020 if ( null === $position )
1021 $menu[] = $new_menu;
1023 $menu[$position] = $new_menu;
1025 $_registered_pages[$hookname] = true;
1027 // No parent as top level
1028 $_parent_pages[$menu_slug] = false;
1034 * Add a top level menu page in the 'objects' section
1036 * This function takes a capability which will be used to determine whether
1037 * or not a page is included in the menu.
1039 * The function which is hooked in to handle the output of the page must check
1040 * that the user has the required capability as well.
1042 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1043 * @param string $menu_title The text to be used for the menu
1044 * @param string $capability The capability required for this menu to be displayed to the user.
1045 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1046 * @param callback $function The function to be called to output the content for this page.
1047 * @param string $icon_url The url to the icon to be used for this menu
1049 * @return string The resulting page's hook_suffix
1051 function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
1052 global $_wp_last_object_menu;
1054 $_wp_last_object_menu++;
1056 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
1060 * Add a top level menu page in the 'utility' section
1062 * This function takes a capability which will be used to determine whether
1063 * or not a page is included in the menu.
1065 * The function which is hooked in to handle the output of the page must check
1066 * that the user has the required capability as well.
1068 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1069 * @param string $menu_title The text to be used for the menu
1070 * @param string $capability The capability required for this menu to be displayed to the user.
1071 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1072 * @param callback $function The function to be called to output the content for this page.
1073 * @param string $icon_url The url to the icon to be used for this menu
1075 * @return string The resulting page's hook_suffix
1077 function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
1078 global $_wp_last_utility_menu;
1080 $_wp_last_utility_menu++;
1082 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
1086 * Add a sub menu page
1088 * This function takes a capability which will be used to determine whether
1089 * or not a page is included in the menu.
1091 * The function which is hooked in to handle the output of the page must check
1092 * that the user has the required capability as well.
1094 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
1095 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1096 * @param string $menu_title The text to be used for the menu
1097 * @param string $capability The capability required for this menu to be displayed to the user.
1098 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1099 * @param callback $function The function to be called to output the content for this page.
1101 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1103 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1106 global $_wp_real_parent_file;
1107 global $_wp_submenu_nopriv;
1108 global $_registered_pages;
1109 global $_parent_pages;
1111 $menu_slug = plugin_basename( $menu_slug );
1112 $parent_slug = plugin_basename( $parent_slug);
1114 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
1115 $parent_slug = $_wp_real_parent_file[$parent_slug];
1117 if ( !current_user_can( $capability ) ) {
1118 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
1122 // If the parent doesn't already have a submenu, add a link to the parent
1123 // as the first item in the submenu. If the submenu file is the same as the
1124 // parent file someone is trying to link back to the parent manually. In
1125 // this case, don't automatically add a link back to avoid duplication.
1126 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
1127 foreach ( (array)$menu as $parent_menu ) {
1128 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
1129 $submenu[$parent_slug][] = $parent_menu;
1133 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1135 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1136 if (!empty ( $function ) && !empty ( $hookname ))
1137 add_action( $hookname, $function );
1139 $_registered_pages[$hookname] = true;
1140 // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
1141 if ( 'tools.php' == $parent_slug )
1142 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1144 // No parent as top level
1145 $_parent_pages[$menu_slug] = $parent_slug;
1151 * Add sub menu page to the tools main menu.
1153 * This function takes a capability which will be used to determine whether
1154 * or not a page is included in the menu.
1156 * The function which is hooked in to handle the output of the page must check
1157 * that the user has the required capability as well.
1159 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1160 * @param string $menu_title The text to be used for the menu
1161 * @param string $capability The capability required for this menu to be displayed to the user.
1162 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1163 * @param callback $function The function to be called to output the content for this page.
1165 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1167 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1168 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1172 * Add sub menu page to the options main menu.
1174 * This function takes a capability which will be used to determine whether
1175 * or not a page is included in the menu.
1177 * The function which is hooked in to handle the output of the page must check
1178 * that the user has the required capability as well.
1180 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1181 * @param string $menu_title The text to be used for the menu
1182 * @param string $capability The capability required for this menu to be displayed to the user.
1183 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1184 * @param callback $function The function to be called to output the content for this page.
1186 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1188 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1189 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1193 * Add sub menu page to the themes main menu.
1195 * This function takes a capability which will be used to determine whether
1196 * or not a page is included in the menu.
1198 * The function which is hooked in to handle the output of the page must check
1199 * that the user has the required capability as well.
1201 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1202 * @param string $menu_title The text to be used for the menu
1203 * @param string $capability The capability required for this menu to be displayed to the user.
1204 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1205 * @param callback $function The function to be called to output the content for this page.
1207 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1209 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1210 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1214 * Add sub menu page to the plugins main menu.
1216 * This function takes a capability which will be used to determine whether
1217 * or not a page is included in the menu.
1219 * The function which is hooked in to handle the output of the page must check
1220 * that the user has the required capability as well.
1222 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1223 * @param string $menu_title The text to be used for the menu
1224 * @param string $capability The capability required for this menu to be displayed to the user.
1225 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1226 * @param callback $function The function to be called to output the content for this page.
1228 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1230 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1231 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1235 * Add sub menu page to the Users/Profile main menu.
1237 * This function takes a capability which will be used to determine whether
1238 * or not a page is included in the menu.
1240 * The function which is hooked in to handle the output of the page must check
1241 * that the user has the required capability as well.
1243 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1244 * @param string $menu_title The text to be used for the menu
1245 * @param string $capability The capability required for this menu to be displayed to the user.
1246 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1247 * @param callback $function The function to be called to output the content for this page.
1249 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1251 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1252 if ( current_user_can('edit_users') )
1253 $parent = 'users.php';
1255 $parent = 'profile.php';
1256 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1259 * Add sub menu page to the Dashboard main menu.
1261 * This function takes a capability which will be used to determine whether
1262 * or not a page is included in the menu.
1264 * The function which is hooked in to handle the output of the page must check
1265 * that the user has the required capability as well.
1267 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1268 * @param string $menu_title The text to be used for the menu
1269 * @param string $capability The capability required for this menu to be displayed to the user.
1270 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1271 * @param callback $function The function to be called to output the content for this page.
1273 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1275 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1276 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1280 * Add sub menu page to the posts main menu.
1282 * This function takes a capability which will be used to determine whether
1283 * or not a page is included in the menu.
1285 * The function which is hooked in to handle the output of the page must check
1286 * that the user has the required capability as well.
1288 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1289 * @param string $menu_title The text to be used for the menu
1290 * @param string $capability The capability required for this menu to be displayed to the user.
1291 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1292 * @param callback $function The function to be called to output the content for this page.
1294 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1296 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1297 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1301 * Add sub menu page to the media main menu.
1303 * This function takes a capability which will be used to determine whether
1304 * or not a page is included in the menu.
1306 * The function which is hooked in to handle the output of the page must check
1307 * that the user has the required capability as well.
1309 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1310 * @param string $menu_title The text to be used for the menu
1311 * @param string $capability The capability required for this menu to be displayed to the user.
1312 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1313 * @param callback $function The function to be called to output the content for this page.
1315 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1317 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1318 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1322 * Add sub menu page to the links main menu.
1324 * This function takes a capability which will be used to determine whether
1325 * or not a page is included in the menu.
1327 * The function which is hooked in to handle the output of the page must check
1328 * that the user has the required capability as well.
1330 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1331 * @param string $menu_title The text to be used for the menu
1332 * @param string $capability The capability required for this menu to be displayed to the user.
1333 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1334 * @param callback $function The function to be called to output the content for this page.
1336 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1338 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1339 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1343 * Add sub menu page to the pages main menu.
1345 * This function takes a capability which will be used to determine whether
1346 * or not a page is included in the menu.
1348 * The function which is hooked in to handle the output of the page must check
1349 * that the user has the required capability as well.
1351 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1352 * @param string $menu_title The text to be used for the menu
1353 * @param string $capability The capability required for this menu to be displayed to the user.
1354 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1355 * @param callback $function The function to be called to output the content for this page.
1357 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1359 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1360 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1364 * Add sub menu page to the comments main menu.
1366 * This function takes a capability which will be used to determine whether
1367 * or not a page is included in the menu.
1369 * The function which is hooked in to handle the output of the page must check
1370 * that the user has the required capability as well.
1372 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1373 * @param string $menu_title The text to be used for the menu
1374 * @param string $capability The capability required for this menu to be displayed to the user.
1375 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1376 * @param callback $function The function to be called to output the content for this page.
1378 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1380 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1381 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1385 * Remove a top level admin menu
1389 * @param string $menu_slug The slug of the menu
1390 * @return array|bool The removed menu on success, False if not found
1392 function remove_menu_page( $menu_slug ) {
1395 foreach ( $menu as $i => $item ) {
1396 if ( $menu_slug == $item[2] ) {
1406 * Remove an admin submenu
1410 * @param string $menu_slug The slug for the parent menu
1411 * @param string $submenu_slug The slug of the submenu
1412 * @return array|bool The removed submenu on success, False if not found
1414 function remove_submenu_page( $menu_slug, $submenu_slug ) {
1417 if ( !isset( $submenu[$menu_slug] ) )
1420 foreach ( $submenu[$menu_slug] as $i => $item ) {
1421 if ( $submenu_slug == $item[2] ) {
1422 unset( $submenu[$menu_slug][$i] );
1431 * Get the url to access a particular menu page based on the slug it was registered with.
1433 * If the slug hasn't been registered properly no url will be returned
1437 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1438 * @param bool $echo Whether or not to echo the url - default is true
1439 * @return string the url
1441 function menu_page_url($menu_slug, $echo = true) {
1442 global $_parent_pages;
1444 if ( isset( $_parent_pages[$menu_slug] ) ) {
1445 $parent_slug = $_parent_pages[$menu_slug];
1446 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1447 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1449 $url = admin_url( 'admin.php?page=' . $menu_slug );
1455 $url = esc_url($url);
1464 // Pluggable Menu Support -- Private
1467 function get_admin_page_parent( $parent = '' ) {
1468 global $parent_file;
1473 global $plugin_page;
1474 global $_wp_real_parent_file;
1475 global $_wp_menu_nopriv;
1476 global $_wp_submenu_nopriv;
1478 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1479 if ( isset( $_wp_real_parent_file[$parent] ) )
1480 $parent = $_wp_real_parent_file[$parent];
1484 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1485 foreach ( (array)$menu as $parent_menu ) {
1486 if ( $parent_menu[2] == $plugin_page ) {
1487 $parent_file = $plugin_page;
1488 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1489 $parent_file = $_wp_real_parent_file[$parent_file];
1490 return $parent_file;
1493 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1494 $parent_file = $plugin_page;
1495 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1496 $parent_file = $_wp_real_parent_file[$parent_file];
1497 return $parent_file;
1501 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1502 $parent_file = $pagenow;
1503 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1504 $parent_file = $_wp_real_parent_file[$parent_file];
1505 return $parent_file;
1508 foreach (array_keys( (array)$submenu ) as $parent) {
1509 foreach ( $submenu[$parent] as $submenu_array ) {
1510 if ( isset( $_wp_real_parent_file[$parent] ) )
1511 $parent = $_wp_real_parent_file[$parent];
1512 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1513 $parent_file = $parent;
1515 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1516 $parent_file = $parent;
1519 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1520 $parent_file = $parent;
1526 if ( empty($parent_file) )
1531 function get_admin_page_title() {
1536 global $plugin_page;
1539 if ( ! empty ( $title ) )
1542 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1544 $parent = $parent1 = get_admin_page_parent();
1546 if ( empty ( $parent) ) {
1547 foreach ( (array)$menu as $menu_array ) {
1548 if ( isset( $menu_array[3] ) ) {
1549 if ( $menu_array[2] == $pagenow ) {
1550 $title = $menu_array[3];
1551 return $menu_array[3];
1553 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1554 $title = $menu_array[3];
1555 return $menu_array[3];
1558 $title = $menu_array[0];
1563 foreach ( array_keys( $submenu ) as $parent ) {
1564 foreach ( $submenu[$parent] as $submenu_array ) {
1565 if ( isset( $plugin_page ) &&
1566 ( $plugin_page == $submenu_array[2] ) &&
1568 ( $parent == $pagenow ) ||
1569 ( $parent == $plugin_page ) ||
1570 ( $plugin_page == $hook ) ||
1571 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1572 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1575 $title = $submenu_array[3];
1576 return $submenu_array[3];
1579 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1582 if ( isset( $submenu_array[3] ) ) {
1583 $title = $submenu_array[3];
1584 return $submenu_array[3];
1586 $title = $submenu_array[0];
1591 if ( empty ( $title ) ) {
1592 foreach ( $menu as $menu_array ) {
1593 if ( isset( $plugin_page ) &&
1594 ( $plugin_page == $menu_array[2] ) &&
1595 ( $pagenow == 'admin.php' ) &&
1596 ( $parent1 == $menu_array[2] ) )
1598 $title = $menu_array[3];
1599 return $menu_array[3];
1608 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1609 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1610 if ( has_action($hook) )
1616 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1617 global $admin_page_hooks;
1619 $parent = get_admin_page_parent( $parent_page );
1621 $page_type = 'admin';
1622 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1623 if ( isset( $admin_page_hooks[$plugin_page] ) )
1624 $page_type = 'toplevel';
1626 if ( isset( $admin_page_hooks[$parent] ))
1627 $page_type = $admin_page_hooks[$parent];
1628 } else if ( isset( $admin_page_hooks[$parent] ) ) {
1629 $page_type = $admin_page_hooks[$parent];
1632 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1634 return $page_type . '_page_' . $plugin_name;
1637 function user_can_access_admin_page() {
1641 global $_wp_menu_nopriv;
1642 global $_wp_submenu_nopriv;
1643 global $plugin_page;
1644 global $_registered_pages;
1646 $parent = get_admin_page_parent();
1648 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1651 if ( isset( $plugin_page ) ) {
1652 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1655 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1657 if ( !isset($_registered_pages[$hookname]) )
1661 if ( empty( $parent) ) {
1662 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1664 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1666 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1668 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1670 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1671 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1673 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1679 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1682 if ( isset( $submenu[$parent] ) ) {
1683 foreach ( $submenu[$parent] as $submenu_array ) {
1684 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1685 if ( current_user_can( $submenu_array[1] ))
1689 } else if ( $submenu_array[2] == $pagenow ) {
1690 if ( current_user_can( $submenu_array[1] ))
1698 foreach ( $menu as $menu_array ) {
1699 if ( $menu_array[2] == $parent) {
1700 if ( current_user_can( $menu_array[1] ))
1710 /* Whitelist functions */
1713 * Register a setting and its sanitization callback
1717 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1718 * Default whitelisted option key names include "general," "discussion," and "reading," among others.
1719 * @param string $option_name The name of an option to sanitize and save.
1720 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
1723 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1724 global $new_whitelist_options;
1726 if ( 'misc' == $option_group ) {
1727 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1728 $option_group = 'general';
1731 if ( 'privacy' == $option_group ) {
1732 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1733 $option_group = 'reading';
1736 $new_whitelist_options[ $option_group ][] = $option_name;
1737 if ( $sanitize_callback != '' )
1738 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1742 * Unregister a setting
1746 * @param unknown_type $option_group
1747 * @param unknown_type $option_name
1748 * @param unknown_type $sanitize_callback
1751 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1752 global $new_whitelist_options;
1754 if ( 'misc' == $option_group ) {
1755 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1756 $option_group = 'general';
1759 if ( 'privacy' == $option_group ) {
1760 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1761 $option_group = 'reading';
1764 $pos = array_search( $option_name, (array) $new_whitelist_options );
1765 if ( $pos !== false )
1766 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1767 if ( $sanitize_callback != '' )
1768 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1772 * {@internal Missing Short Description}}
1776 * @param unknown_type $options
1779 function option_update_filter( $options ) {
1780 global $new_whitelist_options;
1782 if ( is_array( $new_whitelist_options ) )
1783 $options = add_option_whitelist( $new_whitelist_options, $options );
1787 add_filter( 'whitelist_options', 'option_update_filter' );
1790 * {@internal Missing Short Description}}
1794 * @param unknown_type $new_options
1795 * @param unknown_type $options
1798 function add_option_whitelist( $new_options, $options = '' ) {
1799 if ( $options == '' )
1800 global $whitelist_options;
1802 $whitelist_options = $options;
1804 foreach ( $new_options as $page => $keys ) {
1805 foreach ( $keys as $key ) {
1806 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1807 $whitelist_options[ $page ] = array();
1808 $whitelist_options[ $page ][] = $key;
1810 $pos = array_search( $key, $whitelist_options[ $page ] );
1811 if ( $pos === false )
1812 $whitelist_options[ $page ][] = $key;
1817 return $whitelist_options;
1821 * {@internal Missing Short Description}}
1825 * @param unknown_type $del_options
1826 * @param unknown_type $options
1829 function remove_option_whitelist( $del_options, $options = '' ) {
1830 if ( $options == '' )
1831 global $whitelist_options;
1833 $whitelist_options = $options;
1835 foreach ( $del_options as $page => $keys ) {
1836 foreach ( $keys as $key ) {
1837 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1838 $pos = array_search( $key, $whitelist_options[ $page ] );
1839 if ( $pos !== false )
1840 unset( $whitelist_options[ $page ][ $pos ] );
1845 return $whitelist_options;
1849 * Output nonce, action, and option_page fields for a settings page.
1853 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1855 function settings_fields($option_group) {
1856 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1857 echo '<input type="hidden" name="action" value="update" />';
1858 wp_nonce_field("$option_group-options");
1862 * Clears the Plugins cache used by get_plugins() and by default, the Plugin Update cache.
1866 * @param bool $clear_update_cache Whether to clear the Plugin updates cache
1868 function wp_clean_plugins_cache( $clear_update_cache = true ) {
1869 if ( $clear_update_cache )
1870 delete_site_transient( 'update_plugins' );
1871 wp_cache_delete( 'plugins', 'plugins' );