|
1 <?php |
|
2 /** |
|
3 * WordPress Administration Screen API. |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Administration |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Get the column headers for a screen |
|
11 * |
|
12 * @since 2.7.0 |
|
13 * |
|
14 * @param string|WP_Screen $screen The screen you want the headers for |
|
15 * @return array Containing the headers in the format id => UI String |
|
16 */ |
|
17 function get_column_headers( $screen ) { |
|
18 if ( is_string( $screen ) ) |
|
19 $screen = convert_to_screen( $screen ); |
|
20 |
|
21 static $column_headers = array(); |
|
22 |
|
23 if ( ! isset( $column_headers[ $screen->id ] ) ) |
|
24 $column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() ); |
|
25 |
|
26 return $column_headers[ $screen->id ]; |
|
27 } |
|
28 |
|
29 /** |
|
30 * Get a list of hidden columns. |
|
31 * |
|
32 * @since 2.7.0 |
|
33 * |
|
34 * @param string|WP_Screen $screen The screen you want the hidden columns for |
|
35 * @return array |
|
36 */ |
|
37 function get_hidden_columns( $screen ) { |
|
38 if ( is_string( $screen ) ) |
|
39 $screen = convert_to_screen( $screen ); |
|
40 |
|
41 return (array) get_user_option( 'manage' . $screen->id . 'columnshidden' ); |
|
42 } |
|
43 |
|
44 /** |
|
45 * Prints the meta box preferences for screen meta. |
|
46 * |
|
47 * @since 2.7.0 |
|
48 * |
|
49 * @param string|WP_Screen $screen |
|
50 */ |
|
51 function meta_box_prefs( $screen ) { |
|
52 global $wp_meta_boxes; |
|
53 |
|
54 if ( is_string( $screen ) ) |
|
55 $screen = convert_to_screen( $screen ); |
|
56 |
|
57 if ( empty($wp_meta_boxes[$screen->id]) ) |
|
58 return; |
|
59 |
|
60 $hidden = get_hidden_meta_boxes($screen); |
|
61 |
|
62 foreach ( array_keys($wp_meta_boxes[$screen->id]) as $context ) { |
|
63 foreach ( array_keys($wp_meta_boxes[$screen->id][$context]) as $priority ) { |
|
64 foreach ( $wp_meta_boxes[$screen->id][$context][$priority] as $box ) { |
|
65 if ( false == $box || ! $box['title'] ) |
|
66 continue; |
|
67 // Submit box cannot be hidden |
|
68 if ( 'submitdiv' == $box['id'] || 'linksubmitdiv' == $box['id'] ) |
|
69 continue; |
|
70 $box_id = $box['id']; |
|
71 echo '<label for="' . $box_id . '-hide">'; |
|
72 echo '<input class="hide-postbox-tog" name="' . $box_id . '-hide" type="checkbox" id="' . $box_id . '-hide" value="' . $box_id . '"' . (! in_array($box_id, $hidden) ? ' checked="checked"' : '') . ' />'; |
|
73 echo "{$box['title']}</label>\n"; |
|
74 } |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 /** |
|
80 * Get Hidden Meta Boxes |
|
81 * |
|
82 * @since 2.7.0 |
|
83 * |
|
84 * @param string|WP_Screen $screen Screen identifier |
|
85 * @return array Hidden Meta Boxes |
|
86 */ |
|
87 function get_hidden_meta_boxes( $screen ) { |
|
88 if ( is_string( $screen ) ) |
|
89 $screen = convert_to_screen( $screen ); |
|
90 |
|
91 $hidden = get_user_option( "metaboxhidden_{$screen->id}" ); |
|
92 |
|
93 $use_defaults = ! is_array( $hidden ); |
|
94 |
|
95 // Hide slug boxes by default |
|
96 if ( $use_defaults ) { |
|
97 $hidden = array(); |
|
98 if ( 'post' == $screen->base ) { |
|
99 if ( 'post' == $screen->post_type || 'page' == $screen->post_type || 'attachment' == $screen->post_type ) |
|
100 $hidden = array('slugdiv', 'trackbacksdiv', 'postcustom', 'postexcerpt', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv'); |
|
101 else |
|
102 $hidden = array( 'slugdiv' ); |
|
103 } |
|
104 $hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen ); |
|
105 } |
|
106 |
|
107 return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults ); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Register and configure an admin screen option |
|
112 * |
|
113 * @since 3.1.0 |
|
114 * |
|
115 * @param string $option An option name. |
|
116 * @param mixed $args Option-dependent arguments. |
|
117 */ |
|
118 function add_screen_option( $option, $args = array() ) { |
|
119 $current_screen = get_current_screen(); |
|
120 |
|
121 if ( ! $current_screen ) |
|
122 return; |
|
123 |
|
124 $current_screen->add_option( $option, $args ); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Displays a screen icon. |
|
129 * |
|
130 * @uses get_screen_icon() |
|
131 * @since 2.7.0 |
|
132 * |
|
133 * @param string|WP_Screen $screen Optional. Accepts a screen object (and defaults to the current screen object) |
|
134 * which it uses to determine an icon HTML ID. Or, if a string is provided, it is used to form the icon HTML ID. |
|
135 */ |
|
136 function screen_icon( $screen = '' ) { |
|
137 echo get_screen_icon( $screen ); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Gets a screen icon. |
|
142 * |
|
143 * @since 3.2.0 |
|
144 * |
|
145 * @global $post_ID |
|
146 * @param string|WP_Screen $screen Optional. Accepts a screen object (and defaults to the current screen object) |
|
147 * which it uses to determine an icon HTML ID. Or, if a string is provided, it is used to form the icon HTML ID. |
|
148 * @return string HTML for the screen icon. |
|
149 */ |
|
150 function get_screen_icon( $screen = '' ) { |
|
151 if ( empty( $screen ) ) |
|
152 $screen = get_current_screen(); |
|
153 elseif ( is_string( $screen ) ) |
|
154 $icon_id = $screen; |
|
155 |
|
156 $class = 'icon32'; |
|
157 |
|
158 if ( empty( $icon_id ) ) { |
|
159 if ( ! empty( $screen->parent_base ) ) |
|
160 $icon_id = $screen->parent_base; |
|
161 else |
|
162 $icon_id = $screen->base; |
|
163 |
|
164 if ( 'page' == $screen->post_type ) |
|
165 $icon_id = 'edit-pages'; |
|
166 |
|
167 if ( $screen->post_type ) |
|
168 $class .= ' ' . sanitize_html_class( 'icon32-posts-' . $screen->post_type ); |
|
169 } |
|
170 |
|
171 return '<div id="icon-' . esc_attr( $icon_id ) . '" class="' . $class . '"><br /></div>'; |
|
172 } |
|
173 |
|
174 /** |
|
175 * Get the current screen object |
|
176 * |
|
177 * @since 3.1.0 |
|
178 * |
|
179 * @return WP_Screen Current screen object |
|
180 */ |
|
181 function get_current_screen() { |
|
182 global $current_screen; |
|
183 |
|
184 if ( ! isset( $current_screen ) ) |
|
185 return null; |
|
186 |
|
187 return $current_screen; |
|
188 } |
|
189 |
|
190 /** |
|
191 * Set the current screen object |
|
192 * |
|
193 * @since 3.0.0 |
|
194 * @uses $current_screen |
|
195 * |
|
196 * @param mixed $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen, |
|
197 * or an existing screen object. |
|
198 */ |
|
199 function set_current_screen( $hook_name = '' ) { |
|
200 WP_Screen::get( $hook_name )->set_current_screen(); |
|
201 } |
|
202 |
|
203 /** |
|
204 * A class representing the admin screen. |
|
205 * |
|
206 * @since 3.3.0 |
|
207 * @access public |
|
208 */ |
|
209 final class WP_Screen { |
|
210 /** |
|
211 * Any action associated with the screen. 'add' for *-add.php and *-new.php screens. Empty otherwise. |
|
212 * |
|
213 * @since 3.3.0 |
|
214 * @var string |
|
215 * @access public |
|
216 */ |
|
217 public $action; |
|
218 |
|
219 /** |
|
220 * The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped. |
|
221 * For example, for an $id of 'edit-post' the base is 'edit'. |
|
222 * |
|
223 * @since 3.3.0 |
|
224 * @var string |
|
225 * @access public |
|
226 */ |
|
227 public $base; |
|
228 |
|
229 /** |
|
230 * The number of columns to display. Access with get_columns(). |
|
231 * |
|
232 * @since 3.4.0 |
|
233 * @var int |
|
234 * @access private |
|
235 */ |
|
236 private $columns = 0; |
|
237 |
|
238 /** |
|
239 * The unique ID of the screen. |
|
240 * |
|
241 * @since 3.3.0 |
|
242 * @var string |
|
243 * @access public |
|
244 */ |
|
245 public $id; |
|
246 |
|
247 /** |
|
248 * Which admin the screen is in. network | user | site | false |
|
249 * |
|
250 * @since 3.5.0 |
|
251 * @var string |
|
252 * @access protected |
|
253 */ |
|
254 protected $in_admin; |
|
255 |
|
256 /** |
|
257 * Whether the screen is in the network admin. |
|
258 * |
|
259 * Deprecated. Use in_admin() instead. |
|
260 * |
|
261 * @since 3.3.0 |
|
262 * @deprecated 3.5.0 |
|
263 * @var bool |
|
264 * @access public |
|
265 */ |
|
266 public $is_network; |
|
267 |
|
268 /** |
|
269 * Whether the screen is in the user admin. |
|
270 * |
|
271 * Deprecated. Use in_admin() instead. |
|
272 * |
|
273 * @since 3.3.0 |
|
274 * @deprecated 3.5.0 |
|
275 * @var bool |
|
276 * @access public |
|
277 */ |
|
278 public $is_user; |
|
279 |
|
280 /** |
|
281 * The base menu parent. |
|
282 * This is derived from $parent_file by removing the query string and any .php extension. |
|
283 * $parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a $parent_base of 'edit'. |
|
284 * |
|
285 * @since 3.3.0 |
|
286 * @var string |
|
287 * @access public |
|
288 */ |
|
289 public $parent_base; |
|
290 |
|
291 /** |
|
292 * The parent_file for the screen per the admin menu system. |
|
293 * Some $parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'. |
|
294 * |
|
295 * @since 3.3.0 |
|
296 * @var string |
|
297 * @access public |
|
298 */ |
|
299 public $parent_file; |
|
300 |
|
301 /** |
|
302 * The post type associated with the screen, if any. |
|
303 * The 'edit.php?post_type=page' screen has a post type of 'page'. |
|
304 * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'. |
|
305 * |
|
306 * @since 3.3.0 |
|
307 * @var string |
|
308 * @access public |
|
309 */ |
|
310 public $post_type; |
|
311 |
|
312 /** |
|
313 * The taxonomy associated with the screen, if any. |
|
314 * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'. |
|
315 * @since 3.3.0 |
|
316 * @var string |
|
317 * @access public |
|
318 */ |
|
319 public $taxonomy; |
|
320 |
|
321 /** |
|
322 * The help tab data associated with the screen, if any. |
|
323 * |
|
324 * @since 3.3.0 |
|
325 * @var array |
|
326 * @access private |
|
327 */ |
|
328 private $_help_tabs = array(); |
|
329 |
|
330 /** |
|
331 * The help sidebar data associated with screen, if any. |
|
332 * |
|
333 * @since 3.3.0 |
|
334 * @var string |
|
335 * @access private |
|
336 */ |
|
337 private $_help_sidebar = ''; |
|
338 |
|
339 /** |
|
340 * Stores old string-based help. |
|
341 */ |
|
342 private static $_old_compat_help = array(); |
|
343 |
|
344 /** |
|
345 * The screen options associated with screen, if any. |
|
346 * |
|
347 * @since 3.3.0 |
|
348 * @var array |
|
349 * @access private |
|
350 */ |
|
351 private $_options = array(); |
|
352 |
|
353 /** |
|
354 * The screen object registry. |
|
355 * |
|
356 * @since 3.3.0 |
|
357 * @var array |
|
358 * @access private |
|
359 */ |
|
360 private static $_registry = array(); |
|
361 |
|
362 /** |
|
363 * Stores the result of the public show_screen_options function. |
|
364 * |
|
365 * @since 3.3.0 |
|
366 * @var bool |
|
367 * @access private |
|
368 */ |
|
369 private $_show_screen_options; |
|
370 |
|
371 /** |
|
372 * Stores the 'screen_settings' section of screen options. |
|
373 * |
|
374 * @since 3.3.0 |
|
375 * @var string |
|
376 * @access private |
|
377 */ |
|
378 private $_screen_settings; |
|
379 |
|
380 /** |
|
381 * Fetches a screen object. |
|
382 * |
|
383 * @since 3.3.0 |
|
384 * @access public |
|
385 * |
|
386 * @param string $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen. |
|
387 * Defaults to the current $hook_suffix global. |
|
388 * @return WP_Screen Screen object. |
|
389 */ |
|
390 public static function get( $hook_name = '' ) { |
|
391 |
|
392 if ( is_a( $hook_name, 'WP_Screen' ) ) |
|
393 return $hook_name; |
|
394 |
|
395 $post_type = $taxonomy = null; |
|
396 $in_admin = false; |
|
397 $action = ''; |
|
398 |
|
399 if ( $hook_name ) |
|
400 $id = $hook_name; |
|
401 else |
|
402 $id = $GLOBALS['hook_suffix']; |
|
403 |
|
404 // For those pesky meta boxes. |
|
405 if ( $hook_name && post_type_exists( $hook_name ) ) { |
|
406 $post_type = $id; |
|
407 $id = 'post'; // changes later. ends up being $base. |
|
408 } else { |
|
409 if ( '.php' == substr( $id, -4 ) ) |
|
410 $id = substr( $id, 0, -4 ); |
|
411 |
|
412 if ( 'post-new' == $id || 'link-add' == $id || 'media-new' == $id || 'user-new' == $id ) { |
|
413 $id = substr( $id, 0, -4 ); |
|
414 $action = 'add'; |
|
415 } |
|
416 } |
|
417 |
|
418 if ( ! $post_type && $hook_name ) { |
|
419 if ( '-network' == substr( $id, -8 ) ) { |
|
420 $id = substr( $id, 0, -8 ); |
|
421 $in_admin = 'network'; |
|
422 } elseif ( '-user' == substr( $id, -5 ) ) { |
|
423 $id = substr( $id, 0, -5 ); |
|
424 $in_admin = 'user'; |
|
425 } |
|
426 |
|
427 $id = sanitize_key( $id ); |
|
428 if ( 'edit-comments' != $id && 'edit-tags' != $id && 'edit-' == substr( $id, 0, 5 ) ) { |
|
429 $maybe = substr( $id, 5 ); |
|
430 if ( taxonomy_exists( $maybe ) ) { |
|
431 $id = 'edit-tags'; |
|
432 $taxonomy = $maybe; |
|
433 } elseif ( post_type_exists( $maybe ) ) { |
|
434 $id = 'edit'; |
|
435 $post_type = $maybe; |
|
436 } |
|
437 } |
|
438 |
|
439 if ( ! $in_admin ) |
|
440 $in_admin = 'site'; |
|
441 } else { |
|
442 if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) |
|
443 $in_admin = 'network'; |
|
444 elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN ) |
|
445 $in_admin = 'user'; |
|
446 else |
|
447 $in_admin = 'site'; |
|
448 } |
|
449 |
|
450 if ( 'index' == $id ) |
|
451 $id = 'dashboard'; |
|
452 elseif ( 'front' == $id ) |
|
453 $in_admin = false; |
|
454 |
|
455 $base = $id; |
|
456 |
|
457 // If this is the current screen, see if we can be more accurate for post types and taxonomies. |
|
458 if ( ! $hook_name ) { |
|
459 if ( isset( $_REQUEST['post_type'] ) ) |
|
460 $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false; |
|
461 if ( isset( $_REQUEST['taxonomy'] ) ) |
|
462 $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false; |
|
463 |
|
464 switch ( $base ) { |
|
465 case 'post' : |
|
466 if ( isset( $_GET['post'] ) ) |
|
467 $post_id = (int) $_GET['post']; |
|
468 elseif ( isset( $_POST['post_ID'] ) ) |
|
469 $post_id = (int) $_POST['post_ID']; |
|
470 else |
|
471 $post_id = 0; |
|
472 |
|
473 if ( $post_id ) { |
|
474 $post = get_post( $post_id ); |
|
475 if ( $post ) |
|
476 $post_type = $post->post_type; |
|
477 } |
|
478 break; |
|
479 case 'edit-tags' : |
|
480 if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) ) |
|
481 $post_type = 'post'; |
|
482 break; |
|
483 } |
|
484 } |
|
485 |
|
486 switch ( $base ) { |
|
487 case 'post' : |
|
488 if ( null === $post_type ) |
|
489 $post_type = 'post'; |
|
490 $id = $post_type; |
|
491 break; |
|
492 case 'edit' : |
|
493 if ( null === $post_type ) |
|
494 $post_type = 'post'; |
|
495 $id .= '-' . $post_type; |
|
496 break; |
|
497 case 'edit-tags' : |
|
498 if ( null === $taxonomy ) |
|
499 $taxonomy = 'post_tag'; |
|
500 // The edit-tags ID does not contain the post type. Look for it in the request. |
|
501 if ( null === $post_type ) { |
|
502 $post_type = 'post'; |
|
503 if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) |
|
504 $post_type = $_REQUEST['post_type']; |
|
505 } |
|
506 |
|
507 $id = 'edit-' . $taxonomy; |
|
508 break; |
|
509 } |
|
510 |
|
511 if ( 'network' == $in_admin ) { |
|
512 $id .= '-network'; |
|
513 $base .= '-network'; |
|
514 } elseif ( 'user' == $in_admin ) { |
|
515 $id .= '-user'; |
|
516 $base .= '-user'; |
|
517 } |
|
518 |
|
519 if ( isset( self::$_registry[ $id ] ) ) { |
|
520 $screen = self::$_registry[ $id ]; |
|
521 if ( $screen === get_current_screen() ) |
|
522 return $screen; |
|
523 } else { |
|
524 $screen = new WP_Screen(); |
|
525 $screen->id = $id; |
|
526 } |
|
527 |
|
528 $screen->base = $base; |
|
529 $screen->action = $action; |
|
530 $screen->post_type = (string) $post_type; |
|
531 $screen->taxonomy = (string) $taxonomy; |
|
532 $screen->is_user = ( 'user' == $in_admin ); |
|
533 $screen->is_network = ( 'network' == $in_admin ); |
|
534 $screen->in_admin = $in_admin; |
|
535 |
|
536 self::$_registry[ $id ] = $screen; |
|
537 |
|
538 return $screen; |
|
539 } |
|
540 |
|
541 /** |
|
542 * Makes the screen object the current screen. |
|
543 * |
|
544 * @see set_current_screen() |
|
545 * @since 3.3.0 |
|
546 */ |
|
547 function set_current_screen() { |
|
548 global $current_screen, $taxnow, $typenow; |
|
549 $current_screen = $this; |
|
550 $taxnow = $this->taxonomy; |
|
551 $typenow = $this->post_type; |
|
552 do_action( 'current_screen', $current_screen ); |
|
553 } |
|
554 |
|
555 /** |
|
556 * Constructor |
|
557 * |
|
558 * @since 3.3.0 |
|
559 * @access private |
|
560 */ |
|
561 private function __construct() {} |
|
562 |
|
563 /** |
|
564 * Indicates whether the screen is in a particular admin |
|
565 * |
|
566 * @since 3.5.0 |
|
567 * |
|
568 * @param string $admin The admin to check against (network | user | site). |
|
569 * If empty any of the three admins will result in true. |
|
570 * @return boolean True if the screen is in the indicated admin, false otherwise. |
|
571 * |
|
572 */ |
|
573 public function in_admin( $admin = null ) { |
|
574 if ( empty( $admin ) ) |
|
575 return (bool) $this->in_admin; |
|
576 |
|
577 return ( $admin == $this->in_admin ); |
|
578 } |
|
579 |
|
580 /** |
|
581 * Sets the old string-based contextual help for the screen. |
|
582 * |
|
583 * For backwards compatibility. |
|
584 * |
|
585 * @since 3.3.0 |
|
586 * |
|
587 * @param WP_Screen $screen A screen object. |
|
588 * @param string $help Help text. |
|
589 */ |
|
590 static function add_old_compat_help( $screen, $help ) { |
|
591 self::$_old_compat_help[ $screen->id ] = $help; |
|
592 } |
|
593 |
|
594 /** |
|
595 * Set the parent information for the screen. |
|
596 * This is called in admin-header.php after the menu parent for the screen has been determined. |
|
597 * |
|
598 * @since 3.3.0 |
|
599 * |
|
600 * @param string $parent_file The parent file of the screen. Typically the $parent_file global. |
|
601 */ |
|
602 function set_parentage( $parent_file ) { |
|
603 $this->parent_file = $parent_file; |
|
604 list( $this->parent_base ) = explode( '?', $parent_file ); |
|
605 $this->parent_base = str_replace( '.php', '', $this->parent_base ); |
|
606 } |
|
607 |
|
608 /** |
|
609 * Adds an option for the screen. |
|
610 * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options. |
|
611 * |
|
612 * @since 3.3.0 |
|
613 * |
|
614 * @param string $option Option ID |
|
615 * @param mixed $args Option-dependent arguments. |
|
616 */ |
|
617 public function add_option( $option, $args = array() ) { |
|
618 $this->_options[ $option ] = $args; |
|
619 } |
|
620 |
|
621 /** |
|
622 * Gets the arguments for an option for the screen. |
|
623 * |
|
624 * @since 3.3.0 |
|
625 * |
|
626 * @param string $option Option ID. |
|
627 * @param mixed $key Optional. Specific array key for when the option is an array. |
|
628 */ |
|
629 public function get_option( $option, $key = false ) { |
|
630 if ( ! isset( $this->_options[ $option ] ) ) |
|
631 return null; |
|
632 if ( $key ) { |
|
633 if ( isset( $this->_options[ $option ][ $key ] ) ) |
|
634 return $this->_options[ $option ][ $key ]; |
|
635 return null; |
|
636 } |
|
637 return $this->_options[ $option ]; |
|
638 } |
|
639 |
|
640 /** |
|
641 * Gets the help tabs registered for the screen. |
|
642 * |
|
643 * @since 3.4.0 |
|
644 * |
|
645 * @return array Help tabs with arguments. |
|
646 */ |
|
647 public function get_help_tabs() { |
|
648 return $this->_help_tabs; |
|
649 } |
|
650 |
|
651 /** |
|
652 * Gets the arguments for a help tab. |
|
653 * |
|
654 * @since 3.4.0 |
|
655 * |
|
656 * @param string $id Help Tab ID. |
|
657 * @return array Help tab arguments. |
|
658 */ |
|
659 public function get_help_tab( $id ) { |
|
660 if ( ! isset( $this->_help_tabs[ $id ] ) ) |
|
661 return null; |
|
662 return $this->_help_tabs[ $id ]; |
|
663 } |
|
664 |
|
665 /** |
|
666 * Add a help tab to the contextual help for the screen. |
|
667 * Call this on the load-$pagenow hook for the relevant screen. |
|
668 * |
|
669 * @since 3.3.0 |
|
670 * |
|
671 * @param array $args |
|
672 * - string - title - Title for the tab. |
|
673 * - string - id - Tab ID. Must be HTML-safe. |
|
674 * - string - content - Help tab content in plain text or HTML. Optional. |
|
675 * - callback - callback - A callback to generate the tab content. Optional. |
|
676 * |
|
677 */ |
|
678 public function add_help_tab( $args ) { |
|
679 $defaults = array( |
|
680 'title' => false, |
|
681 'id' => false, |
|
682 'content' => '', |
|
683 'callback' => false, |
|
684 ); |
|
685 $args = wp_parse_args( $args, $defaults ); |
|
686 |
|
687 $args['id'] = sanitize_html_class( $args['id'] ); |
|
688 |
|
689 // Ensure we have an ID and title. |
|
690 if ( ! $args['id'] || ! $args['title'] ) |
|
691 return; |
|
692 |
|
693 // Allows for overriding an existing tab with that ID. |
|
694 $this->_help_tabs[ $args['id'] ] = $args; |
|
695 } |
|
696 |
|
697 /** |
|
698 * Removes a help tab from the contextual help for the screen. |
|
699 * |
|
700 * @since 3.3.0 |
|
701 * |
|
702 * @param string $id The help tab ID. |
|
703 */ |
|
704 public function remove_help_tab( $id ) { |
|
705 unset( $this->_help_tabs[ $id ] ); |
|
706 } |
|
707 |
|
708 /** |
|
709 * Removes all help tabs from the contextual help for the screen. |
|
710 * |
|
711 * @since 3.3.0 |
|
712 */ |
|
713 public function remove_help_tabs() { |
|
714 $this->_help_tabs = array(); |
|
715 } |
|
716 |
|
717 /** |
|
718 * Gets the content from a contextual help sidebar. |
|
719 * |
|
720 * @since 3.4.0 |
|
721 * |
|
722 * @return string Contents of the help sidebar. |
|
723 */ |
|
724 public function get_help_sidebar() { |
|
725 return $this->_help_sidebar; |
|
726 } |
|
727 |
|
728 /** |
|
729 * Add a sidebar to the contextual help for the screen. |
|
730 * 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. |
|
731 * |
|
732 * @since 3.3.0 |
|
733 * |
|
734 * @param string $content Sidebar content in plain text or HTML. |
|
735 */ |
|
736 public function set_help_sidebar( $content ) { |
|
737 $this->_help_sidebar = $content; |
|
738 } |
|
739 |
|
740 /** |
|
741 * Gets the number of layout columns the user has selected. |
|
742 * |
|
743 * The layout_columns option controls the max number and default number of |
|
744 * columns. This method returns the number of columns within that range selected |
|
745 * by the user via Screen Options. If no selection has been made, the default |
|
746 * provisioned in layout_columns is returned. If the screen does not support |
|
747 * selecting the number of layout columns, 0 is returned. |
|
748 * |
|
749 * @since 3.4.0 |
|
750 * |
|
751 * @return int Number of columns to display. |
|
752 */ |
|
753 public function get_columns() { |
|
754 return $this->columns; |
|
755 } |
|
756 |
|
757 /** |
|
758 * Render the screen's help section. |
|
759 * |
|
760 * This will trigger the deprecated filters for backwards compatibility. |
|
761 * |
|
762 * @since 3.3.0 |
|
763 */ |
|
764 public function render_screen_meta() { |
|
765 |
|
766 // Call old contextual_help_list filter. |
|
767 self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this ); |
|
768 |
|
769 $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : ''; |
|
770 $old_help = apply_filters( 'contextual_help', $old_help, $this->id, $this ); |
|
771 |
|
772 // Default help only if there is no old-style block of text and no new-style help tabs. |
|
773 if ( empty( $old_help ) && ! $this->get_help_tabs() ) { |
|
774 $default_help = apply_filters( 'default_contextual_help', '' ); |
|
775 if ( $default_help ) |
|
776 $old_help = '<p>' . $default_help . '</p>'; |
|
777 } |
|
778 |
|
779 if ( $old_help ) { |
|
780 $this->add_help_tab( array( |
|
781 'id' => 'old-contextual-help', |
|
782 'title' => __('Overview'), |
|
783 'content' => $old_help, |
|
784 ) ); |
|
785 } |
|
786 |
|
787 $help_sidebar = $this->get_help_sidebar(); |
|
788 |
|
789 $help_class = 'hidden'; |
|
790 if ( ! $help_sidebar ) |
|
791 $help_class .= ' no-sidebar'; |
|
792 |
|
793 // Time to render! |
|
794 ?> |
|
795 <div id="screen-meta" class="metabox-prefs"> |
|
796 |
|
797 <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>" tabindex="-1" aria-label="<?php esc_attr_e('Contextual Help Tab'); ?>"> |
|
798 <div id="contextual-help-back"></div> |
|
799 <div id="contextual-help-columns"> |
|
800 <div class="contextual-help-tabs"> |
|
801 <ul> |
|
802 <?php |
|
803 $class = ' class="active"'; |
|
804 foreach ( $this->get_help_tabs() as $tab ) : |
|
805 $link_id = "tab-link-{$tab['id']}"; |
|
806 $panel_id = "tab-panel-{$tab['id']}"; |
|
807 ?> |
|
808 |
|
809 <li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>> |
|
810 <a href="<?php echo esc_url( "#$panel_id" ); ?>" aria-controls="<?php echo esc_attr( $panel_id ); ?>"> |
|
811 <?php echo esc_html( $tab['title'] ); ?> |
|
812 </a> |
|
813 </li> |
|
814 <?php |
|
815 $class = ''; |
|
816 endforeach; |
|
817 ?> |
|
818 </ul> |
|
819 </div> |
|
820 |
|
821 <?php if ( $help_sidebar ) : ?> |
|
822 <div class="contextual-help-sidebar"> |
|
823 <?php echo $help_sidebar; ?> |
|
824 </div> |
|
825 <?php endif; ?> |
|
826 |
|
827 <div class="contextual-help-tabs-wrap"> |
|
828 <?php |
|
829 $classes = 'help-tab-content active'; |
|
830 foreach ( $this->get_help_tabs() as $tab ): |
|
831 $panel_id = "tab-panel-{$tab['id']}"; |
|
832 ?> |
|
833 |
|
834 <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>"> |
|
835 <?php |
|
836 // Print tab content. |
|
837 echo $tab['content']; |
|
838 |
|
839 // If it exists, fire tab callback. |
|
840 if ( ! empty( $tab['callback'] ) ) |
|
841 call_user_func_array( $tab['callback'], array( $this, $tab ) ); |
|
842 ?> |
|
843 </div> |
|
844 <?php |
|
845 $classes = 'help-tab-content'; |
|
846 endforeach; |
|
847 ?> |
|
848 </div> |
|
849 </div> |
|
850 </div> |
|
851 <?php |
|
852 // Setup layout columns |
|
853 |
|
854 // Back compat for plugins using the filter instead of add_screen_option() |
|
855 $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this ); |
|
856 |
|
857 if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) ) |
|
858 $this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) ); |
|
859 |
|
860 if ( $this->get_option( 'layout_columns' ) ) { |
|
861 $this->columns = (int) get_user_option("screen_layout_$this->id"); |
|
862 |
|
863 if ( ! $this->columns && $this->get_option( 'layout_columns', 'default' ) ) |
|
864 $this->columns = $this->get_option( 'layout_columns', 'default' ); |
|
865 } |
|
866 $GLOBALS[ 'screen_layout_columns' ] = $this->columns; // Set the global for back-compat. |
|
867 |
|
868 // Add screen options |
|
869 if ( $this->show_screen_options() ) |
|
870 $this->render_screen_options(); |
|
871 ?> |
|
872 </div> |
|
873 <?php |
|
874 if ( ! $this->get_help_tabs() && ! $this->show_screen_options() ) |
|
875 return; |
|
876 ?> |
|
877 <div id="screen-meta-links"> |
|
878 <?php if ( $this->get_help_tabs() ) : ?> |
|
879 <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle"> |
|
880 <a href="#contextual-help-wrap" id="contextual-help-link" class="show-settings" aria-controls="contextual-help-wrap" aria-expanded="false"><?php _e( 'Help' ); ?></a> |
|
881 </div> |
|
882 <?php endif; |
|
883 if ( $this->show_screen_options() ) : ?> |
|
884 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle"> |
|
885 <a href="#screen-options-wrap" id="show-settings-link" class="show-settings" aria-controls="screen-options-wrap" aria-expanded="false"><?php _e( 'Screen Options' ); ?></a> |
|
886 </div> |
|
887 <?php endif; ?> |
|
888 </div> |
|
889 <?php |
|
890 } |
|
891 |
|
892 public function show_screen_options() { |
|
893 global $wp_meta_boxes; |
|
894 |
|
895 if ( is_bool( $this->_show_screen_options ) ) |
|
896 return $this->_show_screen_options; |
|
897 |
|
898 $columns = get_column_headers( $this ); |
|
899 |
|
900 $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' ); |
|
901 |
|
902 switch ( $this->id ) { |
|
903 case 'widgets': |
|
904 $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"; |
|
905 break; |
|
906 default: |
|
907 $this->_screen_settings = ''; |
|
908 break; |
|
909 } |
|
910 |
|
911 $this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this ); |
|
912 |
|
913 if ( $this->_screen_settings || $this->_options ) |
|
914 $show_screen = true; |
|
915 |
|
916 $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this ); |
|
917 return $this->_show_screen_options; |
|
918 } |
|
919 |
|
920 /** |
|
921 * Render the screen options tab. |
|
922 * |
|
923 * @since 3.3.0 |
|
924 */ |
|
925 public function render_screen_options() { |
|
926 global $wp_meta_boxes, $wp_list_table; |
|
927 |
|
928 $columns = get_column_headers( $this ); |
|
929 $hidden = get_hidden_columns( $this ); |
|
930 $post = get_post(); |
|
931 |
|
932 ?> |
|
933 <div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="<?php esc_attr_e('Screen Options Tab'); ?>"> |
|
934 <form id="adv-settings" action="" method="post"> |
|
935 <?php if ( isset( $wp_meta_boxes[ $this->id ] ) || $this->get_option( 'per_page' ) || ( $columns && empty( $columns['_title'] ) ) ) : ?> |
|
936 <h5><?php _e( 'Show on screen' ); ?></h5> |
|
937 <?php |
|
938 endif; |
|
939 |
|
940 if ( isset( $wp_meta_boxes[ $this->id ] ) ) : ?> |
|
941 <div class="metabox-prefs"> |
|
942 <?php |
|
943 meta_box_prefs( $this ); |
|
944 |
|
945 if ( 'dashboard' === $this->id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) { |
|
946 if ( isset( $_GET['welcome'] ) ) { |
|
947 $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1; |
|
948 update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked ); |
|
949 } else { |
|
950 $welcome_checked = get_user_meta( get_current_user_id(), 'show_welcome_panel', true ); |
|
951 if ( 2 == $welcome_checked && wp_get_current_user()->user_email != get_option( 'admin_email' ) ) |
|
952 $welcome_checked = false; |
|
953 } |
|
954 echo '<label for="wp_welcome_panel-hide">'; |
|
955 echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />'; |
|
956 echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n"; |
|
957 } |
|
958 ?> |
|
959 <br class="clear" /> |
|
960 </div> |
|
961 <?php endif; |
|
962 if ( $columns ) : |
|
963 if ( ! empty( $columns['_title'] ) ) : ?> |
|
964 <h5><?php echo $columns['_title']; ?></h5> |
|
965 <?php endif; ?> |
|
966 <div class="metabox-prefs"> |
|
967 <?php |
|
968 $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname'); |
|
969 |
|
970 foreach ( $columns as $column => $title ) { |
|
971 // Can't hide these for they are special |
|
972 if ( in_array( $column, $special ) ) |
|
973 continue; |
|
974 if ( empty( $title ) ) |
|
975 continue; |
|
976 |
|
977 if ( 'comments' == $column ) |
|
978 $title = __( 'Comments' ); |
|
979 $id = "$column-hide"; |
|
980 echo '<label for="' . $id . '">'; |
|
981 echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />'; |
|
982 echo "$title</label>\n"; |
|
983 } |
|
984 ?> |
|
985 <br class="clear" /> |
|
986 </div> |
|
987 <?php endif; |
|
988 |
|
989 $this->render_screen_layout(); |
|
990 $this->render_per_page_options(); |
|
991 echo $this->_screen_settings; |
|
992 |
|
993 ?> |
|
994 <div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div> |
|
995 </form> |
|
996 </div> |
|
997 <?php |
|
998 } |
|
999 |
|
1000 /** |
|
1001 * Render the option for number of columns on the page |
|
1002 * |
|
1003 * @since 3.3.0 |
|
1004 */ |
|
1005 function render_screen_layout() { |
|
1006 if ( ! $this->get_option('layout_columns') ) |
|
1007 return; |
|
1008 |
|
1009 $screen_layout_columns = $this->get_columns(); |
|
1010 $num = $this->get_option( 'layout_columns', 'max' ); |
|
1011 |
|
1012 ?> |
|
1013 <h5 class="screen-layout"><?php _e('Screen Layout'); ?></h5> |
|
1014 <div class='columns-prefs'><?php |
|
1015 _e('Number of Columns:'); |
|
1016 for ( $i = 1; $i <= $num; ++$i ): |
|
1017 ?> |
|
1018 <label class="columns-prefs-<?php echo $i; ?>"> |
|
1019 <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>' |
|
1020 <?php checked( $screen_layout_columns, $i ); ?> /> |
|
1021 <?php echo esc_html( $i ); ?> |
|
1022 </label> |
|
1023 <?php |
|
1024 endfor; ?> |
|
1025 </div> |
|
1026 <?php |
|
1027 } |
|
1028 |
|
1029 /** |
|
1030 * Render the items per page option |
|
1031 * |
|
1032 * @since 3.3.0 |
|
1033 */ |
|
1034 function render_per_page_options() { |
|
1035 if ( ! $this->get_option( 'per_page' ) ) |
|
1036 return; |
|
1037 |
|
1038 $per_page_label = $this->get_option( 'per_page', 'label' ); |
|
1039 |
|
1040 $option = $this->get_option( 'per_page', 'option' ); |
|
1041 if ( ! $option ) |
|
1042 $option = str_replace( '-', '_', "{$this->id}_per_page" ); |
|
1043 |
|
1044 $per_page = (int) get_user_option( $option ); |
|
1045 if ( empty( $per_page ) || $per_page < 1 ) { |
|
1046 $per_page = $this->get_option( 'per_page', 'default' ); |
|
1047 if ( ! $per_page ) |
|
1048 $per_page = 20; |
|
1049 } |
|
1050 |
|
1051 if ( 'edit_comments_per_page' == $option ) { |
|
1052 $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all'; |
|
1053 $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status ); |
|
1054 } elseif ( 'categories_per_page' == $option ) { |
|
1055 $per_page = apply_filters( 'edit_categories_per_page', $per_page ); |
|
1056 } else { |
|
1057 $per_page = apply_filters( $option, $per_page ); |
|
1058 } |
|
1059 |
|
1060 // Back compat |
|
1061 if ( isset( $this->post_type ) ) |
|
1062 $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type ); |
|
1063 |
|
1064 ?> |
|
1065 <div class="screen-options"> |
|
1066 <?php if ( $per_page_label ) : ?> |
|
1067 <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]" |
|
1068 id="<?php echo esc_attr( $option ); ?>" maxlength="3" |
|
1069 value="<?php echo esc_attr( $per_page ); ?>" /> |
|
1070 <label for="<?php echo esc_attr( $option ); ?>"> |
|
1071 <?php echo esc_html( $per_page_label ); ?> |
|
1072 </label> |
|
1073 <?php endif; |
|
1074 |
|
1075 echo get_submit_button( __( 'Apply' ), 'button', 'screen-options-apply', false ); ?> |
|
1076 <input type='hidden' name='wp_screen_options[option]' value='<?php echo esc_attr($option); ?>' /> |
|
1077 </div> |
|
1078 <?php |
|
1079 } |
|
1080 } |