-
+ E8D5E52C66FFB577FC51DABBDC937EF4A5092D83A8F395E326CABF5952B1541DB3E25A3C4349509E0B2562FBE8C590278F79E7A20ACA263F09910204B1D69891mp-wp/wp-admin/includes/plugin.php(0 . 0)(1 . 1065)
37734 <?php
37735 /**
37736 * WordPress Plugin Administration API
37737 *
37738 * @package WordPress
37739 * @subpackage Administration
37740 */
37741
37742 /**
37743 * Parse the plugin contents to retrieve plugin's metadata.
37744 *
37745 * The metadata of the plugin's data searches for the following in the plugin's
37746 * header. All plugin data must be on its own line. For plugin description, it
37747 * must not have any newlines or only parts of the description will be displayed
37748 * and the same goes for the plugin data. The below is formatted for printing.
37749 *
37750 * <code>
37751 * /*
37752 * Plugin Name: Name of Plugin
37753 * Plugin URI: Link to plugin information
37754 * Description: Plugin Description
37755 * Author: Plugin author's name
37756 * Author URI: Link to the author's web site
37757 * Version: Must be set in the plugin for WordPress 2.3+
37758 * Text Domain: Optional. Unique identifier, should be same as the one used in
37759 * plugin_text_domain()
37760 * Domain Path: Optional. Only useful if the translations are located in a
37761 * folder above the plugin's base path. For example, if .mo files are
37762 * located in the locale folder then Domain Path will be "/locale/" and
37763 * must have the first slash. Defaults to the base folder the plugin is
37764 * located in.
37765 * * / # Remove the space to close comment
37766 * </code>
37767 *
37768 * Plugin data returned array contains the following:
37769 * 'Name' - Name of the plugin, must be unique.
37770 * 'Title' - Title of the plugin and the link to the plugin's web site.
37771 * 'Description' - Description of what the plugin does and/or notes
37772 * from the author.
37773 * 'Author' - The author's name
37774 * 'AuthorURI' - The authors web site address.
37775 * 'Version' - The plugin version number.
37776 * 'PluginURI' - Plugin web site address.
37777 * 'TextDomain' - Plugin's text domain for localization.
37778 * 'DomainPath' - Plugin's relative directory path to .mo files.
37779 *
37780 * Some users have issues with opening large files and manipulating the contents
37781 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
37782 * the plugin contents when it has all of the required plugin data.
37783 *
37784 * The first 8kiB of the file will be pulled in and if the plugin data is not
37785 * within that first 8kiB, then the plugin author should correct their plugin
37786 * and move the plugin data headers to the top.
37787 *
37788 * The plugin file is assumed to have permissions to allow for scripts to read
37789 * the file. This is not checked however and the file is only opened for
37790 * reading.
37791 *
37792 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
37793 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
37794 * @since 1.5.0
37795 *
37796 * @param string $plugin_file Path to the plugin file
37797 * @param bool $markup If the returned data should have HTML markup applied
37798 * @param bool $translate If the returned data should be translated
37799 * @return array See above for description.
37800 */
37801 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
37802 // We don't need to write to the file, so just open for reading.
37803 $fp = fopen($plugin_file, 'r');
37804
37805 // Pull only the first 8kiB of the file in.
37806 $plugin_data = fread( $fp, 8192 );
37807
37808 // PHP will close file handle, but we are good citizens.
37809 fclose($fp);
37810
37811 preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
37812 preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
37813 preg_match( '|Version:(.*)|i', $plugin_data, $version );
37814 preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
37815 preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
37816 preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
37817 preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
37818 preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
37819
37820 foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
37821 if ( !empty( ${$field} ) )
37822 ${$field} = trim(${$field}[1]);
37823 else
37824 ${$field} = '';
37825 }
37826
37827 $plugin_data = array(
37828 'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
37829 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
37830 'TextDomain' => $text_domain, 'DomainPath' => $domain_path
37831 );
37832 if ( $markup || $translate )
37833 $plugin_data = _get_plugin_data_markup_translate($plugin_data, $markup, $translate);
37834 return $plugin_data;
37835 }
37836
37837 function _get_plugin_data_markup_translate($plugin_data, $markup = true, $translate = true) {
37838
37839 //Translate fields
37840 if( $translate && ! empty($plugin_data['TextDomain']) ) {
37841 if( ! empty( $plugin_data['DomainPath'] ) )
37842 load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file). $plugin_data['DomainPath']);
37843 else
37844 load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file));
37845
37846 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
37847 $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
37848 }
37849
37850 //Apply Markup
37851 if ( $markup ) {
37852 if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
37853 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
37854 else
37855 $plugin_data['Title'] = $plugin_data['Name'];
37856
37857 if ( ! empty($plugin_data['AuthorURI']) )
37858 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
37859
37860 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
37861 if( ! empty($plugin_data['Author']) )
37862 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
37863 }
37864
37865 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
37866
37867 // Sanitize all displayed data
37868 $plugin_data['Title'] = wp_kses($plugin_data['Title'], $plugins_allowedtags);
37869 $plugin_data['Version'] = wp_kses($plugin_data['Version'], $plugins_allowedtags);
37870 $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
37871 $plugin_data['Author'] = wp_kses($plugin_data['Author'], $plugins_allowedtags);
37872
37873 return $plugin_data;
37874 }
37875
37876 /**
37877 * Check the plugins directory and retrieve all plugin files with plugin data.
37878 *
37879 * WordPress only supports plugin files in the base plugins directory
37880 * (wp-content/plugins) and in one directory above the plugins directory
37881 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
37882 * must be found in those two locations. It is recommended that do keep your
37883 * plugin files in directories.
37884 *
37885 * The file with the plugin data is the file that will be included and therefore
37886 * needs to have the main execution for the plugin. This does not mean
37887 * everything must be contained in the file and it is recommended that the file
37888 * be split for maintainability. Keep everything in one file for extreme
37889 * optimization purposes.
37890 *
37891 * @since unknown
37892 *
37893 * @param string $plugin_folder Optional. Relative path to single plugin folder.
37894 * @return array Key is the plugin file path and the value is an array of the plugin data.
37895 */
37896 function get_plugins($plugin_folder = '') {
37897
37898 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
37899 $cache_plugins = array();
37900
37901 if ( isset($cache_plugins[ $plugin_folder ]) )
37902 return $cache_plugins[ $plugin_folder ];
37903
37904 $wp_plugins = array ();
37905 $plugin_root = WP_PLUGIN_DIR;
37906 if( !empty($plugin_folder) )
37907 $plugin_root .= $plugin_folder;
37908
37909 // Files in wp-content/plugins directory
37910 $plugins_dir = @ opendir( $plugin_root);
37911 if ( $plugins_dir ) {
37912 while (($file = readdir( $plugins_dir ) ) !== false ) {
37913 if ( substr($file, 0, 1) == '.' )
37914 continue;
37915 if ( is_dir( $plugin_root.'/'.$file ) ) {
37916 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
37917 if ( $plugins_subdir ) {
37918 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
37919 if ( substr($subfile, 0, 1) == '.' )
37920 continue;
37921 if ( substr($subfile, -4) == '.php' )
37922 $plugin_files[] = "$file/$subfile";
37923 }
37924 }
37925 } else {
37926 if ( substr($file, -4) == '.php' )
37927 $plugin_files[] = $file;
37928 }
37929 }
37930 }
37931 @closedir( $plugins_dir );
37932 @closedir( $plugins_subdir );
37933
37934 if ( !$plugins_dir || !$plugin_files )
37935 return $wp_plugins;
37936
37937 foreach ( $plugin_files as $plugin_file ) {
37938 if ( !is_readable( "$plugin_root/$plugin_file" ) )
37939 continue;
37940
37941 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
37942
37943 if ( empty ( $plugin_data['Name'] ) )
37944 continue;
37945
37946 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
37947 }
37948
37949 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
37950
37951 $cache_plugins[ $plugin_folder ] = $wp_plugins;
37952 wp_cache_set('plugins', $cache_plugins, 'plugins');
37953
37954 return $wp_plugins;
37955 }
37956
37957 /**
37958 * Check whether the plugin is active by checking the active_plugins list.
37959 *
37960 * @since 2.5.0
37961 *
37962 * @param string $plugin Base plugin path from plugins directory.
37963 * @return bool True, if in the active plugins list. False, not in the list.
37964 */
37965 function is_plugin_active($plugin) {
37966 return in_array($plugin, get_option('active_plugins'));
37967 }
37968
37969 /**
37970 * Attempts activation of plugin in a "sandbox" and redirects on success.
37971 *
37972 * A plugin that is already activated will not attempt to be activated again.
37973 *
37974 * The way it works is by setting the redirection to the error before trying to
37975 * include the plugin file. If the plugin fails, then the redirection will not
37976 * be overwritten with the success message. Also, the options will not be
37977 * updated and the activation hook will not be called on plugin error.
37978 *
37979 * It should be noted that in no way the below code will actually prevent errors
37980 * within the file. The code should not be used elsewhere to replicate the
37981 * "sandbox", which uses redirection to work.
37982 * {@source 13 1}
37983 *
37984 * If any errors are found or text is outputted, then it will be captured to
37985 * ensure that the success redirection will update the error redirection.
37986 *
37987 * @since unknown
37988 *
37989 * @param string $plugin Plugin path to main plugin file with plugin data.
37990 * @param string $redirect Optional. URL to redirect to.
37991 * @return WP_Error|null WP_Error on invalid file or null on success.
37992 */
37993 function activate_plugin($plugin, $redirect = '') {
37994 $current = get_option('active_plugins');
37995 $plugin = plugin_basename(trim($plugin));
37996
37997 $valid = validate_plugin($plugin);
37998 if ( is_wp_error($valid) )
37999 return $valid;
38000
38001 if ( !in_array($plugin, $current) ) {
38002 if ( !empty($redirect) )
38003 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
38004 ob_start();
38005 @include(WP_PLUGIN_DIR . '/' . $plugin);
38006 $current[] = $plugin;
38007 sort($current);
38008 update_option('active_plugins', $current);
38009 do_action('activate_' . $plugin);
38010 ob_end_clean();
38011 }
38012
38013 return null;
38014 }
38015
38016 /**
38017 * Deactivate a single plugin or multiple plugins.
38018 *
38019 * The deactivation hook is disabled by the plugin upgrader by using the $silent
38020 * parameter.
38021 *
38022 * @since unknown
38023 *
38024 * @param string|array $plugins Single plugin or list of plugins to deactivate.
38025 * @param bool $silent Optional, default is false. Prevent calling deactivate hook.
38026 */
38027 function deactivate_plugins($plugins, $silent= false) {
38028 $current = get_option('active_plugins');
38029
38030 if ( !is_array($plugins) )
38031 $plugins = array($plugins);
38032
38033 foreach ( $plugins as $plugin ) {
38034 $plugin = plugin_basename($plugin);
38035 if( ! is_plugin_active($plugin) )
38036 continue;
38037 array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
38038 if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
38039 do_action('deactivate_' . trim( $plugin ));
38040 }
38041
38042 update_option('active_plugins', $current);
38043 }
38044
38045 /**
38046 * Activate multiple plugins.
38047 *
38048 * When WP_Error is returned, it does not mean that one of the plugins had
38049 * errors. It means that one or more of the plugins file path was invalid.
38050 *
38051 * The execution will be halted as soon as one of the plugins has an error.
38052 *
38053 * @since unknown
38054 *
38055 * @param string|array $plugins
38056 * @param string $redirect Redirect to page after successful activation.
38057 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
38058 */
38059 function activate_plugins($plugins, $redirect = '') {
38060 if ( !is_array($plugins) )
38061 $plugins = array($plugins);
38062
38063 $errors = array();
38064 foreach ( (array) $plugins as $plugin ) {
38065 if ( !empty($redirect) )
38066 $redirect = add_query_arg('plugin', $plugin, $redirect);
38067 $result = activate_plugin($plugin, $redirect);
38068 if ( is_wp_error($result) )
38069 $errors[$plugin] = $result;
38070 }
38071
38072 if ( !empty($errors) )
38073 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
38074
38075 return true;
38076 }
38077
38078 /**
38079 * Remove directory and files of a plugin for a single or list of plugin(s).
38080 *
38081 * If the plugins parameter list is empty, false will be returned. True when
38082 * completed.
38083 *
38084 * @since unknown
38085 *
38086 * @param array $plugins List of plugin
38087 * @param string $redirect Redirect to page when complete.
38088 * @return mixed
38089 */
38090 function delete_plugins($plugins, $redirect = '' ) {
38091 global $wp_filesystem;
38092
38093 if( empty($plugins) )
38094 return false;
38095
38096 $checked = array();
38097 foreach( $plugins as $plugin )
38098 $checked[] = 'checked[]=' . $plugin;
38099
38100 ob_start();
38101 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
38102 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
38103 $data = ob_get_contents();
38104 ob_end_clean();
38105 if( ! empty($data) ){
38106 include_once( ABSPATH . 'wp-admin/admin-header.php');
38107 echo $data;
38108 include( ABSPATH . 'wp-admin/admin-footer.php');
38109 exit;
38110 }
38111 return;
38112 }
38113
38114 if ( ! WP_Filesystem($credentials) ) {
38115 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
38116 $data = ob_get_contents();
38117 ob_end_clean();
38118 if( ! empty($data) ){
38119 include_once( ABSPATH . 'wp-admin/admin-header.php');
38120 echo $data;
38121 include( ABSPATH . 'wp-admin/admin-footer.php');
38122 exit;
38123 }
38124 return;
38125 }
38126
38127 if ( $wp_filesystem->errors->get_error_code() ) {
38128 return $wp_filesystem->errors;
38129 }
38130
38131 if ( ! is_object($wp_filesystem) )
38132 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
38133
38134 if ( $wp_filesystem->errors->get_error_code() )
38135 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
38136
38137 //Get the base plugin folder
38138 $plugins_dir = $wp_filesystem->wp_plugins_dir();
38139 if ( empty($plugins_dir) )
38140 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
38141
38142 $plugins_dir = trailingslashit( $plugins_dir );
38143
38144 $errors = array();
38145
38146 foreach( $plugins as $plugin_file ) {
38147 // Run Uninstall hook
38148 if ( is_uninstallable_plugin( $plugin_file ) )
38149 uninstall_plugin($plugin_file);
38150
38151 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
38152 // If plugin is in its own directory, recursively delete the directory.
38153 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
38154 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
38155 else
38156 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
38157
38158 if ( ! $deleted )
38159 $errors[] = $plugin_file;
38160 }
38161
38162 if ( ! empty($errors) )
38163 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
38164
38165 // Force refresh of plugin update information
38166 delete_option('update_plugins');
38167
38168 return true;
38169 }
38170
38171 function validate_active_plugins() {
38172 $check_plugins = get_option('active_plugins');
38173
38174 // Sanity check. If the active plugin list is not an array, make it an
38175 // empty array.
38176 if ( !is_array($check_plugins) ) {
38177 update_option('active_plugins', array());
38178 return;
38179 }
38180
38181 //Invalid is any plugin that is deactivated due to error.
38182 $invalid = array();
38183
38184 // If a plugin file does not exist, remove it from the list of active
38185 // plugins.
38186 foreach ( $check_plugins as $check_plugin ) {
38187 $result = validate_plugin($check_plugin);
38188 if ( is_wp_error( $result ) ) {
38189 $invalid[$check_plugin] = $result;
38190 deactivate_plugins( $check_plugin, true);
38191 }
38192 }
38193 return $invalid;
38194 }
38195
38196 /**
38197 * Validate the plugin path.
38198 *
38199 * Checks that the file exists and {@link validate_file() is valid file}.
38200 *
38201 * @since unknown
38202 *
38203 * @param string $plugin Plugin Path
38204 * @return WP_Error|int 0 on success, WP_Error on failure.
38205 */
38206 function validate_plugin($plugin) {
38207 if ( validate_file($plugin) )
38208 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
38209 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
38210 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
38211
38212 return 0;
38213 }
38214
38215 /**
38216 * Whether the plugin can be uninstalled.
38217 *
38218 * @since 2.7.0
38219 *
38220 * @param string $plugin Plugin path to check.
38221 * @return bool Whether plugin can be uninstalled.
38222 */
38223 function is_uninstallable_plugin($plugin) {
38224 $file = plugin_basename($plugin);
38225
38226 $uninstallable_plugins = (array) get_option('uninstall_plugins');
38227 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
38228 return true;
38229
38230 return false;
38231 }
38232
38233 /**
38234 * Uninstall a single plugin.
38235 *
38236 * Calls the uninstall hook, if it is available.
38237 *
38238 * @since 2.7.0
38239 *
38240 * @param string $plugin Relative plugin path from Plugin Directory.
38241 */
38242 function uninstall_plugin($plugin) {
38243 $file = plugin_basename($plugin);
38244
38245 $uninstallable_plugins = (array) get_option('uninstall_plugins');
38246 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
38247 if ( isset( $uninstallable_plugins[$file] ) ) {
38248 unset($uninstallable_plugins[$file]);
38249 update_option('uninstall_plugins', $uninstallable_plugins);
38250 }
38251 unset($uninstallable_plugins);
38252
38253 define('WP_UNINSTALL_PLUGIN', $file);
38254 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
38255
38256 return true;
38257 }
38258
38259 if ( isset( $uninstallable_plugins[$file] ) ) {
38260 $callable = $uninstallable_plugins[$file];
38261 unset($uninstallable_plugins[$file]);
38262 update_option('uninstall_plugins', $uninstallable_plugins);
38263 unset($uninstallable_plugins);
38264
38265 include WP_PLUGIN_DIR . '/' . $file;
38266
38267 add_action( 'uninstall_' . $file, $callable );
38268 do_action( 'uninstall_' . $file );
38269 }
38270 }
38271
38272 //
38273 // Menu
38274 //
38275
38276 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '' ) {
38277 global $menu, $admin_page_hooks;
38278
38279 $file = plugin_basename( $file );
38280
38281 $admin_page_hooks[$file] = sanitize_title( $menu_title );
38282
38283 $hookname = get_plugin_page_hookname( $file, '' );
38284 if (!empty ( $function ) && !empty ( $hookname ))
38285 add_action( $hookname, $function );
38286
38287 if ( empty($icon_url) )
38288 $icon_url = 'images/generic.png';
38289
38290 $menu[] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
38291
38292 return $hookname;
38293 }
38294
38295 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
38296 global $menu, $admin_page_hooks, $_wp_last_object_menu;
38297
38298 $file = plugin_basename( $file );
38299
38300 $admin_page_hooks[$file] = sanitize_title( $menu_title );
38301
38302 $hookname = get_plugin_page_hookname( $file, '' );
38303 if (!empty ( $function ) && !empty ( $hookname ))
38304 add_action( $hookname, $function );
38305
38306 if ( empty($icon_url) )
38307 $icon_url = 'images/generic.png';
38308
38309 $_wp_last_object_menu++;
38310
38311 $menu[$_wp_last_object_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
38312
38313 return $hookname;
38314 }
38315
38316 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
38317 global $menu, $admin_page_hooks, $_wp_last_utility_menu;
38318
38319 $file = plugin_basename( $file );
38320
38321 $admin_page_hooks[$file] = sanitize_title( $menu_title );
38322
38323 $hookname = get_plugin_page_hookname( $file, '' );
38324 if (!empty ( $function ) && !empty ( $hookname ))
38325 add_action( $hookname, $function );
38326
38327 if ( empty($icon_url) )
38328 $icon_url = 'images/generic.png';
38329
38330 $_wp_last_utility_menu++;
38331
38332 $menu[$_wp_last_utility_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
38333
38334 return $hookname;
38335 }
38336
38337 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
38338 global $submenu;
38339 global $menu;
38340 global $_wp_real_parent_file;
38341 global $_wp_submenu_nopriv;
38342
38343 $file = plugin_basename( $file );
38344
38345 $parent = plugin_basename( $parent);
38346 if ( isset( $_wp_real_parent_file[$parent] ) )
38347 $parent = $_wp_real_parent_file[$parent];
38348
38349 if ( !current_user_can( $access_level ) ) {
38350 $_wp_submenu_nopriv[$parent][$file] = true;
38351 return false;
38352 }
38353
38354 // If the parent doesn't already have a submenu, add a link to the parent
38355 // as the first item in the submenu. If the submenu file is the same as the
38356 // parent file someone is trying to link back to the parent manually. In
38357 // this case, don't automatically add a link back to avoid duplication.
38358 if (!isset( $submenu[$parent] ) && $file != $parent ) {
38359 foreach ( $menu as $parent_menu ) {
38360 if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
38361 $submenu[$parent][] = $parent_menu;
38362 }
38363 }
38364
38365 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
38366
38367 $hookname = get_plugin_page_hookname( $file, $parent);
38368 if (!empty ( $function ) && !empty ( $hookname ))
38369 add_action( $hookname, $function );
38370
38371 return $hookname;
38372 }
38373
38374 /**
38375 * Add sub menu page to the tools main menu.
38376 *
38377 * @param string $page_title
38378 * @param unknown_type $menu_title
38379 * @param unknown_type $access_level
38380 * @param unknown_type $file
38381 * @param unknown_type $function
38382 * @return unknown
38383 */
38384 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38385 return add_submenu_page( 'tools.php', $page_title, $menu_title, $access_level, $file, $function );
38386 }
38387
38388 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38389 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
38390 }
38391
38392 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38393 return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
38394 }
38395
38396 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38397 if ( current_user_can('edit_users') )
38398 $parent = 'users.php';
38399 else
38400 $parent = 'profile.php';
38401 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
38402 }
38403
38404 function add_dashboard_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38405 return add_submenu_page( 'index.php', $page_title, $menu_title, $access_level, $file, $function );
38406 }
38407
38408 function add_posts_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38409 return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
38410 }
38411
38412 function add_media_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38413 return add_submenu_page( 'upload.php', $page_title, $menu_title, $access_level, $file, $function );
38414 }
38415
38416 function add_links_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38417 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $access_level, $file, $function );
38418 }
38419
38420 function add_pages_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38421 return add_submenu_page( 'edit-pages.php', $page_title, $menu_title, $access_level, $file, $function );
38422 }
38423
38424 function add_comments_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
38425 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $access_level, $file, $function );
38426 }
38427
38428 //
38429 // Pluggable Menu Support -- Private
38430 //
38431
38432 function get_admin_page_parent( $parent = '' ) {
38433 global $parent_file;
38434 global $menu;
38435 global $submenu;
38436 global $pagenow;
38437 global $plugin_page;
38438 global $_wp_real_parent_file;
38439 global $_wp_menu_nopriv;
38440 global $_wp_submenu_nopriv;
38441
38442 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
38443 if ( isset( $_wp_real_parent_file[$parent] ) )
38444 $parent = $_wp_real_parent_file[$parent];
38445 return $parent;
38446 }
38447 /*
38448 if ( !empty ( $parent_file ) ) {
38449 if ( isset( $_wp_real_parent_file[$parent_file] ) )
38450 $parent_file = $_wp_real_parent_file[$parent_file];
38451
38452 return $parent_file;
38453 }
38454 */
38455
38456 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
38457 foreach ( $menu as $parent_menu ) {
38458 if ( $parent_menu[2] == $plugin_page ) {
38459 $parent_file = $plugin_page;
38460 if ( isset( $_wp_real_parent_file[$parent_file] ) )
38461 $parent_file = $_wp_real_parent_file[$parent_file];
38462 return $parent_file;
38463 }
38464 }
38465 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
38466 $parent_file = $plugin_page;
38467 if ( isset( $_wp_real_parent_file[$parent_file] ) )
38468 $parent_file = $_wp_real_parent_file[$parent_file];
38469 return $parent_file;
38470 }
38471 }
38472
38473 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
38474 $parent_file = $pagenow;
38475 if ( isset( $_wp_real_parent_file[$parent_file] ) )
38476 $parent_file = $_wp_real_parent_file[$parent_file];
38477 return $parent_file;
38478 }
38479
38480 foreach (array_keys( $submenu ) as $parent) {
38481 foreach ( $submenu[$parent] as $submenu_array ) {
38482 if ( isset( $_wp_real_parent_file[$parent] ) )
38483 $parent = $_wp_real_parent_file[$parent];
38484 if ( $submenu_array[2] == $pagenow ) {
38485 $parent_file = $parent;
38486 return $parent;
38487 } else
38488 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
38489 $parent_file = $parent;
38490 return $parent;
38491 }
38492 }
38493 }
38494
38495 if ( empty($parent_file) )
38496 $parent_file = '';
38497 return '';
38498 }
38499
38500 function get_admin_page_title() {
38501 global $title;
38502 global $menu;
38503 global $submenu;
38504 global $pagenow;
38505 global $plugin_page;
38506
38507 if ( isset( $title ) && !empty ( $title ) ) {
38508 return $title;
38509 }
38510
38511 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
38512
38513 $parent = $parent1 = get_admin_page_parent();
38514
38515 if ( empty ( $parent) ) {
38516 foreach ( $menu as $menu_array ) {
38517 if ( isset( $menu_array[3] ) ) {
38518 if ( $menu_array[2] == $pagenow ) {
38519 $title = $menu_array[3];
38520 return $menu_array[3];
38521 } else
38522 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
38523 $title = $menu_array[3];
38524 return $menu_array[3];
38525 }
38526 } else {
38527 $title = $menu_array[0];
38528 return $title;
38529 }
38530 }
38531 } else {
38532 foreach (array_keys( $submenu ) as $parent) {
38533 foreach ( $submenu[$parent] as $submenu_array ) {
38534 if ( isset( $plugin_page ) &&
38535 ($plugin_page == $submenu_array[2] ) &&
38536 (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
38537 ) {
38538 $title = $submenu_array[3];
38539 return $submenu_array[3];
38540 }
38541
38542 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
38543 continue;
38544
38545 if ( isset( $submenu_array[3] ) ) {
38546 $title = $submenu_array[3];
38547 return $submenu_array[3];
38548 } else {
38549 $title = $submenu_array[0];
38550 return $title;
38551 }
38552 }
38553 }
38554 }
38555
38556 return $title;
38557 }
38558
38559 function get_plugin_page_hook( $plugin_page, $parent_page ) {
38560 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
38561 if ( has_action($hook) )
38562 return $hook;
38563 else
38564 return null;
38565 }
38566
38567 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
38568 global $admin_page_hooks;
38569
38570 $parent = get_admin_page_parent( $parent_page );
38571
38572 $page_type = 'admin';
38573 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
38574 if ( isset( $admin_page_hooks[$plugin_page] ) )
38575 $page_type = 'toplevel';
38576 else
38577 if ( isset( $admin_page_hooks[$parent] ))
38578 $page_type = $admin_page_hooks[$parent];
38579 } else if ( isset( $admin_page_hooks[$parent] ) ) {
38580 $page_type = $admin_page_hooks[$parent];
38581 }
38582
38583 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
38584
38585 return $page_type.'_page_'.$plugin_name;
38586 }
38587
38588 function user_can_access_admin_page() {
38589 global $pagenow;
38590 global $menu;
38591 global $submenu;
38592 global $_wp_menu_nopriv;
38593 global $_wp_submenu_nopriv;
38594 global $plugin_page;
38595
38596 $parent = get_admin_page_parent();
38597
38598 if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
38599 return false;
38600
38601 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
38602 return false;
38603
38604 if ( empty( $parent) ) {
38605 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
38606 return false;
38607 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
38608 return false;
38609 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
38610 return false;
38611 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
38612 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
38613 return false;
38614 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
38615 return false;
38616 }
38617 return true;
38618 }
38619
38620 if ( isset( $submenu[$parent] ) ) {
38621 foreach ( $submenu[$parent] as $submenu_array ) {
38622 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
38623 if ( current_user_can( $submenu_array[1] ))
38624 return true;
38625 else
38626 return false;
38627 } else if ( $submenu_array[2] == $pagenow ) {
38628 if ( current_user_can( $submenu_array[1] ))
38629 return true;
38630 else
38631 return false;
38632 }
38633 }
38634 }
38635
38636 foreach ( $menu as $menu_array ) {
38637 if ( $menu_array[2] == $parent) {
38638 if ( current_user_can( $menu_array[1] ))
38639 return true;
38640 else
38641 return false;
38642 }
38643 }
38644
38645 return true;
38646 }
38647
38648 /* Whitelist functions */
38649
38650 /**
38651 * Register a setting and its sanitization callback
38652 *
38653 * @since 2.7.0
38654 *
38655 * @param string $option_group A settings group name. Can be anything.
38656 * @param string $option_name The name of an option to sanitize and save.
38657 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
38658 * @return unknown
38659 */
38660 function register_setting($option_group, $option_name, $sanitize_callback = '') {
38661 return add_option_update_handler($option_group, $option_name, $sanitize_callback);
38662 }
38663
38664 /**
38665 * Unregister a setting
38666 *
38667 * @since 2.7.0
38668 *
38669 * @param unknown_type $option_group
38670 * @param unknown_type $option_name
38671 * @param unknown_type $sanitize_callback
38672 * @return unknown
38673 */
38674 function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
38675 return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
38676 }
38677
38678 /**
38679 * {@internal Missing Short Description}}
38680 *
38681 * @since unknown
38682 *
38683 * @param unknown_type $option_group
38684 * @param unknown_type $option_name
38685 * @param unknown_type $sanitize_callback
38686 */
38687 function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
38688 global $new_whitelist_options;
38689 $new_whitelist_options[ $option_group ][] = $option_name;
38690 if ( $sanitize_callback != '' )
38691 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
38692 }
38693
38694 /**
38695 * {@internal Missing Short Description}}
38696 *
38697 * @since unknown
38698 *
38699 * @param unknown_type $option_group
38700 * @param unknown_type $option_name
38701 * @param unknown_type $sanitize_callback
38702 */
38703 function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
38704 global $new_whitelist_options;
38705 $pos = array_search( $option_name, $new_whitelist_options );
38706 if ( $pos !== false )
38707 unset( $new_whitelist_options[ $option_group ][ $pos ] );
38708 if ( $sanitize_callback != '' )
38709 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
38710 }
38711
38712 /**
38713 * {@internal Missing Short Description}}
38714 *
38715 * @since unknown
38716 *
38717 * @param unknown_type $options
38718 * @return unknown
38719 */
38720 function option_update_filter( $options ) {
38721 global $new_whitelist_options;
38722
38723 if ( is_array( $new_whitelist_options ) )
38724 $options = add_option_whitelist( $new_whitelist_options, $options );
38725
38726 return $options;
38727 }
38728 add_filter( 'whitelist_options', 'option_update_filter' );
38729
38730 /**
38731 * {@internal Missing Short Description}}
38732 *
38733 * @since unknown
38734 *
38735 * @param unknown_type $new_options
38736 * @param unknown_type $options
38737 * @return unknown
38738 */
38739 function add_option_whitelist( $new_options, $options = '' ) {
38740 if( $options == '' ) {
38741 global $whitelist_options;
38742 } else {
38743 $whitelist_options = $options;
38744 }
38745 foreach( $new_options as $page => $keys ) {
38746 foreach( $keys as $key ) {
38747 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
38748 $whitelist_options[ $page ] = array();
38749 $whitelist_options[ $page ][] = $key;
38750 } else {
38751 $pos = array_search( $key, $whitelist_options[ $page ] );
38752 if ( $pos === false )
38753 $whitelist_options[ $page ][] = $key;
38754 }
38755 }
38756 }
38757 return $whitelist_options;
38758 }
38759
38760 /**
38761 * {@internal Missing Short Description}}
38762 *
38763 * @since unknown
38764 *
38765 * @param unknown_type $del_options
38766 * @param unknown_type $options
38767 * @return unknown
38768 */
38769 function remove_option_whitelist( $del_options, $options = '' ) {
38770 if( $options == '' ) {
38771 global $whitelist_options;
38772 } else {
38773 $whitelist_options = $options;
38774 }
38775 foreach( $del_options as $page => $keys ) {
38776 foreach( $keys as $key ) {
38777 $pos = array_search( $key, $whitelist_options[ $page ] );
38778 if( $pos !== false )
38779 unset( $whitelist_options[ $page ][ $pos ] );
38780 }
38781 }
38782 return $whitelist_options;
38783 }
38784
38785 /**
38786 * Output nonce, action, and option_page fields for a settings page.
38787 *
38788 * @since 2.7.0
38789 *
38790 * @param string $option_group A settings group name. This should match the group name used in register_setting().
38791 */
38792 function settings_fields($option_group) {
38793 echo "<input type='hidden' name='option_page' value='$option_group' />";
38794 echo '<input type="hidden" name="action" value="update" />';
38795 wp_nonce_field("$option_group-options");
38796 }
38797
38798 ?>