[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Screen API: WP_Screen class 4 * 5 * @package WordPress 6 * @subpackage Administration 7 * @since 4.4.0 8 */ 9 10 /** 11 * Core class used to implement an admin screen API. 12 * 13 * @since 3.3.0 14 */ 15 final class WP_Screen { 16 /** 17 * Any action associated with the screen. 18 * 19 * 'add' for *-add.php and *-new.php screens. Empty otherwise. 20 * 21 * @since 3.3.0 22 * @var string 23 */ 24 public $action; 25 26 /** 27 * The base type of the screen. 28 * 29 * This is typically the same as `$id` but with any post types and taxonomies stripped. 30 * For example, for an `$id` of 'edit-post' the base is 'edit'. 31 * 32 * @since 3.3.0 33 * @var string 34 */ 35 public $base; 36 37 /** 38 * The number of columns to display. Access with get_columns(). 39 * 40 * @since 3.4.0 41 * @var int 42 */ 43 private $columns = 0; 44 45 /** 46 * The unique ID of the screen. 47 * 48 * @since 3.3.0 49 * @var string 50 */ 51 public $id; 52 53 /** 54 * Which admin the screen is in. network | user | site | false 55 * 56 * @since 3.5.0 57 * @var string 58 */ 59 protected $in_admin; 60 61 /** 62 * Whether the screen is in the network admin. 63 * 64 * Deprecated. Use in_admin() instead. 65 * 66 * @since 3.3.0 67 * @deprecated 3.5.0 68 * @var bool 69 */ 70 public $is_network; 71 72 /** 73 * Whether the screen is in the user admin. 74 * 75 * Deprecated. Use in_admin() instead. 76 * 77 * @since 3.3.0 78 * @deprecated 3.5.0 79 * @var bool 80 */ 81 public $is_user; 82 83 /** 84 * The base menu parent. 85 * 86 * This is derived from `$parent_file` by removing the query string and any .php extension. 87 * `$parent_file` values of 'edit.php?post_type=page' and 'edit.php?post_type=post' 88 * have a `$parent_base` of 'edit'. 89 * 90 * @since 3.3.0 91 * @var string 92 */ 93 public $parent_base; 94 95 /** 96 * The parent_file for the screen per the admin menu system. 97 * 98 * Some `$parent_file` values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'. 99 * 100 * @since 3.3.0 101 * @var string 102 */ 103 public $parent_file; 104 105 /** 106 * The post type associated with the screen, if any. 107 * 108 * The 'edit.php?post_type=page' screen has a post type of 'page'. 109 * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'. 110 * 111 * @since 3.3.0 112 * @var string 113 */ 114 public $post_type; 115 116 /** 117 * The taxonomy associated with the screen, if any. 118 * 119 * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'. 120 * 121 * @since 3.3.0 122 * @var string 123 */ 124 public $taxonomy; 125 126 /** 127 * The help tab data associated with the screen, if any. 128 * 129 * @since 3.3.0 130 * @var array 131 */ 132 private $_help_tabs = array(); 133 134 /** 135 * The help sidebar data associated with screen, if any. 136 * 137 * @since 3.3.0 138 * @var string 139 */ 140 private $_help_sidebar = ''; 141 142 /** 143 * The accessible hidden headings and text associated with the screen, if any. 144 * 145 * @since 4.4.0 146 * @var array 147 */ 148 private $_screen_reader_content = array(); 149 150 /** 151 * Stores old string-based help. 152 * 153 * @var array 154 */ 155 private static $_old_compat_help = array(); 156 157 /** 158 * The screen options associated with screen, if any. 159 * 160 * @since 3.3.0 161 * @var array 162 */ 163 private $_options = array(); 164 165 /** 166 * The screen object registry. 167 * 168 * @since 3.3.0 169 * 170 * @var array 171 */ 172 private static $_registry = array(); 173 174 /** 175 * Stores the result of the public show_screen_options function. 176 * 177 * @since 3.3.0 178 * @var bool 179 */ 180 private $_show_screen_options; 181 182 /** 183 * Stores the 'screen_settings' section of screen options. 184 * 185 * @since 3.3.0 186 * @var string 187 */ 188 private $_screen_settings; 189 190 /** 191 * Whether the screen is using the block editor. 192 * 193 * @since 5.0.0 194 * @var bool 195 */ 196 public $is_block_editor = false; 197 198 /** 199 * Fetches a screen object. 200 * 201 * @since 3.3.0 202 * 203 * @global string $hook_suffix 204 * 205 * @param string|WP_Screen $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen. 206 * Defaults to the current $hook_suffix global. 207 * @return WP_Screen Screen object. 208 */ 209 public static function get( $hook_name = '' ) { 210 if ( $hook_name instanceof WP_Screen ) { 211 return $hook_name; 212 } 213 214 $post_type = null; 215 $taxonomy = null; 216 $in_admin = false; 217 $action = ''; 218 $is_block_editor = false; 219 220 if ( $hook_name ) { 221 $id = $hook_name; 222 } else { 223 $id = $GLOBALS['hook_suffix']; 224 } 225 226 // For those pesky meta boxes. 227 if ( $hook_name && post_type_exists( $hook_name ) ) { 228 $post_type = $id; 229 $id = 'post'; // Changes later. Ends up being $base. 230 } else { 231 if ( '.php' === substr( $id, -4 ) ) { 232 $id = substr( $id, 0, -4 ); 233 } 234 235 if ( in_array( $id, array( 'post-new', 'link-add', 'media-new', 'user-new' ), true ) ) { 236 $id = substr( $id, 0, -4 ); 237 $action = 'add'; 238 } 239 } 240 241 if ( ! $post_type && $hook_name ) { 242 if ( '-network' === substr( $id, -8 ) ) { 243 $id = substr( $id, 0, -8 ); 244 $in_admin = 'network'; 245 } elseif ( '-user' === substr( $id, -5 ) ) { 246 $id = substr( $id, 0, -5 ); 247 $in_admin = 'user'; 248 } 249 250 $id = sanitize_key( $id ); 251 if ( 'edit-comments' !== $id && 'edit-tags' !== $id && 'edit-' === substr( $id, 0, 5 ) ) { 252 $maybe = substr( $id, 5 ); 253 if ( taxonomy_exists( $maybe ) ) { 254 $id = 'edit-tags'; 255 $taxonomy = $maybe; 256 } elseif ( post_type_exists( $maybe ) ) { 257 $id = 'edit'; 258 $post_type = $maybe; 259 } 260 } 261 262 if ( ! $in_admin ) { 263 $in_admin = 'site'; 264 } 265 } else { 266 if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) { 267 $in_admin = 'network'; 268 } elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN ) { 269 $in_admin = 'user'; 270 } else { 271 $in_admin = 'site'; 272 } 273 } 274 275 if ( 'index' === $id ) { 276 $id = 'dashboard'; 277 } elseif ( 'front' === $id ) { 278 $in_admin = false; 279 } 280 281 $base = $id; 282 283 // If this is the current screen, see if we can be more accurate for post types and taxonomies. 284 if ( ! $hook_name ) { 285 if ( isset( $_REQUEST['post_type'] ) ) { 286 $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false; 287 } 288 if ( isset( $_REQUEST['taxonomy'] ) ) { 289 $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false; 290 } 291 292 switch ( $base ) { 293 case 'post': 294 if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) { 295 wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 ); 296 } elseif ( isset( $_GET['post'] ) ) { 297 $post_id = (int) $_GET['post']; 298 } elseif ( isset( $_POST['post_ID'] ) ) { 299 $post_id = (int) $_POST['post_ID']; 300 } else { 301 $post_id = 0; 302 } 303 304 if ( $post_id ) { 305 $post = get_post( $post_id ); 306 if ( $post ) { 307 $post_type = $post->post_type; 308 309 /** This filter is documented in wp-admin/post.php */ 310 $replace_editor = apply_filters( 'replace_editor', false, $post ); 311 312 if ( ! $replace_editor ) { 313 $is_block_editor = use_block_editor_for_post( $post ); 314 } 315 } 316 } 317 break; 318 case 'edit-tags': 319 case 'term': 320 if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) ) { 321 $post_type = 'post'; 322 } 323 break; 324 case 'upload': 325 $post_type = 'attachment'; 326 break; 327 } 328 } 329 330 switch ( $base ) { 331 case 'post': 332 if ( null === $post_type ) { 333 $post_type = 'post'; 334 } 335 336 // When creating a new post, use the default block editor support value for the post type. 337 if ( empty( $post_id ) ) { 338 $is_block_editor = use_block_editor_for_post_type( $post_type ); 339 } 340 341 $id = $post_type; 342 break; 343 case 'edit': 344 if ( null === $post_type ) { 345 $post_type = 'post'; 346 } 347 $id .= '-' . $post_type; 348 break; 349 case 'edit-tags': 350 case 'term': 351 if ( null === $taxonomy ) { 352 $taxonomy = 'post_tag'; 353 } 354 // The edit-tags ID does not contain the post type. Look for it in the request. 355 if ( null === $post_type ) { 356 $post_type = 'post'; 357 if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) { 358 $post_type = $_REQUEST['post_type']; 359 } 360 } 361 362 $id = 'edit-' . $taxonomy; 363 break; 364 } 365 366 if ( 'network' === $in_admin ) { 367 $id .= '-network'; 368 $base .= '-network'; 369 } elseif ( 'user' === $in_admin ) { 370 $id .= '-user'; 371 $base .= '-user'; 372 } 373 374 if ( isset( self::$_registry[ $id ] ) ) { 375 $screen = self::$_registry[ $id ]; 376 if ( get_current_screen() === $screen ) { 377 return $screen; 378 } 379 } else { 380 $screen = new self(); 381 $screen->id = $id; 382 } 383 384 $screen->base = $base; 385 $screen->action = $action; 386 $screen->post_type = (string) $post_type; 387 $screen->taxonomy = (string) $taxonomy; 388 $screen->is_user = ( 'user' === $in_admin ); 389 $screen->is_network = ( 'network' === $in_admin ); 390 $screen->in_admin = $in_admin; 391 $screen->is_block_editor = $is_block_editor; 392 393 self::$_registry[ $id ] = $screen; 394 395 return $screen; 396 } 397 398 /** 399 * Makes the screen object the current screen. 400 * 401 * @see set_current_screen() 402 * @since 3.3.0 403 * 404 * @global WP_Screen $current_screen WordPress current screen object. 405 * @global string $typenow The post type of the current screen. 406 * @global string $taxnow The taxonomy of the current screen. 407 */ 408 public function set_current_screen() { 409 global $current_screen, $taxnow, $typenow; 410 411 $current_screen = $this; 412 $typenow = $this->post_type; 413 $taxnow = $this->taxonomy; 414 415 /** 416 * Fires after the current screen has been set. 417 * 418 * @since 3.0.0 419 * 420 * @param WP_Screen $current_screen Current WP_Screen object. 421 */ 422 do_action( 'current_screen', $current_screen ); 423 } 424 425 /** 426 * Constructor 427 * 428 * @since 3.3.0 429 */ 430 private function __construct() {} 431 432 /** 433 * Indicates whether the screen is in a particular admin 434 * 435 * @since 3.5.0 436 * 437 * @param string $admin The admin to check against (network | user | site). 438 * If empty any of the three admins will result in true. 439 * @return bool True if the screen is in the indicated admin, false otherwise. 440 */ 441 public function in_admin( $admin = null ) { 442 if ( empty( $admin ) ) { 443 return (bool) $this->in_admin; 444 } 445 446 return ( $admin === $this->in_admin ); 447 } 448 449 /** 450 * Sets or returns whether the block editor is loading on the current screen. 451 * 452 * @since 5.0.0 453 * 454 * @param bool $set Optional. Sets whether the block editor is loading on the current screen or not. 455 * @return bool True if the block editor is being loaded, false otherwise. 456 */ 457 public function is_block_editor( $set = null ) { 458 if ( null !== $set ) { 459 $this->is_block_editor = (bool) $set; 460 } 461 462 return $this->is_block_editor; 463 } 464 465 /** 466 * Sets the old string-based contextual help for the screen for backward compatibility. 467 * 468 * @since 3.3.0 469 * 470 * @param WP_Screen $screen A screen object. 471 * @param string $help Help text. 472 */ 473 public static function add_old_compat_help( $screen, $help ) { 474 self::$_old_compat_help[ $screen->id ] = $help; 475 } 476 477 /** 478 * Set the parent information for the screen. 479 * 480 * This is called in admin-header.php after the menu parent for the screen has been determined. 481 * 482 * @since 3.3.0 483 * 484 * @param string $parent_file The parent file of the screen. Typically the $parent_file global. 485 */ 486 public function set_parentage( $parent_file ) { 487 $this->parent_file = $parent_file; 488 list( $this->parent_base ) = explode( '?', $parent_file ); 489 $this->parent_base = str_replace( '.php', '', $this->parent_base ); 490 } 491 492 /** 493 * Adds an option for the screen. 494 * 495 * Call this in template files after admin.php is loaded and before admin-header.php is loaded 496 * to add screen options. 497 * 498 * @since 3.3.0 499 * 500 * @param string $option Option ID. 501 * @param mixed $args Option-dependent arguments. 502 */ 503 public function add_option( $option, $args = array() ) { 504 $this->_options[ $option ] = $args; 505 } 506 507 /** 508 * Remove an option from the screen. 509 * 510 * @since 3.8.0 511 * 512 * @param string $option Option ID. 513 */ 514 public function remove_option( $option ) { 515 unset( $this->_options[ $option ] ); 516 } 517 518 /** 519 * Remove all options from the screen. 520 * 521 * @since 3.8.0 522 */ 523 public function remove_options() { 524 $this->_options = array(); 525 } 526 527 /** 528 * Get the options registered for the screen. 529 * 530 * @since 3.8.0 531 * 532 * @return array Options with arguments. 533 */ 534 public function get_options() { 535 return $this->_options; 536 } 537 538 /** 539 * Gets the arguments for an option for the screen. 540 * 541 * @since 3.3.0 542 * 543 * @param string $option Option name. 544 * @param string|false $key Optional. Specific array key for when the option is an array. 545 * Default false. 546 * @return string The option value if set, null otherwise. 547 */ 548 public function get_option( $option, $key = false ) { 549 if ( ! isset( $this->_options[ $option ] ) ) { 550 return null; 551 } 552 if ( $key ) { 553 if ( isset( $this->_options[ $option ][ $key ] ) ) { 554 return $this->_options[ $option ][ $key ]; 555 } 556 return null; 557 } 558 return $this->_options[ $option ]; 559 } 560 561 /** 562 * Gets the help tabs registered for the screen. 563 * 564 * @since 3.4.0 565 * @since 4.4.0 Help tabs are ordered by their priority. 566 * 567 * @return array Help tabs with arguments. 568 */ 569 public function get_help_tabs() { 570 $help_tabs = $this->_help_tabs; 571 572 $priorities = array(); 573 foreach ( $help_tabs as $help_tab ) { 574 if ( isset( $priorities[ $help_tab['priority'] ] ) ) { 575 $priorities[ $help_tab['priority'] ][] = $help_tab; 576 } else { 577 $priorities[ $help_tab['priority'] ] = array( $help_tab ); 578 } 579 } 580 581 ksort( $priorities ); 582 583 $sorted = array(); 584 foreach ( $priorities as $list ) { 585 foreach ( $list as $tab ) { 586 $sorted[ $tab['id'] ] = $tab; 587 } 588 } 589 590 return $sorted; 591 } 592 593 /** 594 * Gets the arguments for a help tab. 595 * 596 * @since 3.4.0 597 * 598 * @param string $id Help Tab ID. 599 * @return array Help tab arguments. 600 */ 601 public function get_help_tab( $id ) { 602 if ( ! isset( $this->_help_tabs[ $id ] ) ) { 603 return null; 604 } 605 return $this->_help_tabs[ $id ]; 606 } 607 608 /** 609 * Add a help tab to the contextual help for the screen. 610 * 611 * Call this on the `load-$pagenow` hook for the relevant screen, 612 * or fetch the `$current_screen` object, or use get_current_screen() 613 * and then call the method from the object. 614 * 615 * You may need to filter `$current_screen` using an if or switch statement 616 * to prevent new help tabs from being added to ALL admin screens. 617 * 618 * @since 3.3.0 619 * @since 4.4.0 The `$priority` argument was added. 620 * 621 * @param array $args { 622 * Array of arguments used to display the help tab. 623 * 624 * @type string $title Title for the tab. Default false. 625 * @type string $id Tab ID. Must be HTML-safe and should be unique for this menu. 626 * It is NOT allowed to contain any empty spaces. Default false. 627 * @type string $content Optional. Help tab content in plain text or HTML. Default empty string. 628 * @type callable $callback Optional. A callback to generate the tab content. Default false. 629 * @type int $priority Optional. The priority of the tab, used for ordering. Default 10. 630 * } 631 */ 632 public function add_help_tab( $args ) { 633 $defaults = array( 634 'title' => false, 635 'id' => false, 636 'content' => '', 637 'callback' => false, 638 'priority' => 10, 639 ); 640 $args = wp_parse_args( $args, $defaults ); 641 642 $args['id'] = sanitize_html_class( $args['id'] ); 643 644 // Ensure we have an ID and title. 645 if ( ! $args['id'] || ! $args['title'] ) { 646 return; 647 } 648 649 // Allows for overriding an existing tab with that ID. 650 $this->_help_tabs[ $args['id'] ] = $args; 651 } 652 653 /** 654 * Removes a help tab from the contextual help for the screen. 655 * 656 * @since 3.3.0 657 * 658 * @param string $id The help tab ID. 659 */ 660 public function remove_help_tab( $id ) { 661 unset( $this->_help_tabs[ $id ] ); 662 } 663 664 /** 665 * Removes all help tabs from the contextual help for the screen. 666 * 667 * @since 3.3.0 668 */ 669 public function remove_help_tabs() { 670 $this->_help_tabs = array(); 671 } 672 673 /** 674 * Gets the content from a contextual help sidebar. 675 * 676 * @since 3.4.0 677 * 678 * @return string Contents of the help sidebar. 679 */ 680 public function get_help_sidebar() { 681 return $this->_help_sidebar; 682 } 683 684 /** 685 * Add a sidebar to the contextual help for the screen. 686 * 687 * Call this in template files after admin.php is loaded and before admin-header.php is loaded 688 * to add a sidebar to the contextual help. 689 * 690 * @since 3.3.0 691 * 692 * @param string $content Sidebar content in plain text or HTML. 693 */ 694 public function set_help_sidebar( $content ) { 695 $this->_help_sidebar = $content; 696 } 697 698 /** 699 * Gets the number of layout columns the user has selected. 700 * 701 * The layout_columns option controls the max number and default number of 702 * columns. This method returns the number of columns within that range selected 703 * by the user via Screen Options. If no selection has been made, the default 704 * provisioned in layout_columns is returned. If the screen does not support 705 * selecting the number of layout columns, 0 is returned. 706 * 707 * @since 3.4.0 708 * 709 * @return int Number of columns to display. 710 */ 711 public function get_columns() { 712 return $this->columns; 713 } 714 715 /** 716 * Get the accessible hidden headings and text used in the screen. 717 * 718 * @since 4.4.0 719 * 720 * @see set_screen_reader_content() For more information on the array format. 721 * 722 * @return array An associative array of screen reader text strings. 723 */ 724 public function get_screen_reader_content() { 725 return $this->_screen_reader_content; 726 } 727 728 /** 729 * Get a screen reader text string. 730 * 731 * @since 4.4.0 732 * 733 * @param string $key Screen reader text array named key. 734 * @return string Screen reader text string. 735 */ 736 public function get_screen_reader_text( $key ) { 737 if ( ! isset( $this->_screen_reader_content[ $key ] ) ) { 738 return null; 739 } 740 return $this->_screen_reader_content[ $key ]; 741 } 742 743 /** 744 * Add accessible hidden headings and text for the screen. 745 * 746 * @since 4.4.0 747 * 748 * @param array $content { 749 * An associative array of screen reader text strings. 750 * 751 * @type string $heading_views Screen reader text for the filter links heading. 752 * Default 'Filter items list'. 753 * @type string $heading_pagination Screen reader text for the pagination heading. 754 * Default 'Items list navigation'. 755 * @type string $heading_list Screen reader text for the items list heading. 756 * Default 'Items list'. 757 * } 758 */ 759 public function set_screen_reader_content( $content = array() ) { 760 $defaults = array( 761 'heading_views' => __( 'Filter items list' ), 762 'heading_pagination' => __( 'Items list navigation' ), 763 'heading_list' => __( 'Items list' ), 764 ); 765 $content = wp_parse_args( $content, $defaults ); 766 767 $this->_screen_reader_content = $content; 768 } 769 770 /** 771 * Remove all the accessible hidden headings and text for the screen. 772 * 773 * @since 4.4.0 774 */ 775 public function remove_screen_reader_content() { 776 $this->_screen_reader_content = array(); 777 } 778 779 /** 780 * Render the screen's help section. 781 * 782 * This will trigger the deprecated filters for backward compatibility. 783 * 784 * @since 3.3.0 785 * 786 * @global string $screen_layout_columns 787 */ 788 public function render_screen_meta() { 789 790 /** 791 * Filters the legacy contextual help list. 792 * 793 * @since 2.7.0 794 * @deprecated 3.3.0 Use {@see get_current_screen()->add_help_tab()} or 795 * {@see get_current_screen()->remove_help_tab()} instead. 796 * 797 * @param array $old_compat_help Old contextual help. 798 * @param WP_Screen $screen Current WP_Screen instance. 799 */ 800 self::$_old_compat_help = apply_filters_deprecated( 801 'contextual_help_list', 802 array( self::$_old_compat_help, $this ), 803 '3.3.0', 804 'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()' 805 ); 806 807 $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : ''; 808 809 /** 810 * Filters the legacy contextual help text. 811 * 812 * @since 2.7.0 813 * @deprecated 3.3.0 Use {@see get_current_screen()->add_help_tab()} or 814 * {@see get_current_screen()->remove_help_tab()} instead. 815 * 816 * @param string $old_help Help text that appears on the screen. 817 * @param string $screen_id Screen ID. 818 * @param WP_Screen $screen Current WP_Screen instance. 819 */ 820 $old_help = apply_filters_deprecated( 821 'contextual_help', 822 array( $old_help, $this->id, $this ), 823 '3.3.0', 824 'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()' 825 ); 826 827 // Default help only if there is no old-style block of text and no new-style help tabs. 828 if ( empty( $old_help ) && ! $this->get_help_tabs() ) { 829 830 /** 831 * Filters the default legacy contextual help text. 832 * 833 * @since 2.8.0 834 * @deprecated 3.3.0 Use {@see get_current_screen()->add_help_tab()} or 835 * {@see get_current_screen()->remove_help_tab()} instead. 836 * 837 * @param string $old_help_default Default contextual help text. 838 */ 839 $default_help = apply_filters_deprecated( 840 'default_contextual_help', 841 array( '' ), 842 '3.3.0', 843 'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()' 844 ); 845 if ( $default_help ) { 846 $old_help = '<p>' . $default_help . '</p>'; 847 } 848 } 849 850 if ( $old_help ) { 851 $this->add_help_tab( 852 array( 853 'id' => 'old-contextual-help', 854 'title' => __( 'Overview' ), 855 'content' => $old_help, 856 ) 857 ); 858 } 859 860 $help_sidebar = $this->get_help_sidebar(); 861 862 $help_class = 'hidden'; 863 if ( ! $help_sidebar ) { 864 $help_class .= ' no-sidebar'; 865 } 866 867 // Time to render! 868 ?> 869 <div id="screen-meta" class="metabox-prefs"> 870 871 <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>" tabindex="-1" aria-label="<?php esc_attr_e( 'Contextual Help Tab' ); ?>"> 872 <div id="contextual-help-back"></div> 873 <div id="contextual-help-columns"> 874 <div class="contextual-help-tabs"> 875 <ul> 876 <?php 877 $class = ' class="active"'; 878 foreach ( $this->get_help_tabs() as $tab ) : 879 $link_id = "tab-link-{$tab['id']}"; 880 $panel_id = "tab-panel-{$tab['id']}"; 881 ?> 882 883 <li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>> 884 <a href="<?php echo esc_url( "#$panel_id" ); ?>" aria-controls="<?php echo esc_attr( $panel_id ); ?>"> 885 <?php echo esc_html( $tab['title'] ); ?> 886 </a> 887 </li> 888 <?php 889 $class = ''; 890 endforeach; 891 ?> 892 </ul> 893 </div> 894 895 <?php if ( $help_sidebar ) : ?> 896 <div class="contextual-help-sidebar"> 897 <?php echo $help_sidebar; ?> 898 </div> 899 <?php endif; ?> 900 901 <div class="contextual-help-tabs-wrap"> 902 <?php 903 $classes = 'help-tab-content active'; 904 foreach ( $this->get_help_tabs() as $tab ) : 905 $panel_id = "tab-panel-{$tab['id']}"; 906 ?> 907 908 <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>"> 909 <?php 910 // Print tab content. 911 echo $tab['content']; 912 913 // If it exists, fire tab callback. 914 if ( ! empty( $tab['callback'] ) ) { 915 call_user_func_array( $tab['callback'], array( $this, $tab ) ); 916 } 917 ?> 918 </div> 919 <?php 920 $classes = 'help-tab-content'; 921 endforeach; 922 ?> 923 </div> 924 </div> 925 </div> 926 <?php 927 // Setup layout columns. 928 929 /** 930 * Filters the array of screen layout columns. 931 * 932 * This hook provides back-compat for plugins using the back-compat 933 * Filters instead of add_screen_option(). 934 * 935 * @since 2.8.0 936 * 937 * @param array $empty_columns Empty array. 938 * @param string $screen_id Screen ID. 939 * @param WP_Screen $screen Current WP_Screen instance. 940 */ 941 $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this ); 942 943 if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) ) { 944 $this->add_option( 'layout_columns', array( 'max' => $columns[ $this->id ] ) ); 945 } 946 947 if ( $this->get_option( 'layout_columns' ) ) { 948 $this->columns = (int) get_user_option( "screen_layout_$this->id" ); 949 950 if ( ! $this->columns && $this->get_option( 'layout_columns', 'default' ) ) { 951 $this->columns = $this->get_option( 'layout_columns', 'default' ); 952 } 953 } 954 $GLOBALS['screen_layout_columns'] = $this->columns; // Set the global for back-compat. 955 956 // Add screen options. 957 if ( $this->show_screen_options() ) { 958 $this->render_screen_options(); 959 } 960 ?> 961 </div> 962 <?php 963 if ( ! $this->get_help_tabs() && ! $this->show_screen_options() ) { 964 return; 965 } 966 ?> 967 <div id="screen-meta-links"> 968 <?php if ( $this->show_screen_options() ) : ?> 969 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle"> 970 <button type="button" id="show-settings-link" class="button show-settings" aria-controls="screen-options-wrap" aria-expanded="false"><?php _e( 'Screen Options' ); ?></button> 971 </div> 972 <?php 973 endif; 974 if ( $this->get_help_tabs() ) : 975 ?> 976 <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle"> 977 <button type="button" id="contextual-help-link" class="button show-settings" aria-controls="contextual-help-wrap" aria-expanded="false"><?php _e( 'Help' ); ?></button> 978 </div> 979 <?php endif; ?> 980 </div> 981 <?php 982 } 983 984 /** 985 * @global array $wp_meta_boxes 986 * 987 * @return bool 988 */ 989 public function show_screen_options() { 990 global $wp_meta_boxes; 991 992 if ( is_bool( $this->_show_screen_options ) ) { 993 return $this->_show_screen_options; 994 } 995 996 $columns = get_column_headers( $this ); 997 998 $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' ); 999 1000 $this->_screen_settings = ''; 1001 1002 if ( 'post' === $this->base ) { 1003 $expand = '<fieldset class="editor-expand hidden"><legend>' . __( 'Additional settings' ) . '</legend><label for="editor-expand-toggle">'; 1004 $expand .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />'; 1005 $expand .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label></fieldset>'; 1006 $this->_screen_settings = $expand; 1007 } 1008 1009 /** 1010 * Filters the screen settings text displayed in the Screen Options tab. 1011 * 1012 * @since 3.0.0 1013 * 1014 * @param string $screen_settings Screen settings. 1015 * @param WP_Screen $screen WP_Screen object. 1016 */ 1017 $this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this ); 1018 1019 if ( $this->_screen_settings || $this->_options ) { 1020 $show_screen = true; 1021 } 1022 1023 /** 1024 * Filters whether to show the Screen Options tab. 1025 * 1026 * @since 3.2.0 1027 * 1028 * @param bool $show_screen Whether to show Screen Options tab. 1029 * Default true. 1030 * @param WP_Screen $screen Current WP_Screen instance. 1031 */ 1032 $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this ); 1033 return $this->_show_screen_options; 1034 } 1035 1036 /** 1037 * Render the screen options tab. 1038 * 1039 * @since 3.3.0 1040 * 1041 * @param array $options { 1042 * Options for the tab. 1043 * 1044 * @type bool $wrap Whether the screen-options-wrap div will be included. Defaults to true. 1045 * } 1046 */ 1047 public function render_screen_options( $options = array() ) { 1048 $options = wp_parse_args( 1049 $options, 1050 array( 1051 'wrap' => true, 1052 ) 1053 ); 1054 1055 $wrapper_start = ''; 1056 $wrapper_end = ''; 1057 $form_start = ''; 1058 $form_end = ''; 1059 1060 // Output optional wrapper. 1061 if ( $options['wrap'] ) { 1062 $wrapper_start = '<div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="' . esc_attr__( 'Screen Options Tab' ) . '">'; 1063 $wrapper_end = '</div>'; 1064 } 1065 1066 // Don't output the form and nonce for the widgets accessibility mode links. 1067 if ( 'widgets' !== $this->base ) { 1068 $form_start = "\n<form id='adv-settings' method='post'>\n"; 1069 $form_end = "\n" . wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false, false ) . "\n</form>\n"; 1070 } 1071 1072 echo $wrapper_start . $form_start; 1073 1074 $this->render_meta_boxes_preferences(); 1075 $this->render_list_table_columns_preferences(); 1076 $this->render_screen_layout(); 1077 $this->render_per_page_options(); 1078 $this->render_view_mode(); 1079 echo $this->_screen_settings; 1080 1081 /** 1082 * Filters whether to show the Screen Options submit button. 1083 * 1084 * @since 4.4.0 1085 * 1086 * @param bool $show_button Whether to show Screen Options submit button. 1087 * Default false. 1088 * @param WP_Screen $screen Current WP_Screen instance. 1089 */ 1090 $show_button = apply_filters( 'screen_options_show_submit', false, $this ); 1091 1092 if ( $show_button ) { 1093 submit_button( __( 'Apply' ), 'primary', 'screen-options-apply', true ); 1094 } 1095 1096 echo $form_end . $wrapper_end; 1097 } 1098 1099 /** 1100 * Render the meta boxes preferences. 1101 * 1102 * @since 4.4.0 1103 * 1104 * @global array $wp_meta_boxes 1105 */ 1106 public function render_meta_boxes_preferences() { 1107 global $wp_meta_boxes; 1108 1109 if ( ! isset( $wp_meta_boxes[ $this->id ] ) ) { 1110 return; 1111 } 1112 ?> 1113 <fieldset class="metabox-prefs"> 1114 <legend><?php _e( 'Screen elements' ); ?></legend> 1115 <p> 1116 <?php _e( 'Some screen elements can be shown or hidden by using the checkboxes.' ); ?> 1117 <?php _e( 'They can be expanded and collapsed by clickling on their headings, and arranged by dragging their headings or by clicking on the up and down arrows.' ); ?> 1118 </p> 1119 <?php 1120 1121 meta_box_prefs( $this ); 1122 1123 if ( 'dashboard' === $this->id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) { 1124 if ( isset( $_GET['welcome'] ) ) { 1125 $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1; 1126 update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked ); 1127 } else { 1128 $welcome_checked = (int) get_user_meta( get_current_user_id(), 'show_welcome_panel', true ); 1129 if ( 2 === $welcome_checked && wp_get_current_user()->user_email !== get_option( 'admin_email' ) ) { 1130 $welcome_checked = false; 1131 } 1132 } 1133 echo '<label for="wp_welcome_panel-hide">'; 1134 echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />'; 1135 echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n"; 1136 } 1137 ?> 1138 </fieldset> 1139 <?php 1140 } 1141 1142 /** 1143 * Render the list table columns preferences. 1144 * 1145 * @since 4.4.0 1146 */ 1147 public function render_list_table_columns_preferences() { 1148 1149 $columns = get_column_headers( $this ); 1150 $hidden = get_hidden_columns( $this ); 1151 1152 if ( ! $columns ) { 1153 return; 1154 } 1155 1156 $legend = ! empty( $columns['_title'] ) ? $columns['_title'] : __( 'Columns' ); 1157 ?> 1158 <fieldset class="metabox-prefs"> 1159 <legend><?php echo $legend; ?></legend> 1160 <?php 1161 $special = array( '_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname' ); 1162 1163 foreach ( $columns as $column => $title ) { 1164 // Can't hide these for they are special. 1165 if ( in_array( $column, $special, true ) ) { 1166 continue; 1167 } 1168 1169 if ( empty( $title ) ) { 1170 continue; 1171 } 1172 1173 /* 1174 * The Comments column uses HTML in the display name with some screen 1175 * reader text. Make sure to strip tags from the Comments column 1176 * title and any other custom column title plugins might add. 1177 */ 1178 $title = wp_strip_all_tags( $title ); 1179 1180 $id = "$column-hide"; 1181 echo '<label>'; 1182 echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( ! in_array( $column, $hidden, true ), true, false ) . ' />'; 1183 echo "$title</label>\n"; 1184 } 1185 ?> 1186 </fieldset> 1187 <?php 1188 } 1189 1190 /** 1191 * Render the option for number of columns on the page 1192 * 1193 * @since 3.3.0 1194 */ 1195 public function render_screen_layout() { 1196 if ( ! $this->get_option( 'layout_columns' ) ) { 1197 return; 1198 } 1199 1200 $screen_layout_columns = $this->get_columns(); 1201 $num = $this->get_option( 'layout_columns', 'max' ); 1202 1203 ?> 1204 <fieldset class='columns-prefs'> 1205 <legend class="screen-layout"><?php _e( 'Layout' ); ?></legend> 1206 <?php for ( $i = 1; $i <= $num; ++$i ) : ?> 1207 <label class="columns-prefs-<?php echo $i; ?>"> 1208 <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>' <?php checked( $screen_layout_columns, $i ); ?> /> 1209 <?php 1210 printf( 1211 /* translators: %s: Number of columns on the page. */ 1212 _n( '%s column', '%s columns', $i ), 1213 number_format_i18n( $i ) 1214 ); 1215 ?> 1216 </label> 1217 <?php endfor; ?> 1218 </fieldset> 1219 <?php 1220 } 1221 1222 /** 1223 * Render the items per page option 1224 * 1225 * @since 3.3.0 1226 */ 1227 public function render_per_page_options() { 1228 if ( null === $this->get_option( 'per_page' ) ) { 1229 return; 1230 } 1231 1232 $per_page_label = $this->get_option( 'per_page', 'label' ); 1233 if ( null === $per_page_label ) { 1234 $per_page_label = __( 'Number of items per page:' ); 1235 } 1236 1237 $option = $this->get_option( 'per_page', 'option' ); 1238 if ( ! $option ) { 1239 $option = str_replace( '-', '_', "{$this->id}_per_page" ); 1240 } 1241 1242 $per_page = (int) get_user_option( $option ); 1243 if ( empty( $per_page ) || $per_page < 1 ) { 1244 $per_page = $this->get_option( 'per_page', 'default' ); 1245 if ( ! $per_page ) { 1246 $per_page = 20; 1247 } 1248 } 1249 1250 if ( 'edit_comments_per_page' === $option ) { 1251 $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all'; 1252 1253 /** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */ 1254 $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status ); 1255 } elseif ( 'categories_per_page' === $option ) { 1256 /** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */ 1257 $per_page = apply_filters( 'edit_categories_per_page', $per_page ); 1258 } else { 1259 /** This filter is documented in wp-admin/includes/class-wp-list-table.php */ 1260 $per_page = apply_filters( "{$option}", $per_page ); 1261 } 1262 1263 // Back compat. 1264 if ( isset( $this->post_type ) ) { 1265 /** This filter is documented in wp-admin/includes/post.php */ 1266 $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type ); 1267 } 1268 1269 // This needs a submit button. 1270 add_filter( 'screen_options_show_submit', '__return_true' ); 1271 1272 ?> 1273 <fieldset class="screen-options"> 1274 <legend><?php _e( 'Pagination' ); ?></legend> 1275 <?php if ( $per_page_label ) : ?> 1276 <label for="<?php echo esc_attr( $option ); ?>"><?php echo $per_page_label; ?></label> 1277 <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]" 1278 id="<?php echo esc_attr( $option ); ?>" maxlength="3" 1279 value="<?php echo esc_attr( $per_page ); ?>" /> 1280 <?php endif; ?> 1281 <input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" /> 1282 </fieldset> 1283 <?php 1284 } 1285 1286 /** 1287 * Render the list table view mode preferences. 1288 * 1289 * @since 4.4.0 1290 * 1291 * @global string $mode List table view mode. 1292 */ 1293 public function render_view_mode() { 1294 global $mode; 1295 1296 $screen = get_current_screen(); 1297 1298 // Currently only enabled for posts and comments lists. 1299 if ( 'edit' !== $screen->base && 'edit-comments' !== $screen->base ) { 1300 return; 1301 } 1302 1303 $view_mode_post_types = get_post_types( array( 'show_ui' => true ) ); 1304 1305 /** 1306 * Filters the post types that have different view mode options. 1307 * 1308 * @since 4.4.0 1309 * 1310 * @param string[] $view_mode_post_types Array of post types that can change view modes. 1311 * Default post types with show_ui on. 1312 */ 1313 $view_mode_post_types = apply_filters( 'view_mode_post_types', $view_mode_post_types ); 1314 1315 if ( 'edit' === $screen->base && ! in_array( $this->post_type, $view_mode_post_types, true ) ) { 1316 return; 1317 } 1318 1319 if ( ! isset( $mode ) ) { 1320 $mode = get_user_setting( 'posts_list_mode', 'list' ); 1321 } 1322 1323 // This needs a submit button. 1324 add_filter( 'screen_options_show_submit', '__return_true' ); 1325 ?> 1326 <fieldset class="metabox-prefs view-mode"> 1327 <legend><?php _e( 'View mode' ); ?></legend> 1328 <label for="list-view-mode"> 1329 <input id="list-view-mode" type="radio" name="mode" value="list" <?php checked( 'list', $mode ); ?> /> 1330 <?php _e( 'Compact view' ); ?> 1331 </label> 1332 <label for="excerpt-view-mode"> 1333 <input id="excerpt-view-mode" type="radio" name="mode" value="excerpt" <?php checked( 'excerpt', $mode ); ?> /> 1334 <?php _e( 'Extended view' ); ?> 1335 </label> 1336 </fieldset> 1337 <?php 1338 } 1339 1340 /** 1341 * Render screen reader text. 1342 * 1343 * @since 4.4.0 1344 * 1345 * @param string $key The screen reader text array named key. 1346 * @param string $tag Optional. The HTML tag to wrap the screen reader text. Default h2. 1347 */ 1348 public function render_screen_reader_content( $key = '', $tag = 'h2' ) { 1349 1350 if ( ! isset( $this->_screen_reader_content[ $key ] ) ) { 1351 return; 1352 } 1353 echo "<$tag class='screen-reader-text'>" . $this->_screen_reader_content[ $key ] . "</$tag>"; 1354 } 1355 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 5 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |