Completed
Push — master ( a7cd2a...eabd6c )
by Stephen
38:42
created

WP_Screen::render_meta_boxes_preferences()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 30
rs 4.909
cc 9
eloc 23
nc 6
nop 0
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
	 * @access public
22
	 */
23
	public $action;
24
25
	/**
26
	 * The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped.
27
	 * For example, for an $id of 'edit-post' the base is 'edit'.
28
	 *
29
	 * @since 3.3.0
30
	 * @var string
31
	 * @access public
32
	 */
33
	public $base;
34
35
	/**
36
	 * The number of columns to display. Access with get_columns().
37
	 *
38
	 * @since 3.4.0
39
	 * @var int
40
	 * @access private
41
	 */
42
	private $columns = 0;
43
44
	/**
45
	 * The unique ID of the screen.
46
	 *
47
	 * @since 3.3.0
48
	 * @var string
49
	 * @access public
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
	 * @access protected
59
	 */
60
	protected $in_admin;
61
62
	/**
63
	 * Whether the screen is in the network admin.
64
	 *
65
	 * Deprecated. Use in_admin() instead.
66
	 *
67
	 * @since 3.3.0
68
	 * @deprecated 3.5.0
69
	 * @var bool
70
	 * @access public
71
	 */
72
	public $is_network;
73
74
	/**
75
	 * Whether the screen is in the user admin.
76
	 *
77
	 * Deprecated. Use in_admin() instead.
78
	 *
79
	 * @since 3.3.0
80
	 * @deprecated 3.5.0
81
	 * @var bool
82
	 * @access public
83
	 */
84
	public $is_user;
85
86
	/**
87
	 * The base menu parent.
88
	 * This is derived from $parent_file by removing the query string and any .php extension.
89
	 * $parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a $parent_base of 'edit'.
90
	 *
91
	 * @since 3.3.0
92
	 * @var string
93
	 * @access public
94
	 */
95
	public $parent_base;
96
97
	/**
98
	 * The parent_file for the screen per the admin menu system.
99
	 * Some $parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'.
100
	 *
101
	 * @since 3.3.0
102
	 * @var string
103
	 * @access public
104
	 */
105
	public $parent_file;
106
107
	/**
108
	 * The post type associated with the screen, if any.
109
	 * The 'edit.php?post_type=page' screen has a post type of 'page'.
110
	 * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'.
111
	 *
112
	 * @since 3.3.0
113
	 * @var string
114
	 * @access public
115
	 */
116
	public $post_type;
117
118
	/**
119
	 * The taxonomy associated with the screen, if any.
120
	 * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'.
121
	 * @since 3.3.0
122
	 * @var string
123
	 * @access public
124
	 */
125
	public $taxonomy;
126
127
	/**
128
	 * The help tab data associated with the screen, if any.
129
	 *
130
	 * @since 3.3.0
131
	 * @var array
132
	 * @access private
133
	 */
134
	private $_help_tabs = array();
135
136
	/**
137
	 * The help sidebar data associated with screen, if any.
138
	 *
139
	 * @since 3.3.0
140
	 * @var string
141
	 * @access private
142
	 */
143
	private $_help_sidebar = '';
144
145
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
146
	 * The accessible hidden headings and text associated with the screen, if any.
147
	 *
148
	 * @since 4.4.0
149
	 * @access private
150
	 * @var array
151
	 */
152
	private $_screen_reader_content = array();
153
154
	/**
155
	 * Stores old string-based help.
156
	 *
157
	 * @static
158
	 * @access private
159
	 *
160
	 * @var array
161
	 */
162
	private static $_old_compat_help = array();
163
164
	/**
165
	 * The screen options associated with screen, if any.
166
	 *
167
	 * @since 3.3.0
168
	 * @var array
169
	 * @access private
170
	 */
171
	private $_options = array();
172
173
	/**
174
	 * The screen object registry.
175
	 *
176
	 * @since 3.3.0
177
	 *
178
	 * @static
179
	 * @access private
180
	 *
181
	 * @var array
182
	 */
183
	private static $_registry = array();
184
185
	/**
186
	 * Stores the result of the public show_screen_options function.
187
	 *
188
	 * @since 3.3.0
189
	 * @var bool
190
	 * @access private
191
	 */
192
	private $_show_screen_options;
193
194
	/**
195
	 * Stores the 'screen_settings' section of screen options.
196
	 *
197
	 * @since 3.3.0
198
	 * @var string
199
	 * @access private
200
	 */
201
	private $_screen_settings;
202
203
	/**
204
	 * Fetches a screen object.
205
	 *
206
	 * @since 3.3.0
207
	 * @access public
208
	 *
209
	 * @static
210
	 *
211
	 * @global string $hook_suffix
212
	 *
213
	 * @param string|WP_Screen $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
214
	 * 	                                  Defaults to the current $hook_suffix global.
215
	 * @return WP_Screen Screen object.
216
	 */
217
	public static function get( $hook_name = '' ) {
218
		if ( $hook_name instanceof WP_Screen ) {
219
			return $hook_name;
220
		}
221
222
		$post_type = $taxonomy = null;
223
		$in_admin = false;
224
		$action = '';
225
226
		if ( $hook_name )
227
			$id = $hook_name;
228
		else
229
			$id = $GLOBALS['hook_suffix'];
230
231
		// For those pesky meta boxes.
232
		if ( $hook_name && post_type_exists( $hook_name ) ) {
233
			$post_type = $id;
234
			$id = 'post'; // changes later. ends up being $base.
235
		} else {
236
			if ( '.php' == substr( $id, -4 ) )
237
				$id = substr( $id, 0, -4 );
238
239
			if ( 'post-new' == $id || 'link-add' == $id || 'media-new' == $id || 'user-new' == $id ) {
240
				$id = substr( $id, 0, -4 );
241
				$action = 'add';
242
			}
243
		}
244
245
		if ( ! $post_type && $hook_name ) {
246
			if ( '-network' == substr( $id, -8 ) ) {
247
				$id = substr( $id, 0, -8 );
248
				$in_admin = 'network';
249
			} elseif ( '-user' == substr( $id, -5 ) ) {
250
				$id = substr( $id, 0, -5 );
251
				$in_admin = 'user';
252
			}
253
254
			$id = sanitize_key( $id );
255
			if ( 'edit-comments' != $id && 'edit-tags' != $id && 'edit-' == substr( $id, 0, 5 ) ) {
256
				$maybe = substr( $id, 5 );
257
				if ( taxonomy_exists( $maybe ) ) {
258
					$id = 'edit-tags';
259
					$taxonomy = $maybe;
260
				} elseif ( post_type_exists( $maybe ) ) {
261
					$id = 'edit';
262
					$post_type = $maybe;
263
				}
264
			}
265
266
			if ( ! $in_admin )
0 ignored issues
show
Bug Best Practice introduced by
The expression $in_admin of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
267
				$in_admin = 'site';
268
		} else {
269
			if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
270
				$in_admin = 'network';
271
			elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
272
				$in_admin = 'user';
273
			else
274
				$in_admin = 'site';
275
		}
276
277
		if ( 'index' == $id )
278
			$id = 'dashboard';
279
		elseif ( 'front' == $id )
280
			$in_admin = false;
281
282
		$base = $id;
283
284
		// If this is the current screen, see if we can be more accurate for post types and taxonomies.
285
		if ( ! $hook_name ) {
286 View Code Duplication
			if ( isset( $_REQUEST['post_type'] ) )
287
				$post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false;
288 View Code Duplication
			if ( isset( $_REQUEST['taxonomy'] ) )
289
				$taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false;
290
291
			switch ( $base ) {
292
				case 'post' :
293
					if ( isset( $_GET['post'] ) )
294
						$post_id = (int) $_GET['post'];
295
					elseif ( isset( $_POST['post_ID'] ) )
296
						$post_id = (int) $_POST['post_ID'];
297
					else
298
						$post_id = 0;
299
300
					if ( $post_id ) {
301
						$post = get_post( $post_id );
302
						if ( $post )
303
							$post_type = $post->post_type;
304
					}
305
					break;
306
				case 'edit-tags' :
307
				case 'term' :
308
					if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) )
309
						$post_type = 'post';
310
					break;
311
			}
312
		}
313
314
		switch ( $base ) {
315
			case 'post' :
316
				if ( null === $post_type )
317
					$post_type = 'post';
318
				$id = $post_type;
319
				break;
320
			case 'edit' :
321
				if ( null === $post_type )
322
					$post_type = 'post';
323
				$id .= '-' . $post_type;
324
				break;
325
			case 'edit-tags' :
326
			case 'term' :
327
				if ( null === $taxonomy )
328
					$taxonomy = 'post_tag';
329
				// The edit-tags ID does not contain the post type. Look for it in the request.
330 View Code Duplication
				if ( null === $post_type ) {
331
					$post_type = 'post';
332
					if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) )
333
						$post_type = $_REQUEST['post_type'];
334
				}
335
336
				$id = 'edit-' . $taxonomy;
337
				break;
338
		}
339
340
		if ( 'network' == $in_admin ) {
341
			$id   .= '-network';
342
			$base .= '-network';
343
		} elseif ( 'user' == $in_admin ) {
344
			$id   .= '-user';
345
			$base .= '-user';
346
		}
347
348
		if ( isset( self::$_registry[ $id ] ) ) {
349
			$screen = self::$_registry[ $id ];
350
			if ( $screen === get_current_screen() )
351
				return $screen;
352
		} else {
353
			$screen = new WP_Screen();
354
			$screen->id     = $id;
355
		}
356
357
		$screen->base       = $base;
358
		$screen->action     = $action;
359
		$screen->post_type  = (string) $post_type;
360
		$screen->taxonomy   = (string) $taxonomy;
361
		$screen->is_user    = ( 'user' == $in_admin );
362
		$screen->is_network = ( 'network' == $in_admin );
363
		$screen->in_admin   = $in_admin;
364
365
		self::$_registry[ $id ] = $screen;
366
367
		return $screen;
368
	}
369
370
	/**
371
	 * Makes the screen object the current screen.
372
	 *
373
	 * @see set_current_screen()
374
	 * @since 3.3.0
375
	 *
376
	 * @global WP_Screen $current_screen
377
	 * @global string    $taxnow
378
	 * @global string    $typenow
379
	 */
380
	public function set_current_screen() {
381
		global $current_screen, $taxnow, $typenow;
382
		$current_screen = $this;
383
		$taxnow = $this->taxonomy;
384
		$typenow = $this->post_type;
385
386
		/**
387
		 * Fires after the current screen has been set.
388
		 *
389
		 * @since 3.0.0
390
		 *
391
		 * @param WP_Screen $current_screen Current WP_Screen object.
392
		 */
393
		do_action( 'current_screen', $current_screen );
394
	}
395
396
	/**
397
	 * Constructor
398
	 *
399
	 * @since 3.3.0
400
	 * @access private
401
	 */
402
	private function __construct() {}
403
404
	/**
405
	 * Indicates whether the screen is in a particular admin
406
	 *
407
	 * @since 3.5.0
408
	 *
409
	 * @param string $admin The admin to check against (network | user | site).
410
	 *                      If empty any of the three admins will result in true.
411
	 * @return bool True if the screen is in the indicated admin, false otherwise.
412
	 */
413
	public function in_admin( $admin = null ) {
414
		if ( empty( $admin ) )
415
			return (bool) $this->in_admin;
416
417
		return ( $admin == $this->in_admin );
418
	}
419
420
	/**
421
	 * Sets the old string-based contextual help for the screen.
422
	 *
423
	 * For backwards compatibility.
424
	 *
425
	 * @since 3.3.0
426
	 *
427
	 * @static
428
	 *
429
	 * @param WP_Screen $screen A screen object.
430
	 * @param string $help Help text.
431
	 */
432
	public static function add_old_compat_help( $screen, $help ) {
433
		self::$_old_compat_help[ $screen->id ] = $help;
434
	}
435
436
	/**
437
	 * Set the parent information for the screen.
438
	 * This is called in admin-header.php after the menu parent for the screen has been determined.
439
	 *
440
	 * @since 3.3.0
441
	 *
442
	 * @param string $parent_file The parent file of the screen. Typically the $parent_file global.
443
	 */
444
	public function set_parentage( $parent_file ) {
445
		$this->parent_file = $parent_file;
446
		list( $this->parent_base ) = explode( '?', $parent_file );
447
		$this->parent_base = str_replace( '.php', '', $this->parent_base );
448
	}
449
450
	/**
451
	 * Adds an option for the screen.
452
	 * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options.
453
	 *
454
	 * @since 3.3.0
455
	 *
456
	 * @param string $option Option ID
457
	 * @param mixed $args Option-dependent arguments.
458
	 */
459
	public function add_option( $option, $args = array() ) {
460
		$this->_options[ $option ] = $args;
461
	}
462
463
	/**
464
	 * Remove an option from the screen.
465
	 *
466
	 * @since 3.8.0
467
	 *
468
	 * @param string $option Option ID.
469
	 */
470
	public function remove_option( $option ) {
471
		unset( $this->_options[ $option ] );
472
	}
473
474
	/**
475
	 * Remove all options from the screen.
476
	 *
477
	 * @since 3.8.0
478
	 */
479
	public function remove_options() {
480
		$this->_options = array();
481
	}
482
483
	/**
484
	 * Get the options registered for the screen.
485
	 *
486
	 * @since 3.8.0
487
	 *
488
	 * @return array Options with arguments.
489
	 */
490
	public function get_options() {
491
		return $this->_options;
492
	}
493
494
	/**
495
	 * Gets the arguments for an option for the screen.
496
	 *
497
	 * @since 3.3.0
498
	 *
499
	 * @param string $option Option name.
500
	 * @param string $key    Optional. Specific array key for when the option is an array.
501
	 *                       Default false.
502
	 * @return string The option value if set, null otherwise.
503
	 */
504
	public function get_option( $option, $key = false ) {
505
		if ( ! isset( $this->_options[ $option ] ) )
506
			return null;
507
		if ( $key ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
508
			if ( isset( $this->_options[ $option ][ $key ] ) )
509
				return $this->_options[ $option ][ $key ];
510
			return null;
511
		}
512
		return $this->_options[ $option ];
513
	}
514
515
	/**
516
	 * Gets the help tabs registered for the screen.
517
	 *
518
	 * @since 3.4.0
519
	 * @since 4.4.0 Help tabs are ordered by their priority.
520
	 *
521
	 * @return array Help tabs with arguments.
522
	 */
523
	public function get_help_tabs() {
524
		$help_tabs = $this->_help_tabs;
525
526
		$priorities = array();
527
		foreach ( $help_tabs as $help_tab ) {
528
			if ( isset( $priorities[ $help_tab['priority'] ] ) ) {
529
				$priorities[ $help_tab['priority'] ][] = $help_tab;
530
			} else {
531
				$priorities[ $help_tab['priority'] ] = array( $help_tab );
532
			}
533
		}
534
535
		ksort( $priorities );
536
537
		$sorted = array();
538
		foreach ( $priorities as $list ) {
539
			foreach ( $list as $tab ) {
540
				$sorted[ $tab['id'] ] = $tab;
541
			}
542
		}
543
544
		return $sorted;
545
	}
546
547
	/**
548
	 * Gets the arguments for a help tab.
549
	 *
550
	 * @since 3.4.0
551
	 *
552
	 * @param string $id Help Tab ID.
553
	 * @return array Help tab arguments.
554
	 */
555
	public function get_help_tab( $id ) {
556
		if ( ! isset( $this->_help_tabs[ $id ] ) )
557
			return null;
558
		return $this->_help_tabs[ $id ];
559
	}
560
561
	/**
562
	 * Add a help tab to the contextual help for the screen.
563
	 * Call this on the load-$pagenow hook for the relevant screen.
564
	 *
565
	 * @since 3.3.0
566
	 * @since 4.4.0 The `$priority` argument was added.
567
	 *
568
	 * @param array $args {
569
	 *     Array of arguments used to display the help tab.
570
	 *
571
	 *     @type string $title    Title for the tab. Default false.
572
	 *     @type string $id       Tab ID. Must be HTML-safe. Default false.
573
	 *     @type string $content  Optional. Help tab content in plain text or HTML. Default empty string.
574
	 *     @type string $callback Optional. A callback to generate the tab content. Default false.
575
	 *     @type int    $priority Optional. The priority of the tab, used for ordering. Default 10.
576
	 * }
577
	 */
578
	public function add_help_tab( $args ) {
579
		$defaults = array(
580
			'title'    => false,
581
			'id'       => false,
582
			'content'  => '',
583
			'callback' => false,
584
			'priority' => 10,
585
		);
586
		$args = wp_parse_args( $args, $defaults );
587
588
		$args['id'] = sanitize_html_class( $args['id'] );
589
590
		// Ensure we have an ID and title.
591
		if ( ! $args['id'] || ! $args['title'] )
592
			return;
593
594
		// Allows for overriding an existing tab with that ID.
595
		$this->_help_tabs[ $args['id'] ] = $args;
596
	}
597
598
	/**
599
	 * Removes a help tab from the contextual help for the screen.
600
	 *
601
	 * @since 3.3.0
602
	 *
603
	 * @param string $id The help tab ID.
604
	 */
605
	public function remove_help_tab( $id ) {
606
		unset( $this->_help_tabs[ $id ] );
607
	}
608
609
	/**
610
	 * Removes all help tabs from the contextual help for the screen.
611
	 *
612
	 * @since 3.3.0
613
	 */
614
	public function remove_help_tabs() {
615
		$this->_help_tabs = array();
616
	}
617
618
	/**
619
	 * Gets the content from a contextual help sidebar.
620
	 *
621
	 * @since 3.4.0
622
	 *
623
	 * @return string Contents of the help sidebar.
624
	 */
625
	public function get_help_sidebar() {
626
		return $this->_help_sidebar;
627
	}
628
629
	/**
630
	 * Add a sidebar to the contextual help for the screen.
631
	 * 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.
632
	 *
633
	 * @since 3.3.0
634
	 *
635
	 * @param string $content Sidebar content in plain text or HTML.
636
	 */
637
	public function set_help_sidebar( $content ) {
638
		$this->_help_sidebar = $content;
639
	}
640
641
	/**
642
	 * Gets the number of layout columns the user has selected.
643
	 *
644
	 * The layout_columns option controls the max number and default number of
645
	 * columns. This method returns the number of columns within that range selected
646
	 * by the user via Screen Options. If no selection has been made, the default
647
	 * provisioned in layout_columns is returned. If the screen does not support
648
	 * selecting the number of layout columns, 0 is returned.
649
	 *
650
	 * @since 3.4.0
651
	 *
652
	 * @return int Number of columns to display.
653
	 */
654
	public function get_columns() {
655
		return $this->columns;
656
	}
657
658
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
659
	 * Get the accessible hidden headings and text used in the screen.
660
	 *
661
	 * @since 4.4.0
662
	 *
663
	 * @see set_screen_reader_content() For more information on the array format.
664
	 *
665
	 * @return array An associative array of screen reader text strings.
666
	 */
667
	public function get_screen_reader_content() {
668
		return $this->_screen_reader_content;
669
	}
670
671
	/**
672
	 * Get a screen reader text string.
673
	 *
674
	 * @since 4.4.0
675
	 *
676
	 * @param string $key Screen reader text array named key.
677
	 * @return string Screen reader text string.
678
	 */
679
	public function get_screen_reader_text( $key ) {
680
		if ( ! isset( $this->_screen_reader_content[ $key ] ) ) {
681
			return null;
682
		}
683
		return $this->_screen_reader_content[ $key ];
684
	}
685
686
	/**
687
	 * Add accessible hidden headings and text for the screen.
688
	 *
689
	 * @since 4.4.0
690
	 *
691
	 * @param array $content {
692
	 *     An associative array of screen reader text strings.
693
	 *
694
	 *     @type string $heading_views      Screen reader text for the filter links heading.
695
	 *                                      Default 'Filter items list'.
696
	 *     @type string $heading_pagination Screen reader text for the pagination heading.
697
	 *                                      Default 'Items list navigation'.
698
	 *     @type string $heading_list       Screen reader text for the items list heading.
699
	 *                                      Default 'Items list'.
700
	 * }
701
	 */
702
	public function set_screen_reader_content( $content = array() ) {
703
		$defaults = array(
704
			'heading_views'      => __( 'Filter items list' ),
705
			'heading_pagination' => __( 'Items list navigation' ),
706
			'heading_list'       => __( 'Items list' ),
707
		);
708
		$content = wp_parse_args( $content, $defaults );
709
710
		$this->_screen_reader_content = $content;
711
	}
712
713
	/**
714
	 * Remove all the accessible hidden headings and text for the screen.
715
	 *
716
	 * @since 4.4.0
717
	 */
718
	public function remove_screen_reader_content() {
719
		$this->_screen_reader_content = array();
720
	}
721
722
	/**
723
	 * Render the screen's help section.
724
	 *
725
	 * This will trigger the deprecated filters for backwards compatibility.
726
	 *
727
	 * @since 3.3.0
728
	 *
729
	 * @global string $screen_layout_columns
730
	 */
731
	public function render_screen_meta() {
732
733
		/**
734
		 * Filter the legacy contextual help list.
735
		 *
736
		 * @since 2.7.0
737
		 * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or
738
		 *                   get_current_screen()->remove_help_tab() instead.
739
		 *
740
		 * @param array     $old_compat_help Old contextual help.
741
		 * @param WP_Screen $this            Current WP_Screen instance.
742
		 */
743
		self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this );
0 ignored issues
show
Documentation Bug introduced by
It seems like apply_filters('contextua...old_compat_help, $this) of type * is incompatible with the declared type array of property $_old_compat_help.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
744
745
		$old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : '';
746
747
		/**
748
		 * Filter the legacy contextual help text.
749
		 *
750
		 * @since 2.7.0
751
		 * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or
752
		 *                   get_current_screen()->remove_help_tab() instead.
753
		 *
754
		 * @param string    $old_help  Help text that appears on the screen.
755
		 * @param string    $screen_id Screen ID.
756
		 * @param WP_Screen $this      Current WP_Screen instance.
757
		 *
758
		 */
759
		$old_help = apply_filters( 'contextual_help', $old_help, $this->id, $this );
760
761
		// Default help only if there is no old-style block of text and no new-style help tabs.
762
		if ( empty( $old_help ) && ! $this->get_help_tabs() ) {
763
764
			/**
765
			 * Filter the default legacy contextual help text.
766
			 *
767
			 * @since 2.8.0
768
			 * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or
769
			 *                   get_current_screen()->remove_help_tab() instead.
770
			 *
771
			 * @param string $old_help_default Default contextual help text.
772
			 */
773
			$default_help = apply_filters( 'default_contextual_help', '' );
774
			if ( $default_help )
775
				$old_help = '<p>' . $default_help . '</p>';
776
		}
777
778
		if ( $old_help ) {
779
			$this->add_help_tab( array(
780
				'id'      => 'old-contextual-help',
781
				'title'   => __('Overview'),
782
				'content' => $old_help,
783
			) );
784
		}
785
786
		$help_sidebar = $this->get_help_sidebar();
787
788
		$help_class = 'hidden';
789
		if ( ! $help_sidebar )
790
			$help_class .= ' no-sidebar';
791
792
		// Time to render!
793
		?>
794
		<div id="screen-meta" class="metabox-prefs">
795
796
			<div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>" tabindex="-1" aria-label="<?php esc_attr_e('Contextual Help Tab'); ?>">
797
				<div id="contextual-help-back"></div>
798
				<div id="contextual-help-columns">
799
					<div class="contextual-help-tabs">
800
						<ul>
801
						<?php
802
						$class = ' class="active"';
803
						foreach ( $this->get_help_tabs() as $tab ) :
804
							$link_id  = "tab-link-{$tab['id']}";
805
							$panel_id = "tab-panel-{$tab['id']}";
806
							?>
807
808
							<li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>>
809
								<a href="<?php echo esc_url( "#$panel_id" ); ?>" aria-controls="<?php echo esc_attr( $panel_id ); ?>">
810
									<?php echo esc_html( $tab['title'] ); ?>
811
								</a>
812
							</li>
813
						<?php
814
							$class = '';
815
						endforeach;
816
						?>
817
						</ul>
818
					</div>
819
820
					<?php if ( $help_sidebar ) : ?>
821
					<div class="contextual-help-sidebar">
822
						<?php echo $help_sidebar; ?>
823
					</div>
824
					<?php endif; ?>
825
826
					<div class="contextual-help-tabs-wrap">
827
						<?php
828
						$classes = 'help-tab-content active';
829
						foreach ( $this->get_help_tabs() as $tab ):
830
							$panel_id = "tab-panel-{$tab['id']}";
831
							?>
832
833
							<div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>">
834
								<?php
835
								// Print tab content.
836
								echo $tab['content'];
837
838
								// If it exists, fire tab callback.
839
								if ( ! empty( $tab['callback'] ) )
840
									call_user_func_array( $tab['callback'], array( $this, $tab ) );
841
								?>
842
							</div>
843
						<?php
844
							$classes = 'help-tab-content';
845
						endforeach;
846
						?>
847
					</div>
848
				</div>
849
			</div>
850
		<?php
851
		// Setup layout columns
852
853
		/**
854
		 * Filter the array of screen layout columns.
855
		 *
856
		 * This hook provides back-compat for plugins using the back-compat
857
		 * filter instead of add_screen_option().
858
		 *
859
		 * @since 2.8.0
860
		 *
861
		 * @param array     $empty_columns Empty array.
862
		 * @param string    $screen_id     Screen ID.
863
		 * @param WP_Screen $this          Current WP_Screen instance.
864
		 */
865
		$columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this );
866
867
		if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) )
868
			$this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) );
869
870
		if ( $this->get_option( 'layout_columns' ) ) {
871
			$this->columns = (int) get_user_option("screen_layout_$this->id");
872
873
			if ( ! $this->columns && $this->get_option( 'layout_columns', 'default' ) )
874
				$this->columns = $this->get_option( 'layout_columns', 'default' );
0 ignored issues
show
Documentation Bug introduced by
The property $columns was declared of type integer, but $this->get_option('layout_columns', 'default') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
875
		}
876
		$GLOBALS[ 'screen_layout_columns' ] = $this->columns; // Set the global for back-compat.
877
878
		// Add screen options
879
		if ( $this->show_screen_options() )
880
			$this->render_screen_options();
881
		?>
882
		</div>
883
		<?php
884
		if ( ! $this->get_help_tabs() && ! $this->show_screen_options() )
885
			return;
886
		?>
887
		<div id="screen-meta-links">
888
		<?php if ( $this->get_help_tabs() ) : ?>
889
			<div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
890
			<button type="button" id="contextual-help-link" class="button show-settings" aria-controls="contextual-help-wrap" aria-expanded="false"><?php _e( 'Help' ); ?></button>
891
			</div>
892
		<?php endif;
893
		if ( $this->show_screen_options() ) : ?>
894
			<div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
895
			<button type="button" id="show-settings-link" class="button show-settings" aria-controls="screen-options-wrap" aria-expanded="false"><?php _e( 'Screen Options' ); ?></button>
896
			</div>
897
		<?php endif; ?>
898
		</div>
899
		<?php
900
	}
901
902
	/**
903
	 *
904
	 * @global array $wp_meta_boxes
905
	 *
906
	 * @return bool
907
	 */
908
	public function show_screen_options() {
909
		global $wp_meta_boxes;
910
911
		if ( is_bool( $this->_show_screen_options ) )
912
			return $this->_show_screen_options;
913
914
		$columns = get_column_headers( $this );
915
916
		$show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' );
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
917
918
		switch ( $this->base ) {
919
			case 'widgets':
920
				$this->_screen_settings = '<p><a id="access-on" href="widgets.php?widgets-access=on">' . __('Enable accessibility mode') . '</a><a id="access-off" href="widgets.php?widgets-access=off">' . __('Disable accessibility mode') . "</a></p>\n";
921
				break;
922
			case 'post' :
923
				$expand = '<fieldset class="editor-expand hidden"><legend>' . __( 'Additional settings' ) . '</legend><label for="editor-expand-toggle">';
924
				$expand .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
925
				$expand .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label></fieldset>';
926
				$this->_screen_settings = $expand;
927
				break;
928
			default:
929
				$this->_screen_settings = '';
930
				break;
931
		}
932
933
		/**
934
		 * Filter the screen settings text displayed in the Screen Options tab.
935
		 *
936
		 * This filter is currently only used on the Widgets screen to enable
937
		 * accessibility mode.
938
		 *
939
		 * @since 3.0.0
940
		 *
941
		 * @param string    $screen_settings Screen settings.
942
		 * @param WP_Screen $this            WP_Screen object.
943
		 */
944
		$this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this );
945
946
		if ( $this->_screen_settings || $this->_options )
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
947
			$show_screen = true;
948
949
		/**
950
		 * Filter whether to show the Screen Options tab.
951
		 *
952
		 * @since 3.2.0
953
		 *
954
		 * @param bool      $show_screen Whether to show Screen Options tab.
955
		 *                               Default true.
956
		 * @param WP_Screen $this        Current WP_Screen instance.
957
		 */
958
		$this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
959
		return $this->_show_screen_options;
960
	}
961
962
	/**
963
	 * Render the screen options tab.
964
	 *
965
	 * @since 3.3.0
966
	 *
967
	 * @param array $options {
968
	 *     @type bool $wrap  Whether the screen-options-wrap div will be included. Defaults to true.
969
	 * }
970
	 */
971
	public function render_screen_options( $options = array() ) {
972
		$options = wp_parse_args( $options, array(
973
			'wrap' => true,
974
		) );
975
976
		$wrapper_start = $wrapper_end = $form_start = $form_end = '';
977
978
		// Output optional wrapper.
979
		if ( $options['wrap'] ) {
980
			$wrapper_start = '<div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="' . esc_attr__( 'Screen Options Tab' ) . '">';
981
			$wrapper_end = '</div>';
982
		}
983
984
		// Don't output the form and nonce for the widgets accessibility mode links.
985
		if ( 'widgets' !== $this->base ) {
986
			$form_start = "\n<form id='adv-settings' method='post'>\n";
987
			$form_end = "\n" . wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false, false ) . "\n</form>\n";
988
		}
989
990
		echo $wrapper_start . $form_start;
991
992
		$this->render_meta_boxes_preferences();
993
		$this->render_list_table_columns_preferences();
994
		$this->render_screen_layout();
995
		$this->render_per_page_options();
996
		$this->render_view_mode();
997
		echo $this->_screen_settings;
998
999
		/**
1000
		 * Filter whether to show the Screen Options submit button.
1001
		 *
1002
		 * @since 4.4.0
1003
		 *
1004
		 * @param bool      $show_button Whether to show Screen Options submit button.
1005
		 *                               Default false.
1006
		 * @param WP_Screen $this        Current WP_Screen instance.
1007
		 */
1008
		$show_button = apply_filters( 'screen_options_show_submit', false, $this );
1009
1010
		if ( $show_button ) {
1011
			submit_button( __( 'Apply' ), 'primary', 'screen-options-apply', true );
1012
		}
1013
1014
		echo $form_end . $wrapper_end;
1015
	}
1016
1017
	/**
1018
	 * Render the meta boxes preferences.
1019
	 *
1020
	 * @since 4.4.0
1021
	 *
1022
	 * @global array $wp_meta_boxes
1023
	 */
1024
	public function render_meta_boxes_preferences() {
1025
		global $wp_meta_boxes;
1026
1027
		if ( ! isset( $wp_meta_boxes[ $this->id ] ) ) {
1028
			return;
1029
		}
1030
		?>
1031
		<fieldset class="metabox-prefs">
1032
		<legend><?php _e( 'Boxes' ); ?></legend>
1033
		<?php
1034
			meta_box_prefs( $this );
1035
1036
			if ( 'dashboard' === $this->id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) {
1037
				if ( isset( $_GET['welcome'] ) ) {
1038
					$welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
1039
					update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
1040
				} else {
1041
					$welcome_checked = get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
1042
					if ( 2 == $welcome_checked && wp_get_current_user()->user_email != get_option( 'admin_email' ) ) {
1043
						$welcome_checked = false;
1044
					}
1045
				}
1046
				echo '<label for="wp_welcome_panel-hide">';
1047
				echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />';
1048
				echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n";
1049
			}
1050
		?>
1051
		</fieldset>
1052
		<?php
1053
	}
1054
1055
	/**
1056
	 * Render the list table columns preferences.
1057
	 *
1058
	 * @since 4.4.0
1059
	 */
1060
	public function render_list_table_columns_preferences() {
1061
1062
		$columns = get_column_headers( $this );
1063
		$hidden  = get_hidden_columns( $this );
1064
1065
		if ( ! $columns ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1066
			return;
1067
		}
1068
1069
		$legend = ! empty( $columns['_title'] ) ? $columns['_title'] : __( 'Columns' );
1070
		?>
1071
		<fieldset class="metabox-prefs">
1072
		<legend><?php echo $legend; ?></legend>
1073
		<?php
1074
		$special = array( '_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname' );
1075
1076
		foreach ( $columns as $column => $title ) {
1077
			// Can't hide these for they are special
1078
			if ( in_array( $column, $special ) ) {
1079
				continue;
1080
			}
1081
1082
			if ( empty( $title ) ) {
1083
				continue;
1084
			}
1085
1086
			if ( 'comments' == $column ) {
1087
				$title = __( 'Comments' );
1088
			}
1089
1090
			$id = "$column-hide";
1091
			echo '<label>';
1092
			echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( ! in_array( $column, $hidden ), true, false ) . ' />';
1093
			echo "$title</label>\n";
1094
		}
1095
		?>
1096
		</fieldset>
1097
		<?php
1098
	}
1099
1100
	/**
1101
	 * Render the option for number of columns on the page
1102
	 *
1103
	 * @since 3.3.0
1104
	 */
1105
	public function render_screen_layout() {
1106
		if ( ! $this->get_option( 'layout_columns' ) ) {
1107
			return;
1108
		}
1109
1110
		$screen_layout_columns = $this->get_columns();
1111
		$num = $this->get_option( 'layout_columns', 'max' );
1112
1113
		?>
1114
		<fieldset class='columns-prefs'>
1115
		<legend class="screen-layout"><?php _e( 'Layout' ); ?></legend><?php
1116
			for ( $i = 1; $i <= $num; ++$i ):
1117
				?>
1118
				<label class="columns-prefs-<?php echo $i; ?>">
1119
					<input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>'
1120
						<?php checked( $screen_layout_columns, $i ); ?> />
1121
					<?php printf( _n( '%s column', '%s columns', $i ), number_format_i18n( $i ) ); ?>
1122
				</label>
1123
				<?php
1124
			endfor; ?>
1125
		</fieldset>
1126
		<?php
1127
	}
1128
1129
	/**
1130
	 * Render the items per page option
1131
	 *
1132
	 * @since 3.3.0
1133
	 */
1134
	public function render_per_page_options() {
1135
		if ( null === $this->get_option( 'per_page' ) ) {
1136
			return;
1137
		}
1138
1139
		$per_page_label = $this->get_option( 'per_page', 'label' );
1140
		if ( null === $per_page_label ) {
1141
			$per_page_label = __( 'Number of items per page:' );
1142
		}
1143
1144
		$option = $this->get_option( 'per_page', 'option' );
1145
		if ( ! $option ) {
1146
			$option = str_replace( '-', '_', "{$this->id}_per_page" );
1147
		}
1148
1149
		$per_page = (int) get_user_option( $option );
1150
		if ( empty( $per_page ) || $per_page < 1 ) {
1151
			$per_page = $this->get_option( 'per_page', 'default' );
1152
			if ( ! $per_page ) {
1153
				$per_page = 20;
1154
			}
1155
		}
1156
1157
		if ( 'edit_comments_per_page' == $option ) {
1158
			$comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
1159
1160
			/** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
1161
			$per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
1162
		} elseif ( 'categories_per_page' == $option ) {
1163
			/** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */
1164
			$per_page = apply_filters( 'edit_categories_per_page', $per_page );
1165
		} else {
1166
			/** This filter is documented in wp-admin/includes/class-wp-list-table.php */
1167
			$per_page = apply_filters( $option, $per_page );
1168
		}
1169
1170
		// Back compat
1171
		if ( isset( $this->post_type ) ) {
1172
			/** This filter is documented in wp-admin/includes/post.php */
1173
			$per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
1174
		}
1175
1176
		// This needs a submit button
1177
		add_filter( 'screen_options_show_submit', '__return_true' );
1178
1179
		?>
1180
		<fieldset class="screen-options">
1181
		<legend><?php _e( 'Pagination' ); ?></legend>
1182
			<?php if ( $per_page_label ) : ?>
1183
				<label for="<?php echo esc_attr( $option ); ?>"><?php echo $per_page_label; ?></label>
1184
				<input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]"
1185
					id="<?php echo esc_attr( $option ); ?>" maxlength="3"
1186
					value="<?php echo esc_attr( $per_page ); ?>" />
1187
			<?php endif; ?>
1188
				<input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" />
1189
		</fieldset>
1190
		<?php
1191
	}
1192
1193
	/**
1194
	 * Render the list table view mode preferences.
1195
	 *
1196
	 * @since 4.4.0
1197
	 */
1198
	public function render_view_mode() {
1199
		$screen = get_current_screen();
1200
1201
		// Currently only enabled for posts lists
1202
		if ( 'edit' !== $screen->base ) {
1203
			return;
1204
		}
1205
1206
		$view_mode_post_types = get_post_types( array( 'hierarchical' => false, 'show_ui' => true ) );
1207
1208
		/**
1209
		 * Filter the post types that have different view mode options.
1210
		 *
1211
		 * @since 4.4.0
1212
		 *
1213
		 * @param array $view_mode_post_types Array of post types that can change view modes.
1214
		 *                                    Default hierarchical post types with show_ui on.
1215
		 */
1216
		$view_mode_post_types = apply_filters( 'view_mode_post_types', $view_mode_post_types );
1217
1218
		if ( ! in_array( $this->post_type, $view_mode_post_types ) ) {
1219
			return;
1220
		}
1221
1222
		global $mode;
1223
1224
		// This needs a submit button
1225
		add_filter( 'screen_options_show_submit', '__return_true' );
1226
?>
1227
		<fieldset class="metabox-prefs view-mode">
1228
		<legend><?php _e( 'View Mode' ); ?></legend>
1229
				<label for="list-view-mode">
1230
					<input id="list-view-mode" type="radio" name="mode" value="list" <?php checked( 'list', $mode ); ?> />
1231
					<?php _e( 'List View' ); ?>
1232
				</label>
1233
				<label for="excerpt-view-mode">
1234
					<input id="excerpt-view-mode" type="radio" name="mode" value="excerpt" <?php checked( 'excerpt', $mode ); ?> />
1235
					<?php _e( 'Excerpt View' ); ?>
1236
				</label>
1237
		</fieldset>
1238
<?php
1239
	}
1240
1241
	/**
1242
	 * Render screen reader text.
1243
	 *
1244
	 * @since 4.4.0
1245
	 *
1246
	 * @param string $key The screen reader text array named key.
1247
	 * @param string $tag Optional. The HTML tag to wrap the screen reader text. Default h2.
1248
	 */
1249
	public function render_screen_reader_content( $key = '', $tag = 'h2' ) {
1250
1251
		if ( ! isset( $this->_screen_reader_content[ $key ] ) ) {
1252
			return;
1253
		}
1254
		echo "<$tag class='screen-reader-text'>" . $this->_screen_reader_content[ $key ] . "</$tag>";
1255
	}
1256
}
1257