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