[ Index ]

PHP Cross Reference of WordPress

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


Generated: Mon Nov 18 01:00:04 2019 Cross-referenced by PHPXref 0.7.1