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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7