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