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