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