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