3 * WordPress Plugin Administration API
6 * @subpackage Administration
10 * Parse the plugin contents to retrieve plugin's metadata.
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.
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
32 * * / # Remove the space to close comment
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
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.
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.
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.
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
59 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
60 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
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.
68 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
70 $default_headers = array(
71 'Name' => 'Plugin Name',
72 'PluginURI' => 'Plugin URI',
73 'Version' => 'Version',
74 'Description' => 'Description',
76 'AuthorURI' => 'Author URI',
77 'TextDomain' => 'Text Domain',
78 'DomainPath' => 'Domain Path'
81 $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
83 //For backward compatibility by default Title is the same as Name.
84 $plugin_data['Title'] = $plugin_data['Name'];
86 if ( $markup || $translate )
87 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
92 function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
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']);
99 load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file));
101 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
102 $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
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>';
110 $plugin_data['Title'] = $plugin_data['Name'];
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>';
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>';
120 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
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);
132 * Get a list of a plugin's files.
136 * @param string $plugin Plugin ID
137 * @return array List of files relative to the plugin root.
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) == '.' )
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) == '.' )
155 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
157 @closedir( $plugins_subdir );
160 if ( plugin_basename("$dir/$file") != $plugin )
161 $plugin_files[] = plugin_basename("$dir/$file");
164 @closedir( $plugins_dir );
168 return $plugin_files;
172 * Check the plugins directory and retrieve all plugin files with plugin data.
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.
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.
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.
191 function get_plugins($plugin_folder = '') {
193 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
194 $cache_plugins = array();
196 if ( isset($cache_plugins[ $plugin_folder ]) )
197 return $cache_plugins[ $plugin_folder ];
199 $wp_plugins = array ();
200 $plugin_root = WP_PLUGIN_DIR;
201 if( !empty($plugin_folder) )
202 $plugin_root .= $plugin_folder;
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) == '.' )
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) == '.' )
217 if ( substr($subfile, -4) == '.php' )
218 $plugin_files[] = "$file/$subfile";
222 if ( substr($file, -4) == '.php' )
223 $plugin_files[] = $file;
227 @closedir( $plugins_dir );
228 @closedir( $plugins_subdir );
230 if ( !$plugins_dir || empty($plugin_files) )
233 foreach ( $plugin_files as $plugin_file ) {
234 if ( !is_readable( "$plugin_root/$plugin_file" ) )
237 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
239 if ( empty ( $plugin_data['Name'] ) )
242 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
245 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
247 $cache_plugins[ $plugin_folder ] = $wp_plugins;
248 wp_cache_set('plugins', $cache_plugins, 'plugins');
254 * Check whether the plugin is active by checking the active_plugins list.
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.
261 function is_plugin_active($plugin) {
262 return in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
266 * Attempts activation of plugin in a "sandbox" and redirects on success.
268 * A plugin that is already activated will not attempt to be activated again.
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.
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.
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.
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.
289 function activate_plugin($plugin, $redirect = '') {
290 $current = get_option('active_plugins');
291 $plugin = plugin_basename(trim($plugin));
293 $valid = validate_plugin($plugin);
294 if ( is_wp_error($valid) )
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
301 @include(WP_PLUGIN_DIR . '/' . $plugin);
302 $current[] = $plugin;
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) );
315 * Deactivate a single plugin or multiple plugins.
317 * The deactivation hook is disabled by the plugin upgrader by using the $silent
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.
325 function deactivate_plugins($plugins, $silent= false) {
326 $current = get_option('active_plugins');
328 if ( !is_array($plugins) )
329 $plugins = array($plugins);
331 foreach ( $plugins as $plugin ) {
332 $plugin = plugin_basename($plugin);
333 if( ! is_plugin_active($plugin) )
336 do_action( 'deactivate_plugin', trim( $plugin ) );
338 $key = array_search( $plugin, (array) $current );
340 if ( false !== $key )
341 array_splice( $current, $key, 1 );
343 //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
345 do_action( 'deactivate_' . trim( $plugin ) );
346 do_action( 'deactivated_plugin', trim( $plugin ) );
350 update_option('active_plugins', $current);
354 * Activate multiple plugins.
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.
359 * The execution will be halted as soon as one of the plugins has an error.
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.
367 function activate_plugins($plugins, $redirect = '') {
368 if ( !is_array($plugins) )
369 $plugins = array($plugins);
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;
380 if ( !empty($errors) )
381 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
387 * Remove directory and files of a plugin for a single or list of plugin(s).
389 * If the plugins parameter list is empty, false will be returned. True when
394 * @param array $plugins List of plugin
395 * @param string $redirect Redirect to page when complete.
398 function delete_plugins($plugins, $redirect = '' ) {
399 global $wp_filesystem;
401 if( empty($plugins) )
405 foreach( $plugins as $plugin )
406 $checked[] = 'checked[]=' . $plugin;
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();
413 if( ! empty($data) ){
414 include_once( ABSPATH . 'wp-admin/admin-header.php');
416 include( ABSPATH . 'wp-admin/admin-footer.php');
422 if ( ! WP_Filesystem($credentials) ) {
423 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
424 $data = ob_get_contents();
426 if( ! empty($data) ){
427 include_once( ABSPATH . 'wp-admin/admin-header.php');
429 include( ABSPATH . 'wp-admin/admin-footer.php');
435 if ( ! is_object($wp_filesystem) )
436 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
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);
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.'));
446 $plugins_dir = trailingslashit( $plugins_dir );
450 foreach( $plugins as $plugin_file ) {
451 // Run Uninstall hook
452 if ( is_uninstallable_plugin( $plugin_file ) )
453 uninstall_plugin($plugin_file);
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);
460 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
463 $errors[] = $plugin_file;
466 if ( ! empty($errors) )
467 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
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);
478 function validate_active_plugins() {
479 $check_plugins = apply_filters( 'active_plugins', get_option('active_plugins') );
481 // Sanity check. If the active plugin list is not an array, make it an
483 if ( !is_array($check_plugins) ) {
484 update_option('active_plugins', array());
488 //Invalid is any plugin that is deactivated due to error.
491 // If a plugin file does not exist, remove it from the list of active
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);
504 * Validate the plugin path.
506 * Checks that the file exists and {@link validate_file() is valid file}.
510 * @param string $plugin Plugin Path
511 * @return WP_Error|int 0 on success, WP_Error on failure.
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.'));
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.'));
526 * Whether the plugin can be uninstalled.
530 * @param string $plugin Plugin path to check.
531 * @return bool Whether plugin can be uninstalled.
533 function is_uninstallable_plugin($plugin) {
534 $file = plugin_basename($plugin);
536 $uninstallable_plugins = (array) get_option('uninstall_plugins');
537 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
544 * Uninstall a single plugin.
546 * Calls the uninstall hook, if it is available.
550 * @param string $plugin Relative plugin path from Plugin Directory.
552 function uninstall_plugin($plugin) {
553 $file = plugin_basename($plugin);
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);
561 unset($uninstallable_plugins);
563 define('WP_UNINSTALL_PLUGIN', $file);
564 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
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);
575 include WP_PLUGIN_DIR . '/' . $file;
577 add_action( 'uninstall_' . $file, $callable );
578 do_action( 'uninstall_' . $file );
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;
589 $file = plugin_basename( $file );
591 $admin_page_hooks[$file] = sanitize_title( $menu_title );
593 $hookname = get_plugin_page_hookname( $file, '' );
594 if (!empty ( $function ) && !empty ( $hookname ) && current_user_can( $access_level ) )
595 add_action( $hookname, $function );
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);
603 $new_menu = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
605 if ( NULL === $position ) {
608 $menu[$position] = $new_menu;
611 $_registered_pages[$hookname] = true;
616 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
617 global $_wp_last_object_menu;
619 $_wp_last_object_menu++;
621 return add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon_url, $_wp_last_object_menu);
624 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
625 global $_wp_last_utility_menu;
627 $_wp_last_utility_menu++;
629 return add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon_url, $_wp_last_utility_menu);
632 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
635 global $_wp_real_parent_file;
636 global $_wp_submenu_nopriv;
637 global $_registered_pages;
639 $file = plugin_basename( $file );
641 $parent = plugin_basename( $parent);
642 if ( isset( $_wp_real_parent_file[$parent] ) )
643 $parent = $_wp_real_parent_file[$parent];
645 if ( !current_user_can( $access_level ) ) {
646 $_wp_submenu_nopriv[$parent][$file] = true;
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;
661 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
663 $hookname = get_plugin_page_hookname( $file, $parent);
664 if (!empty ( $function ) && !empty ( $hookname ))
665 add_action( $hookname, $function );
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;
676 * Add sub menu page to the tools main menu.
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
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 );
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 );
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 );
697 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
698 if ( current_user_can('edit_users') )
699 $parent = 'users.php';
701 $parent = 'profile.php';
702 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
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 );
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 );
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 );
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 );
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 );
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 );
730 // Pluggable Menu Support -- Private
733 function get_admin_page_parent( $parent = '' ) {
739 global $_wp_real_parent_file;
740 global $_wp_menu_nopriv;
741 global $_wp_submenu_nopriv;
743 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
744 if ( isset( $_wp_real_parent_file[$parent] ) )
745 $parent = $_wp_real_parent_file[$parent];
749 if ( !empty ( $parent_file ) ) {
750 if ( isset( $_wp_real_parent_file[$parent_file] ) )
751 $parent_file = $_wp_real_parent_file[$parent_file];
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];
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];
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];
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;
789 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
790 $parent_file = $parent;
796 if ( empty($parent_file) )
801 function get_admin_page_title() {
808 if ( isset( $title ) && !empty ( $title ) ) {
812 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
814 $parent = $parent1 = get_admin_page_parent();
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];
823 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
824 $title = $menu_array[3];
825 return $menu_array[3];
828 $title = $menu_array[0];
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] ) ) )
839 $title = $submenu_array[3];
840 return $submenu_array[3];
843 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
846 if ( isset( $submenu_array[3] ) ) {
847 $title = $submenu_array[3];
848 return $submenu_array[3];
850 $title = $submenu_array[0];
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] ) )
862 $title = $menu_array[3];
863 return $menu_array[3];
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) )
880 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
881 global $admin_page_hooks;
883 $parent = get_admin_page_parent( $parent_page );
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';
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];
896 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
898 return $page_type.'_page_'.$plugin_name;
901 function user_can_access_admin_page() {
905 global $_wp_menu_nopriv;
906 global $_wp_submenu_nopriv;
908 global $_registered_pages;
910 $parent = get_admin_page_parent();
912 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
915 if ( isset( $plugin_page ) ) {
916 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
919 $hookname = get_plugin_page_hookname($plugin_page, $parent);
920 if ( !isset($_registered_pages[$hookname]) )
924 if ( empty( $parent) ) {
925 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
927 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
929 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
931 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
933 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
934 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
936 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
942 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
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] ))
952 } else if ( $submenu_array[2] == $pagenow ) {
953 if ( current_user_can( $submenu_array[1] ))
961 foreach ( $menu as $menu_array ) {
962 if ( $menu_array[2] == $parent) {
963 if ( current_user_can( $menu_array[1] ))
973 /* Whitelist functions */
976 * Register a setting and its sanitization callback
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.
986 function register_setting($option_group, $option_name, $sanitize_callback = '') {
987 return add_option_update_handler($option_group, $option_name, $sanitize_callback);
991 * Unregister a setting
995 * @param unknown_type $option_group
996 * @param unknown_type $option_name
997 * @param unknown_type $sanitize_callback
1000 function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
1001 return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
1005 * {@internal Missing Short Description}}
1009 * @param unknown_type $option_group
1010 * @param unknown_type $option_name
1011 * @param unknown_type $sanitize_callback
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 );
1021 * {@internal Missing Short Description}}
1025 * @param unknown_type $option_group
1026 * @param unknown_type $option_name
1027 * @param unknown_type $sanitize_callback
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 );
1039 * {@internal Missing Short Description}}
1043 * @param unknown_type $options
1046 function option_update_filter( $options ) {
1047 global $new_whitelist_options;
1049 if ( is_array( $new_whitelist_options ) )
1050 $options = add_option_whitelist( $new_whitelist_options, $options );
1054 add_filter( 'whitelist_options', 'option_update_filter' );
1057 * {@internal Missing Short Description}}
1061 * @param unknown_type $new_options
1062 * @param unknown_type $options
1065 function add_option_whitelist( $new_options, $options = '' ) {
1066 if( $options == '' ) {
1067 global $whitelist_options;
1069 $whitelist_options = $options;
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;
1077 $pos = array_search( $key, $whitelist_options[ $page ] );
1078 if ( $pos === false )
1079 $whitelist_options[ $page ][] = $key;
1083 return $whitelist_options;
1087 * {@internal Missing Short Description}}
1091 * @param unknown_type $del_options
1092 * @param unknown_type $options
1095 function remove_option_whitelist( $del_options, $options = '' ) {
1096 if( $options == '' ) {
1097 global $whitelist_options;
1099 $whitelist_options = $options;
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 ] );
1110 return $whitelist_options;
1114 * Output nonce, action, and option_page fields for a settings page.
1118 * @param string $option_group A settings group name. This should match the group name used in register_setting().
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");