raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Plugin Administration API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Parse the plugin contents to retrieve plugin's metadata.
mp-wp_genesis 11 *
mp-wp_genesis 12 * The metadata of the plugin's data searches for the following in the plugin's
mp-wp_genesis 13 * header. All plugin data must be on its own line. For plugin description, it
mp-wp_genesis 14 * must not have any newlines or only parts of the description will be displayed
mp-wp_genesis 15 * and the same goes for the plugin data. The below is formatted for printing.
mp-wp_genesis 16 *
mp-wp_genesis 17 * <code>
mp-wp_genesis 18 * /*
mp-wp_genesis 19 * Plugin Name: Name of Plugin
mp-wp_genesis 20 * Plugin URI: Link to plugin information
mp-wp_genesis 21 * Description: Plugin Description
mp-wp_genesis 22 * Author: Plugin author's name
mp-wp_genesis 23 * Author URI: Link to the author's web site
mp-wp_genesis 24 * Version: Must be set in the plugin for WordPress 2.3+
mp-wp_genesis 25 * Text Domain: Optional. Unique identifier, should be same as the one used in
mp-wp_genesis 26 * plugin_text_domain()
mp-wp_genesis 27 * Domain Path: Optional. Only useful if the translations are located in a
mp-wp_genesis 28 * folder above the plugin's base path. For example, if .mo files are
mp-wp_genesis 29 * located in the locale folder then Domain Path will be "/locale/" and
mp-wp_genesis 30 * must have the first slash. Defaults to the base folder the plugin is
mp-wp_genesis 31 * located in.
mp-wp_genesis 32 * * / # Remove the space to close comment
mp-wp_genesis 33 * </code>
mp-wp_genesis 34 *
mp-wp_genesis 35 * Plugin data returned array contains the following:
mp-wp_genesis 36 * 'Name' - Name of the plugin, must be unique.
mp-wp_genesis 37 * 'Title' - Title of the plugin and the link to the plugin's web site.
mp-wp_genesis 38 * 'Description' - Description of what the plugin does and/or notes
mp-wp_genesis 39 * from the author.
mp-wp_genesis 40 * 'Author' - The author's name
mp-wp_genesis 41 * 'AuthorURI' - The authors web site address.
mp-wp_genesis 42 * 'Version' - The plugin version number.
mp-wp_genesis 43 * 'PluginURI' - Plugin web site address.
mp-wp_genesis 44 * 'TextDomain' - Plugin's text domain for localization.
mp-wp_genesis 45 * 'DomainPath' - Plugin's relative directory path to .mo files.
mp-wp_genesis 46 *
mp-wp_genesis 47 * Some users have issues with opening large files and manipulating the contents
mp-wp_genesis 48 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
mp-wp_genesis 49 * the plugin contents when it has all of the required plugin data.
mp-wp_genesis 50 *
mp-wp_genesis 51 * The first 8kiB of the file will be pulled in and if the plugin data is not
mp-wp_genesis 52 * within that first 8kiB, then the plugin author should correct their plugin
mp-wp_genesis 53 * and move the plugin data headers to the top.
mp-wp_genesis 54 *
mp-wp_genesis 55 * The plugin file is assumed to have permissions to allow for scripts to read
mp-wp_genesis 56 * the file. This is not checked however and the file is only opened for
mp-wp_genesis 57 * reading.
mp-wp_genesis 58 *
mp-wp_genesis 59 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
mp-wp_genesis 60 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
mp-wp_genesis 61 * @since 1.5.0
mp-wp_genesis 62 *
mp-wp_genesis 63 * @param string $plugin_file Path to the plugin file
mp-wp_genesis 64 * @param bool $markup If the returned data should have HTML markup applied
mp-wp_genesis 65 * @param bool $translate If the returned data should be translated
mp-wp_genesis 66 * @return array See above for description.
mp-wp_genesis 67 */
mp-wp_genesis 68 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
mp-wp_genesis 69 // We don't need to write to the file, so just open for reading.
mp-wp_genesis 70 $fp = fopen($plugin_file, 'r');
mp-wp_genesis 71
mp-wp_genesis 72 // Pull only the first 8kiB of the file in.
mp-wp_genesis 73 $plugin_data = fread( $fp, 8192 );
mp-wp_genesis 74
mp-wp_genesis 75 // PHP will close file handle, but we are good citizens.
mp-wp_genesis 76 fclose($fp);
mp-wp_genesis 77
mp-wp_genesis 78 preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
mp-wp_genesis 79 preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
mp-wp_genesis 80 preg_match( '|Version:(.*)|i', $plugin_data, $version );
mp-wp_genesis 81 preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
mp-wp_genesis 82 preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
mp-wp_genesis 83 preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
mp-wp_genesis 84 preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
mp-wp_genesis 85 preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
mp-wp_genesis 86
mp-wp_genesis 87 foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
mp-wp_genesis 88 if ( !empty( ${$field} ) )
mp-wp_genesis 89 ${$field} = trim(${$field}[1]);
mp-wp_genesis 90 else
mp-wp_genesis 91 ${$field} = '';
mp-wp_genesis 92 }
mp-wp_genesis 93
mp-wp_genesis 94 $plugin_data = array(
mp-wp_genesis 95 'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
mp-wp_genesis 96 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
mp-wp_genesis 97 'TextDomain' => $text_domain, 'DomainPath' => $domain_path
mp-wp_genesis 98 );
mp-wp_genesis 99 if ( $markup || $translate )
mp-wp_genesis 100 $plugin_data = _get_plugin_data_markup_translate($plugin_data, $markup, $translate);
mp-wp_genesis 101 return $plugin_data;
mp-wp_genesis 102 }
mp-wp_genesis 103
mp-wp_genesis 104 function _get_plugin_data_markup_translate($plugin_data, $markup = true, $translate = true) {
mp-wp_genesis 105
mp-wp_genesis 106 //Translate fields
mp-wp_genesis 107 if( $translate && ! empty($plugin_data['TextDomain']) ) {
mp-wp_genesis 108 if( ! empty( $plugin_data['DomainPath'] ) )
mp-wp_genesis 109 load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file). $plugin_data['DomainPath']);
mp-wp_genesis 110 else
mp-wp_genesis 111 load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file));
mp-wp_genesis 112
mp-wp_genesis 113 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
mp-wp_genesis 114 $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
mp-wp_genesis 115 }
mp-wp_genesis 116
mp-wp_genesis 117 //Apply Markup
mp-wp_genesis 118 if ( $markup ) {
mp-wp_genesis 119 if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
mp-wp_genesis 120 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
mp-wp_genesis 121 else
mp-wp_genesis 122 $plugin_data['Title'] = $plugin_data['Name'];
mp-wp_genesis 123
mp-wp_genesis 124 if ( ! empty($plugin_data['AuthorURI']) )
mp-wp_genesis 125 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
mp-wp_genesis 126
mp-wp_remove-all-... 127 $plugin_data['Description'] = $plugin_data['Description'];
mp-wp_genesis 128 if( ! empty($plugin_data['Author']) )
mp-wp_genesis 129 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
mp-wp_genesis 130 }
mp-wp_genesis 131
mp-wp_genesis 132 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
mp-wp_genesis 133
mp-wp_genesis 134 // Sanitize all displayed data
mp-wp_genesis 135 $plugin_data['Title'] = wp_kses($plugin_data['Title'], $plugins_allowedtags);
mp-wp_genesis 136 $plugin_data['Version'] = wp_kses($plugin_data['Version'], $plugins_allowedtags);
mp-wp_genesis 137 $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
mp-wp_genesis 138 $plugin_data['Author'] = wp_kses($plugin_data['Author'], $plugins_allowedtags);
mp-wp_genesis 139
mp-wp_genesis 140 return $plugin_data;
mp-wp_genesis 141 }
mp-wp_genesis 142
mp-wp_genesis 143 /**
mp-wp_genesis 144 * Check the plugins directory and retrieve all plugin files with plugin data.
mp-wp_genesis 145 *
mp-wp_genesis 146 * WordPress only supports plugin files in the base plugins directory
mp-wp_genesis 147 * (wp-content/plugins) and in one directory above the plugins directory
mp-wp_genesis 148 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
mp-wp_genesis 149 * must be found in those two locations. It is recommended that do keep your
mp-wp_genesis 150 * plugin files in directories.
mp-wp_genesis 151 *
mp-wp_genesis 152 * The file with the plugin data is the file that will be included and therefore
mp-wp_genesis 153 * needs to have the main execution for the plugin. This does not mean
mp-wp_genesis 154 * everything must be contained in the file and it is recommended that the file
mp-wp_genesis 155 * be split for maintainability. Keep everything in one file for extreme
mp-wp_genesis 156 * optimization purposes.
mp-wp_genesis 157 *
mp-wp_genesis 158 * @since unknown
mp-wp_genesis 159 *
mp-wp_genesis 160 * @param string $plugin_folder Optional. Relative path to single plugin folder.
mp-wp_genesis 161 * @return array Key is the plugin file path and the value is an array of the plugin data.
mp-wp_genesis 162 */
mp-wp_genesis 163 function get_plugins($plugin_folder = '') {
mp-wp_genesis 164
mp-wp_genesis 165 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
mp-wp_genesis 166 $cache_plugins = array();
mp-wp_genesis 167
mp-wp_genesis 168 if ( isset($cache_plugins[ $plugin_folder ]) )
mp-wp_genesis 169 return $cache_plugins[ $plugin_folder ];
mp-wp_genesis 170
mp-wp_genesis 171 $wp_plugins = array ();
mp-wp_genesis 172 $plugin_root = WP_PLUGIN_DIR;
mp-wp_genesis 173 if( !empty($plugin_folder) )
mp-wp_genesis 174 $plugin_root .= $plugin_folder;
mp-wp_genesis 175
mp-wp_genesis 176 // Files in wp-content/plugins directory
mp-wp_genesis 177 $plugins_dir = @ opendir( $plugin_root);
mp-wp_genesis 178 if ( $plugins_dir ) {
mp-wp_genesis 179 while (($file = readdir( $plugins_dir ) ) !== false ) {
mp-wp_genesis 180 if ( substr($file, 0, 1) == '.' )
mp-wp_genesis 181 continue;
mp-wp_genesis 182 if ( is_dir( $plugin_root.'/'.$file ) ) {
mp-wp_genesis 183 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
mp-wp_genesis 184 if ( $plugins_subdir ) {
mp-wp_genesis 185 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
mp-wp_genesis 186 if ( substr($subfile, 0, 1) == '.' )
mp-wp_genesis 187 continue;
mp-wp_genesis 188 if ( substr($subfile, -4) == '.php' )
mp-wp_genesis 189 $plugin_files[] = "$file/$subfile";
mp-wp_genesis 190 }
mp-wp_genesis 191 }
mp-wp_genesis 192 } else {
mp-wp_genesis 193 if ( substr($file, -4) == '.php' )
mp-wp_genesis 194 $plugin_files[] = $file;
mp-wp_genesis 195 }
mp-wp_genesis 196 }
mp-wp_genesis 197 }
mp-wp_genesis 198 @closedir( $plugins_dir );
mp-wp_genesis 199 @closedir( $plugins_subdir );
mp-wp_genesis 200
mp-wp_genesis 201 if ( !$plugins_dir || !$plugin_files )
mp-wp_genesis 202 return $wp_plugins;
mp-wp_genesis 203
mp-wp_genesis 204 foreach ( $plugin_files as $plugin_file ) {
mp-wp_genesis 205 if ( !is_readable( "$plugin_root/$plugin_file" ) )
mp-wp_genesis 206 continue;
mp-wp_genesis 207
mp-wp_genesis 208 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
mp-wp_genesis 209
mp-wp_genesis 210 if ( empty ( $plugin_data['Name'] ) )
mp-wp_genesis 211 continue;
mp-wp_genesis 212
mp-wp_genesis 213 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
mp-wp_genesis 214 }
mp-wp_genesis 215
mp-wp_genesis 216 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
mp-wp_genesis 217
mp-wp_genesis 218 $cache_plugins[ $plugin_folder ] = $wp_plugins;
mp-wp_genesis 219 wp_cache_set('plugins', $cache_plugins, 'plugins');
mp-wp_genesis 220
mp-wp_genesis 221 return $wp_plugins;
mp-wp_genesis 222 }
mp-wp_genesis 223
mp-wp_genesis 224 /**
mp-wp_genesis 225 * Check whether the plugin is active by checking the active_plugins list.
mp-wp_genesis 226 *
mp-wp_genesis 227 * @since 2.5.0
mp-wp_genesis 228 *
mp-wp_genesis 229 * @param string $plugin Base plugin path from plugins directory.
mp-wp_genesis 230 * @return bool True, if in the active plugins list. False, not in the list.
mp-wp_genesis 231 */
mp-wp_genesis 232 function is_plugin_active($plugin) {
mp-wp_genesis 233 return in_array($plugin, get_option('active_plugins'));
mp-wp_genesis 234 }
mp-wp_genesis 235
mp-wp_genesis 236 /**
mp-wp_genesis 237 * Attempts activation of plugin in a "sandbox" and redirects on success.
mp-wp_genesis 238 *
mp-wp_genesis 239 * A plugin that is already activated will not attempt to be activated again.
mp-wp_genesis 240 *
mp-wp_genesis 241 * The way it works is by setting the redirection to the error before trying to
mp-wp_genesis 242 * include the plugin file. If the plugin fails, then the redirection will not
mp-wp_genesis 243 * be overwritten with the success message. Also, the options will not be
mp-wp_genesis 244 * updated and the activation hook will not be called on plugin error.
mp-wp_genesis 245 *
mp-wp_genesis 246 * It should be noted that in no way the below code will actually prevent errors
mp-wp_genesis 247 * within the file. The code should not be used elsewhere to replicate the
mp-wp_genesis 248 * "sandbox", which uses redirection to work.
mp-wp_genesis 249 * {@source 13 1}
mp-wp_genesis 250 *
mp-wp_genesis 251 * If any errors are found or text is outputted, then it will be captured to
mp-wp_genesis 252 * ensure that the success redirection will update the error redirection.
mp-wp_genesis 253 *
mp-wp_genesis 254 * @since unknown
mp-wp_genesis 255 *
mp-wp_genesis 256 * @param string $plugin Plugin path to main plugin file with plugin data.
mp-wp_genesis 257 * @param string $redirect Optional. URL to redirect to.
mp-wp_genesis 258 * @return WP_Error|null WP_Error on invalid file or null on success.
mp-wp_genesis 259 */
mp-wp_genesis 260 function activate_plugin($plugin, $redirect = '') {
mp-wp_genesis 261 $current = get_option('active_plugins');
mp-wp_genesis 262 $plugin = plugin_basename(trim($plugin));
mp-wp_genesis 263
mp-wp_genesis 264 $valid = validate_plugin($plugin);
mp-wp_genesis 265 if ( is_wp_error($valid) )
mp-wp_genesis 266 return $valid;
mp-wp_genesis 267
mp-wp_genesis 268 if ( !in_array($plugin, $current) ) {
mp-wp_genesis 269 if ( !empty($redirect) )
mp-wp_genesis 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
mp-wp_genesis 271 ob_start();
mp-wp_genesis 272 @include(WP_PLUGIN_DIR . '/' . $plugin);
mp-wp_genesis 273 $current[] = $plugin;
mp-wp_genesis 274 sort($current);
mp-wp_genesis 275 update_option('active_plugins', $current);
mp-wp_genesis 276 do_action('activate_' . $plugin);
mp-wp_genesis 277 ob_end_clean();
mp-wp_genesis 278 }
mp-wp_genesis 279
mp-wp_genesis 280 return null;
mp-wp_genesis 281 }
mp-wp_genesis 282
mp-wp_genesis 283 /**
mp-wp_genesis 284 * Deactivate a single plugin or multiple plugins.
mp-wp_genesis 285 *
mp-wp_genesis 286 * The deactivation hook is disabled by the plugin upgrader by using the $silent
mp-wp_genesis 287 * parameter.
mp-wp_genesis 288 *
mp-wp_genesis 289 * @since unknown
mp-wp_genesis 290 *
mp-wp_genesis 291 * @param string|array $plugins Single plugin or list of plugins to deactivate.
mp-wp_genesis 292 * @param bool $silent Optional, default is false. Prevent calling deactivate hook.
mp-wp_genesis 293 */
mp-wp_genesis 294 function deactivate_plugins($plugins, $silent= false) {
mp-wp_genesis 295 $current = get_option('active_plugins');
mp-wp_genesis 296
mp-wp_genesis 297 if ( !is_array($plugins) )
mp-wp_genesis 298 $plugins = array($plugins);
mp-wp_genesis 299
mp-wp_genesis 300 foreach ( $plugins as $plugin ) {
mp-wp_genesis 301 $plugin = plugin_basename($plugin);
mp-wp_genesis 302 if( ! is_plugin_active($plugin) )
mp-wp_genesis 303 continue;
mp-wp_genesis 304 array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
mp-wp_genesis 305 if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
mp-wp_genesis 306 do_action('deactivate_' . trim( $plugin ));
mp-wp_genesis 307 }
mp-wp_genesis 308
mp-wp_genesis 309 update_option('active_plugins', $current);
mp-wp_genesis 310 }
mp-wp_genesis 311
mp-wp_genesis 312 /**
mp-wp_genesis 313 * Activate multiple plugins.
mp-wp_genesis 314 *
mp-wp_genesis 315 * When WP_Error is returned, it does not mean that one of the plugins had
mp-wp_genesis 316 * errors. It means that one or more of the plugins file path was invalid.
mp-wp_genesis 317 *
mp-wp_genesis 318 * The execution will be halted as soon as one of the plugins has an error.
mp-wp_genesis 319 *
mp-wp_genesis 320 * @since unknown
mp-wp_genesis 321 *
mp-wp_genesis 322 * @param string|array $plugins
mp-wp_genesis 323 * @param string $redirect Redirect to page after successful activation.
mp-wp_genesis 324 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
mp-wp_genesis 325 */
mp-wp_genesis 326 function activate_plugins($plugins, $redirect = '') {
mp-wp_genesis 327 if ( !is_array($plugins) )
mp-wp_genesis 328 $plugins = array($plugins);
mp-wp_genesis 329
mp-wp_genesis 330 $errors = array();
mp-wp_genesis 331 foreach ( (array) $plugins as $plugin ) {
mp-wp_genesis 332 if ( !empty($redirect) )
mp-wp_genesis 333 $redirect = add_query_arg('plugin', $plugin, $redirect);
mp-wp_genesis 334 $result = activate_plugin($plugin, $redirect);
mp-wp_genesis 335 if ( is_wp_error($result) )
mp-wp_genesis 336 $errors[$plugin] = $result;
mp-wp_genesis 337 }
mp-wp_genesis 338
mp-wp_genesis 339 if ( !empty($errors) )
mp-wp_genesis 340 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
mp-wp_genesis 341
mp-wp_genesis 342 return true;
mp-wp_genesis 343 }
mp-wp_genesis 344
mp-wp_genesis 345 function validate_active_plugins() {
mp-wp_genesis 346 $check_plugins = get_option('active_plugins');
mp-wp_genesis 347
mp-wp_genesis 348 // Sanity check. If the active plugin list is not an array, make it an
mp-wp_genesis 349 // empty array.
mp-wp_genesis 350 if ( !is_array($check_plugins) ) {
mp-wp_genesis 351 update_option('active_plugins', array());
mp-wp_genesis 352 return;
mp-wp_genesis 353 }
mp-wp_genesis 354
mp-wp_genesis 355 //Invalid is any plugin that is deactivated due to error.
mp-wp_genesis 356 $invalid = array();
mp-wp_genesis 357
mp-wp_genesis 358 // If a plugin file does not exist, remove it from the list of active
mp-wp_genesis 359 // plugins.
mp-wp_genesis 360 foreach ( $check_plugins as $check_plugin ) {
mp-wp_genesis 361 $result = validate_plugin($check_plugin);
mp-wp_genesis 362 if ( is_wp_error( $result ) ) {
mp-wp_genesis 363 $invalid[$check_plugin] = $result;
mp-wp_genesis 364 deactivate_plugins( $check_plugin, true);
mp-wp_genesis 365 }
mp-wp_genesis 366 }
mp-wp_genesis 367 return $invalid;
mp-wp_genesis 368 }
mp-wp_genesis 369
mp-wp_genesis 370 /**
mp-wp_genesis 371 * Validate the plugin path.
mp-wp_genesis 372 *
mp-wp_genesis 373 * Checks that the file exists and {@link validate_file() is valid file}.
mp-wp_genesis 374 *
mp-wp_genesis 375 * @since unknown
mp-wp_genesis 376 *
mp-wp_genesis 377 * @param string $plugin Plugin Path
mp-wp_genesis 378 * @return WP_Error|int 0 on success, WP_Error on failure.
mp-wp_genesis 379 */
mp-wp_genesis 380 function validate_plugin($plugin) {
mp-wp_genesis 381 if ( validate_file($plugin) )
mp-wp_genesis 382 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
mp-wp_genesis 383 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
mp-wp_genesis 384 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
mp-wp_genesis 385
mp-wp_genesis 386 return 0;
mp-wp_genesis 387 }
mp-wp_genesis 388
mp-wp_genesis 389 //
mp-wp_genesis 390 // Menu
mp-wp_genesis 391 //
mp-wp_genesis 392
mp-wp_genesis 393 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '' ) {
mp-wp_genesis 394 global $menu, $admin_page_hooks;
mp-wp_genesis 395
mp-wp_genesis 396 $file = plugin_basename( $file );
mp-wp_genesis 397
mp-wp_genesis 398 $admin_page_hooks[$file] = sanitize_title( $menu_title );
mp-wp_genesis 399
mp-wp_genesis 400 $hookname = get_plugin_page_hookname( $file, '' );
mp-wp_genesis 401 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 402 add_action( $hookname, $function );
mp-wp_genesis 403
mp-wp_genesis 404 if ( empty($icon_url) )
mp-wp_update-imag... 405 $icon_url = 'images/generic.svg';
mp-wp_genesis 406
mp-wp_genesis 407 $menu[] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
mp-wp_genesis 408
mp-wp_genesis 409 return $hookname;
mp-wp_genesis 410 }
mp-wp_genesis 411
mp-wp_genesis 412 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
mp-wp_genesis 413 global $menu, $admin_page_hooks, $_wp_last_object_menu;
mp-wp_genesis 414
mp-wp_genesis 415 $file = plugin_basename( $file );
mp-wp_genesis 416
mp-wp_genesis 417 $admin_page_hooks[$file] = sanitize_title( $menu_title );
mp-wp_genesis 418
mp-wp_genesis 419 $hookname = get_plugin_page_hookname( $file, '' );
mp-wp_genesis 420 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 421 add_action( $hookname, $function );
mp-wp_genesis 422
mp-wp_genesis 423 if ( empty($icon_url) )
mp-wp_update-imag... 424 $icon_url = 'images/generic.svg';
mp-wp_genesis 425
mp-wp_genesis 426 $_wp_last_object_menu++;
mp-wp_genesis 427
mp-wp_genesis 428 $menu[$_wp_last_object_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
mp-wp_genesis 429
mp-wp_genesis 430 return $hookname;
mp-wp_genesis 431 }
mp-wp_genesis 432
mp-wp_genesis 433 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
mp-wp_genesis 434 global $menu, $admin_page_hooks, $_wp_last_utility_menu;
mp-wp_genesis 435
mp-wp_genesis 436 $file = plugin_basename( $file );
mp-wp_genesis 437
mp-wp_genesis 438 $admin_page_hooks[$file] = sanitize_title( $menu_title );
mp-wp_genesis 439
mp-wp_genesis 440 $hookname = get_plugin_page_hookname( $file, '' );
mp-wp_genesis 441 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 442 add_action( $hookname, $function );
mp-wp_genesis 443
mp-wp_genesis 444 if ( empty($icon_url) )
mp-wp_update-imag... 445 $icon_url = 'images/generic.svg';
mp-wp_genesis 446
mp-wp_genesis 447 $_wp_last_utility_menu++;
mp-wp_genesis 448
mp-wp_genesis 449 $menu[$_wp_last_utility_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
mp-wp_genesis 450
mp-wp_genesis 451 return $hookname;
mp-wp_genesis 452 }
mp-wp_genesis 453
mp-wp_genesis 454 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 455 global $submenu;
mp-wp_genesis 456 global $menu;
mp-wp_genesis 457 global $_wp_real_parent_file;
mp-wp_genesis 458 global $_wp_submenu_nopriv;
mp-wp_genesis 459
mp-wp_genesis 460 $file = plugin_basename( $file );
mp-wp_genesis 461
mp-wp_genesis 462 $parent = plugin_basename( $parent);
mp-wp_genesis 463 if ( isset( $_wp_real_parent_file[$parent] ) )
mp-wp_genesis 464 $parent = $_wp_real_parent_file[$parent];
mp-wp_genesis 465
mp-wp_genesis 466 if ( !current_user_can( $access_level ) ) {
mp-wp_genesis 467 $_wp_submenu_nopriv[$parent][$file] = true;
mp-wp_genesis 468 return false;
mp-wp_genesis 469 }
mp-wp_genesis 470
mp-wp_genesis 471 // If the parent doesn't already have a submenu, add a link to the parent
mp-wp_genesis 472 // as the first item in the submenu. If the submenu file is the same as the
mp-wp_genesis 473 // parent file someone is trying to link back to the parent manually. In
mp-wp_genesis 474 // this case, don't automatically add a link back to avoid duplication.
mp-wp_genesis 475 if (!isset( $submenu[$parent] ) && $file != $parent ) {
mp-wp_genesis 476 foreach ( $menu as $parent_menu ) {
mp-wp_genesis 477 if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
mp-wp_genesis 478 $submenu[$parent][] = $parent_menu;
mp-wp_genesis 479 }
mp-wp_genesis 480 }
mp-wp_genesis 481
mp-wp_genesis 482 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
mp-wp_genesis 483
mp-wp_genesis 484 $hookname = get_plugin_page_hookname( $file, $parent);
mp-wp_genesis 485 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 486 add_action( $hookname, $function );
mp-wp_genesis 487
mp-wp_genesis 488 return $hookname;
mp-wp_genesis 489 }
mp-wp_genesis 490
mp-wp_genesis 491 /**
mp-wp_genesis 492 * Add sub menu page to the tools main menu.
mp-wp_genesis 493 *
mp-wp_genesis 494 * @param string $page_title
mp-wp_genesis 495 * @param unknown_type $menu_title
mp-wp_genesis 496 * @param unknown_type $access_level
mp-wp_genesis 497 * @param unknown_type $file
mp-wp_genesis 498 * @param unknown_type $function
mp-wp_genesis 499 * @return unknown
mp-wp_genesis 500 */
mp-wp_genesis 501 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 502 return add_submenu_page( 'tools.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 503 }
mp-wp_genesis 504
mp-wp_genesis 505 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 506 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 507 }
mp-wp_genesis 508
mp-wp_genesis 509 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 510 return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 511 }
mp-wp_genesis 512
mp-wp_genesis 513 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 514 if ( current_user_can('edit_users') )
mp-wp_genesis 515 $parent = 'users.php';
mp-wp_genesis 516 else
mp-wp_genesis 517 $parent = 'profile.php';
mp-wp_genesis 518 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 519 }
mp-wp_genesis 520
mp-wp_genesis 521 function add_dashboard_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 522 return add_submenu_page( 'index.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 523 }
mp-wp_genesis 524
mp-wp_genesis 525 function add_posts_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 526 return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 527 }
mp-wp_genesis 528
mp-wp_genesis 529 function add_media_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 530 return add_submenu_page( 'upload.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 531 }
mp-wp_genesis 532
mp-wp_genesis 533 function add_links_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 534 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 535 }
mp-wp_genesis 536
mp-wp_genesis 537 function add_pages_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 538 return add_submenu_page( 'edit-pages.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 539 }
mp-wp_genesis 540
mp-wp_genesis 541 function add_comments_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 542 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 543 }
mp-wp_genesis 544
mp-wp_genesis 545 //
mp-wp_genesis 546 // Pluggable Menu Support -- Private
mp-wp_genesis 547 //
mp-wp_genesis 548
mp-wp_genesis 549 function get_admin_page_parent( $parent = '' ) {
mp-wp_genesis 550 global $parent_file;
mp-wp_genesis 551 global $menu;
mp-wp_genesis 552 global $submenu;
mp-wp_genesis 553 global $pagenow;
mp-wp_genesis 554 global $plugin_page;
mp-wp_genesis 555 global $_wp_real_parent_file;
mp-wp_genesis 556 global $_wp_menu_nopriv;
mp-wp_genesis 557 global $_wp_submenu_nopriv;
mp-wp_genesis 558
mp-wp_genesis 559 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
mp-wp_genesis 560 if ( isset( $_wp_real_parent_file[$parent] ) )
mp-wp_genesis 561 $parent = $_wp_real_parent_file[$parent];
mp-wp_genesis 562 return $parent;
mp-wp_genesis 563 }
mp-wp_genesis 564
mp-wp_genesis 565 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
mp-wp_genesis 566 foreach ( $menu as $parent_menu ) {
mp-wp_genesis 567 if ( $parent_menu[2] == $plugin_page ) {
mp-wp_genesis 568 $parent_file = $plugin_page;
mp-wp_genesis 569 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 570 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 571 return $parent_file;
mp-wp_genesis 572 }
mp-wp_genesis 573 }
mp-wp_genesis 574 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
mp-wp_genesis 575 $parent_file = $plugin_page;
mp-wp_genesis 576 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 577 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 578 return $parent_file;
mp-wp_genesis 579 }
mp-wp_genesis 580 }
mp-wp_genesis 581
mp-wp_genesis 582 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
mp-wp_genesis 583 $parent_file = $pagenow;
mp-wp_genesis 584 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 585 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 586 return $parent_file;
mp-wp_genesis 587 }
mp-wp_genesis 588
mp-wp_genesis 589 foreach (array_keys( $submenu ) as $parent) {
mp-wp_genesis 590 foreach ( $submenu[$parent] as $submenu_array ) {
mp-wp_genesis 591 if ( isset( $_wp_real_parent_file[$parent] ) )
mp-wp_genesis 592 $parent = $_wp_real_parent_file[$parent];
mp-wp_genesis 593 if ( $submenu_array[2] == $pagenow ) {
mp-wp_genesis 594 $parent_file = $parent;
mp-wp_genesis 595 return $parent;
mp-wp_genesis 596 } else
mp-wp_genesis 597 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
mp-wp_genesis 598 $parent_file = $parent;
mp-wp_genesis 599 return $parent;
mp-wp_genesis 600 }
mp-wp_genesis 601 }
mp-wp_genesis 602 }
mp-wp_genesis 603
mp-wp_genesis 604 if ( empty($parent_file) )
mp-wp_genesis 605 $parent_file = '';
mp-wp_genesis 606 return '';
mp-wp_genesis 607 }
mp-wp_genesis 608
mp-wp_genesis 609 function get_admin_page_title() {
mp-wp_genesis 610 global $title;
mp-wp_genesis 611 global $menu;
mp-wp_genesis 612 global $submenu;
mp-wp_genesis 613 global $pagenow;
mp-wp_genesis 614 global $plugin_page;
mp-wp_genesis 615
mp-wp_genesis 616 if ( isset( $title ) && !empty ( $title ) ) {
mp-wp_genesis 617 return $title;
mp-wp_genesis 618 }
mp-wp_genesis 619
mp-wp_genesis 620 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
mp-wp_genesis 621
mp-wp_genesis 622 $parent = $parent1 = get_admin_page_parent();
mp-wp_genesis 623
mp-wp_genesis 624 if ( empty ( $parent) ) {
mp-wp_genesis 625 foreach ( $menu as $menu_array ) {
mp-wp_genesis 626 if ( isset( $menu_array[3] ) ) {
mp-wp_genesis 627 if ( $menu_array[2] == $pagenow ) {
mp-wp_genesis 628 $title = $menu_array[3];
mp-wp_genesis 629 return $menu_array[3];
mp-wp_genesis 630 } else
mp-wp_genesis 631 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
mp-wp_genesis 632 $title = $menu_array[3];
mp-wp_genesis 633 return $menu_array[3];
mp-wp_genesis 634 }
mp-wp_genesis 635 } else {
mp-wp_genesis 636 $title = $menu_array[0];
mp-wp_genesis 637 return $title;
mp-wp_genesis 638 }
mp-wp_genesis 639 }
mp-wp_genesis 640 } else {
mp-wp_genesis 641 foreach (array_keys( $submenu ) as $parent) {
mp-wp_genesis 642 foreach ( $submenu[$parent] as $submenu_array ) {
mp-wp_genesis 643 if ( isset( $plugin_page ) &&
mp-wp_genesis 644 ($plugin_page == $submenu_array[2] ) &&
mp-wp_genesis 645 (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
mp-wp_genesis 646 ) {
mp-wp_genesis 647 $title = $submenu_array[3];
mp-wp_genesis 648 return $submenu_array[3];
mp-wp_genesis 649 }
mp-wp_genesis 650
mp-wp_genesis 651 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
mp-wp_genesis 652 continue;
mp-wp_genesis 653
mp-wp_genesis 654 if ( isset( $submenu_array[3] ) ) {
mp-wp_genesis 655 $title = $submenu_array[3];
mp-wp_genesis 656 return $submenu_array[3];
mp-wp_genesis 657 } else {
mp-wp_genesis 658 $title = $submenu_array[0];
mp-wp_genesis 659 return $title;
mp-wp_genesis 660 }
mp-wp_genesis 661 }
mp-wp_genesis 662 }
mp-wp_genesis 663 }
mp-wp_genesis 664
mp-wp_genesis 665 return $title;
mp-wp_genesis 666 }
mp-wp_genesis 667
mp-wp_genesis 668 function get_plugin_page_hook( $plugin_page, $parent_page ) {
mp-wp_genesis 669 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
mp-wp_genesis 670 if ( has_action($hook) )
mp-wp_genesis 671 return $hook;
mp-wp_genesis 672 else
mp-wp_genesis 673 return null;
mp-wp_genesis 674 }
mp-wp_genesis 675
mp-wp_genesis 676 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
mp-wp_genesis 677 global $admin_page_hooks;
mp-wp_genesis 678
mp-wp_genesis 679 $parent = get_admin_page_parent( $parent_page );
mp-wp_genesis 680
mp-wp_genesis 681 $page_type = 'admin';
mp-wp_genesis 682 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
mp-wp_genesis 683 if ( isset( $admin_page_hooks[$plugin_page] ) )
mp-wp_genesis 684 $page_type = 'toplevel';
mp-wp_genesis 685 else
mp-wp_genesis 686 if ( isset( $admin_page_hooks[$parent] ))
mp-wp_genesis 687 $page_type = $admin_page_hooks[$parent];
mp-wp_genesis 688 } else if ( isset( $admin_page_hooks[$parent] ) ) {
mp-wp_genesis 689 $page_type = $admin_page_hooks[$parent];
mp-wp_genesis 690 }
mp-wp_genesis 691
mp-wp_genesis 692 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
mp-wp_genesis 693
mp-wp_genesis 694 return $page_type.'_page_'.$plugin_name;
mp-wp_genesis 695 }
mp-wp_genesis 696
mp-wp_genesis 697 function user_can_access_admin_page() {
mp-wp_genesis 698 global $pagenow;
mp-wp_genesis 699 global $menu;
mp-wp_genesis 700 global $submenu;
mp-wp_genesis 701 global $_wp_menu_nopriv;
mp-wp_genesis 702 global $_wp_submenu_nopriv;
mp-wp_genesis 703 global $plugin_page;
mp-wp_genesis 704
mp-wp_genesis 705 $parent = get_admin_page_parent();
mp-wp_genesis 706
mp-wp_genesis 707 if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
mp-wp_genesis 708 return false;
mp-wp_genesis 709
mp-wp_genesis 710 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
mp-wp_genesis 711 return false;
mp-wp_genesis 712
mp-wp_genesis 713 if ( empty( $parent) ) {
mp-wp_genesis 714 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
mp-wp_genesis 715 return false;
mp-wp_genesis 716 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
mp-wp_genesis 717 return false;
mp-wp_genesis 718 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
mp-wp_genesis 719 return false;
mp-wp_genesis 720 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
mp-wp_genesis 721 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
mp-wp_genesis 722 return false;
mp-wp_genesis 723 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
mp-wp_genesis 724 return false;
mp-wp_genesis 725 }
mp-wp_genesis 726 return true;
mp-wp_genesis 727 }
mp-wp_genesis 728
mp-wp_genesis 729 if ( isset( $submenu[$parent] ) ) {
mp-wp_genesis 730 foreach ( $submenu[$parent] as $submenu_array ) {
mp-wp_genesis 731 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
mp-wp_genesis 732 if ( current_user_can( $submenu_array[1] ))
mp-wp_genesis 733 return true;
mp-wp_genesis 734 else
mp-wp_genesis 735 return false;
mp-wp_genesis 736 } else if ( $submenu_array[2] == $pagenow ) {
mp-wp_genesis 737 if ( current_user_can( $submenu_array[1] ))
mp-wp_genesis 738 return true;
mp-wp_genesis 739 else
mp-wp_genesis 740 return false;
mp-wp_genesis 741 }
mp-wp_genesis 742 }
mp-wp_genesis 743 }
mp-wp_genesis 744
mp-wp_genesis 745 foreach ( $menu as $menu_array ) {
mp-wp_genesis 746 if ( $menu_array[2] == $parent) {
mp-wp_genesis 747 if ( current_user_can( $menu_array[1] ))
mp-wp_genesis 748 return true;
mp-wp_genesis 749 else
mp-wp_genesis 750 return false;
mp-wp_genesis 751 }
mp-wp_genesis 752 }
mp-wp_genesis 753
mp-wp_genesis 754 return true;
mp-wp_genesis 755 }
mp-wp_genesis 756
mp-wp_genesis 757 /* Whitelist functions */
mp-wp_genesis 758
mp-wp_genesis 759 /**
mp-wp_genesis 760 * Register a setting and its sanitization callback
mp-wp_genesis 761 *
mp-wp_genesis 762 * @since 2.7.0
mp-wp_genesis 763 *
mp-wp_genesis 764 * @param string $option_group A settings group name. Can be anything.
mp-wp_genesis 765 * @param string $option_name The name of an option to sanitize and save.
mp-wp_genesis 766 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
mp-wp_genesis 767 * @return unknown
mp-wp_genesis 768 */
mp-wp_genesis 769 function register_setting($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 770 return add_option_update_handler($option_group, $option_name, $sanitize_callback);
mp-wp_genesis 771 }
mp-wp_genesis 772
mp-wp_genesis 773 /**
mp-wp_genesis 774 * Unregister a setting
mp-wp_genesis 775 *
mp-wp_genesis 776 * @since 2.7.0
mp-wp_genesis 777 *
mp-wp_genesis 778 * @param unknown_type $option_group
mp-wp_genesis 779 * @param unknown_type $option_name
mp-wp_genesis 780 * @param unknown_type $sanitize_callback
mp-wp_genesis 781 * @return unknown
mp-wp_genesis 782 */
mp-wp_genesis 783 function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 784 return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
mp-wp_genesis 785 }
mp-wp_genesis 786
mp-wp_genesis 787 /**
mp-wp_genesis 788 * {@internal Missing Short Description}}
mp-wp_genesis 789 *
mp-wp_genesis 790 * @since unknown
mp-wp_genesis 791 *
mp-wp_genesis 792 * @param unknown_type $option_group
mp-wp_genesis 793 * @param unknown_type $option_name
mp-wp_genesis 794 * @param unknown_type $sanitize_callback
mp-wp_genesis 795 */
mp-wp_genesis 796 function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 797 global $new_whitelist_options;
mp-wp_genesis 798 $new_whitelist_options[ $option_group ][] = $option_name;
mp-wp_genesis 799 if ( $sanitize_callback != '' )
mp-wp_genesis 800 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
mp-wp_genesis 801 }
mp-wp_genesis 802
mp-wp_genesis 803 /**
mp-wp_genesis 804 * {@internal Missing Short Description}}
mp-wp_genesis 805 *
mp-wp_genesis 806 * @since unknown
mp-wp_genesis 807 *
mp-wp_genesis 808 * @param unknown_type $option_group
mp-wp_genesis 809 * @param unknown_type $option_name
mp-wp_genesis 810 * @param unknown_type $sanitize_callback
mp-wp_genesis 811 */
mp-wp_genesis 812 function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 813 global $new_whitelist_options;
mp-wp_genesis 814 $pos = array_search( $option_name, $new_whitelist_options );
mp-wp_genesis 815 if ( $pos !== false )
mp-wp_genesis 816 unset( $new_whitelist_options[ $option_group ][ $pos ] );
mp-wp_genesis 817 if ( $sanitize_callback != '' )
mp-wp_genesis 818 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
mp-wp_genesis 819 }
mp-wp_genesis 820
mp-wp_genesis 821 /**
mp-wp_genesis 822 * {@internal Missing Short Description}}
mp-wp_genesis 823 *
mp-wp_genesis 824 * @since unknown
mp-wp_genesis 825 *
mp-wp_genesis 826 * @param unknown_type $options
mp-wp_genesis 827 * @return unknown
mp-wp_genesis 828 */
mp-wp_genesis 829 function option_update_filter( $options ) {
mp-wp_genesis 830 global $new_whitelist_options;
mp-wp_genesis 831
mp-wp_genesis 832 if ( is_array( $new_whitelist_options ) )
mp-wp_genesis 833 $options = add_option_whitelist( $new_whitelist_options, $options );
mp-wp_genesis 834
mp-wp_genesis 835 return $options;
mp-wp_genesis 836 }
mp-wp_genesis 837 add_filter( 'whitelist_options', 'option_update_filter' );
mp-wp_genesis 838
mp-wp_genesis 839 /**
mp-wp_genesis 840 * {@internal Missing Short Description}}
mp-wp_genesis 841 *
mp-wp_genesis 842 * @since unknown
mp-wp_genesis 843 *
mp-wp_genesis 844 * @param unknown_type $new_options
mp-wp_genesis 845 * @param unknown_type $options
mp-wp_genesis 846 * @return unknown
mp-wp_genesis 847 */
mp-wp_genesis 848 function add_option_whitelist( $new_options, $options = '' ) {
mp-wp_genesis 849 if( $options == '' ) {
mp-wp_genesis 850 global $whitelist_options;
mp-wp_genesis 851 } else {
mp-wp_genesis 852 $whitelist_options = $options;
mp-wp_genesis 853 }
mp-wp_genesis 854 foreach( $new_options as $page => $keys ) {
mp-wp_genesis 855 foreach( $keys as $key ) {
mp-wp_genesis 856 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
mp-wp_genesis 857 $whitelist_options[ $page ] = array();
mp-wp_genesis 858 $whitelist_options[ $page ][] = $key;
mp-wp_genesis 859 } else {
mp-wp_genesis 860 $pos = array_search( $key, $whitelist_options[ $page ] );
mp-wp_genesis 861 if ( $pos === false )
mp-wp_genesis 862 $whitelist_options[ $page ][] = $key;
mp-wp_genesis 863 }
mp-wp_genesis 864 }
mp-wp_genesis 865 }
mp-wp_genesis 866 return $whitelist_options;
mp-wp_genesis 867 }
mp-wp_genesis 868
mp-wp_genesis 869 /**
mp-wp_genesis 870 * {@internal Missing Short Description}}
mp-wp_genesis 871 *
mp-wp_genesis 872 * @since unknown
mp-wp_genesis 873 *
mp-wp_genesis 874 * @param unknown_type $del_options
mp-wp_genesis 875 * @param unknown_type $options
mp-wp_genesis 876 * @return unknown
mp-wp_genesis 877 */
mp-wp_genesis 878 function remove_option_whitelist( $del_options, $options = '' ) {
mp-wp_genesis 879 if( $options == '' ) {
mp-wp_genesis 880 global $whitelist_options;
mp-wp_genesis 881 } else {
mp-wp_genesis 882 $whitelist_options = $options;
mp-wp_genesis 883 }
mp-wp_genesis 884 foreach( $del_options as $page => $keys ) {
mp-wp_genesis 885 foreach( $keys as $key ) {
mp-wp_genesis 886 $pos = array_search( $key, $whitelist_options[ $page ] );
mp-wp_genesis 887 if( $pos !== false )
mp-wp_genesis 888 unset( $whitelist_options[ $page ][ $pos ] );
mp-wp_genesis 889 }
mp-wp_genesis 890 }
mp-wp_genesis 891 return $whitelist_options;
mp-wp_genesis 892 }
mp-wp_genesis 893
mp-wp_genesis 894 /**
mp-wp_genesis 895 * Output nonce, action, and option_page fields for a settings page.
mp-wp_genesis 896 *
mp-wp_genesis 897 * @since 2.7.0
mp-wp_genesis 898 *
mp-wp_genesis 899 * @param string $option_group A settings group name. This should match the group name used in register_setting().
mp-wp_genesis 900 */
mp-wp_genesis 901 function settings_fields($option_group) {
mp-wp_genesis 902 echo "<input type='hidden' name='option_page' value='$option_group' />";
mp-wp_genesis 903 echo '<input type="hidden" name="action" value="update" />';
mp-wp_genesis 904 wp_nonce_field("$option_group-options");
mp-wp_genesis 905 }
mp-wp_genesis 906
mp-wp_genesis 907 ?>