[ 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()} with
 796           *                   {@see WP_Screen::add_help_tab()} or
 797           *                   {@see WP_Screen::remove_help_tab()} instead.
 798           *
 799           * @param array     $old_compat_help Old contextual help.
 800           * @param WP_Screen $screen          Current WP_Screen instance.
 801           */
 802          self::$_old_compat_help = apply_filters_deprecated(
 803              'contextual_help_list',
 804              array( self::$_old_compat_help, $this ),
 805              '3.3.0',
 806              'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()'
 807          );
 808  
 809          $old_help = self::$_old_compat_help[ $this->id ] ?? '';
 810  
 811          /**
 812           * Filters the legacy contextual help text.
 813           *
 814           * @since 2.7.0
 815           * @deprecated 3.3.0 Use {@see get_current_screen()} with
 816           *                   {@see WP_Screen::add_help_tab()} or
 817           *                   {@see WP_Screen::remove_help_tab()} instead.
 818           *
 819           * @param string    $old_help  Help text that appears on the screen.
 820           * @param string    $screen_id Screen ID.
 821           * @param WP_Screen $screen    Current WP_Screen instance.
 822           */
 823          $old_help = apply_filters_deprecated(
 824              'contextual_help',
 825              array( $old_help, $this->id, $this ),
 826              '3.3.0',
 827              'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()'
 828          );
 829  
 830          // Default help only if there is no old-style block of text and no new-style help tabs.
 831          if ( empty( $old_help ) && ! $this->get_help_tabs() ) {
 832  
 833              /**
 834               * Filters the default legacy contextual help text.
 835               *
 836               * @since 2.8.0
 837               * @deprecated 3.3.0 Use {@see get_current_screen()} with
 838               *                   {@see WP_Screen::add_help_tab()} or
 839               *                   {@see WP_Screen::remove_help_tab()} instead.
 840               *
 841               * @param string $old_help_default Default contextual help text.
 842               */
 843              $default_help = apply_filters_deprecated(
 844                  'default_contextual_help',
 845                  array( '' ),
 846                  '3.3.0',
 847                  'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()'
 848              );
 849              if ( $default_help ) {
 850                  $old_help = '<p>' . $default_help . '</p>';
 851              }
 852          }
 853  
 854          if ( $old_help ) {
 855              $this->add_help_tab(
 856                  array(
 857                      'id'      => 'old-contextual-help',
 858                      'title'   => __( 'Overview' ),
 859                      'content' => $old_help,
 860                  )
 861              );
 862          }
 863  
 864          $help_sidebar = $this->get_help_sidebar();
 865  
 866          $help_class = 'hidden';
 867          if ( ! $help_sidebar ) {
 868              $help_class .= ' no-sidebar';
 869          }
 870  
 871          // Time to render!
 872          ?>
 873          <div id="screen-meta" class="metabox-prefs">
 874  
 875              <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>" tabindex="-1" aria-label="<?php esc_attr_e( 'Contextual Help Tab' ); ?>">
 876                  <div id="contextual-help-back"></div>
 877                  <div id="contextual-help-columns">
 878                      <div class="contextual-help-tabs">
 879                          <ul>
 880                          <?php
 881                          $class = ' class="active"';
 882                          foreach ( $this->get_help_tabs() as $tab ) :
 883                              $link_id  = "tab-link-{$tab['id']}";
 884                              $panel_id = "tab-panel-{$tab['id']}";
 885                              ?>
 886  
 887                              <li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>>
 888                                  <a href="<?php echo esc_url( "#$panel_id" ); ?>" aria-controls="<?php echo esc_attr( $panel_id ); ?>">
 889                                      <?php echo esc_html( $tab['title'] ); ?>
 890                                  </a>
 891                              </li>
 892                              <?php
 893                              $class = '';
 894                          endforeach;
 895                          ?>
 896                          </ul>
 897                      </div>
 898  
 899                      <?php if ( $help_sidebar ) : ?>
 900                      <div class="contextual-help-sidebar">
 901                          <?php echo $help_sidebar; ?>
 902                      </div>
 903                      <?php endif; ?>
 904  
 905                      <div class="contextual-help-tabs-wrap">
 906                          <?php
 907                          $classes = 'help-tab-content active';
 908                          foreach ( $this->get_help_tabs() as $tab ) :
 909                              $panel_id = "tab-panel-{$tab['id']}";
 910                              ?>
 911  
 912                              <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>">
 913                                  <?php
 914                                  // Print tab content.
 915                                  echo $tab['content'];
 916  
 917                                  // If it exists, fire tab callback.
 918                                  if ( ! empty( $tab['callback'] ) ) {
 919                                      call_user_func_array( $tab['callback'], array( $this, $tab ) );
 920                                  }
 921                                  ?>
 922                              </div>
 923                              <?php
 924                              $classes = 'help-tab-content';
 925                          endforeach;
 926                          ?>
 927                      </div>
 928                  </div>
 929              </div>
 930          <?php
 931          // Setup layout columns.
 932  
 933          /**
 934           * Filters the array of screen layout columns.
 935           *
 936           * This hook provides back-compat for plugins using the back-compat
 937           * Filters instead of add_screen_option().
 938           *
 939           * @since 2.8.0
 940           *
 941           * @param array     $empty_columns Empty array.
 942           * @param string    $screen_id     Screen ID.
 943           * @param WP_Screen $screen        Current WP_Screen instance.
 944           */
 945          $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this );
 946  
 947          if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) ) {
 948              $this->add_option( 'layout_columns', array( 'max' => $columns[ $this->id ] ) );
 949          }
 950  
 951          if ( $this->get_option( 'layout_columns' ) ) {
 952              $this->columns = (int) get_user_option( "screen_layout_$this->id" );
 953  
 954              $layout_columns = (int) $this->get_option( 'layout_columns', 'default' );
 955              if ( ! $this->columns && $layout_columns ) {
 956                  $this->columns = $layout_columns;
 957              }
 958          }
 959          $GLOBALS['screen_layout_columns'] = $this->columns; // Set the global for back-compat.
 960  
 961          // Add screen options.
 962          if ( $this->show_screen_options() ) {
 963              $this->render_screen_options();
 964          }
 965          ?>
 966          </div>
 967          <?php
 968          if ( ! $this->get_help_tabs() && ! $this->show_screen_options() ) {
 969              return;
 970          }
 971          ?>
 972          <div id="screen-meta-links">
 973          <?php if ( $this->show_screen_options() ) : ?>
 974              <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
 975              <button type="button" id="show-settings-link" class="button button-compact show-settings" aria-controls="screen-options-wrap" aria-expanded="false"><?php _e( 'Screen Options' ); ?></button>
 976              </div>
 977              <?php
 978          endif;
 979          if ( $this->get_help_tabs() ) :
 980              ?>
 981              <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
 982              <button type="button" id="contextual-help-link" class="button button-compact show-settings" aria-controls="contextual-help-wrap" aria-expanded="false"><?php _e( 'Help' ); ?></button>
 983              </div>
 984          <?php endif; ?>
 985          </div>
 986          <?php
 987      }
 988  
 989      /**
 990       * @since 3.3.0
 991       *
 992       * @global array $wp_meta_boxes Global meta box state.
 993       *
 994       * @return bool Whether to show the Screen Options tab for the current screen.
 995       */
 996  	public function show_screen_options() {
 997          global $wp_meta_boxes;
 998  
 999          if ( is_bool( $this->_show_screen_options ) ) {
1000              return $this->_show_screen_options;
1001          }
1002  
1003          $columns = get_column_headers( $this );
1004  
1005          $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' );
1006  
1007          $this->_screen_settings = '';
1008  
1009          if ( 'post' === $this->base ) {
1010              $expand                 = '<fieldset class="editor-expand hidden"><legend>' . __( 'Additional settings' ) . '</legend><label for="editor-expand-toggle">';
1011              $expand                .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
1012              $expand                .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label></fieldset>';
1013              $this->_screen_settings = $expand;
1014          }
1015  
1016          /**
1017           * Filters the screen settings text displayed in the Screen Options tab.
1018           *
1019           * @since 3.0.0
1020           *
1021           * @param string    $screen_settings Screen settings.
1022           * @param WP_Screen $screen          WP_Screen object.
1023           */
1024          $this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this );
1025  
1026          if ( $this->_screen_settings || $this->_options ) {
1027              $show_screen = true;
1028          }
1029  
1030          /**
1031           * Filters whether to show the Screen Options tab.
1032           *
1033           * @since 3.2.0
1034           *
1035           * @param bool      $show_screen Whether to show Screen Options tab.
1036           *                               Default true.
1037           * @param WP_Screen $screen      Current WP_Screen instance.
1038           */
1039          $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
1040          return $this->_show_screen_options;
1041      }
1042  
1043      /**
1044       * Renders the screen options tab.
1045       *
1046       * @since 3.3.0
1047       *
1048       * @param array $options {
1049       *     Options for the tab.
1050       *
1051       *     @type bool $wrap Whether the screen-options-wrap div will be included. Defaults to true.
1052       * }
1053       */
1054  	public function render_screen_options( $options = array() ) {
1055          $options = wp_parse_args(
1056              $options,
1057              array(
1058                  'wrap' => true,
1059              )
1060          );
1061  
1062          $wrapper_start = '';
1063          $wrapper_end   = '';
1064          $form_start    = '';
1065          $form_end      = '';
1066  
1067          // Output optional wrapper.
1068          if ( $options['wrap'] ) {
1069              $wrapper_start = '<div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="' . esc_attr__( 'Screen Options Tab' ) . '">';
1070              $wrapper_end   = '</div>';
1071          }
1072  
1073          // Don't output the form and nonce for the widgets accessibility mode links.
1074          if ( 'widgets' !== $this->base ) {
1075              $form_start = "\n<form id='adv-settings' method='post'>\n";
1076              $form_end   = "\n" . wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false, false ) . "\n</form>\n";
1077          }
1078  
1079          echo $wrapper_start . $form_start;
1080  
1081          $this->render_meta_boxes_preferences();
1082          $this->render_list_table_columns_preferences();
1083          $this->render_screen_layout();
1084          $this->render_per_page_options();
1085          $this->render_view_mode();
1086          echo $this->_screen_settings;
1087  
1088          /**
1089           * Filters whether to show the Screen Options submit button.
1090           *
1091           * @since 4.4.0
1092           *
1093           * @param bool      $show_button Whether to show Screen Options submit button.
1094           *                               Default false.
1095           * @param WP_Screen $screen      Current WP_Screen instance.
1096           */
1097          $show_button = apply_filters( 'screen_options_show_submit', false, $this );
1098  
1099          if ( $show_button ) {
1100              submit_button( __( 'Apply' ), 'primary', 'screen-options-apply', true );
1101          }
1102  
1103          echo $form_end . $wrapper_end;
1104      }
1105  
1106      /**
1107       * Renders the meta boxes preferences.
1108       *
1109       * @since 4.4.0
1110       *
1111       * @global array $wp_meta_boxes Global meta box state.
1112       */
1113  	public function render_meta_boxes_preferences() {
1114          global $wp_meta_boxes;
1115  
1116          if ( ! isset( $wp_meta_boxes[ $this->id ] ) ) {
1117              return;
1118          }
1119          ?>
1120          <fieldset class="metabox-prefs">
1121          <legend><?php _e( 'Screen elements' ); ?></legend>
1122          <p>
1123              <?php _e( 'Some screen elements can be shown or hidden by using the checkboxes.' ); ?>
1124              <?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.' ); ?>
1125          </p>
1126          <div class="metabox-prefs-container">
1127          <?php
1128  
1129          meta_box_prefs( $this );
1130  
1131          if ( 'dashboard' === $this->id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) {
1132              if ( isset( $_GET['welcome'] ) ) {
1133                  $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
1134                  update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
1135              } else {
1136                  $welcome_checked = (int) get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
1137                  if ( 2 === $welcome_checked && wp_get_current_user()->user_email !== get_option( 'admin_email' ) ) {
1138                      $welcome_checked = false;
1139                  }
1140              }
1141              echo '<label for="wp_welcome_panel-hide">';
1142              echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />';
1143              echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n";
1144          }
1145          ?>
1146          </div>
1147          </fieldset>
1148          <?php
1149      }
1150  
1151      /**
1152       * Renders the list table columns preferences.
1153       *
1154       * @since 4.4.0
1155       */
1156  	public function render_list_table_columns_preferences() {
1157  
1158          $columns = get_column_headers( $this );
1159          $hidden  = get_hidden_columns( $this );
1160  
1161          if ( ! $columns ) {
1162              return;
1163          }
1164  
1165          $legend = ! empty( $columns['_title'] ) ? $columns['_title'] : __( 'Columns' );
1166          ?>
1167          <fieldset class="metabox-prefs">
1168          <legend><?php echo $legend; ?></legend>
1169          <?php
1170          $special = array( '_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname' );
1171  
1172          foreach ( $columns as $column => $title ) {
1173              // Can't hide these for they are special.
1174              if ( in_array( $column, $special, true ) ) {
1175                  continue;
1176              }
1177  
1178              if ( empty( $title ) ) {
1179                  continue;
1180              }
1181  
1182              /*
1183               * The Comments column uses HTML in the display name with some screen
1184               * reader text. Make sure to strip tags from the Comments column
1185               * title and any other custom column title plugins might add.
1186               */
1187              $title = wp_strip_all_tags( $title );
1188  
1189              $id = "$column-hide";
1190              echo '<label>';
1191              echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( ! in_array( $column, $hidden, true ), true, false ) . ' />';
1192              echo "$title</label>\n";
1193          }
1194          ?>
1195          </fieldset>
1196          <?php
1197      }
1198  
1199      /**
1200       * Renders the option for number of columns on the page.
1201       *
1202       * @since 3.3.0
1203       */
1204  	public function render_screen_layout() {
1205          if ( ! $this->get_option( 'layout_columns' ) ) {
1206              return;
1207          }
1208  
1209          $screen_layout_columns = $this->get_columns();
1210          $num                   = $this->get_option( 'layout_columns', 'max' );
1211  
1212          ?>
1213          <fieldset class='columns-prefs'>
1214          <legend class="screen-layout"><?php _e( 'Layout' ); ?></legend>
1215          <?php for ( $i = 1; $i <= $num; ++$i ) : ?>
1216              <label class="columns-prefs-<?php echo $i; ?>">
1217              <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>' <?php checked( $screen_layout_columns, $i ); ?> />
1218              <?php
1219                  printf(
1220                      /* translators: %s: Number of columns on the page. */
1221                      _n( '%s column', '%s columns', $i ),
1222                      number_format_i18n( $i )
1223                  );
1224              ?>
1225              </label>
1226          <?php endfor; ?>
1227          </fieldset>
1228          <?php
1229      }
1230  
1231      /**
1232       * Renders the items per page option.
1233       *
1234       * @since 3.3.0
1235       */
1236  	public function render_per_page_options() {
1237          if ( null === $this->get_option( 'per_page' ) ) {
1238              return;
1239          }
1240  
1241          $per_page_label = $this->get_option( 'per_page', 'label' );
1242          if ( null === $per_page_label ) {
1243              $per_page_label = __( 'Number of items per page:' );
1244          }
1245  
1246          $option = $this->get_option( 'per_page', 'option' );
1247          if ( ! $option ) {
1248              $option = str_replace( '-', '_', "{$this->id}_per_page" );
1249          }
1250  
1251          $per_page = (int) get_user_option( $option );
1252          if ( empty( $per_page ) || $per_page < 1 ) {
1253              $per_page = $this->get_option( 'per_page', 'default' );
1254              if ( ! $per_page ) {
1255                  $per_page = 20;
1256              }
1257          }
1258  
1259          if ( 'edit_comments_per_page' === $option ) {
1260              $comment_status = $_REQUEST['comment_status'] ?? 'all';
1261  
1262              /** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
1263              $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
1264          } elseif ( 'categories_per_page' === $option ) {
1265              /** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */
1266              $per_page = apply_filters( 'edit_categories_per_page', $per_page );
1267          } else {
1268              /** This filter is documented in wp-admin/includes/class-wp-list-table.php */
1269              $per_page = apply_filters( "{$option}", $per_page );
1270          }
1271  
1272          // Back compat.
1273          if ( isset( $this->post_type ) ) {
1274              /** This filter is documented in wp-admin/includes/post.php */
1275              $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
1276          }
1277  
1278          // This needs a submit button.
1279          add_filter( 'screen_options_show_submit', '__return_true' );
1280  
1281          ?>
1282          <fieldset class="screen-options">
1283          <legend><?php _e( 'Pagination' ); ?></legend>
1284              <?php if ( $per_page_label ) : ?>
1285                  <label for="<?php echo esc_attr( $option ); ?>"><?php echo $per_page_label; ?></label>
1286                  <input type="number" step="1" min="1" max="999" class="screen-per-page small-text" name="wp_screen_options[value]"
1287                      id="<?php echo esc_attr( $option ); ?>"
1288                      value="<?php echo esc_attr( $per_page ); ?>" />
1289              <?php endif; ?>
1290                  <input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" />
1291          </fieldset>
1292          <?php
1293      }
1294  
1295      /**
1296       * Renders the list table view mode preferences.
1297       *
1298       * @since 4.4.0
1299       *
1300       * @global string $mode List table view mode.
1301       */
1302  	public function render_view_mode() {
1303          global $mode;
1304  
1305          $screen = get_current_screen();
1306  
1307          // Currently only enabled for posts and comments lists.
1308          if ( 'edit' !== $screen->base && 'edit-comments' !== $screen->base ) {
1309              return;
1310          }
1311  
1312          $view_mode_post_types = get_post_types( array( 'show_ui' => true ) );
1313  
1314          /**
1315           * Filters the post types that have different view mode options.
1316           *
1317           * @since 4.4.0
1318           *
1319           * @param string[] $view_mode_post_types Array of post types that can change view modes.
1320           *                                       Default post types with show_ui on.
1321           */
1322          $view_mode_post_types = apply_filters( 'view_mode_post_types', $view_mode_post_types );
1323  
1324          if ( 'edit' === $screen->base && ! in_array( $this->post_type, $view_mode_post_types, true ) ) {
1325              return;
1326          }
1327  
1328          if ( ! isset( $mode ) ) {
1329              $mode = get_user_setting( 'posts_list_mode', 'list' );
1330          }
1331  
1332          // This needs a submit button.
1333          add_filter( 'screen_options_show_submit', '__return_true' );
1334          ?>
1335          <fieldset class="metabox-prefs view-mode">
1336              <legend><?php _e( 'View mode' ); ?></legend>
1337              <label for="list-view-mode">
1338                  <input id="list-view-mode" type="radio" name="mode" value="list" <?php checked( 'list', $mode ); ?> />
1339                  <?php _e( 'Compact view' ); ?>
1340              </label>
1341              <label for="excerpt-view-mode">
1342                  <input id="excerpt-view-mode" type="radio" name="mode" value="excerpt" <?php checked( 'excerpt', $mode ); ?> />
1343                  <?php _e( 'Extended view' ); ?>
1344              </label>
1345          </fieldset>
1346          <?php
1347      }
1348  
1349      /**
1350       * Renders screen reader text.
1351       *
1352       * @since 4.4.0
1353       *
1354       * @param string $key The screen reader text array named key.
1355       * @param string $tag Optional. The HTML tag to wrap the screen reader text. Default h2.
1356       */
1357  	public function render_screen_reader_content( $key = '', $tag = 'h2' ) {
1358  
1359          if ( ! isset( $this->_screen_reader_content[ $key ] ) ) {
1360              return;
1361          }
1362          echo "<$tag class='screen-reader-text'>" . $this->_screen_reader_content[ $key ] . "</$tag>";
1363      }
1364  }


Generated : Fri Jun 12 08:20:09 2026 Cross-referenced by PHPXref