[ Index ] |
WordPress Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Plugin Administration API 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * Parse the plugin contents to retrieve plugin's metadata. 11 * 12 * The metadata of the plugin's data searches for the following in the plugin's 13 * header. All plugin data must be on its own line. For plugin description, it 14 * must not have any newlines or only parts of the description will be displayed 15 * and the same goes for the plugin data. The below is formatted for printing. 16 * 17 * <code> 18 * /* 19 * Plugin Name: Name of Plugin 20 * Plugin URI: Link to plugin information 21 * Description: Plugin Description 22 * Author: Plugin author's name 23 * Author URI: Link to the author's web site 24 * Version: Must be set in the plugin for WordPress 2.3+ 25 * Text Domain: Optional. Unique identifier, should be same as the one used in 26 * plugin_text_domain() 27 * Domain Path: Optional. Only useful if the translations are located in a 28 * folder above the plugin's base path. For example, if .mo files are 29 * located in the locale folder then Domain Path will be "/locale/" and 30 * must have the first slash. Defaults to the base folder the plugin is 31 * located in. 32 * Network: Optional. Specify "Network: true" to require that a plugin is activated 33 * across all sites in an installation. This will prevent a plugin from being 34 * activated on a single site when Multisite is enabled. 35 * * / # Remove the space to close comment 36 * </code> 37 * 38 * Plugin data returned array contains the following: 39 * 'Name' - Name of the plugin, must be unique. 40 * 'Title' - Title of the plugin and the link to the plugin's web site. 41 * 'Description' - Description of what the plugin does and/or notes 42 * from the author. 43 * 'Author' - The author's name 44 * 'AuthorURI' - The authors web site address. 45 * 'Version' - The plugin version number. 46 * 'PluginURI' - Plugin web site address. 47 * 'TextDomain' - Plugin's text domain for localization. 48 * 'DomainPath' - Plugin's relative directory path to .mo files. 49 * 'Network' - Boolean. Whether the plugin can only be activated network wide. 50 * 51 * Some users have issues with opening large files and manipulating the contents 52 * for want is usually the first 1kiB or 2kiB. This function stops pulling in 53 * the plugin contents when it has all of the required plugin data. 54 * 55 * The first 8kiB of the file will be pulled in and if the plugin data is not 56 * within that first 8kiB, then the plugin author should correct their plugin 57 * and move the plugin data headers to the top. 58 * 59 * The plugin file is assumed to have permissions to allow for scripts to read 60 * the file. This is not checked however and the file is only opened for 61 * reading. 62 * 63 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations. 64 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations. 65 * @since 1.5.0 66 * 67 * @param string $plugin_file Path to the plugin file 68 * @param bool $markup Optional. If the returned data should have HTML markup applied. Defaults to true. 69 * @param bool $translate Optional. If the returned data should be translated. Defaults to true. 70 * @return array See above for description. 71 */ 72 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { 73 74 $default_headers = array( 75 'Name' => 'Plugin Name', 76 'PluginURI' => 'Plugin URI', 77 'Version' => 'Version', 78 'Description' => 'Description', 79 'Author' => 'Author', 80 'AuthorURI' => 'Author URI', 81 'TextDomain' => 'Text Domain', 82 'DomainPath' => 'Domain Path', 83 'Network' => 'Network', 84 // Site Wide Only is deprecated in favor of Network. 85 '_sitewide' => 'Site Wide Only', 86 ); 87 88 $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' ); 89 90 // Site Wide Only is the old header for Network 91 if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) { 92 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The <code>%1$s</code> plugin header is deprecated. Use <code>%2$s</code> instead.' ), 'Site Wide Only: true', 'Network: true' ) ); 93 $plugin_data['Network'] = $plugin_data['_sitewide']; 94 } 95 $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) ); 96 unset( $plugin_data['_sitewide'] ); 97 98 if ( $markup || $translate ) { 99 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate ); 100 } else { 101 $plugin_data['Title'] = $plugin_data['Name']; 102 $plugin_data['AuthorName'] = $plugin_data['Author']; 103 } 104 105 return $plugin_data; 106 } 107 108 /** 109 * Sanitizes plugin data, optionally adds markup, optionally translates. 110 * 111 * @since 2.7.0 112 * @access private 113 * @see get_plugin_data() 114 */ 115 function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) { 116 117 // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path 118 $plugin_file = plugin_basename( $plugin_file ); 119 120 // Translate fields 121 if ( $translate ) { 122 if ( $textdomain = $plugin_data['TextDomain'] ) { 123 if ( $plugin_data['DomainPath'] ) 124 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] ); 125 else 126 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) ); 127 } elseif ( in_array( basename( $plugin_file ), array( 'hello.php', 'akismet.php' ) ) ) { 128 $textdomain = 'default'; 129 } 130 if ( $textdomain ) { 131 foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) 132 $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain ); 133 } 134 } 135 136 // Sanitize fields 137 $allowed_tags = $allowed_tags_in_links = array( 138 'abbr' => array( 'title' => true ), 139 'acronym' => array( 'title' => true ), 140 'code' => true, 141 'em' => true, 142 'strong' => true, 143 ); 144 $allowed_tags['a'] = array( 'href' => true, 'title' => true ); 145 146 // Name is marked up inside <a> tags. Don't allow these. 147 // Author is too, but some plugins have used <a> here (omitting Author URI). 148 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links ); 149 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags ); 150 151 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags ); 152 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags ); 153 154 $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] ); 155 $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] ); 156 157 $plugin_data['Title'] = $plugin_data['Name']; 158 $plugin_data['AuthorName'] = $plugin_data['Author']; 159 160 // Apply markup 161 if ( $markup ) { 162 if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) 163 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>'; 164 165 if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) 166 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>'; 167 168 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] ); 169 170 if ( $plugin_data['Author'] ) 171 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>'; 172 } 173 174 return $plugin_data; 175 } 176 177 /** 178 * Get a list of a plugin's files. 179 * 180 * @since 2.8.0 181 * 182 * @param string $plugin Plugin ID 183 * @return array List of files relative to the plugin root. 184 */ 185 function get_plugin_files($plugin) { 186 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin; 187 $dir = dirname($plugin_file); 188 $plugin_files = array($plugin); 189 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) { 190 $plugins_dir = @ opendir( $dir ); 191 if ( $plugins_dir ) { 192 while (($file = readdir( $plugins_dir ) ) !== false ) { 193 if ( substr($file, 0, 1) == '.' ) 194 continue; 195 if ( is_dir( $dir . '/' . $file ) ) { 196 $plugins_subdir = @ opendir( $dir . '/' . $file ); 197 if ( $plugins_subdir ) { 198 while (($subfile = readdir( $plugins_subdir ) ) !== false ) { 199 if ( substr($subfile, 0, 1) == '.' ) 200 continue; 201 $plugin_files[] = plugin_basename("$dir/$file/$subfile"); 202 } 203 @closedir( $plugins_subdir ); 204 } 205 } else { 206 if ( plugin_basename("$dir/$file") != $plugin ) 207 $plugin_files[] = plugin_basename("$dir/$file"); 208 } 209 } 210 @closedir( $plugins_dir ); 211 } 212 } 213 214 return $plugin_files; 215 } 216 217 /** 218 * Check the plugins directory and retrieve all plugin files with plugin data. 219 * 220 * WordPress only supports plugin files in the base plugins directory 221 * (wp-content/plugins) and in one directory above the plugins directory 222 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and 223 * must be found in those two locations. It is recommended that do keep your 224 * plugin files in directories. 225 * 226 * The file with the plugin data is the file that will be included and therefore 227 * needs to have the main execution for the plugin. This does not mean 228 * everything must be contained in the file and it is recommended that the file 229 * be split for maintainability. Keep everything in one file for extreme 230 * optimization purposes. 231 * 232 * @since 1.5.0 233 * 234 * @param string $plugin_folder Optional. Relative path to single plugin folder. 235 * @return array Key is the plugin file path and the value is an array of the plugin data. 236 */ 237 function get_plugins($plugin_folder = '') { 238 239 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') ) 240 $cache_plugins = array(); 241 242 if ( isset($cache_plugins[ $plugin_folder ]) ) 243 return $cache_plugins[ $plugin_folder ]; 244 245 $wp_plugins = array (); 246 $plugin_root = WP_PLUGIN_DIR; 247 if ( !empty($plugin_folder) ) 248 $plugin_root .= $plugin_folder; 249 250 // Files in wp-content/plugins directory 251 $plugins_dir = @ opendir( $plugin_root); 252 $plugin_files = array(); 253 if ( $plugins_dir ) { 254 while (($file = readdir( $plugins_dir ) ) !== false ) { 255 if ( substr($file, 0, 1) == '.' ) 256 continue; 257 if ( is_dir( $plugin_root.'/'.$file ) ) { 258 $plugins_subdir = @ opendir( $plugin_root.'/'.$file ); 259 if ( $plugins_subdir ) { 260 while (($subfile = readdir( $plugins_subdir ) ) !== false ) { 261 if ( substr($subfile, 0, 1) == '.' ) 262 continue; 263 if ( substr($subfile, -4) == '.php' ) 264 $plugin_files[] = "$file/$subfile"; 265 } 266 closedir( $plugins_subdir ); 267 } 268 } else { 269 if ( substr($file, -4) == '.php' ) 270 $plugin_files[] = $file; 271 } 272 } 273 closedir( $plugins_dir ); 274 } 275 276 if ( empty($plugin_files) ) 277 return $wp_plugins; 278 279 foreach ( $plugin_files as $plugin_file ) { 280 if ( !is_readable( "$plugin_root/$plugin_file" ) ) 281 continue; 282 283 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. 284 285 if ( empty ( $plugin_data['Name'] ) ) 286 continue; 287 288 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data; 289 } 290 291 uasort( $wp_plugins, '_sort_uname_callback' ); 292 293 $cache_plugins[ $plugin_folder ] = $wp_plugins; 294 wp_cache_set('plugins', $cache_plugins, 'plugins'); 295 296 return $wp_plugins; 297 } 298 299 /** 300 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data. 301 * 302 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins). 303 * 304 * @since 3.0.0 305 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data. 306 */ 307 function get_mu_plugins() { 308 $wp_plugins = array(); 309 // Files in wp-content/mu-plugins directory 310 $plugin_files = array(); 311 312 if ( ! is_dir( WPMU_PLUGIN_DIR ) ) 313 return $wp_plugins; 314 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) { 315 while ( ( $file = readdir( $plugins_dir ) ) !== false ) { 316 if ( substr( $file, -4 ) == '.php' ) 317 $plugin_files[] = $file; 318 } 319 } else { 320 return $wp_plugins; 321 } 322 323 @closedir( $plugins_dir ); 324 325 if ( empty($plugin_files) ) 326 return $wp_plugins; 327 328 foreach ( $plugin_files as $plugin_file ) { 329 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) ) 330 continue; 331 332 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. 333 334 if ( empty ( $plugin_data['Name'] ) ) 335 $plugin_data['Name'] = $plugin_file; 336 337 $wp_plugins[ $plugin_file ] = $plugin_data; 338 } 339 340 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden 341 unset( $wp_plugins['index.php'] ); 342 343 uasort( $wp_plugins, '_sort_uname_callback' ); 344 345 return $wp_plugins; 346 } 347 348 /** 349 * Callback to sort array by a 'Name' key. 350 * 351 * @since 3.1.0 352 * @access private 353 */ 354 function _sort_uname_callback( $a, $b ) { 355 return strnatcasecmp( $a['Name'], $b['Name'] ); 356 } 357 358 /** 359 * Check the wp-content directory and retrieve all drop-ins with any plugin data. 360 * 361 * @since 3.0.0 362 * @return array Key is the file path and the value is an array of the plugin data. 363 */ 364 function get_dropins() { 365 $dropins = array(); 366 $plugin_files = array(); 367 368 $_dropins = _get_dropins(); 369 370 // These exist in the wp-content directory 371 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) { 372 while ( ( $file = readdir( $plugins_dir ) ) !== false ) { 373 if ( isset( $_dropins[ $file ] ) ) 374 $plugin_files[] = $file; 375 } 376 } else { 377 return $dropins; 378 } 379 380 @closedir( $plugins_dir ); 381 382 if ( empty($plugin_files) ) 383 return $dropins; 384 385 foreach ( $plugin_files as $plugin_file ) { 386 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) 387 continue; 388 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. 389 if ( empty( $plugin_data['Name'] ) ) 390 $plugin_data['Name'] = $plugin_file; 391 $dropins[ $plugin_file ] = $plugin_data; 392 } 393 394 uksort( $dropins, 'strnatcasecmp' ); 395 396 return $dropins; 397 } 398 399 /** 400 * Returns drop-ins that WordPress uses. 401 * 402 * Includes Multisite drop-ins only when is_multisite() 403 * 404 * @since 3.0.0 405 * @return array Key is file name. The value is an array, with the first value the 406 * purpose of the drop-in and the second value the name of the constant that must be 407 * true for the drop-in to be used, or true if no constant is required. 408 */ 409 function _get_dropins() { 410 $dropins = array( 411 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE 412 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load 413 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error 414 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install 415 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance 416 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load 417 ); 418 419 if ( is_multisite() ) { 420 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE 421 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog 422 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog 423 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog 424 } 425 426 return $dropins; 427 } 428 429 /** 430 * Check whether the plugin is active by checking the active_plugins list. 431 * 432 * @since 2.5.0 433 * 434 * @param string $plugin Base plugin path from plugins directory. 435 * @return bool True, if in the active plugins list. False, not in the list. 436 */ 437 function is_plugin_active( $plugin ) { 438 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin ); 439 } 440 441 /** 442 * Check whether the plugin is inactive. 443 * 444 * Reverse of is_plugin_active(). Used as a callback. 445 * 446 * @since 3.1.0 447 * @see is_plugin_active() 448 * 449 * @param string $plugin Base plugin path from plugins directory. 450 * @return bool True if inactive. False if active. 451 */ 452 function is_plugin_inactive( $plugin ) { 453 return ! is_plugin_active( $plugin ); 454 } 455 456 /** 457 * Check whether the plugin is active for the entire network. 458 * 459 * @since 3.0.0 460 * 461 * @param string $plugin Base plugin path from plugins directory. 462 * @return bool True, if active for the network, otherwise false. 463 */ 464 function is_plugin_active_for_network( $plugin ) { 465 if ( !is_multisite() ) 466 return false; 467 468 $plugins = get_site_option( 'active_sitewide_plugins'); 469 if ( isset($plugins[$plugin]) ) 470 return true; 471 472 return false; 473 } 474 475 /** 476 * Checks for "Network: true" in the plugin header to see if this should 477 * be activated only as a network wide plugin. The plugin would also work 478 * when Multisite is not enabled. 479 * 480 * Checks for "Site Wide Only: true" for backwards compatibility. 481 * 482 * @since 3.0.0 483 * 484 * @param string $plugin Plugin to check 485 * @return bool True if plugin is network only, false otherwise. 486 */ 487 function is_network_only_plugin( $plugin ) { 488 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 489 if ( $plugin_data ) 490 return $plugin_data['Network']; 491 return false; 492 } 493 494 /** 495 * Attempts activation of plugin in a "sandbox" and redirects on success. 496 * 497 * A plugin that is already activated will not attempt to be activated again. 498 * 499 * The way it works is by setting the redirection to the error before trying to 500 * include the plugin file. If the plugin fails, then the redirection will not 501 * be overwritten with the success message. Also, the options will not be 502 * updated and the activation hook will not be called on plugin error. 503 * 504 * It should be noted that in no way the below code will actually prevent errors 505 * within the file. The code should not be used elsewhere to replicate the 506 * "sandbox", which uses redirection to work. 507 * {@source 13 1} 508 * 509 * If any errors are found or text is outputted, then it will be captured to 510 * ensure that the success redirection will update the error redirection. 511 * 512 * @since 2.5.0 513 * 514 * @param string $plugin Plugin path to main plugin file with plugin data. 515 * @param string $redirect Optional. URL to redirect to. 516 * @param bool $network_wide Whether to enable the plugin for all sites in the 517 * network or just the current site. Multisite only. Default is false. 518 * @param bool $silent Prevent calling activation hooks. Optional, default is false. 519 * @return WP_Error|null WP_Error on invalid file or null on success. 520 */ 521 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) { 522 $plugin = plugin_basename( trim( $plugin ) ); 523 524 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) { 525 $network_wide = true; 526 $current = get_site_option( 'active_sitewide_plugins', array() ); 527 $_GET['networkwide'] = 1; // Back compat for plugins looking for this value. 528 } else { 529 $current = get_option( 'active_plugins', array() ); 530 } 531 532 $valid = validate_plugin($plugin); 533 if ( is_wp_error($valid) ) 534 return $valid; 535 536 if ( !in_array($plugin, $current) ) { 537 if ( !empty($redirect) ) 538 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 539 ob_start(); 540 include_once(WP_PLUGIN_DIR . '/' . $plugin); 541 542 if ( ! $silent ) { 543 /** 544 * Fires before a plugin is activated in activate_plugin() when the $silent parameter is false. 545 * 546 * @since 2.9.0 547 * 548 * @param string $plugin Plugin path to main plugin file with plugin data. 549 * @param bool $network_wide Whether to enable the plugin for all sites in the network 550 * or just the current site. Multisite only. Default is false. 551 */ 552 do_action( 'activate_plugin', $plugin, $network_wide ); 553 554 /** 555 * Fires before a plugin is activated in activate_plugin() when the $silent parameter is false. 556 * 557 * The action concatenates the 'activate_' prefix with the $plugin value passed to 558 * activate_plugin() to create a dynamically-named action. 559 * 560 * @since 2.0.0 561 * 562 * @param bool $network_wide Whether to enable the plugin for all sites in the network 563 * or just the current site. Multisite only. Default is false. 564 */ 565 do_action( 'activate_' . $plugin, $network_wide ); 566 } 567 568 if ( $network_wide ) { 569 $current[$plugin] = time(); 570 update_site_option( 'active_sitewide_plugins', $current ); 571 } else { 572 $current[] = $plugin; 573 sort($current); 574 update_option('active_plugins', $current); 575 } 576 577 if ( ! $silent ) { 578 /** 579 * Fires after a plugin has been activated in activate_plugin() when the $silent parameter is false. 580 * 581 * @since 2.9.0 582 * 583 * @param string $plugin Plugin path to main plugin file with plugin data. 584 * @param bool $network_wide Whether to enable the plugin for all sites in the network 585 * or just the current site. Multisite only. Default is false. 586 */ 587 do_action( 'activated_plugin', $plugin, $network_wide ); 588 } 589 590 if ( ob_get_length() > 0 ) { 591 $output = ob_get_clean(); 592 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output); 593 } 594 ob_end_clean(); 595 } 596 597 return null; 598 } 599 600 /** 601 * Deactivate a single plugin or multiple plugins. 602 * 603 * The deactivation hook is disabled by the plugin upgrader by using the $silent 604 * parameter. 605 * 606 * @since 2.5.0 607 * 608 * @param string|array $plugins Single plugin or list of plugins to deactivate. 609 * @param bool $silent Prevent calling deactivation hooks. Default is false. 610 * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network. 611 * A value of null (the default) will deactivate plugins for both the site and the network. 612 */ 613 function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) { 614 if ( is_multisite() ) 615 $network_current = get_site_option( 'active_sitewide_plugins', array() ); 616 $current = get_option( 'active_plugins', array() ); 617 $do_blog = $do_network = false; 618 619 foreach ( (array) $plugins as $plugin ) { 620 $plugin = plugin_basename( trim( $plugin ) ); 621 if ( ! is_plugin_active($plugin) ) 622 continue; 623 624 $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin ); 625 626 if ( ! $silent ) { 627 /** 628 * Fires for each plugin being deactivated in deactivate_plugins(), before deactivation 629 * and when the $silent parameter is false. 630 * 631 * @since 2.9.0 632 * 633 * @param string $plugin Plugin path to main plugin file with plugin data. 634 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network 635 * or just the current site. Multisite only. Default is false. 636 */ 637 do_action( 'deactivate_plugin', $plugin, $network_deactivating ); 638 } 639 640 if ( false !== $network_wide ) { 641 if ( is_plugin_active_for_network( $plugin ) ) { 642 $do_network = true; 643 unset( $network_current[ $plugin ] ); 644 } elseif ( $network_wide ) { 645 continue; 646 } 647 } 648 649 if ( true !== $network_wide ) { 650 $key = array_search( $plugin, $current ); 651 if ( false !== $key ) { 652 $do_blog = true; 653 unset( $current[ $key ] ); 654 } 655 } 656 657 if ( ! $silent ) { 658 /** 659 * Fires for each plugin being deactivated in deactivate_plugins(), after deactivation 660 * and when the $silent parameter is false. 661 * 662 * The action concatenates the 'deactivate_' prefix with the plugin's basename 663 * to create a dynamically-named action. 664 * 665 * @since 2.0.0 666 * 667 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network 668 * or just the current site. Multisite only. Default is false. 669 */ 670 do_action( 'deactivate_' . $plugin, $network_deactivating ); 671 672 /** 673 * Fires for each plugin being deactivated in deactivate_plugins(), after deactivation 674 * and when the $silent parameter is false. 675 * 676 * @since 2.9.0 677 * 678 * @param string $plugin Plugin path to main plugin file with plugin data. 679 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network 680 * or just the current site. Multisite only. Default is false. 681 */ 682 do_action( 'deactivated_plugin', $plugin, $network_deactivating ); 683 } 684 } 685 686 if ( $do_blog ) 687 update_option('active_plugins', $current); 688 if ( $do_network ) 689 update_site_option( 'active_sitewide_plugins', $network_current ); 690 } 691 692 /** 693 * Activate multiple plugins. 694 * 695 * When WP_Error is returned, it does not mean that one of the plugins had 696 * errors. It means that one or more of the plugins file path was invalid. 697 * 698 * The execution will be halted as soon as one of the plugins has an error. 699 * 700 * @since 2.6.0 701 * 702 * @param string|array $plugins 703 * @param string $redirect Redirect to page after successful activation. 704 * @param bool $network_wide Whether to enable the plugin for all sites in the network. 705 * @param bool $silent Prevent calling activation hooks. Default is false. 706 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation. 707 */ 708 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) { 709 if ( !is_array($plugins) ) 710 $plugins = array($plugins); 711 712 $errors = array(); 713 foreach ( $plugins as $plugin ) { 714 if ( !empty($redirect) ) 715 $redirect = add_query_arg('plugin', $plugin, $redirect); 716 $result = activate_plugin($plugin, $redirect, $network_wide, $silent); 717 if ( is_wp_error($result) ) 718 $errors[$plugin] = $result; 719 } 720 721 if ( !empty($errors) ) 722 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors); 723 724 return true; 725 } 726 727 /** 728 * Remove directory and files of a plugin for a single or list of plugin(s). 729 * 730 * If the plugins parameter list is empty, false will be returned. True when 731 * completed. 732 * 733 * @since 2.6.0 734 * 735 * @param array $plugins List of plugin 736 * @param string $redirect Redirect to page when complete. 737 * @return mixed 738 */ 739 function delete_plugins($plugins, $redirect = '' ) { 740 global $wp_filesystem; 741 742 if ( empty($plugins) ) 743 return false; 744 745 $checked = array(); 746 foreach( $plugins as $plugin ) 747 $checked[] = 'checked[]=' . $plugin; 748 749 ob_start(); 750 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins'); 751 if ( false === ($credentials = request_filesystem_credentials($url)) ) { 752 $data = ob_get_contents(); 753 ob_end_clean(); 754 if ( ! empty($data) ){ 755 include_once ( ABSPATH . 'wp-admin/admin-header.php'); 756 echo $data; 757 include ( ABSPATH . 'wp-admin/admin-footer.php'); 758 exit; 759 } 760 return; 761 } 762 763 if ( ! WP_Filesystem($credentials) ) { 764 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again 765 $data = ob_get_contents(); 766 ob_end_clean(); 767 if ( ! empty($data) ){ 768 include_once ( ABSPATH . 'wp-admin/admin-header.php'); 769 echo $data; 770 include ( ABSPATH . 'wp-admin/admin-footer.php'); 771 exit; 772 } 773 return; 774 } 775 776 if ( ! is_object($wp_filesystem) ) 777 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 778 779 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) 780 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors); 781 782 //Get the base plugin folder 783 $plugins_dir = $wp_filesystem->wp_plugins_dir(); 784 if ( empty($plugins_dir) ) 785 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.')); 786 787 $plugins_dir = trailingslashit( $plugins_dir ); 788 789 $errors = array(); 790 791 foreach( $plugins as $plugin_file ) { 792 // Run Uninstall hook 793 if ( is_uninstallable_plugin( $plugin_file ) ) 794 uninstall_plugin($plugin_file); 795 796 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) ); 797 // If plugin is in its own directory, recursively delete the directory. 798 if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder 799 $deleted = $wp_filesystem->delete($this_plugin_dir, true); 800 else 801 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file); 802 803 if ( ! $deleted ) 804 $errors[] = $plugin_file; 805 } 806 807 // Remove deleted plugins from the plugin updates list. 808 if ( $current = get_site_transient('update_plugins') ) { 809 // Don't remove the plugins that weren't deleted. 810 $deleted = array_diff( $plugins, $errors ); 811 812 foreach ( $deleted as $plugin_file ) { 813 unset( $current->response[ $plugin_file ] ); 814 } 815 816 set_site_transient( 'update_plugins', $current ); 817 } 818 819 if ( ! empty($errors) ) 820 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) ); 821 822 return true; 823 } 824 825 /** 826 * Validate active plugins 827 * 828 * Validate all active plugins, deactivates invalid and 829 * returns an array of deactivated ones. 830 * 831 * @since 2.5.0 832 * @return array invalid plugins, plugin as key, error as value 833 */ 834 function validate_active_plugins() { 835 $plugins = get_option( 'active_plugins', array() ); 836 // validate vartype: array 837 if ( ! is_array( $plugins ) ) { 838 update_option( 'active_plugins', array() ); 839 $plugins = array(); 840 } 841 842 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) { 843 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() ); 844 $plugins = array_merge( $plugins, array_keys( $network_plugins ) ); 845 } 846 847 if ( empty( $plugins ) ) 848 return; 849 850 $invalid = array(); 851 852 // invalid plugins get deactivated 853 foreach ( $plugins as $plugin ) { 854 $result = validate_plugin( $plugin ); 855 if ( is_wp_error( $result ) ) { 856 $invalid[$plugin] = $result; 857 deactivate_plugins( $plugin, true ); 858 } 859 } 860 return $invalid; 861 } 862 863 /** 864 * Validate the plugin path. 865 * 866 * Checks that the file exists and {@link validate_file() is valid file}. 867 * 868 * @since 2.5.0 869 * 870 * @param string $plugin Plugin Path 871 * @return WP_Error|int 0 on success, WP_Error on failure. 872 */ 873 function validate_plugin($plugin) { 874 if ( validate_file($plugin) ) 875 return new WP_Error('plugin_invalid', __('Invalid plugin path.')); 876 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) ) 877 return new WP_Error('plugin_not_found', __('Plugin file does not exist.')); 878 879 $installed_plugins = get_plugins(); 880 if ( ! isset($installed_plugins[$plugin]) ) 881 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.')); 882 return 0; 883 } 884 885 /** 886 * Whether the plugin can be uninstalled. 887 * 888 * @since 2.7.0 889 * 890 * @param string $plugin Plugin path to check. 891 * @return bool Whether plugin can be uninstalled. 892 */ 893 function is_uninstallable_plugin($plugin) { 894 $file = plugin_basename($plugin); 895 896 $uninstallable_plugins = (array) get_option('uninstall_plugins'); 897 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) 898 return true; 899 900 return false; 901 } 902 903 /** 904 * Uninstall a single plugin. 905 * 906 * Calls the uninstall hook, if it is available. 907 * 908 * @since 2.7.0 909 * 910 * @param string $plugin Relative plugin path from Plugin Directory. 911 */ 912 function uninstall_plugin($plugin) { 913 $file = plugin_basename($plugin); 914 915 $uninstallable_plugins = (array) get_option('uninstall_plugins'); 916 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) { 917 if ( isset( $uninstallable_plugins[$file] ) ) { 918 unset($uninstallable_plugins[$file]); 919 update_option('uninstall_plugins', $uninstallable_plugins); 920 } 921 unset($uninstallable_plugins); 922 923 define('WP_UNINSTALL_PLUGIN', $file); 924 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php'; 925 926 return true; 927 } 928 929 if ( isset( $uninstallable_plugins[$file] ) ) { 930 $callable = $uninstallable_plugins[$file]; 931 unset($uninstallable_plugins[$file]); 932 update_option('uninstall_plugins', $uninstallable_plugins); 933 unset($uninstallable_plugins); 934 935 include WP_PLUGIN_DIR . '/' . $file; 936 937 add_action( 'uninstall_' . $file, $callable ); 938 939 /** 940 * Fires in uninstall_plugin() once the plugin has been uninstalled. 941 * 942 * The action concatenates the 'uninstall_' prefix with the basename of the 943 * plugin passed to {@see uninstall_plugin()} to create a dynamically-named action. 944 * 945 * @since 2.7.0 946 */ 947 do_action( 'uninstall_' . $file ); 948 } 949 } 950 951 // 952 // Menu 953 // 954 955 /** 956 * Add a top level menu page 957 * 958 * This function takes a capability which will be used to determine whether 959 * or not a page is included in the menu. 960 * 961 * The function which is hooked in to handle the output of the page must check 962 * that the user has the required capability as well. 963 * 964 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 965 * @param string $menu_title The text to be used for the menu 966 * @param string $capability The capability required for this menu to be displayed to the user. 967 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 968 * @param callback $function The function to be called to output the content for this page. 969 * @param string $icon_url The url to the icon to be used for this menu. 970 * * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. 971 * This should begin with 'data:image/svg+xml;base64,'. 972 * * Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-piechart'. 973 * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. 974 * @param int $position The position in the menu order this one should appear 975 * 976 * @return string The resulting page's hook_suffix 977 */ 978 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) { 979 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages; 980 981 $menu_slug = plugin_basename( $menu_slug ); 982 983 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title ); 984 985 $hookname = get_plugin_page_hookname( $menu_slug, '' ); 986 987 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) ) 988 add_action( $hookname, $function ); 989 990 if ( empty($icon_url) ) { 991 $icon_url = 'none'; 992 $icon_class = 'menu-icon-generic '; 993 } else { 994 $icon_url = set_url_scheme( $icon_url ); 995 $icon_class = ''; 996 } 997 998 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url ); 999 1000 if ( null === $position ) 1001 $menu[] = $new_menu; 1002 else 1003 $menu[$position] = $new_menu; 1004 1005 $_registered_pages[$hookname] = true; 1006 1007 // No parent as top level 1008 $_parent_pages[$menu_slug] = false; 1009 1010 return $hookname; 1011 } 1012 1013 /** 1014 * Add a top level menu page in the 'objects' section 1015 * 1016 * This function takes a capability which will be used to determine whether 1017 * or not a page is included in the menu. 1018 * 1019 * The function which is hooked in to handle the output of the page must check 1020 * that the user has the required capability as well. 1021 * 1022 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1023 * @param string $menu_title The text to be used for the menu 1024 * @param string $capability The capability required for this menu to be displayed to the user. 1025 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1026 * @param callback $function The function to be called to output the content for this page. 1027 * @param string $icon_url The url to the icon to be used for this menu 1028 * 1029 * @return string The resulting page's hook_suffix 1030 */ 1031 function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') { 1032 global $_wp_last_object_menu; 1033 1034 $_wp_last_object_menu++; 1035 1036 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu); 1037 } 1038 1039 /** 1040 * Add a top level menu page in the 'utility' section 1041 * 1042 * This function takes a capability which will be used to determine whether 1043 * or not a page is included in the menu. 1044 * 1045 * The function which is hooked in to handle the output of the page must check 1046 * that the user has the required capability as well. 1047 * 1048 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1049 * @param string $menu_title The text to be used for the menu 1050 * @param string $capability The capability required for this menu to be displayed to the user. 1051 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1052 * @param callback $function The function to be called to output the content for this page. 1053 * @param string $icon_url The url to the icon to be used for this menu 1054 * 1055 * @return string The resulting page's hook_suffix 1056 */ 1057 function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') { 1058 global $_wp_last_utility_menu; 1059 1060 $_wp_last_utility_menu++; 1061 1062 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu); 1063 } 1064 1065 /** 1066 * Add a sub menu page 1067 * 1068 * This function takes a capability which will be used to determine whether 1069 * or not a page is included in the menu. 1070 * 1071 * The function which is hooked in to handle the output of the page must check 1072 * that the user has the required capability as well. 1073 * 1074 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page) 1075 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1076 * @param string $menu_title The text to be used for the menu 1077 * @param string $capability The capability required for this menu to be displayed to the user. 1078 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1079 * @param callback $function The function to be called to output the content for this page. 1080 * 1081 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1082 */ 1083 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1084 global $submenu; 1085 global $menu; 1086 global $_wp_real_parent_file; 1087 global $_wp_submenu_nopriv; 1088 global $_registered_pages; 1089 global $_parent_pages; 1090 1091 $menu_slug = plugin_basename( $menu_slug ); 1092 $parent_slug = plugin_basename( $parent_slug); 1093 1094 if ( isset( $_wp_real_parent_file[$parent_slug] ) ) 1095 $parent_slug = $_wp_real_parent_file[$parent_slug]; 1096 1097 if ( !current_user_can( $capability ) ) { 1098 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true; 1099 return false; 1100 } 1101 1102 // If the parent doesn't already have a submenu, add a link to the parent 1103 // as the first item in the submenu. If the submenu file is the same as the 1104 // parent file someone is trying to link back to the parent manually. In 1105 // this case, don't automatically add a link back to avoid duplication. 1106 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) { 1107 foreach ( (array)$menu as $parent_menu ) { 1108 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) ) 1109 $submenu[$parent_slug][] = $parent_menu; 1110 } 1111 } 1112 1113 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title ); 1114 1115 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug); 1116 if (!empty ( $function ) && !empty ( $hookname )) 1117 add_action( $hookname, $function ); 1118 1119 $_registered_pages[$hookname] = true; 1120 // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php 1121 if ( 'tools.php' == $parent_slug ) 1122 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true; 1123 1124 // No parent as top level 1125 $_parent_pages[$menu_slug] = $parent_slug; 1126 1127 return $hookname; 1128 } 1129 1130 /** 1131 * Add sub menu page to the tools main menu. 1132 * 1133 * This function takes a capability which will be used to determine whether 1134 * or not a page is included in the menu. 1135 * 1136 * The function which is hooked in to handle the output of the page must check 1137 * that the user has the required capability as well. 1138 * 1139 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1140 * @param string $menu_title The text to be used for the menu 1141 * @param string $capability The capability required for this menu to be displayed to the user. 1142 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1143 * @param callback $function The function to be called to output the content for this page. 1144 * 1145 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1146 */ 1147 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1148 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1149 } 1150 1151 /** 1152 * Add sub menu page to the options main menu. 1153 * 1154 * This function takes a capability which will be used to determine whether 1155 * or not a page is included in the menu. 1156 * 1157 * The function which is hooked in to handle the output of the page must check 1158 * that the user has the required capability as well. 1159 * 1160 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1161 * @param string $menu_title The text to be used for the menu 1162 * @param string $capability The capability required for this menu to be displayed to the user. 1163 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1164 * @param callback $function The function to be called to output the content for this page. 1165 * 1166 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1167 */ 1168 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1169 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1170 } 1171 1172 /** 1173 * Add sub menu page to the themes main menu. 1174 * 1175 * This function takes a capability which will be used to determine whether 1176 * or not a page is included in the menu. 1177 * 1178 * The function which is hooked in to handle the output of the page must check 1179 * that the user has the required capability as well. 1180 * 1181 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1182 * @param string $menu_title The text to be used for the menu 1183 * @param string $capability The capability required for this menu to be displayed to the user. 1184 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1185 * @param callback $function The function to be called to output the content for this page. 1186 * 1187 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1188 */ 1189 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1190 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1191 } 1192 1193 /** 1194 * Add sub menu page to the plugins main menu. 1195 * 1196 * This function takes a capability which will be used to determine whether 1197 * or not a page is included in the menu. 1198 * 1199 * The function which is hooked in to handle the output of the page must check 1200 * that the user has the required capability as well. 1201 * 1202 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1203 * @param string $menu_title The text to be used for the menu 1204 * @param string $capability The capability required for this menu to be displayed to the user. 1205 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1206 * @param callback $function The function to be called to output the content for this page. 1207 * 1208 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1209 */ 1210 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1211 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1212 } 1213 1214 /** 1215 * Add sub menu page to the Users/Profile main menu. 1216 * 1217 * This function takes a capability which will be used to determine whether 1218 * or not a page is included in the menu. 1219 * 1220 * The function which is hooked in to handle the output of the page must check 1221 * that the user has the required capability as well. 1222 * 1223 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1224 * @param string $menu_title The text to be used for the menu 1225 * @param string $capability The capability required for this menu to be displayed to the user. 1226 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1227 * @param callback $function The function to be called to output the content for this page. 1228 * 1229 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1230 */ 1231 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1232 if ( current_user_can('edit_users') ) 1233 $parent = 'users.php'; 1234 else 1235 $parent = 'profile.php'; 1236 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function ); 1237 } 1238 /** 1239 * Add sub menu page to the Dashboard main menu. 1240 * 1241 * This function takes a capability which will be used to determine whether 1242 * or not a page is included in the menu. 1243 * 1244 * The function which is hooked in to handle the output of the page must check 1245 * that the user has the required capability as well. 1246 * 1247 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1248 * @param string $menu_title The text to be used for the menu 1249 * @param string $capability The capability required for this menu to be displayed to the user. 1250 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1251 * @param callback $function The function to be called to output the content for this page. 1252 * 1253 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1254 */ 1255 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1256 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1257 } 1258 1259 /** 1260 * Add sub menu page to the posts main menu. 1261 * 1262 * This function takes a capability which will be used to determine whether 1263 * or not a page is included in the menu. 1264 * 1265 * The function which is hooked in to handle the output of the page must check 1266 * that the user has the required capability as well. 1267 * 1268 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1269 * @param string $menu_title The text to be used for the menu 1270 * @param string $capability The capability required for this menu to be displayed to the user. 1271 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1272 * @param callback $function The function to be called to output the content for this page. 1273 * 1274 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1275 */ 1276 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1277 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1278 } 1279 1280 /** 1281 * Add sub menu page to the media main menu. 1282 * 1283 * This function takes a capability which will be used to determine whether 1284 * or not a page is included in the menu. 1285 * 1286 * The function which is hooked in to handle the output of the page must check 1287 * that the user has the required capability as well. 1288 * 1289 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1290 * @param string $menu_title The text to be used for the menu 1291 * @param string $capability The capability required for this menu to be displayed to the user. 1292 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1293 * @param callback $function The function to be called to output the content for this page. 1294 * 1295 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1296 */ 1297 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1298 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1299 } 1300 1301 /** 1302 * Add sub menu page to the links main menu. 1303 * 1304 * This function takes a capability which will be used to determine whether 1305 * or not a page is included in the menu. 1306 * 1307 * The function which is hooked in to handle the output of the page must check 1308 * that the user has the required capability as well. 1309 * 1310 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1311 * @param string $menu_title The text to be used for the menu 1312 * @param string $capability The capability required for this menu to be displayed to the user. 1313 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1314 * @param callback $function The function to be called to output the content for this page. 1315 * 1316 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1317 */ 1318 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1319 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1320 } 1321 1322 /** 1323 * Add sub menu page to the pages main menu. 1324 * 1325 * This function takes a capability which will be used to determine whether 1326 * or not a page is included in the menu. 1327 * 1328 * The function which is hooked in to handle the output of the page must check 1329 * that the user has the required capability as well. 1330 * 1331 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1332 * @param string $menu_title The text to be used for the menu 1333 * @param string $capability The capability required for this menu to be displayed to the user. 1334 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1335 * @param callback $function The function to be called to output the content for this page. 1336 * 1337 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1338 */ 1339 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1340 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function ); 1341 } 1342 1343 /** 1344 * Add sub menu page to the comments main menu. 1345 * 1346 * This function takes a capability which will be used to determine whether 1347 * or not a page is included in the menu. 1348 * 1349 * The function which is hooked in to handle the output of the page must check 1350 * that the user has the required capability as well. 1351 * 1352 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 1353 * @param string $menu_title The text to be used for the menu 1354 * @param string $capability The capability required for this menu to be displayed to the user. 1355 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1356 * @param callback $function The function to be called to output the content for this page. 1357 * 1358 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. 1359 */ 1360 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1361 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function ); 1362 } 1363 1364 /** 1365 * Remove a top level admin menu 1366 * 1367 * @since 3.1.0 1368 * 1369 * @param string $menu_slug The slug of the menu 1370 * @return array|bool The removed menu on success, False if not found 1371 */ 1372 function remove_menu_page( $menu_slug ) { 1373 global $menu; 1374 1375 foreach ( $menu as $i => $item ) { 1376 if ( $menu_slug == $item[2] ) { 1377 unset( $menu[$i] ); 1378 return $item; 1379 } 1380 } 1381 1382 return false; 1383 } 1384 1385 /** 1386 * Remove an admin submenu 1387 * 1388 * @since 3.1.0 1389 * 1390 * @param string $menu_slug The slug for the parent menu 1391 * @param string $submenu_slug The slug of the submenu 1392 * @return array|bool The removed submenu on success, False if not found 1393 */ 1394 function remove_submenu_page( $menu_slug, $submenu_slug ) { 1395 global $submenu; 1396 1397 if ( !isset( $submenu[$menu_slug] ) ) 1398 return false; 1399 1400 foreach ( $submenu[$menu_slug] as $i => $item ) { 1401 if ( $submenu_slug == $item[2] ) { 1402 unset( $submenu[$menu_slug][$i] ); 1403 return $item; 1404 } 1405 } 1406 1407 return false; 1408 } 1409 1410 /** 1411 * Get the url to access a particular menu page based on the slug it was registered with. 1412 * 1413 * If the slug hasn't been registered properly no url will be returned 1414 * 1415 * @since 3.0 1416 * 1417 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu) 1418 * @param bool $echo Whether or not to echo the url - default is true 1419 * @return string the url 1420 */ 1421 function menu_page_url($menu_slug, $echo = true) { 1422 global $_parent_pages; 1423 1424 if ( isset( $_parent_pages[$menu_slug] ) ) { 1425 $parent_slug = $_parent_pages[$menu_slug]; 1426 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) { 1427 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) ); 1428 } else { 1429 $url = admin_url( 'admin.php?page=' . $menu_slug ); 1430 } 1431 } else { 1432 $url = ''; 1433 } 1434 1435 $url = esc_url($url); 1436 1437 if ( $echo ) 1438 echo $url; 1439 1440 return $url; 1441 } 1442 1443 // 1444 // Pluggable Menu Support -- Private 1445 // 1446 1447 function get_admin_page_parent( $parent = '' ) { 1448 global $parent_file; 1449 global $menu; 1450 global $submenu; 1451 global $pagenow; 1452 global $typenow; 1453 global $plugin_page; 1454 global $_wp_real_parent_file; 1455 global $_wp_menu_nopriv; 1456 global $_wp_submenu_nopriv; 1457 1458 if ( !empty ( $parent ) && 'admin.php' != $parent ) { 1459 if ( isset( $_wp_real_parent_file[$parent] ) ) 1460 $parent = $_wp_real_parent_file[$parent]; 1461 return $parent; 1462 } 1463 1464 /* 1465 if ( !empty ( $parent_file ) ) { 1466 if ( isset( $_wp_real_parent_file[$parent_file] ) ) 1467 $parent_file = $_wp_real_parent_file[$parent_file]; 1468 1469 return $parent_file; 1470 } 1471 */ 1472 1473 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) { 1474 foreach ( (array)$menu as $parent_menu ) { 1475 if ( $parent_menu[2] == $plugin_page ) { 1476 $parent_file = $plugin_page; 1477 if ( isset( $_wp_real_parent_file[$parent_file] ) ) 1478 $parent_file = $_wp_real_parent_file[$parent_file]; 1479 return $parent_file; 1480 } 1481 } 1482 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) { 1483 $parent_file = $plugin_page; 1484 if ( isset( $_wp_real_parent_file[$parent_file] ) ) 1485 $parent_file = $_wp_real_parent_file[$parent_file]; 1486 return $parent_file; 1487 } 1488 } 1489 1490 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) { 1491 $parent_file = $pagenow; 1492 if ( isset( $_wp_real_parent_file[$parent_file] ) ) 1493 $parent_file = $_wp_real_parent_file[$parent_file]; 1494 return $parent_file; 1495 } 1496 1497 foreach (array_keys( (array)$submenu ) as $parent) { 1498 foreach ( $submenu[$parent] as $submenu_array ) { 1499 if ( isset( $_wp_real_parent_file[$parent] ) ) 1500 $parent = $_wp_real_parent_file[$parent]; 1501 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) { 1502 $parent_file = $parent; 1503 return $parent; 1504 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) { 1505 $parent_file = $parent; 1506 return $parent; 1507 } else 1508 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) { 1509 $parent_file = $parent; 1510 return $parent; 1511 } 1512 } 1513 } 1514 1515 if ( empty($parent_file) ) 1516 $parent_file = ''; 1517 return ''; 1518 } 1519 1520 function get_admin_page_title() { 1521 global $title; 1522 global $menu; 1523 global $submenu; 1524 global $pagenow; 1525 global $plugin_page; 1526 global $typenow; 1527 1528 if ( ! empty ( $title ) ) 1529 return $title; 1530 1531 $hook = get_plugin_page_hook( $plugin_page, $pagenow ); 1532 1533 $parent = $parent1 = get_admin_page_parent(); 1534 1535 if ( empty ( $parent) ) { 1536 foreach ( (array)$menu as $menu_array ) { 1537 if ( isset( $menu_array[3] ) ) { 1538 if ( $menu_array[2] == $pagenow ) { 1539 $title = $menu_array[3]; 1540 return $menu_array[3]; 1541 } else 1542 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) { 1543 $title = $menu_array[3]; 1544 return $menu_array[3]; 1545 } 1546 } else { 1547 $title = $menu_array[0]; 1548 return $title; 1549 } 1550 } 1551 } else { 1552 foreach ( array_keys( $submenu ) as $parent ) { 1553 foreach ( $submenu[$parent] as $submenu_array ) { 1554 if ( isset( $plugin_page ) && 1555 ( $plugin_page == $submenu_array[2] ) && 1556 ( 1557 ( $parent == $pagenow ) || 1558 ( $parent == $plugin_page ) || 1559 ( $plugin_page == $hook ) || 1560 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) || 1561 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow) 1562 ) 1563 ) { 1564 $title = $submenu_array[3]; 1565 return $submenu_array[3]; 1566 } 1567 1568 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page 1569 continue; 1570 1571 if ( isset( $submenu_array[3] ) ) { 1572 $title = $submenu_array[3]; 1573 return $submenu_array[3]; 1574 } else { 1575 $title = $submenu_array[0]; 1576 return $title; 1577 } 1578 } 1579 } 1580 if ( empty ( $title ) ) { 1581 foreach ( $menu as $menu_array ) { 1582 if ( isset( $plugin_page ) && 1583 ( $plugin_page == $menu_array[2] ) && 1584 ( $pagenow == 'admin.php' ) && 1585 ( $parent1 == $menu_array[2] ) ) 1586 { 1587 $title = $menu_array[3]; 1588 return $menu_array[3]; 1589 } 1590 } 1591 } 1592 } 1593 1594 return $title; 1595 } 1596 1597 function get_plugin_page_hook( $plugin_page, $parent_page ) { 1598 $hook = get_plugin_page_hookname( $plugin_page, $parent_page ); 1599 if ( has_action($hook) ) 1600 return $hook; 1601 else 1602 return null; 1603 } 1604 1605 function get_plugin_page_hookname( $plugin_page, $parent_page ) { 1606 global $admin_page_hooks; 1607 1608 $parent = get_admin_page_parent( $parent_page ); 1609 1610 $page_type = 'admin'; 1611 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) { 1612 if ( isset( $admin_page_hooks[$plugin_page] ) ) 1613 $page_type = 'toplevel'; 1614 else 1615 if ( isset( $admin_page_hooks[$parent] )) 1616 $page_type = $admin_page_hooks[$parent]; 1617 } else if ( isset( $admin_page_hooks[$parent] ) ) { 1618 $page_type = $admin_page_hooks[$parent]; 1619 } 1620 1621 $plugin_name = preg_replace( '!\.php!', '', $plugin_page ); 1622 1623 return $page_type . '_page_' . $plugin_name; 1624 } 1625 1626 function user_can_access_admin_page() { 1627 global $pagenow; 1628 global $menu; 1629 global $submenu; 1630 global $_wp_menu_nopriv; 1631 global $_wp_submenu_nopriv; 1632 global $plugin_page; 1633 global $_registered_pages; 1634 1635 $parent = get_admin_page_parent(); 1636 1637 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) ) 1638 return false; 1639 1640 if ( isset( $plugin_page ) ) { 1641 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) ) 1642 return false; 1643 1644 $hookname = get_plugin_page_hookname($plugin_page, $parent); 1645 1646 if ( !isset($_registered_pages[$hookname]) ) 1647 return false; 1648 } 1649 1650 if ( empty( $parent) ) { 1651 if ( isset( $_wp_menu_nopriv[$pagenow] ) ) 1652 return false; 1653 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) ) 1654 return false; 1655 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) 1656 return false; 1657 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) ) 1658 return false; 1659 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) { 1660 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) ) 1661 return false; 1662 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) ) 1663 return false; 1664 } 1665 return true; 1666 } 1667 1668 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) ) 1669 return false; 1670 1671 if ( isset( $submenu[$parent] ) ) { 1672 foreach ( $submenu[$parent] as $submenu_array ) { 1673 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) { 1674 if ( current_user_can( $submenu_array[1] )) 1675 return true; 1676 else 1677 return false; 1678 } else if ( $submenu_array[2] == $pagenow ) { 1679 if ( current_user_can( $submenu_array[1] )) 1680 return true; 1681 else 1682 return false; 1683 } 1684 } 1685 } 1686 1687 foreach ( $menu as $menu_array ) { 1688 if ( $menu_array[2] == $parent) { 1689 if ( current_user_can( $menu_array[1] )) 1690 return true; 1691 else 1692 return false; 1693 } 1694 } 1695 1696 return true; 1697 } 1698 1699 /* Whitelist functions */ 1700 1701 /** 1702 * Register a setting and its sanitization callback 1703 * 1704 * @since 2.7.0 1705 * 1706 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name. 1707 * Default whitelisted option key names include "general," "discussion," and "reading," among others. 1708 * @param string $option_name The name of an option to sanitize and save. 1709 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value. 1710 * @return unknown 1711 */ 1712 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) { 1713 global $new_whitelist_options; 1714 1715 if ( 'misc' == $option_group ) { 1716 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) ); 1717 $option_group = 'general'; 1718 } 1719 1720 if ( 'privacy' == $option_group ) { 1721 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) ); 1722 $option_group = 'reading'; 1723 } 1724 1725 $new_whitelist_options[ $option_group ][] = $option_name; 1726 if ( $sanitize_callback != '' ) 1727 add_filter( "sanitize_option_{$option_name}", $sanitize_callback ); 1728 } 1729 1730 /** 1731 * Unregister a setting 1732 * 1733 * @since 2.7.0 1734 * 1735 * @param unknown_type $option_group 1736 * @param unknown_type $option_name 1737 * @param unknown_type $sanitize_callback 1738 * @return unknown 1739 */ 1740 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) { 1741 global $new_whitelist_options; 1742 1743 if ( 'misc' == $option_group ) { 1744 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) ); 1745 $option_group = 'general'; 1746 } 1747 1748 if ( 'privacy' == $option_group ) { 1749 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) ); 1750 $option_group = 'reading'; 1751 } 1752 1753 $pos = array_search( $option_name, (array) $new_whitelist_options ); 1754 if ( $pos !== false ) 1755 unset( $new_whitelist_options[ $option_group ][ $pos ] ); 1756 if ( $sanitize_callback != '' ) 1757 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback ); 1758 } 1759 1760 /** 1761 * {@internal Missing Short Description}} 1762 * 1763 * @since 2.7.0 1764 * 1765 * @param unknown_type $options 1766 * @return unknown 1767 */ 1768 function option_update_filter( $options ) { 1769 global $new_whitelist_options; 1770 1771 if ( is_array( $new_whitelist_options ) ) 1772 $options = add_option_whitelist( $new_whitelist_options, $options ); 1773 1774 return $options; 1775 } 1776 add_filter( 'whitelist_options', 'option_update_filter' ); 1777 1778 /** 1779 * {@internal Missing Short Description}} 1780 * 1781 * @since 2.7.0 1782 * 1783 * @param unknown_type $new_options 1784 * @param unknown_type $options 1785 * @return unknown 1786 */ 1787 function add_option_whitelist( $new_options, $options = '' ) { 1788 if ( $options == '' ) 1789 global $whitelist_options; 1790 else 1791 $whitelist_options = $options; 1792 1793 foreach ( $new_options as $page => $keys ) { 1794 foreach ( $keys as $key ) { 1795 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) { 1796 $whitelist_options[ $page ] = array(); 1797 $whitelist_options[ $page ][] = $key; 1798 } else { 1799 $pos = array_search( $key, $whitelist_options[ $page ] ); 1800 if ( $pos === false ) 1801 $whitelist_options[ $page ][] = $key; 1802 } 1803 } 1804 } 1805 1806 return $whitelist_options; 1807 } 1808 1809 /** 1810 * {@internal Missing Short Description}} 1811 * 1812 * @since 2.7.0 1813 * 1814 * @param unknown_type $del_options 1815 * @param unknown_type $options 1816 * @return unknown 1817 */ 1818 function remove_option_whitelist( $del_options, $options = '' ) { 1819 if ( $options == '' ) 1820 global $whitelist_options; 1821 else 1822 $whitelist_options = $options; 1823 1824 foreach ( $del_options as $page => $keys ) { 1825 foreach ( $keys as $key ) { 1826 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) { 1827 $pos = array_search( $key, $whitelist_options[ $page ] ); 1828 if ( $pos !== false ) 1829 unset( $whitelist_options[ $page ][ $pos ] ); 1830 } 1831 } 1832 } 1833 1834 return $whitelist_options; 1835 } 1836 1837 /** 1838 * Output nonce, action, and option_page fields for a settings page. 1839 * 1840 * @since 2.7.0 1841 * 1842 * @param string $option_group A settings group name. This should match the group name used in register_setting(). 1843 */ 1844 function settings_fields($option_group) { 1845 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />"; 1846 echo '<input type="hidden" name="action" value="update" />'; 1847 wp_nonce_field("$option_group-options"); 1848 } 1849 1850 /** 1851 * Clears the Plugins cache used by get_plugins() and by default, the Plugin Update cache. 1852 * 1853 * @since 3.7.0 1854 * 1855 * @param bool $clear_update_cache Whether to clear the Plugin updates cache 1856 */ 1857 function wp_clean_plugins_cache( $clear_update_cache = true ) { 1858 if ( $clear_update_cache ) 1859 delete_site_transient( 'update_plugins' ); 1860 wp_cache_delete( 'plugins', 'plugins' ); 1861 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 25 01:41:18 2014 | WordPress honlapkészítés: online1.hu |