[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7