]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/plugin.php
Wordpress 2.9.2-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  *  * / # Remove the space to close comment
33  * </code>
34  *
35  * Plugin data returned array contains the following:
36  *              'Name' - Name of the plugin, must be unique.
37  *              'Title' - Title of the plugin and the link to the plugin's web site.
38  *              'Description' - Description of what the plugin does and/or notes
39  *              from the author.
40  *              'Author' - The author's name
41  *              'AuthorURI' - The authors web site address.
42  *              'Version' - The plugin version number.
43  *              'PluginURI' - Plugin web site address.
44  *              'TextDomain' - Plugin's text domain for localization.
45  *              'DomainPath' - Plugin's relative directory path to .mo files.
46  *
47  * Some users have issues with opening large files and manipulating the contents
48  * for want is usually the first 1kiB or 2kiB. This function stops pulling in
49  * the plugin contents when it has all of the required plugin data.
50  *
51  * The first 8kiB of the file will be pulled in and if the plugin data is not
52  * within that first 8kiB, then the plugin author should correct their plugin
53  * and move the plugin data headers to the top.
54  *
55  * The plugin file is assumed to have permissions to allow for scripts to read
56  * the file. This is not checked however and the file is only opened for
57  * reading.
58  *
59  * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
60  * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
61  * @since 1.5.0
62  *
63  * @param string $plugin_file Path to the plugin file
64  * @param bool $markup If the returned data should have HTML markup applied
65  * @param bool $translate If the returned data should be translated
66  * @return array See above for description.
67  */
68 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
69
70         $default_headers = array( 
71                 'Name' => 'Plugin Name', 
72                 'PluginURI' => 'Plugin URI', 
73                 'Version' => 'Version', 
74                 'Description' => 'Description', 
75                 'Author' => 'Author', 
76                 'AuthorURI' => 'Author URI', 
77                 'TextDomain' => 'Text Domain', 
78                 'DomainPath' => 'Domain Path' 
79                 );
80
81         $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
82
83         //For backward compatibility by default Title is the same as Name.
84         $plugin_data['Title'] = $plugin_data['Name'];
85
86         if ( $markup || $translate )
87                 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
88
89         return $plugin_data;
90 }
91
92 function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
93
94         //Translate fields
95         if( $translate && ! empty($plugin_data['TextDomain']) ) {
96                 if( ! empty( $plugin_data['DomainPath'] ) )
97                         load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file). $plugin_data['DomainPath']);
98                 else
99                         load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file));
100
101                 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
102                         $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
103         }
104
105         //Apply Markup
106         if ( $markup ) {
107                 if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
108                         $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
109                 else
110                         $plugin_data['Title'] = $plugin_data['Name'];
111
112                 if ( ! empty($plugin_data['AuthorURI']) && ! empty($plugin_data['Author']) )
113                         $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
114
115                 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
116                 if( ! empty($plugin_data['Author']) )
117                         $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
118         }
119
120         $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
121
122         // Sanitize all displayed data
123         $plugin_data['Title']       = wp_kses($plugin_data['Title'], $plugins_allowedtags);
124         $plugin_data['Version']     = wp_kses($plugin_data['Version'], $plugins_allowedtags);
125         $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
126         $plugin_data['Author']      = wp_kses($plugin_data['Author'], $plugins_allowedtags);
127
128         return $plugin_data;
129 }
130
131 /**
132  * Get a list of a plugin's files.
133  *
134  * @since 2.8.0
135  *
136  * @param string $plugin Plugin ID
137  * @return array List of files relative to the plugin root.
138  */
139 function get_plugin_files($plugin) {
140         $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
141         $dir = dirname($plugin_file);
142         $plugin_files = array($plugin);
143         if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
144                 $plugins_dir = @ opendir( $dir );
145                 if ( $plugins_dir ) {
146                         while (($file = readdir( $plugins_dir ) ) !== false ) {
147                                 if ( substr($file, 0, 1) == '.' )
148                                         continue;
149                                 if ( is_dir( $dir . '/' . $file ) ) {
150                                         $plugins_subdir = @ opendir( $dir . '/' . $file );
151                                         if ( $plugins_subdir ) {
152                                                 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
153                                                         if ( substr($subfile, 0, 1) == '.' )
154                                                                 continue;
155                                                         $plugin_files[] = plugin_basename("$dir/$file/$subfile");
156                                                 }
157                                                 @closedir( $plugins_subdir );
158                                         }
159                                 } else {
160                                         if ( plugin_basename("$dir/$file") != $plugin )
161                                                 $plugin_files[] = plugin_basename("$dir/$file");
162                                 }
163                         }
164                         @closedir( $plugins_dir );
165                 }
166         }
167
168         return $plugin_files;
169 }
170
171 /**
172  * Check the plugins directory and retrieve all plugin files with plugin data.
173  *
174  * WordPress only supports plugin files in the base plugins directory
175  * (wp-content/plugins) and in one directory above the plugins directory
176  * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
177  * must be found in those two locations. It is recommended that do keep your
178  * plugin files in directories.
179  *
180  * The file with the plugin data is the file that will be included and therefore
181  * needs to have the main execution for the plugin. This does not mean
182  * everything must be contained in the file and it is recommended that the file
183  * be split for maintainability. Keep everything in one file for extreme
184  * optimization purposes.
185  *
186  * @since unknown
187  *
188  * @param string $plugin_folder Optional. Relative path to single plugin folder.
189  * @return array Key is the plugin file path and the value is an array of the plugin data.
190  */
191 function get_plugins($plugin_folder = '') {
192
193         if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
194                 $cache_plugins = array();
195
196         if ( isset($cache_plugins[ $plugin_folder ]) )
197                 return $cache_plugins[ $plugin_folder ];
198
199         $wp_plugins = array ();
200         $plugin_root = WP_PLUGIN_DIR;
201         if( !empty($plugin_folder) )
202                 $plugin_root .= $plugin_folder;
203
204         // Files in wp-content/plugins directory
205         $plugins_dir = @ opendir( $plugin_root);
206         $plugin_files = array();
207         if ( $plugins_dir ) {
208                 while (($file = readdir( $plugins_dir ) ) !== false ) {
209                         if ( substr($file, 0, 1) == '.' )
210                                 continue;
211                         if ( is_dir( $plugin_root.'/'.$file ) ) {
212                                 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
213                                 if ( $plugins_subdir ) {
214                                         while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
215                                                 if ( substr($subfile, 0, 1) == '.' )
216                                                         continue;
217                                                 if ( substr($subfile, -4) == '.php' )
218                                                         $plugin_files[] = "$file/$subfile";
219                                         }
220                                 }
221                         } else {
222                                 if ( substr($file, -4) == '.php' )
223                                         $plugin_files[] = $file;
224                         }
225                 }
226         }
227         @closedir( $plugins_dir );
228         @closedir( $plugins_subdir );
229
230         if ( !$plugins_dir || empty($plugin_files) )
231                 return $wp_plugins;
232
233         foreach ( $plugin_files as $plugin_file ) {
234                 if ( !is_readable( "$plugin_root/$plugin_file" ) )
235                         continue;
236
237                 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
238
239                 if ( empty ( $plugin_data['Name'] ) )
240                         continue;
241
242                 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
243         }
244
245         uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
246
247         $cache_plugins[ $plugin_folder ] = $wp_plugins;
248         wp_cache_set('plugins', $cache_plugins, 'plugins');
249
250         return $wp_plugins;
251 }
252
253 /**
254  * Check whether the plugin is active by checking the active_plugins list.
255  *
256  * @since 2.5.0
257  *
258  * @param string $plugin Base plugin path from plugins directory.
259  * @return bool True, if in the active plugins list. False, not in the list.
260  */
261 function is_plugin_active($plugin) {
262         return in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
263 }
264
265 /**
266  * Attempts activation of plugin in a "sandbox" and redirects on success.
267  *
268  * A plugin that is already activated will not attempt to be activated again.
269  *
270  * The way it works is by setting the redirection to the error before trying to
271  * include the plugin file. If the plugin fails, then the redirection will not
272  * be overwritten with the success message. Also, the options will not be
273  * updated and the activation hook will not be called on plugin error.
274  *
275  * It should be noted that in no way the below code will actually prevent errors
276  * within the file. The code should not be used elsewhere to replicate the
277  * "sandbox", which uses redirection to work.
278  * {@source 13 1}
279  *
280  * If any errors are found or text is outputted, then it will be captured to
281  * ensure that the success redirection will update the error redirection.
282  *
283  * @since unknown
284  *
285  * @param string $plugin Plugin path to main plugin file with plugin data.
286  * @param string $redirect Optional. URL to redirect to.
287  * @return WP_Error|null WP_Error on invalid file or null on success.
288  */
289 function activate_plugin($plugin, $redirect = '') {
290         $current = get_option('active_plugins');
291         $plugin = plugin_basename(trim($plugin));
292
293         $valid = validate_plugin($plugin);
294         if ( is_wp_error($valid) )
295                 return $valid;
296
297         if ( !in_array($plugin, $current) ) {
298                 if ( !empty($redirect) )
299                         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
300                 ob_start();
301                 @include(WP_PLUGIN_DIR . '/' . $plugin);
302                 $current[] = $plugin;
303                 sort($current);
304                 do_action( 'activate_plugin', trim( $plugin) );
305                 update_option('active_plugins', $current);
306                 do_action( 'activate_' . trim( $plugin ) );
307                 do_action( 'activated_plugin', trim( $plugin) );
308                 ob_end_clean();
309         }
310
311         return null;
312 }
313
314 /**
315  * Deactivate a single plugin or multiple plugins.
316  *
317  * The deactivation hook is disabled by the plugin upgrader by using the $silent
318  * parameter.
319  *
320  * @since unknown
321  *
322  * @param string|array $plugins Single plugin or list of plugins to deactivate.
323  * @param bool $silent Optional, default is false. Prevent calling deactivate hook.
324  */
325 function deactivate_plugins($plugins, $silent= false) {
326         $current = get_option('active_plugins');
327
328         if ( !is_array($plugins) )
329                 $plugins = array($plugins);
330
331         foreach ( $plugins as $plugin ) {
332                 $plugin = plugin_basename($plugin);
333                 if( ! is_plugin_active($plugin) )
334                         continue;
335                 if ( ! $silent )
336                         do_action( 'deactivate_plugin', trim( $plugin ) );
337
338                 $key = array_search( $plugin, (array) $current );
339
340                 if ( false !== $key )
341                         array_splice( $current, $key, 1 );
342
343                 //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
344                 if ( ! $silent ) {
345                         do_action( 'deactivate_' . trim( $plugin ) );
346                         do_action( 'deactivated_plugin', trim( $plugin ) );
347                 }
348         }
349
350         update_option('active_plugins', $current);
351 }
352
353 /**
354  * Activate multiple plugins.
355  *
356  * When WP_Error is returned, it does not mean that one of the plugins had
357  * errors. It means that one or more of the plugins file path was invalid.
358  *
359  * The execution will be halted as soon as one of the plugins has an error.
360  *
361  * @since unknown
362  *
363  * @param string|array $plugins
364  * @param string $redirect Redirect to page after successful activation.
365  * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
366  */
367 function activate_plugins($plugins, $redirect = '') {
368         if ( !is_array($plugins) )
369                 $plugins = array($plugins);
370
371         $errors = array();
372         foreach ( (array) $plugins as $plugin ) {
373                 if ( !empty($redirect) )
374                         $redirect = add_query_arg('plugin', $plugin, $redirect);
375                 $result = activate_plugin($plugin, $redirect);
376                 if ( is_wp_error($result) )
377                         $errors[$plugin] = $result;
378         }
379
380         if ( !empty($errors) )
381                 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
382
383         return true;
384 }
385
386 /**
387  * Remove directory and files of a plugin for a single or list of plugin(s).
388  *
389  * If the plugins parameter list is empty, false will be returned. True when
390  * completed.
391  *
392  * @since unknown
393  *
394  * @param array $plugins List of plugin
395  * @param string $redirect Redirect to page when complete.
396  * @return mixed
397  */
398 function delete_plugins($plugins, $redirect = '' ) {
399         global $wp_filesystem;
400
401         if( empty($plugins) )
402                 return false;
403
404         $checked = array();
405         foreach( $plugins as $plugin )
406                 $checked[] = 'checked[]=' . $plugin;
407
408         ob_start();
409         $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
410         if ( false === ($credentials = request_filesystem_credentials($url)) ) {
411                 $data = ob_get_contents();
412                 ob_end_clean();
413                 if( ! empty($data) ){
414                         include_once( ABSPATH . 'wp-admin/admin-header.php');
415                         echo $data;
416                         include( ABSPATH . 'wp-admin/admin-footer.php');
417                         exit;
418                 }
419                 return;
420         }
421
422         if ( ! WP_Filesystem($credentials) ) {
423                 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
424                 $data = ob_get_contents();
425                 ob_end_clean();
426                 if( ! empty($data) ){
427                         include_once( ABSPATH . 'wp-admin/admin-header.php');
428                         echo $data;
429                         include( ABSPATH . 'wp-admin/admin-footer.php');
430                         exit;
431                 }
432                 return;
433         }
434
435         if ( ! is_object($wp_filesystem) )
436                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
437
438         if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
439                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
440
441         //Get the base plugin folder
442         $plugins_dir = $wp_filesystem->wp_plugins_dir();
443         if ( empty($plugins_dir) )
444                 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
445
446         $plugins_dir = trailingslashit( $plugins_dir );
447
448         $errors = array();
449
450         foreach( $plugins as $plugin_file ) {
451                 // Run Uninstall hook
452                 if ( is_uninstallable_plugin( $plugin_file ) )
453                         uninstall_plugin($plugin_file);
454
455                 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
456                 // If plugin is in its own directory, recursively delete the directory.
457                 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
458                         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
459                 else
460                         $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
461
462                 if ( ! $deleted )
463                         $errors[] = $plugin_file;
464         }
465
466         if ( ! empty($errors) )
467                 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
468
469         // Force refresh of plugin update information
470         if ( $current = get_transient('update_plugins') ) {
471                 unset( $current->response[ $plugin_file ] );
472                 set_transient('update_plugins', $current);
473         }
474
475         return true;
476 }
477
478 function validate_active_plugins() {
479         $check_plugins = apply_filters( 'active_plugins', get_option('active_plugins') );
480
481         // Sanity check.  If the active plugin list is not an array, make it an
482         // empty array.
483         if ( !is_array($check_plugins) ) {
484                 update_option('active_plugins', array());
485                 return;
486         }
487
488         //Invalid is any plugin that is deactivated due to error.
489         $invalid = array();
490
491         // If a plugin file does not exist, remove it from the list of active
492         // plugins.
493         foreach ( $check_plugins as $check_plugin ) {
494                 $result = validate_plugin($check_plugin);
495                 if ( is_wp_error( $result ) ) {
496                         $invalid[$check_plugin] = $result;
497                         deactivate_plugins( $check_plugin, true);
498                 }
499         }
500         return $invalid;
501 }
502
503 /**
504  * Validate the plugin path.
505  *
506  * Checks that the file exists and {@link validate_file() is valid file}.
507  *
508  * @since unknown
509  *
510  * @param string $plugin Plugin Path
511  * @return WP_Error|int 0 on success, WP_Error on failure.
512  */
513 function validate_plugin($plugin) {
514         if ( validate_file($plugin) )
515                 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
516         if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
517                 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
518
519         $installed_plugins = get_plugins();
520         if ( ! isset($installed_plugins[$plugin]) )
521                 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
522         return 0;
523 }
524
525 /**
526  * Whether the plugin can be uninstalled.
527  *
528  * @since 2.7.0
529  *
530  * @param string $plugin Plugin path to check.
531  * @return bool Whether plugin can be uninstalled.
532  */
533 function is_uninstallable_plugin($plugin) {
534         $file = plugin_basename($plugin);
535
536         $uninstallable_plugins = (array) get_option('uninstall_plugins');
537         if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
538                 return true;
539
540         return false;
541 }
542
543 /**
544  * Uninstall a single plugin.
545  *
546  * Calls the uninstall hook, if it is available.
547  *
548  * @since 2.7.0
549  *
550  * @param string $plugin Relative plugin path from Plugin Directory.
551  */
552 function uninstall_plugin($plugin) {
553         $file = plugin_basename($plugin);
554
555         $uninstallable_plugins = (array) get_option('uninstall_plugins');
556         if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
557                 if ( isset( $uninstallable_plugins[$file] ) ) {
558                         unset($uninstallable_plugins[$file]);
559                         update_option('uninstall_plugins', $uninstallable_plugins);
560                 }
561                 unset($uninstallable_plugins);
562
563                 define('WP_UNINSTALL_PLUGIN', $file);
564                 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
565
566                 return true;
567         }
568
569         if ( isset( $uninstallable_plugins[$file] ) ) {
570                 $callable = $uninstallable_plugins[$file];
571                 unset($uninstallable_plugins[$file]);
572                 update_option('uninstall_plugins', $uninstallable_plugins);
573                 unset($uninstallable_plugins);
574
575                 include WP_PLUGIN_DIR . '/' . $file;
576
577                 add_action( 'uninstall_' . $file, $callable );
578                 do_action( 'uninstall_' . $file );
579         }
580 }
581
582 //
583 // Menu
584 //
585
586 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '', $position = NULL ) {
587         global $menu, $admin_page_hooks, $_registered_pages;
588
589         $file = plugin_basename( $file );
590
591         $admin_page_hooks[$file] = sanitize_title( $menu_title );
592
593         $hookname = get_plugin_page_hookname( $file, '' );
594         if (!empty ( $function ) && !empty ( $hookname ) && current_user_can( $access_level ) )
595                 add_action( $hookname, $function );
596
597         if ( empty($icon_url) ) {
598                 $icon_url = 'images/generic.png';
599         } elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') ) {
600                 $icon_url = 'https://' . substr($icon_url, 7);
601         }
602
603         $new_menu = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
604
605         if ( NULL === $position  ) {
606                 $menu[] = $new_menu;
607         } else {
608                 $menu[$position] = $new_menu;
609         }
610
611         $_registered_pages[$hookname] = true;
612
613         return $hookname;
614 }
615
616 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
617         global $_wp_last_object_menu;
618
619         $_wp_last_object_menu++;
620
621         return add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon_url, $_wp_last_object_menu);
622 }
623
624 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
625         global $_wp_last_utility_menu;
626
627         $_wp_last_utility_menu++;
628
629         return add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon_url, $_wp_last_utility_menu);
630 }
631
632 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
633         global $submenu;
634         global $menu;
635         global $_wp_real_parent_file;
636         global $_wp_submenu_nopriv;
637         global $_registered_pages;
638
639         $file = plugin_basename( $file );
640
641         $parent = plugin_basename( $parent);
642         if ( isset( $_wp_real_parent_file[$parent] ) )
643                 $parent = $_wp_real_parent_file[$parent];
644
645         if ( !current_user_can( $access_level ) ) {
646                 $_wp_submenu_nopriv[$parent][$file] = true;
647                 return false;
648         }
649
650         // If the parent doesn't already have a submenu, add a link to the parent
651         // as the first item in the submenu.  If the submenu file is the same as the
652         // parent file someone is trying to link back to the parent manually.  In
653         // this case, don't automatically add a link back to avoid duplication.
654         if (!isset( $submenu[$parent] ) && $file != $parent  ) {
655                 foreach ( (array)$menu as $parent_menu ) {
656                         if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
657                                 $submenu[$parent][] = $parent_menu;
658                 }
659         }
660
661         $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
662
663         $hookname = get_plugin_page_hookname( $file, $parent);
664         if (!empty ( $function ) && !empty ( $hookname ))
665                 add_action( $hookname, $function );
666
667         $_registered_pages[$hookname] = true;
668         // backwards-compatibility for plugins using add_management page.  See wp-admin/admin.php for redirect from edit.php to tools.php
669         if ( 'tools.php' == $parent )
670                 $_registered_pages[get_plugin_page_hookname( $file, 'edit.php')] = true;
671
672         return $hookname;
673 }
674
675 /**
676  * Add sub menu page to the tools main menu.
677  *
678  * @param string $page_title
679  * @param unknown_type $menu_title
680  * @param unknown_type $access_level
681  * @param unknown_type $file
682  * @param unknown_type $function
683  * @return unknown
684  */
685 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
686         return add_submenu_page( 'tools.php', $page_title, $menu_title, $access_level, $file, $function );
687 }
688
689 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
690         return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
691 }
692
693 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
694         return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
695 }
696
697 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
698         if ( current_user_can('edit_users') )
699                 $parent = 'users.php';
700         else
701                 $parent = 'profile.php';
702         return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
703 }
704
705 function add_dashboard_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
706         return add_submenu_page( 'index.php', $page_title, $menu_title, $access_level, $file, $function );
707 }
708
709 function add_posts_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
710         return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
711 }
712
713 function add_media_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
714         return add_submenu_page( 'upload.php', $page_title, $menu_title, $access_level, $file, $function );
715 }
716
717 function add_links_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
718         return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $access_level, $file, $function );
719 }
720
721 function add_pages_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
722         return add_submenu_page( 'edit-pages.php', $page_title, $menu_title, $access_level, $file, $function );
723 }
724
725 function add_comments_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
726         return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $access_level, $file, $function );
727 }
728
729 //
730 // Pluggable Menu Support -- Private
731 //
732
733 function get_admin_page_parent( $parent = '' ) {
734         global $parent_file;
735         global $menu;
736         global $submenu;
737         global $pagenow;
738         global $plugin_page;
739         global $_wp_real_parent_file;
740         global $_wp_menu_nopriv;
741         global $_wp_submenu_nopriv;
742
743         if ( !empty ( $parent ) && 'admin.php' != $parent ) {
744                 if ( isset( $_wp_real_parent_file[$parent] ) )
745                         $parent = $_wp_real_parent_file[$parent];
746                 return $parent;
747         }
748 /*
749         if ( !empty ( $parent_file ) ) {
750                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
751                         $parent_file = $_wp_real_parent_file[$parent_file];
752
753                 return $parent_file;
754         }
755 */
756
757         if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
758                 foreach ( (array)$menu as $parent_menu ) {
759                         if ( $parent_menu[2] == $plugin_page ) {
760                                 $parent_file = $plugin_page;
761                                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
762                                         $parent_file = $_wp_real_parent_file[$parent_file];
763                                 return $parent_file;
764                         }
765                 }
766                 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
767                         $parent_file = $plugin_page;
768                         if ( isset( $_wp_real_parent_file[$parent_file] ) )
769                                         $parent_file = $_wp_real_parent_file[$parent_file];
770                         return $parent_file;
771                 }
772         }
773
774         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
775                 $parent_file = $pagenow;
776                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
777                         $parent_file = $_wp_real_parent_file[$parent_file];
778                 return $parent_file;
779         }
780
781         foreach (array_keys( (array)$submenu ) as $parent) {
782                 foreach ( $submenu[$parent] as $submenu_array ) {
783                         if ( isset( $_wp_real_parent_file[$parent] ) )
784                                 $parent = $_wp_real_parent_file[$parent];
785                         if ( $submenu_array[2] == $pagenow ) {
786                                 $parent_file = $parent;
787                                 return $parent;
788                         } else
789                                 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
790                                         $parent_file = $parent;
791                                         return $parent;
792                                 }
793                 }
794         }
795
796         if ( empty($parent_file) )
797                 $parent_file = '';
798         return '';
799 }
800
801 function get_admin_page_title() {
802         global $title;
803         global $menu;
804         global $submenu;
805         global $pagenow;
806         global $plugin_page;
807
808         if ( isset( $title ) && !empty ( $title ) ) {
809                 return $title;
810         }
811
812         $hook = get_plugin_page_hook( $plugin_page, $pagenow );
813
814         $parent = $parent1 = get_admin_page_parent();
815
816         if ( empty ( $parent) ) {
817                 foreach ( (array)$menu as $menu_array ) {
818                         if ( isset( $menu_array[3] ) ) {
819                                 if ( $menu_array[2] == $pagenow ) {
820                                         $title = $menu_array[3];
821                                         return $menu_array[3];
822                                 } else
823                                         if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
824                                                 $title = $menu_array[3];
825                                                 return $menu_array[3];
826                                         }
827                         } else {
828                                 $title = $menu_array[0];
829                                 return $title;
830                         }
831                 }
832         } else {
833                 foreach (array_keys( $submenu ) as $parent) {
834                         foreach ( $submenu[$parent] as $submenu_array ) {
835                                 if ( isset( $plugin_page ) &&
836                                         ($plugin_page == $submenu_array[2] ) &&
837                                         (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
838                                         ) {
839                                                 $title = $submenu_array[3];
840                                                 return $submenu_array[3];
841                                         }
842
843                                 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
844                                         continue;
845
846                                 if ( isset( $submenu_array[3] ) ) {
847                                         $title = $submenu_array[3];
848                                         return $submenu_array[3];
849                                 } else {
850                                         $title = $submenu_array[0];
851                                         return $title;
852                                 }
853                         }
854                 }
855                 if ( !isset($title) || empty ( $title ) ) {
856                         foreach ( $menu as $menu_array ) {
857                                 if ( isset( $plugin_page ) &&
858                                         ($plugin_page == $menu_array[2] ) &&
859                                         ($pagenow == 'admin.php' ) &&
860                                         ($parent1 == $menu_array[2] ) )
861                                         {
862                                                 $title = $menu_array[3];
863                                                 return $menu_array[3];
864                                         }
865                         }
866                 }
867         }
868
869         return $title;
870 }
871
872 function get_plugin_page_hook( $plugin_page, $parent_page ) {
873         $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
874         if ( has_action($hook) )
875                 return $hook;
876         else
877                 return null;
878 }
879
880 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
881         global $admin_page_hooks;
882
883         $parent = get_admin_page_parent( $parent_page );
884
885         $page_type = 'admin';
886         if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
887                 if ( isset( $admin_page_hooks[$plugin_page] ) )
888                         $page_type = 'toplevel';
889                 else
890                         if ( isset( $admin_page_hooks[$parent] ))
891                                 $page_type = $admin_page_hooks[$parent];
892         } else if ( isset( $admin_page_hooks[$parent] ) ) {
893                 $page_type = $admin_page_hooks[$parent];
894         }
895
896         $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
897
898         return $page_type.'_page_'.$plugin_name;
899 }
900
901 function user_can_access_admin_page() {
902         global $pagenow;
903         global $menu;
904         global $submenu;
905         global $_wp_menu_nopriv;
906         global $_wp_submenu_nopriv;
907         global $plugin_page;
908         global $_registered_pages;
909
910         $parent = get_admin_page_parent();
911
912         if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
913                 return false;
914
915         if ( isset( $plugin_page ) ) {
916                 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
917                         return false;
918
919                 $hookname = get_plugin_page_hookname($plugin_page, $parent);
920                 if ( !isset($_registered_pages[$hookname]) )
921                         return false;
922         }
923
924         if ( empty( $parent) ) {
925                 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
926                         return false;
927                 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
928                         return false;
929                 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
930                         return false;
931                 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
932                         return false;
933                 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
934                         if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
935                                 return false;
936                         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
937                         return false;
938                 }
939                 return true;
940         }
941
942         if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
943                 return false;
944
945         if ( isset( $submenu[$parent] ) ) {
946                 foreach ( $submenu[$parent] as $submenu_array ) {
947                         if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
948                                 if ( current_user_can( $submenu_array[1] ))
949                                         return true;
950                                 else
951                                         return false;
952                         } else if ( $submenu_array[2] == $pagenow ) {
953                                 if ( current_user_can( $submenu_array[1] ))
954                                         return true;
955                                 else
956                                         return false;
957                         }
958                 }
959         }
960
961         foreach ( $menu as $menu_array ) {
962                 if ( $menu_array[2] == $parent) {
963                         if ( current_user_can( $menu_array[1] ))
964                                 return true;
965                         else
966                                 return false;
967                 }
968         }
969
970         return true;
971 }
972
973 /* Whitelist functions */
974
975 /**
976  * Register a setting and its sanitization callback
977  *
978  * @since 2.7.0
979  *
980  * @param string $option_group A settings group name.  Should correspond to a whitelisted option key name.
981  *      Default whitelisted option key names include "general," "discussion," and "reading," among others.
982  * @param string $option_name The name of an option to sanitize and save.
983  * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
984  * @return unknown
985  */
986 function register_setting($option_group, $option_name, $sanitize_callback = '') {
987         return add_option_update_handler($option_group, $option_name, $sanitize_callback);
988 }
989
990 /**
991  * Unregister a setting
992  *
993  * @since 2.7.0
994  *
995  * @param unknown_type $option_group
996  * @param unknown_type $option_name
997  * @param unknown_type $sanitize_callback
998  * @return unknown
999  */
1000 function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
1001         return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
1002 }
1003
1004 /**
1005  * {@internal Missing Short Description}}
1006  *
1007  * @since unknown
1008  *
1009  * @param unknown_type $option_group
1010  * @param unknown_type $option_name
1011  * @param unknown_type $sanitize_callback
1012  */
1013 function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
1014         global $new_whitelist_options;
1015         $new_whitelist_options[ $option_group ][] = $option_name;
1016         if ( $sanitize_callback != '' )
1017                 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1018 }
1019
1020 /**
1021  * {@internal Missing Short Description}}
1022  *
1023  * @since unknown
1024  *
1025  * @param unknown_type $option_group
1026  * @param unknown_type $option_name
1027  * @param unknown_type $sanitize_callback
1028  */
1029 function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
1030         global $new_whitelist_options;
1031         $pos = array_search( $option_name, (array) $new_whitelist_options );
1032         if ( $pos !== false )
1033                 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1034         if ( $sanitize_callback != '' )
1035                 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1036 }
1037
1038 /**
1039  * {@internal Missing Short Description}}
1040  *
1041  * @since unknown
1042  *
1043  * @param unknown_type $options
1044  * @return unknown
1045  */
1046 function option_update_filter( $options ) {
1047         global $new_whitelist_options;
1048
1049         if ( is_array( $new_whitelist_options ) )
1050                 $options = add_option_whitelist( $new_whitelist_options, $options );
1051
1052         return $options;
1053 }
1054 add_filter( 'whitelist_options', 'option_update_filter' );
1055
1056 /**
1057  * {@internal Missing Short Description}}
1058  *
1059  * @since unknown
1060  *
1061  * @param unknown_type $new_options
1062  * @param unknown_type $options
1063  * @return unknown
1064  */
1065 function add_option_whitelist( $new_options, $options = '' ) {
1066         if( $options == '' ) {
1067                 global $whitelist_options;
1068         } else {
1069                 $whitelist_options = $options;
1070         }
1071         foreach( $new_options as $page => $keys ) {
1072                 foreach( $keys as $key ) {
1073                         if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1074                                 $whitelist_options[ $page ] = array();
1075                                 $whitelist_options[ $page ][] = $key;
1076                         } else {
1077                                 $pos = array_search( $key, $whitelist_options[ $page ] );
1078                                 if ( $pos === false )
1079                                         $whitelist_options[ $page ][] = $key;
1080                         }
1081                 }
1082         }
1083         return $whitelist_options;
1084 }
1085
1086 /**
1087  * {@internal Missing Short Description}}
1088  *
1089  * @since unknown
1090  *
1091  * @param unknown_type $del_options
1092  * @param unknown_type $options
1093  * @return unknown
1094  */
1095 function remove_option_whitelist( $del_options, $options = '' ) {
1096         if( $options == '' ) {
1097                 global $whitelist_options;
1098         } else {
1099                 $whitelist_options = $options;
1100         }
1101         foreach( $del_options as $page => $keys ) {
1102                 foreach( $keys as $key ) {
1103                         if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1104                                 $pos = array_search( $key, $whitelist_options[ $page ] );
1105                                 if( $pos !== false )
1106                                         unset( $whitelist_options[ $page ][ $pos ] );
1107                         }
1108                 }
1109         }
1110         return $whitelist_options;
1111 }
1112
1113 /**
1114  * Output nonce, action, and option_page fields for a settings page.
1115  *
1116  * @since 2.7.0
1117  *
1118  * @param string $option_group A settings group name.  This should match the group name used in register_setting().
1119  */
1120 function settings_fields($option_group) {
1121         echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1122         echo '<input type="hidden" name="action" value="update" />';
1123         wp_nonce_field("$option_group-options");
1124 }
1125
1126 ?>