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