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 ) {
69 // We don't need to write to the file, so just open for reading.
70 $fp = fopen($plugin_file, 'r');
72 // Pull only the first 8kiB of the file in.
73 $plugin_data = fread( $fp, 8192 );
75 // PHP will close file handle, but we are good citizens.
78 preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
79 preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
80 preg_match( '|Version:(.*)|i', $plugin_data, $version );
81 preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
82 preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
83 preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
84 preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
85 preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
87 foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
88 if ( !empty( ${$field} ) )
89 ${$field} = trim(${$field}[1]);
95 'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
96 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
97 'TextDomain' => $text_domain, 'DomainPath' => $domain_path
99 if ( $markup || $translate )
100 $plugin_data = _get_plugin_data_markup_translate($plugin_data, $markup, $translate);
104 function _get_plugin_data_markup_translate($plugin_data, $markup = true, $translate = true) {
107 if( $translate && ! empty($plugin_data['TextDomain']) ) {
108 if( ! empty( $plugin_data['DomainPath'] ) )
109 load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file). $plugin_data['DomainPath']);
111 load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file));
113 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
114 $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
119 if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
120 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
122 $plugin_data['Title'] = $plugin_data['Name'];
124 if ( ! empty($plugin_data['AuthorURI']) && ! empty($plugin_data['Author']) )
125 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
127 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
128 if( ! empty($plugin_data['Author']) )
129 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
132 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
134 // Sanitize all displayed data
135 $plugin_data['Title'] = wp_kses($plugin_data['Title'], $plugins_allowedtags);
136 $plugin_data['Version'] = wp_kses($plugin_data['Version'], $plugins_allowedtags);
137 $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
138 $plugin_data['Author'] = wp_kses($plugin_data['Author'], $plugins_allowedtags);
144 * Check the plugins directory and retrieve all plugin files with plugin data.
146 * WordPress only supports plugin files in the base plugins directory
147 * (wp-content/plugins) and in one directory above the plugins directory
148 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
149 * must be found in those two locations. It is recommended that do keep your
150 * plugin files in directories.
152 * The file with the plugin data is the file that will be included and therefore
153 * needs to have the main execution for the plugin. This does not mean
154 * everything must be contained in the file and it is recommended that the file
155 * be split for maintainability. Keep everything in one file for extreme
156 * optimization purposes.
160 * @param string $plugin_folder Optional. Relative path to single plugin folder.
161 * @return array Key is the plugin file path and the value is an array of the plugin data.
163 function get_plugins($plugin_folder = '') {
165 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
166 $cache_plugins = array();
168 if ( isset($cache_plugins[ $plugin_folder ]) )
169 return $cache_plugins[ $plugin_folder ];
171 $wp_plugins = array ();
172 $plugin_root = WP_PLUGIN_DIR;
173 if( !empty($plugin_folder) )
174 $plugin_root .= $plugin_folder;
176 // Files in wp-content/plugins directory
177 $plugins_dir = @ opendir( $plugin_root);
178 if ( $plugins_dir ) {
179 while (($file = readdir( $plugins_dir ) ) !== false ) {
180 if ( substr($file, 0, 1) == '.' )
182 if ( is_dir( $plugin_root.'/'.$file ) ) {
183 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
184 if ( $plugins_subdir ) {
185 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
186 if ( substr($subfile, 0, 1) == '.' )
188 if ( substr($subfile, -4) == '.php' )
189 $plugin_files[] = "$file/$subfile";
193 if ( substr($file, -4) == '.php' )
194 $plugin_files[] = $file;
198 @closedir( $plugins_dir );
199 @closedir( $plugins_subdir );
201 if ( !$plugins_dir || !$plugin_files )
204 foreach ( $plugin_files as $plugin_file ) {
205 if ( !is_readable( "$plugin_root/$plugin_file" ) )
208 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
210 if ( empty ( $plugin_data['Name'] ) )
213 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
216 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
218 $cache_plugins[ $plugin_folder ] = $wp_plugins;
219 wp_cache_set('plugins', $cache_plugins, 'plugins');
225 * Check whether the plugin is active by checking the active_plugins list.
229 * @param string $plugin Base plugin path from plugins directory.
230 * @return bool True, if in the active plugins list. False, not in the list.
232 function is_plugin_active($plugin) {
233 return in_array($plugin, get_option('active_plugins'));
237 * Attempts activation of plugin in a "sandbox" and redirects on success.
239 * A plugin that is already activated will not attempt to be activated again.
241 * The way it works is by setting the redirection to the error before trying to
242 * include the plugin file. If the plugin fails, then the redirection will not
243 * be overwritten with the success message. Also, the options will not be
244 * updated and the activation hook will not be called on plugin error.
246 * It should be noted that in no way the below code will actually prevent errors
247 * within the file. The code should not be used elsewhere to replicate the
248 * "sandbox", which uses redirection to work.
251 * If any errors are found or text is outputted, then it will be captured to
252 * ensure that the success redirection will update the error redirection.
256 * @param string $plugin Plugin path to main plugin file with plugin data.
257 * @param string $redirect Optional. URL to redirect to.
258 * @return WP_Error|null WP_Error on invalid file or null on success.
260 function activate_plugin($plugin, $redirect = '') {
261 $current = get_option('active_plugins');
262 $plugin = plugin_basename(trim($plugin));
264 $valid = validate_plugin($plugin);
265 if ( is_wp_error($valid) )
268 if ( !in_array($plugin, $current) ) {
269 if ( !empty($redirect) )
270 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
272 @include(WP_PLUGIN_DIR . '/' . $plugin);
273 $current[] = $plugin;
275 update_option('active_plugins', $current);
276 do_action('activate_' . $plugin);
284 * Deactivate a single plugin or multiple plugins.
286 * The deactivation hook is disabled by the plugin upgrader by using the $silent
291 * @param string|array $plugins Single plugin or list of plugins to deactivate.
292 * @param bool $silent Optional, default is false. Prevent calling deactivate hook.
294 function deactivate_plugins($plugins, $silent= false) {
295 $current = get_option('active_plugins');
297 if ( !is_array($plugins) )
298 $plugins = array($plugins);
300 foreach ( $plugins as $plugin ) {
301 $plugin = plugin_basename($plugin);
302 if( ! is_plugin_active($plugin) )
304 array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
305 if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
306 do_action('deactivate_' . trim( $plugin ));
309 update_option('active_plugins', $current);
313 * Activate multiple plugins.
315 * When WP_Error is returned, it does not mean that one of the plugins had
316 * errors. It means that one or more of the plugins file path was invalid.
318 * The execution will be halted as soon as one of the plugins has an error.
322 * @param string|array $plugins
323 * @param string $redirect Redirect to page after successful activation.
324 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
326 function activate_plugins($plugins, $redirect = '') {
327 if ( !is_array($plugins) )
328 $plugins = array($plugins);
331 foreach ( (array) $plugins as $plugin ) {
332 if ( !empty($redirect) )
333 $redirect = add_query_arg('plugin', $plugin, $redirect);
334 $result = activate_plugin($plugin, $redirect);
335 if ( is_wp_error($result) )
336 $errors[$plugin] = $result;
339 if ( !empty($errors) )
340 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
346 * Remove directory and files of a plugin for a single or list of plugin(s).
348 * If the plugins parameter list is empty, false will be returned. True when
353 * @param array $plugins List of plugin
354 * @param string $redirect Redirect to page when complete.
357 function delete_plugins($plugins, $redirect = '' ) {
358 global $wp_filesystem;
360 if( empty($plugins) )
364 foreach( $plugins as $plugin )
365 $checked[] = 'checked[]=' . $plugin;
368 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
369 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
370 $data = ob_get_contents();
372 if( ! empty($data) ){
373 include_once( ABSPATH . 'wp-admin/admin-header.php');
375 include( ABSPATH . 'wp-admin/admin-footer.php');
381 if ( ! WP_Filesystem($credentials) ) {
382 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
383 $data = ob_get_contents();
385 if( ! empty($data) ){
386 include_once( ABSPATH . 'wp-admin/admin-header.php');
388 include( ABSPATH . 'wp-admin/admin-footer.php');
394 if ( $wp_filesystem->errors->get_error_code() ) {
395 return $wp_filesystem->errors;
398 if ( ! is_object($wp_filesystem) )
399 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
401 if ( $wp_filesystem->errors->get_error_code() )
402 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
404 //Get the base plugin folder
405 $plugins_dir = $wp_filesystem->wp_plugins_dir();
406 if ( empty($plugins_dir) )
407 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
409 $plugins_dir = trailingslashit( $plugins_dir );
413 foreach( $plugins as $plugin_file ) {
414 // Run Uninstall hook
415 if ( is_uninstallable_plugin( $plugin_file ) )
416 uninstall_plugin($plugin_file);
418 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
419 // If plugin is in its own directory, recursively delete the directory.
420 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
421 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
423 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
426 $errors[] = $plugin_file;
429 if ( ! empty($errors) )
430 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
432 // Force refresh of plugin update information
433 delete_option('update_plugins');
438 function validate_active_plugins() {
439 $check_plugins = get_option('active_plugins');
441 // Sanity check. If the active plugin list is not an array, make it an
443 if ( !is_array($check_plugins) ) {
444 update_option('active_plugins', array());
448 //Invalid is any plugin that is deactivated due to error.
451 // If a plugin file does not exist, remove it from the list of active
453 foreach ( $check_plugins as $check_plugin ) {
454 $result = validate_plugin($check_plugin);
455 if ( is_wp_error( $result ) ) {
456 $invalid[$check_plugin] = $result;
457 deactivate_plugins( $check_plugin, true);
464 * Validate the plugin path.
466 * Checks that the file exists and {@link validate_file() is valid file}.
470 * @param string $plugin Plugin Path
471 * @return WP_Error|int 0 on success, WP_Error on failure.
473 function validate_plugin($plugin) {
474 if ( validate_file($plugin) )
475 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
476 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
477 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
483 * Whether the plugin can be uninstalled.
487 * @param string $plugin Plugin path to check.
488 * @return bool Whether plugin can be uninstalled.
490 function is_uninstallable_plugin($plugin) {
491 $file = plugin_basename($plugin);
493 $uninstallable_plugins = (array) get_option('uninstall_plugins');
494 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
501 * Uninstall a single plugin.
503 * Calls the uninstall hook, if it is available.
507 * @param string $plugin Relative plugin path from Plugin Directory.
509 function uninstall_plugin($plugin) {
510 $file = plugin_basename($plugin);
512 $uninstallable_plugins = (array) get_option('uninstall_plugins');
513 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
514 if ( isset( $uninstallable_plugins[$file] ) ) {
515 unset($uninstallable_plugins[$file]);
516 update_option('uninstall_plugins', $uninstallable_plugins);
518 unset($uninstallable_plugins);
520 define('WP_UNINSTALL_PLUGIN', $file);
521 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
526 if ( isset( $uninstallable_plugins[$file] ) ) {
527 $callable = $uninstallable_plugins[$file];
528 unset($uninstallable_plugins[$file]);
529 update_option('uninstall_plugins', $uninstallable_plugins);
530 unset($uninstallable_plugins);
532 include WP_PLUGIN_DIR . '/' . $file;
534 add_action( 'uninstall_' . $file, $callable );
535 do_action( 'uninstall_' . $file );
543 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '' ) {
544 global $menu, $admin_page_hooks;
546 $file = plugin_basename( $file );
548 $admin_page_hooks[$file] = sanitize_title( $menu_title );
550 $hookname = get_plugin_page_hookname( $file, '' );
551 if (!empty ( $function ) && !empty ( $hookname ))
552 add_action( $hookname, $function );
554 if ( empty($icon_url) )
555 $icon_url = 'images/generic.png';
557 $menu[] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
562 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
563 global $menu, $admin_page_hooks, $_wp_last_object_menu;
565 $file = plugin_basename( $file );
567 $admin_page_hooks[$file] = sanitize_title( $menu_title );
569 $hookname = get_plugin_page_hookname( $file, '' );
570 if (!empty ( $function ) && !empty ( $hookname ))
571 add_action( $hookname, $function );
573 if ( empty($icon_url) )
574 $icon_url = 'images/generic.png';
576 $_wp_last_object_menu++;
578 $menu[$_wp_last_object_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
583 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
584 global $menu, $admin_page_hooks, $_wp_last_utility_menu;
586 $file = plugin_basename( $file );
588 $admin_page_hooks[$file] = sanitize_title( $menu_title );
590 $hookname = get_plugin_page_hookname( $file, '' );
591 if (!empty ( $function ) && !empty ( $hookname ))
592 add_action( $hookname, $function );
594 if ( empty($icon_url) )
595 $icon_url = 'images/generic.png';
597 $_wp_last_utility_menu++;
599 $menu[$_wp_last_utility_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
604 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
607 global $_wp_real_parent_file;
608 global $_wp_submenu_nopriv;
610 $file = plugin_basename( $file );
612 $parent = plugin_basename( $parent);
613 if ( isset( $_wp_real_parent_file[$parent] ) )
614 $parent = $_wp_real_parent_file[$parent];
616 if ( !current_user_can( $access_level ) ) {
617 $_wp_submenu_nopriv[$parent][$file] = true;
621 // If the parent doesn't already have a submenu, add a link to the parent
622 // as the first item in the submenu. If the submenu file is the same as the
623 // parent file someone is trying to link back to the parent manually. In
624 // this case, don't automatically add a link back to avoid duplication.
625 if (!isset( $submenu[$parent] ) && $file != $parent ) {
626 foreach ( $menu as $parent_menu ) {
627 if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
628 $submenu[$parent][] = $parent_menu;
632 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
634 $hookname = get_plugin_page_hookname( $file, $parent);
635 if (!empty ( $function ) && !empty ( $hookname ))
636 add_action( $hookname, $function );
642 * Add sub menu page to the tools main menu.
644 * @param string $page_title
645 * @param unknown_type $menu_title
646 * @param unknown_type $access_level
647 * @param unknown_type $file
648 * @param unknown_type $function
651 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
652 return add_submenu_page( 'tools.php', $page_title, $menu_title, $access_level, $file, $function );
655 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
656 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
659 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
660 return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
663 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
664 if ( current_user_can('edit_users') )
665 $parent = 'users.php';
667 $parent = 'profile.php';
668 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
671 function add_dashboard_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
672 return add_submenu_page( 'index.php', $page_title, $menu_title, $access_level, $file, $function );
675 function add_posts_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
676 return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
679 function add_media_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
680 return add_submenu_page( 'upload.php', $page_title, $menu_title, $access_level, $file, $function );
683 function add_links_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
684 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $access_level, $file, $function );
687 function add_pages_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
688 return add_submenu_page( 'edit-pages.php', $page_title, $menu_title, $access_level, $file, $function );
691 function add_comments_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
692 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $access_level, $file, $function );
696 // Pluggable Menu Support -- Private
699 function get_admin_page_parent( $parent = '' ) {
705 global $_wp_real_parent_file;
706 global $_wp_menu_nopriv;
707 global $_wp_submenu_nopriv;
709 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
710 if ( isset( $_wp_real_parent_file[$parent] ) )
711 $parent = $_wp_real_parent_file[$parent];
715 if ( !empty ( $parent_file ) ) {
716 if ( isset( $_wp_real_parent_file[$parent_file] ) )
717 $parent_file = $_wp_real_parent_file[$parent_file];
723 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
724 foreach ( $menu as $parent_menu ) {
725 if ( $parent_menu[2] == $plugin_page ) {
726 $parent_file = $plugin_page;
727 if ( isset( $_wp_real_parent_file[$parent_file] ) )
728 $parent_file = $_wp_real_parent_file[$parent_file];
732 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
733 $parent_file = $plugin_page;
734 if ( isset( $_wp_real_parent_file[$parent_file] ) )
735 $parent_file = $_wp_real_parent_file[$parent_file];
740 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
741 $parent_file = $pagenow;
742 if ( isset( $_wp_real_parent_file[$parent_file] ) )
743 $parent_file = $_wp_real_parent_file[$parent_file];
747 foreach (array_keys( $submenu ) as $parent) {
748 foreach ( $submenu[$parent] as $submenu_array ) {
749 if ( isset( $_wp_real_parent_file[$parent] ) )
750 $parent = $_wp_real_parent_file[$parent];
751 if ( $submenu_array[2] == $pagenow ) {
752 $parent_file = $parent;
755 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
756 $parent_file = $parent;
762 if ( empty($parent_file) )
767 function get_admin_page_title() {
774 if ( isset( $title ) && !empty ( $title ) ) {
778 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
780 $parent = $parent1 = get_admin_page_parent();
782 if ( empty ( $parent) ) {
783 foreach ( $menu as $menu_array ) {
784 if ( isset( $menu_array[3] ) ) {
785 if ( $menu_array[2] == $pagenow ) {
786 $title = $menu_array[3];
787 return $menu_array[3];
789 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
790 $title = $menu_array[3];
791 return $menu_array[3];
794 $title = $menu_array[0];
799 foreach (array_keys( $submenu ) as $parent) {
800 foreach ( $submenu[$parent] as $submenu_array ) {
801 if ( isset( $plugin_page ) &&
802 ($plugin_page == $submenu_array[2] ) &&
803 (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
805 $title = $submenu_array[3];
806 return $submenu_array[3];
809 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
812 if ( isset( $submenu_array[3] ) ) {
813 $title = $submenu_array[3];
814 return $submenu_array[3];
816 $title = $submenu_array[0];
826 function get_plugin_page_hook( $plugin_page, $parent_page ) {
827 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
828 if ( has_action($hook) )
834 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
835 global $admin_page_hooks;
837 $parent = get_admin_page_parent( $parent_page );
839 $page_type = 'admin';
840 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
841 if ( isset( $admin_page_hooks[$plugin_page] ) )
842 $page_type = 'toplevel';
844 if ( isset( $admin_page_hooks[$parent] ))
845 $page_type = $admin_page_hooks[$parent];
846 } else if ( isset( $admin_page_hooks[$parent] ) ) {
847 $page_type = $admin_page_hooks[$parent];
850 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
852 return $page_type.'_page_'.$plugin_name;
855 function user_can_access_admin_page() {
859 global $_wp_menu_nopriv;
860 global $_wp_submenu_nopriv;
863 $parent = get_admin_page_parent();
865 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
868 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
871 if ( empty( $parent) ) {
872 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
874 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
876 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
878 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
879 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
881 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
887 if ( isset( $submenu[$parent] ) ) {
888 foreach ( $submenu[$parent] as $submenu_array ) {
889 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
890 if ( current_user_can( $submenu_array[1] ))
894 } else if ( $submenu_array[2] == $pagenow ) {
895 if ( current_user_can( $submenu_array[1] ))
903 foreach ( $menu as $menu_array ) {
904 if ( $menu_array[2] == $parent) {
905 if ( current_user_can( $menu_array[1] ))
915 /* Whitelist functions */
918 * Register a setting and its sanitization callback
922 * @param string $option_group A settings group name. Can be anything.
923 * @param string $option_name The name of an option to sanitize and save.
924 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
927 function register_setting($option_group, $option_name, $sanitize_callback = '') {
928 return add_option_update_handler($option_group, $option_name, $sanitize_callback);
932 * Unregister a setting
936 * @param unknown_type $option_group
937 * @param unknown_type $option_name
938 * @param unknown_type $sanitize_callback
941 function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
942 return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
946 * {@internal Missing Short Description}}
950 * @param unknown_type $option_group
951 * @param unknown_type $option_name
952 * @param unknown_type $sanitize_callback
954 function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
955 global $new_whitelist_options;
956 $new_whitelist_options[ $option_group ][] = $option_name;
957 if ( $sanitize_callback != '' )
958 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
962 * {@internal Missing Short Description}}
966 * @param unknown_type $option_group
967 * @param unknown_type $option_name
968 * @param unknown_type $sanitize_callback
970 function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
971 global $new_whitelist_options;
972 $pos = array_search( $option_name, $new_whitelist_options );
973 if ( $pos !== false )
974 unset( $new_whitelist_options[ $option_group ][ $pos ] );
975 if ( $sanitize_callback != '' )
976 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
980 * {@internal Missing Short Description}}
984 * @param unknown_type $options
987 function option_update_filter( $options ) {
988 global $new_whitelist_options;
990 if ( is_array( $new_whitelist_options ) )
991 $options = add_option_whitelist( $new_whitelist_options, $options );
995 add_filter( 'whitelist_options', 'option_update_filter' );
998 * {@internal Missing Short Description}}
1002 * @param unknown_type $new_options
1003 * @param unknown_type $options
1006 function add_option_whitelist( $new_options, $options = '' ) {
1007 if( $options == '' ) {
1008 global $whitelist_options;
1010 $whitelist_options = $options;
1012 foreach( $new_options as $page => $keys ) {
1013 foreach( $keys as $key ) {
1014 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1015 $whitelist_options[ $page ] = array();
1016 $whitelist_options[ $page ][] = $key;
1018 $pos = array_search( $key, $whitelist_options[ $page ] );
1019 if ( $pos === false )
1020 $whitelist_options[ $page ][] = $key;
1024 return $whitelist_options;
1028 * {@internal Missing Short Description}}
1032 * @param unknown_type $del_options
1033 * @param unknown_type $options
1036 function remove_option_whitelist( $del_options, $options = '' ) {
1037 if( $options == '' ) {
1038 global $whitelist_options;
1040 $whitelist_options = $options;
1042 foreach( $del_options as $page => $keys ) {
1043 foreach( $keys as $key ) {
1044 $pos = array_search( $key, $whitelist_options[ $page ] );
1045 if( $pos !== false )
1046 unset( $whitelist_options[ $page ][ $pos ] );
1049 return $whitelist_options;
1053 * Output nonce, action, and option_page fields for a settings page.
1057 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1059 function settings_fields($option_group) {
1060 echo "<input type='hidden' name='option_page' value='$option_group' />";
1061 echo '<input type="hidden" name="action" value="update" />';
1062 wp_nonce_field("$option_group-options");