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_genesis 127 $plugin_data['Description'] = wptexturize( $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 /**
mp-wp_genesis 346 * Remove directory and files of a plugin for a single or list of plugin(s).
mp-wp_genesis 347 *
mp-wp_genesis 348 * If the plugins parameter list is empty, false will be returned. True when
mp-wp_genesis 349 * completed.
mp-wp_genesis 350 *
mp-wp_genesis 351 * @since unknown
mp-wp_genesis 352 *
mp-wp_genesis 353 * @param array $plugins List of plugin
mp-wp_genesis 354 * @param string $redirect Redirect to page when complete.
mp-wp_genesis 355 * @return mixed
mp-wp_genesis 356 */
mp-wp_genesis 357 function delete_plugins($plugins, $redirect = '' ) {
mp-wp_genesis 358 global $wp_filesystem;
mp-wp_genesis 359
mp-wp_genesis 360 if( empty($plugins) )
mp-wp_genesis 361 return false;
mp-wp_genesis 362
mp-wp_genesis 363 $checked = array();
mp-wp_genesis 364 foreach( $plugins as $plugin )
mp-wp_genesis 365 $checked[] = 'checked[]=' . $plugin;
mp-wp_genesis 366
mp-wp_genesis 367 ob_start();
mp-wp_genesis 368 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
mp-wp_genesis 369 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
mp-wp_genesis 370 $data = ob_get_contents();
mp-wp_genesis 371 ob_end_clean();
mp-wp_genesis 372 if( ! empty($data) ){
mp-wp_genesis 373 include_once( ABSPATH . 'wp-admin/admin-header.php');
mp-wp_genesis 374 echo $data;
mp-wp_genesis 375 include( ABSPATH . 'wp-admin/admin-footer.php');
mp-wp_genesis 376 exit;
mp-wp_genesis 377 }
mp-wp_genesis 378 return;
mp-wp_genesis 379 }
mp-wp_genesis 380
mp-wp_genesis 381 if ( ! WP_Filesystem($credentials) ) {
mp-wp_genesis 382 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
mp-wp_genesis 383 $data = ob_get_contents();
mp-wp_genesis 384 ob_end_clean();
mp-wp_genesis 385 if( ! empty($data) ){
mp-wp_genesis 386 include_once( ABSPATH . 'wp-admin/admin-header.php');
mp-wp_genesis 387 echo $data;
mp-wp_genesis 388 include( ABSPATH . 'wp-admin/admin-footer.php');
mp-wp_genesis 389 exit;
mp-wp_genesis 390 }
mp-wp_genesis 391 return;
mp-wp_genesis 392 }
mp-wp_genesis 393
mp-wp_genesis 394 if ( $wp_filesystem->errors->get_error_code() ) {
mp-wp_genesis 395 return $wp_filesystem->errors;
mp-wp_genesis 396 }
mp-wp_genesis 397
mp-wp_genesis 398 if ( ! is_object($wp_filesystem) )
mp-wp_genesis 399 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
mp-wp_genesis 400
mp-wp_genesis 401 if ( $wp_filesystem->errors->get_error_code() )
mp-wp_genesis 402 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
mp-wp_genesis 403
mp-wp_genesis 404 //Get the base plugin folder
mp-wp_genesis 405 $plugins_dir = $wp_filesystem->wp_plugins_dir();
mp-wp_genesis 406 if ( empty($plugins_dir) )
mp-wp_genesis 407 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
mp-wp_genesis 408
mp-wp_genesis 409 $plugins_dir = trailingslashit( $plugins_dir );
mp-wp_genesis 410
mp-wp_genesis 411 $errors = array();
mp-wp_genesis 412
mp-wp_genesis 413 foreach( $plugins as $plugin_file ) {
mp-wp_genesis 414 // Run Uninstall hook
mp-wp_genesis 415 if ( is_uninstallable_plugin( $plugin_file ) )
mp-wp_genesis 416 uninstall_plugin($plugin_file);
mp-wp_genesis 417
mp-wp_genesis 418 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
mp-wp_genesis 419 // If plugin is in its own directory, recursively delete the directory.
mp-wp_genesis 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
mp-wp_genesis 421 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
mp-wp_genesis 422 else
mp-wp_genesis 423 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
mp-wp_genesis 424
mp-wp_genesis 425 if ( ! $deleted )
mp-wp_genesis 426 $errors[] = $plugin_file;
mp-wp_genesis 427 }
mp-wp_genesis 428
mp-wp_genesis 429 if ( ! empty($errors) )
mp-wp_genesis 430 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
mp-wp_genesis 431
mp-wp_genesis 432 // Force refresh of plugin update information
mp-wp_genesis 433 delete_option('update_plugins');
mp-wp_genesis 434
mp-wp_genesis 435 return true;
mp-wp_genesis 436 }
mp-wp_genesis 437
mp-wp_genesis 438 function validate_active_plugins() {
mp-wp_genesis 439 $check_plugins = get_option('active_plugins');
mp-wp_genesis 440
mp-wp_genesis 441 // Sanity check. If the active plugin list is not an array, make it an
mp-wp_genesis 442 // empty array.
mp-wp_genesis 443 if ( !is_array($check_plugins) ) {
mp-wp_genesis 444 update_option('active_plugins', array());
mp-wp_genesis 445 return;
mp-wp_genesis 446 }
mp-wp_genesis 447
mp-wp_genesis 448 //Invalid is any plugin that is deactivated due to error.
mp-wp_genesis 449 $invalid = array();
mp-wp_genesis 450
mp-wp_genesis 451 // If a plugin file does not exist, remove it from the list of active
mp-wp_genesis 452 // plugins.
mp-wp_genesis 453 foreach ( $check_plugins as $check_plugin ) {
mp-wp_genesis 454 $result = validate_plugin($check_plugin);
mp-wp_genesis 455 if ( is_wp_error( $result ) ) {
mp-wp_genesis 456 $invalid[$check_plugin] = $result;
mp-wp_genesis 457 deactivate_plugins( $check_plugin, true);
mp-wp_genesis 458 }
mp-wp_genesis 459 }
mp-wp_genesis 460 return $invalid;
mp-wp_genesis 461 }
mp-wp_genesis 462
mp-wp_genesis 463 /**
mp-wp_genesis 464 * Validate the plugin path.
mp-wp_genesis 465 *
mp-wp_genesis 466 * Checks that the file exists and {@link validate_file() is valid file}.
mp-wp_genesis 467 *
mp-wp_genesis 468 * @since unknown
mp-wp_genesis 469 *
mp-wp_genesis 470 * @param string $plugin Plugin Path
mp-wp_genesis 471 * @return WP_Error|int 0 on success, WP_Error on failure.
mp-wp_genesis 472 */
mp-wp_genesis 473 function validate_plugin($plugin) {
mp-wp_genesis 474 if ( validate_file($plugin) )
mp-wp_genesis 475 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
mp-wp_genesis 476 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
mp-wp_genesis 477 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
mp-wp_genesis 478
mp-wp_genesis 479 return 0;
mp-wp_genesis 480 }
mp-wp_genesis 481
mp-wp_genesis 482 /**
mp-wp_genesis 483 * Whether the plugin can be uninstalled.
mp-wp_genesis 484 *
mp-wp_genesis 485 * @since 2.7.0
mp-wp_genesis 486 *
mp-wp_genesis 487 * @param string $plugin Plugin path to check.
mp-wp_genesis 488 * @return bool Whether plugin can be uninstalled.
mp-wp_genesis 489 */
mp-wp_genesis 490 function is_uninstallable_plugin($plugin) {
mp-wp_genesis 491 $file = plugin_basename($plugin);
mp-wp_genesis 492
mp-wp_genesis 493 $uninstallable_plugins = (array) get_option('uninstall_plugins');
mp-wp_genesis 494 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
mp-wp_genesis 495 return true;
mp-wp_genesis 496
mp-wp_genesis 497 return false;
mp-wp_genesis 498 }
mp-wp_genesis 499
mp-wp_genesis 500 /**
mp-wp_genesis 501 * Uninstall a single plugin.
mp-wp_genesis 502 *
mp-wp_genesis 503 * Calls the uninstall hook, if it is available.
mp-wp_genesis 504 *
mp-wp_genesis 505 * @since 2.7.0
mp-wp_genesis 506 *
mp-wp_genesis 507 * @param string $plugin Relative plugin path from Plugin Directory.
mp-wp_genesis 508 */
mp-wp_genesis 509 function uninstall_plugin($plugin) {
mp-wp_genesis 510 $file = plugin_basename($plugin);
mp-wp_genesis 511
mp-wp_genesis 512 $uninstallable_plugins = (array) get_option('uninstall_plugins');
mp-wp_genesis 513 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
mp-wp_genesis 514 if ( isset( $uninstallable_plugins[$file] ) ) {
mp-wp_genesis 515 unset($uninstallable_plugins[$file]);
mp-wp_genesis 516 update_option('uninstall_plugins', $uninstallable_plugins);
mp-wp_genesis 517 }
mp-wp_genesis 518 unset($uninstallable_plugins);
mp-wp_genesis 519
mp-wp_genesis 520 define('WP_UNINSTALL_PLUGIN', $file);
mp-wp_genesis 521 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
mp-wp_genesis 522
mp-wp_genesis 523 return true;
mp-wp_genesis 524 }
mp-wp_genesis 525
mp-wp_genesis 526 if ( isset( $uninstallable_plugins[$file] ) ) {
mp-wp_genesis 527 $callable = $uninstallable_plugins[$file];
mp-wp_genesis 528 unset($uninstallable_plugins[$file]);
mp-wp_genesis 529 update_option('uninstall_plugins', $uninstallable_plugins);
mp-wp_genesis 530 unset($uninstallable_plugins);
mp-wp_genesis 531
mp-wp_genesis 532 include WP_PLUGIN_DIR . '/' . $file;
mp-wp_genesis 533
mp-wp_genesis 534 add_action( 'uninstall_' . $file, $callable );
mp-wp_genesis 535 do_action( 'uninstall_' . $file );
mp-wp_genesis 536 }
mp-wp_genesis 537 }
mp-wp_genesis 538
mp-wp_genesis 539 //
mp-wp_genesis 540 // Menu
mp-wp_genesis 541 //
mp-wp_genesis 542
mp-wp_genesis 543 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '' ) {
mp-wp_genesis 544 global $menu, $admin_page_hooks;
mp-wp_genesis 545
mp-wp_genesis 546 $file = plugin_basename( $file );
mp-wp_genesis 547
mp-wp_genesis 548 $admin_page_hooks[$file] = sanitize_title( $menu_title );
mp-wp_genesis 549
mp-wp_genesis 550 $hookname = get_plugin_page_hookname( $file, '' );
mp-wp_genesis 551 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 552 add_action( $hookname, $function );
mp-wp_genesis 553
mp-wp_genesis 554 if ( empty($icon_url) )
mp-wp_genesis 555 $icon_url = 'images/generic.png';
mp-wp_genesis 556
mp-wp_genesis 557 $menu[] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
mp-wp_genesis 558
mp-wp_genesis 559 return $hookname;
mp-wp_genesis 560 }
mp-wp_genesis 561
mp-wp_genesis 562 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
mp-wp_genesis 563 global $menu, $admin_page_hooks, $_wp_last_object_menu;
mp-wp_genesis 564
mp-wp_genesis 565 $file = plugin_basename( $file );
mp-wp_genesis 566
mp-wp_genesis 567 $admin_page_hooks[$file] = sanitize_title( $menu_title );
mp-wp_genesis 568
mp-wp_genesis 569 $hookname = get_plugin_page_hookname( $file, '' );
mp-wp_genesis 570 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 571 add_action( $hookname, $function );
mp-wp_genesis 572
mp-wp_genesis 573 if ( empty($icon_url) )
mp-wp_genesis 574 $icon_url = 'images/generic.png';
mp-wp_genesis 575
mp-wp_genesis 576 $_wp_last_object_menu++;
mp-wp_genesis 577
mp-wp_genesis 578 $menu[$_wp_last_object_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
mp-wp_genesis 579
mp-wp_genesis 580 return $hookname;
mp-wp_genesis 581 }
mp-wp_genesis 582
mp-wp_genesis 583 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
mp-wp_genesis 584 global $menu, $admin_page_hooks, $_wp_last_utility_menu;
mp-wp_genesis 585
mp-wp_genesis 586 $file = plugin_basename( $file );
mp-wp_genesis 587
mp-wp_genesis 588 $admin_page_hooks[$file] = sanitize_title( $menu_title );
mp-wp_genesis 589
mp-wp_genesis 590 $hookname = get_plugin_page_hookname( $file, '' );
mp-wp_genesis 591 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 592 add_action( $hookname, $function );
mp-wp_genesis 593
mp-wp_genesis 594 if ( empty($icon_url) )
mp-wp_genesis 595 $icon_url = 'images/generic.png';
mp-wp_genesis 596
mp-wp_genesis 597 $_wp_last_utility_menu++;
mp-wp_genesis 598
mp-wp_genesis 599 $menu[$_wp_last_utility_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
mp-wp_genesis 600
mp-wp_genesis 601 return $hookname;
mp-wp_genesis 602 }
mp-wp_genesis 603
mp-wp_genesis 604 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 605 global $submenu;
mp-wp_genesis 606 global $menu;
mp-wp_genesis 607 global $_wp_real_parent_file;
mp-wp_genesis 608 global $_wp_submenu_nopriv;
mp-wp_genesis 609
mp-wp_genesis 610 $file = plugin_basename( $file );
mp-wp_genesis 611
mp-wp_genesis 612 $parent = plugin_basename( $parent);
mp-wp_genesis 613 if ( isset( $_wp_real_parent_file[$parent] ) )
mp-wp_genesis 614 $parent = $_wp_real_parent_file[$parent];
mp-wp_genesis 615
mp-wp_genesis 616 if ( !current_user_can( $access_level ) ) {
mp-wp_genesis 617 $_wp_submenu_nopriv[$parent][$file] = true;
mp-wp_genesis 618 return false;
mp-wp_genesis 619 }
mp-wp_genesis 620
mp-wp_genesis 621 // If the parent doesn't already have a submenu, add a link to the parent
mp-wp_genesis 622 // as the first item in the submenu. If the submenu file is the same as the
mp-wp_genesis 623 // parent file someone is trying to link back to the parent manually. In
mp-wp_genesis 624 // this case, don't automatically add a link back to avoid duplication.
mp-wp_genesis 625 if (!isset( $submenu[$parent] ) && $file != $parent ) {
mp-wp_genesis 626 foreach ( $menu as $parent_menu ) {
mp-wp_genesis 627 if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
mp-wp_genesis 628 $submenu[$parent][] = $parent_menu;
mp-wp_genesis 629 }
mp-wp_genesis 630 }
mp-wp_genesis 631
mp-wp_genesis 632 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
mp-wp_genesis 633
mp-wp_genesis 634 $hookname = get_plugin_page_hookname( $file, $parent);
mp-wp_genesis 635 if (!empty ( $function ) && !empty ( $hookname ))
mp-wp_genesis 636 add_action( $hookname, $function );
mp-wp_genesis 637
mp-wp_genesis 638 return $hookname;
mp-wp_genesis 639 }
mp-wp_genesis 640
mp-wp_genesis 641 /**
mp-wp_genesis 642 * Add sub menu page to the tools main menu.
mp-wp_genesis 643 *
mp-wp_genesis 644 * @param string $page_title
mp-wp_genesis 645 * @param unknown_type $menu_title
mp-wp_genesis 646 * @param unknown_type $access_level
mp-wp_genesis 647 * @param unknown_type $file
mp-wp_genesis 648 * @param unknown_type $function
mp-wp_genesis 649 * @return unknown
mp-wp_genesis 650 */
mp-wp_genesis 651 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 652 return add_submenu_page( 'tools.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 653 }
mp-wp_genesis 654
mp-wp_genesis 655 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 656 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 657 }
mp-wp_genesis 658
mp-wp_genesis 659 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 660 return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 661 }
mp-wp_genesis 662
mp-wp_genesis 663 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 664 if ( current_user_can('edit_users') )
mp-wp_genesis 665 $parent = 'users.php';
mp-wp_genesis 666 else
mp-wp_genesis 667 $parent = 'profile.php';
mp-wp_genesis 668 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 669 }
mp-wp_genesis 670
mp-wp_genesis 671 function add_dashboard_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 672 return add_submenu_page( 'index.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 673 }
mp-wp_genesis 674
mp-wp_genesis 675 function add_posts_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 676 return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 677 }
mp-wp_genesis 678
mp-wp_genesis 679 function add_media_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 680 return add_submenu_page( 'upload.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 681 }
mp-wp_genesis 682
mp-wp_genesis 683 function add_links_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 684 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 685 }
mp-wp_genesis 686
mp-wp_genesis 687 function add_pages_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 688 return add_submenu_page( 'edit-pages.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 689 }
mp-wp_genesis 690
mp-wp_genesis 691 function add_comments_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
mp-wp_genesis 692 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $access_level, $file, $function );
mp-wp_genesis 693 }
mp-wp_genesis 694
mp-wp_genesis 695 //
mp-wp_genesis 696 // Pluggable Menu Support -- Private
mp-wp_genesis 697 //
mp-wp_genesis 698
mp-wp_genesis 699 function get_admin_page_parent( $parent = '' ) {
mp-wp_genesis 700 global $parent_file;
mp-wp_genesis 701 global $menu;
mp-wp_genesis 702 global $submenu;
mp-wp_genesis 703 global $pagenow;
mp-wp_genesis 704 global $plugin_page;
mp-wp_genesis 705 global $_wp_real_parent_file;
mp-wp_genesis 706 global $_wp_menu_nopriv;
mp-wp_genesis 707 global $_wp_submenu_nopriv;
mp-wp_genesis 708
mp-wp_genesis 709 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
mp-wp_genesis 710 if ( isset( $_wp_real_parent_file[$parent] ) )
mp-wp_genesis 711 $parent = $_wp_real_parent_file[$parent];
mp-wp_genesis 712 return $parent;
mp-wp_genesis 713 }
mp-wp_genesis 714 /*
mp-wp_genesis 715 if ( !empty ( $parent_file ) ) {
mp-wp_genesis 716 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 717 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 718
mp-wp_genesis 719 return $parent_file;
mp-wp_genesis 720 }
mp-wp_genesis 721 */
mp-wp_genesis 722
mp-wp_genesis 723 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
mp-wp_genesis 724 foreach ( $menu as $parent_menu ) {
mp-wp_genesis 725 if ( $parent_menu[2] == $plugin_page ) {
mp-wp_genesis 726 $parent_file = $plugin_page;
mp-wp_genesis 727 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 728 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 729 return $parent_file;
mp-wp_genesis 730 }
mp-wp_genesis 731 }
mp-wp_genesis 732 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
mp-wp_genesis 733 $parent_file = $plugin_page;
mp-wp_genesis 734 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 735 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 736 return $parent_file;
mp-wp_genesis 737 }
mp-wp_genesis 738 }
mp-wp_genesis 739
mp-wp_genesis 740 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
mp-wp_genesis 741 $parent_file = $pagenow;
mp-wp_genesis 742 if ( isset( $_wp_real_parent_file[$parent_file] ) )
mp-wp_genesis 743 $parent_file = $_wp_real_parent_file[$parent_file];
mp-wp_genesis 744 return $parent_file;
mp-wp_genesis 745 }
mp-wp_genesis 746
mp-wp_genesis 747 foreach (array_keys( $submenu ) as $parent) {
mp-wp_genesis 748 foreach ( $submenu[$parent] as $submenu_array ) {
mp-wp_genesis 749 if ( isset( $_wp_real_parent_file[$parent] ) )
mp-wp_genesis 750 $parent = $_wp_real_parent_file[$parent];
mp-wp_genesis 751 if ( $submenu_array[2] == $pagenow ) {
mp-wp_genesis 752 $parent_file = $parent;
mp-wp_genesis 753 return $parent;
mp-wp_genesis 754 } else
mp-wp_genesis 755 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
mp-wp_genesis 756 $parent_file = $parent;
mp-wp_genesis 757 return $parent;
mp-wp_genesis 758 }
mp-wp_genesis 759 }
mp-wp_genesis 760 }
mp-wp_genesis 761
mp-wp_genesis 762 if ( empty($parent_file) )
mp-wp_genesis 763 $parent_file = '';
mp-wp_genesis 764 return '';
mp-wp_genesis 765 }
mp-wp_genesis 766
mp-wp_genesis 767 function get_admin_page_title() {
mp-wp_genesis 768 global $title;
mp-wp_genesis 769 global $menu;
mp-wp_genesis 770 global $submenu;
mp-wp_genesis 771 global $pagenow;
mp-wp_genesis 772 global $plugin_page;
mp-wp_genesis 773
mp-wp_genesis 774 if ( isset( $title ) && !empty ( $title ) ) {
mp-wp_genesis 775 return $title;
mp-wp_genesis 776 }
mp-wp_genesis 777
mp-wp_genesis 778 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
mp-wp_genesis 779
mp-wp_genesis 780 $parent = $parent1 = get_admin_page_parent();
mp-wp_genesis 781
mp-wp_genesis 782 if ( empty ( $parent) ) {
mp-wp_genesis 783 foreach ( $menu as $menu_array ) {
mp-wp_genesis 784 if ( isset( $menu_array[3] ) ) {
mp-wp_genesis 785 if ( $menu_array[2] == $pagenow ) {
mp-wp_genesis 786 $title = $menu_array[3];
mp-wp_genesis 787 return $menu_array[3];
mp-wp_genesis 788 } else
mp-wp_genesis 789 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
mp-wp_genesis 790 $title = $menu_array[3];
mp-wp_genesis 791 return $menu_array[3];
mp-wp_genesis 792 }
mp-wp_genesis 793 } else {
mp-wp_genesis 794 $title = $menu_array[0];
mp-wp_genesis 795 return $title;
mp-wp_genesis 796 }
mp-wp_genesis 797 }
mp-wp_genesis 798 } else {
mp-wp_genesis 799 foreach (array_keys( $submenu ) as $parent) {
mp-wp_genesis 800 foreach ( $submenu[$parent] as $submenu_array ) {
mp-wp_genesis 801 if ( isset( $plugin_page ) &&
mp-wp_genesis 802 ($plugin_page == $submenu_array[2] ) &&
mp-wp_genesis 803 (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
mp-wp_genesis 804 ) {
mp-wp_genesis 805 $title = $submenu_array[3];
mp-wp_genesis 806 return $submenu_array[3];
mp-wp_genesis 807 }
mp-wp_genesis 808
mp-wp_genesis 809 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
mp-wp_genesis 810 continue;
mp-wp_genesis 811
mp-wp_genesis 812 if ( isset( $submenu_array[3] ) ) {
mp-wp_genesis 813 $title = $submenu_array[3];
mp-wp_genesis 814 return $submenu_array[3];
mp-wp_genesis 815 } else {
mp-wp_genesis 816 $title = $submenu_array[0];
mp-wp_genesis 817 return $title;
mp-wp_genesis 818 }
mp-wp_genesis 819 }
mp-wp_genesis 820 }
mp-wp_genesis 821 }
mp-wp_genesis 822
mp-wp_genesis 823 return $title;
mp-wp_genesis 824 }
mp-wp_genesis 825
mp-wp_genesis 826 function get_plugin_page_hook( $plugin_page, $parent_page ) {
mp-wp_genesis 827 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
mp-wp_genesis 828 if ( has_action($hook) )
mp-wp_genesis 829 return $hook;
mp-wp_genesis 830 else
mp-wp_genesis 831 return null;
mp-wp_genesis 832 }
mp-wp_genesis 833
mp-wp_genesis 834 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
mp-wp_genesis 835 global $admin_page_hooks;
mp-wp_genesis 836
mp-wp_genesis 837 $parent = get_admin_page_parent( $parent_page );
mp-wp_genesis 838
mp-wp_genesis 839 $page_type = 'admin';
mp-wp_genesis 840 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
mp-wp_genesis 841 if ( isset( $admin_page_hooks[$plugin_page] ) )
mp-wp_genesis 842 $page_type = 'toplevel';
mp-wp_genesis 843 else
mp-wp_genesis 844 if ( isset( $admin_page_hooks[$parent] ))
mp-wp_genesis 845 $page_type = $admin_page_hooks[$parent];
mp-wp_genesis 846 } else if ( isset( $admin_page_hooks[$parent] ) ) {
mp-wp_genesis 847 $page_type = $admin_page_hooks[$parent];
mp-wp_genesis 848 }
mp-wp_genesis 849
mp-wp_genesis 850 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
mp-wp_genesis 851
mp-wp_genesis 852 return $page_type.'_page_'.$plugin_name;
mp-wp_genesis 853 }
mp-wp_genesis 854
mp-wp_genesis 855 function user_can_access_admin_page() {
mp-wp_genesis 856 global $pagenow;
mp-wp_genesis 857 global $menu;
mp-wp_genesis 858 global $submenu;
mp-wp_genesis 859 global $_wp_menu_nopriv;
mp-wp_genesis 860 global $_wp_submenu_nopriv;
mp-wp_genesis 861 global $plugin_page;
mp-wp_genesis 862
mp-wp_genesis 863 $parent = get_admin_page_parent();
mp-wp_genesis 864
mp-wp_genesis 865 if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
mp-wp_genesis 866 return false;
mp-wp_genesis 867
mp-wp_genesis 868 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
mp-wp_genesis 869 return false;
mp-wp_genesis 870
mp-wp_genesis 871 if ( empty( $parent) ) {
mp-wp_genesis 872 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
mp-wp_genesis 873 return false;
mp-wp_genesis 874 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
mp-wp_genesis 875 return false;
mp-wp_genesis 876 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
mp-wp_genesis 877 return false;
mp-wp_genesis 878 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
mp-wp_genesis 879 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
mp-wp_genesis 880 return false;
mp-wp_genesis 881 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
mp-wp_genesis 882 return false;
mp-wp_genesis 883 }
mp-wp_genesis 884 return true;
mp-wp_genesis 885 }
mp-wp_genesis 886
mp-wp_genesis 887 if ( isset( $submenu[$parent] ) ) {
mp-wp_genesis 888 foreach ( $submenu[$parent] as $submenu_array ) {
mp-wp_genesis 889 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
mp-wp_genesis 890 if ( current_user_can( $submenu_array[1] ))
mp-wp_genesis 891 return true;
mp-wp_genesis 892 else
mp-wp_genesis 893 return false;
mp-wp_genesis 894 } else if ( $submenu_array[2] == $pagenow ) {
mp-wp_genesis 895 if ( current_user_can( $submenu_array[1] ))
mp-wp_genesis 896 return true;
mp-wp_genesis 897 else
mp-wp_genesis 898 return false;
mp-wp_genesis 899 }
mp-wp_genesis 900 }
mp-wp_genesis 901 }
mp-wp_genesis 902
mp-wp_genesis 903 foreach ( $menu as $menu_array ) {
mp-wp_genesis 904 if ( $menu_array[2] == $parent) {
mp-wp_genesis 905 if ( current_user_can( $menu_array[1] ))
mp-wp_genesis 906 return true;
mp-wp_genesis 907 else
mp-wp_genesis 908 return false;
mp-wp_genesis 909 }
mp-wp_genesis 910 }
mp-wp_genesis 911
mp-wp_genesis 912 return true;
mp-wp_genesis 913 }
mp-wp_genesis 914
mp-wp_genesis 915 /* Whitelist functions */
mp-wp_genesis 916
mp-wp_genesis 917 /**
mp-wp_genesis 918 * Register a setting and its sanitization callback
mp-wp_genesis 919 *
mp-wp_genesis 920 * @since 2.7.0
mp-wp_genesis 921 *
mp-wp_genesis 922 * @param string $option_group A settings group name. Can be anything.
mp-wp_genesis 923 * @param string $option_name The name of an option to sanitize and save.
mp-wp_genesis 924 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
mp-wp_genesis 925 * @return unknown
mp-wp_genesis 926 */
mp-wp_genesis 927 function register_setting($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 928 return add_option_update_handler($option_group, $option_name, $sanitize_callback);
mp-wp_genesis 929 }
mp-wp_genesis 930
mp-wp_genesis 931 /**
mp-wp_genesis 932 * Unregister a setting
mp-wp_genesis 933 *
mp-wp_genesis 934 * @since 2.7.0
mp-wp_genesis 935 *
mp-wp_genesis 936 * @param unknown_type $option_group
mp-wp_genesis 937 * @param unknown_type $option_name
mp-wp_genesis 938 * @param unknown_type $sanitize_callback
mp-wp_genesis 939 * @return unknown
mp-wp_genesis 940 */
mp-wp_genesis 941 function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 942 return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
mp-wp_genesis 943 }
mp-wp_genesis 944
mp-wp_genesis 945 /**
mp-wp_genesis 946 * {@internal Missing Short Description}}
mp-wp_genesis 947 *
mp-wp_genesis 948 * @since unknown
mp-wp_genesis 949 *
mp-wp_genesis 950 * @param unknown_type $option_group
mp-wp_genesis 951 * @param unknown_type $option_name
mp-wp_genesis 952 * @param unknown_type $sanitize_callback
mp-wp_genesis 953 */
mp-wp_genesis 954 function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 955 global $new_whitelist_options;
mp-wp_genesis 956 $new_whitelist_options[ $option_group ][] = $option_name;
mp-wp_genesis 957 if ( $sanitize_callback != '' )
mp-wp_genesis 958 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
mp-wp_genesis 959 }
mp-wp_genesis 960
mp-wp_genesis 961 /**
mp-wp_genesis 962 * {@internal Missing Short Description}}
mp-wp_genesis 963 *
mp-wp_genesis 964 * @since unknown
mp-wp_genesis 965 *
mp-wp_genesis 966 * @param unknown_type $option_group
mp-wp_genesis 967 * @param unknown_type $option_name
mp-wp_genesis 968 * @param unknown_type $sanitize_callback
mp-wp_genesis 969 */
mp-wp_genesis 970 function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
mp-wp_genesis 971 global $new_whitelist_options;
mp-wp_genesis 972 $pos = array_search( $option_name, $new_whitelist_options );
mp-wp_genesis 973 if ( $pos !== false )
mp-wp_genesis 974 unset( $new_whitelist_options[ $option_group ][ $pos ] );
mp-wp_genesis 975 if ( $sanitize_callback != '' )
mp-wp_genesis 976 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
mp-wp_genesis 977 }
mp-wp_genesis 978
mp-wp_genesis 979 /**
mp-wp_genesis 980 * {@internal Missing Short Description}}
mp-wp_genesis 981 *
mp-wp_genesis 982 * @since unknown
mp-wp_genesis 983 *
mp-wp_genesis 984 * @param unknown_type $options
mp-wp_genesis 985 * @return unknown
mp-wp_genesis 986 */
mp-wp_genesis 987 function option_update_filter( $options ) {
mp-wp_genesis 988 global $new_whitelist_options;
mp-wp_genesis 989
mp-wp_genesis 990 if ( is_array( $new_whitelist_options ) )
mp-wp_genesis 991 $options = add_option_whitelist( $new_whitelist_options, $options );
mp-wp_genesis 992
mp-wp_genesis 993 return $options;
mp-wp_genesis 994 }
mp-wp_genesis 995 add_filter( 'whitelist_options', 'option_update_filter' );
mp-wp_genesis 996
mp-wp_genesis 997 /**
mp-wp_genesis 998 * {@internal Missing Short Description}}
mp-wp_genesis 999 *
mp-wp_genesis 1000 * @since unknown
mp-wp_genesis 1001 *
mp-wp_genesis 1002 * @param unknown_type $new_options
mp-wp_genesis 1003 * @param unknown_type $options
mp-wp_genesis 1004 * @return unknown
mp-wp_genesis 1005 */
mp-wp_genesis 1006 function add_option_whitelist( $new_options, $options = '' ) {
mp-wp_genesis 1007 if( $options == '' ) {
mp-wp_genesis 1008 global $whitelist_options;
mp-wp_genesis 1009 } else {
mp-wp_genesis 1010 $whitelist_options = $options;
mp-wp_genesis 1011 }
mp-wp_genesis 1012 foreach( $new_options as $page => $keys ) {
mp-wp_genesis 1013 foreach( $keys as $key ) {
mp-wp_genesis 1014 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
mp-wp_genesis 1015 $whitelist_options[ $page ] = array();
mp-wp_genesis 1016 $whitelist_options[ $page ][] = $key;
mp-wp_genesis 1017 } else {
mp-wp_genesis 1018 $pos = array_search( $key, $whitelist_options[ $page ] );
mp-wp_genesis 1019 if ( $pos === false )
mp-wp_genesis 1020 $whitelist_options[ $page ][] = $key;
mp-wp_genesis 1021 }
mp-wp_genesis 1022 }
mp-wp_genesis 1023 }
mp-wp_genesis 1024 return $whitelist_options;
mp-wp_genesis 1025 }
mp-wp_genesis 1026
mp-wp_genesis 1027 /**
mp-wp_genesis 1028 * {@internal Missing Short Description}}
mp-wp_genesis 1029 *
mp-wp_genesis 1030 * @since unknown
mp-wp_genesis 1031 *
mp-wp_genesis 1032 * @param unknown_type $del_options
mp-wp_genesis 1033 * @param unknown_type $options
mp-wp_genesis 1034 * @return unknown
mp-wp_genesis 1035 */
mp-wp_genesis 1036 function remove_option_whitelist( $del_options, $options = '' ) {
mp-wp_genesis 1037 if( $options == '' ) {
mp-wp_genesis 1038 global $whitelist_options;
mp-wp_genesis 1039 } else {
mp-wp_genesis 1040 $whitelist_options = $options;
mp-wp_genesis 1041 }
mp-wp_genesis 1042 foreach( $del_options as $page => $keys ) {
mp-wp_genesis 1043 foreach( $keys as $key ) {
mp-wp_genesis 1044 $pos = array_search( $key, $whitelist_options[ $page ] );
mp-wp_genesis 1045 if( $pos !== false )
mp-wp_genesis 1046 unset( $whitelist_options[ $page ][ $pos ] );
mp-wp_genesis 1047 }
mp-wp_genesis 1048 }
mp-wp_genesis 1049 return $whitelist_options;
mp-wp_genesis 1050 }
mp-wp_genesis 1051
mp-wp_genesis 1052 /**
mp-wp_genesis 1053 * Output nonce, action, and option_page fields for a settings page.
mp-wp_genesis 1054 *
mp-wp_genesis 1055 * @since 2.7.0
mp-wp_genesis 1056 *
mp-wp_genesis 1057 * @param string $option_group A settings group name. This should match the group name used in register_setting().
mp-wp_genesis 1058 */
mp-wp_genesis 1059 function settings_fields($option_group) {
mp-wp_genesis 1060 echo "<input type='hidden' name='option_page' value='$option_group' />";
mp-wp_genesis 1061 echo '<input type="hidden" name="action" value="update" />';
mp-wp_genesis 1062 wp_nonce_field("$option_group-options");
mp-wp_genesis 1063 }
mp-wp_genesis 1064
mp-wp_genesis 1065 ?>