[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-wp-screen.php (source)

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


Generated : Thu Apr 16 08:20:10 2026 Cross-referenced by PHPXref