3 * WordPress Administration Screen API.
6 * @subpackage Administration
10 * Get the column headers for a screen
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
17 function get_column_headers( $screen ) {
18 if ( is_string( $screen ) )
19 $screen = convert_to_screen( $screen );
21 static $column_headers = array();
23 if ( ! isset( $column_headers[ $screen->id ] ) )
24 $column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() );
26 return $column_headers[ $screen->id ];
30 * Get a list of hidden columns.
34 * @param string|WP_Screen $screen The screen you want the hidden columns for
37 function get_hidden_columns( $screen ) {
38 if ( is_string( $screen ) )
39 $screen = convert_to_screen( $screen );
41 return (array) get_user_option( 'manage' . $screen->id . 'columnshidden' );
45 * Prints the meta box preferences for screen meta.
49 * @param string|WP_Screen $screen
51 function meta_box_prefs( $screen ) {
52 global $wp_meta_boxes;
54 if ( is_string( $screen ) )
55 $screen = convert_to_screen( $screen );
57 if ( empty($wp_meta_boxes[$screen->id]) )
60 $hidden = get_hidden_meta_boxes($screen);
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'] )
67 // Submit box cannot be hidden
68 if ( 'submitdiv' == $box['id'] || 'linksubmitdiv' == $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";
80 * Get Hidden Meta Boxes
84 * @param string|WP_Screen $screen Screen identifier
85 * @return array Hidden Meta Boxes
87 function get_hidden_meta_boxes( $screen ) {
88 if ( is_string( $screen ) )
89 $screen = convert_to_screen( $screen );
91 $hidden = get_user_option( "metaboxhidden_{$screen->id}" );
93 $use_defaults = ! is_array( $hidden );
95 // Hide slug boxes by default
96 if ( $use_defaults ) {
98 if ( 'post' == $screen->base ) {
99 if ( 'post' == $screen->post_type || 'page' == $screen->post_type )
100 $hidden = array('slugdiv', 'trackbacksdiv', 'postcustom', 'postexcerpt', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv');
102 $hidden = array( 'slugdiv' );
104 $hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen );
107 return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults );
111 * Register and configure an admin screen option
115 * @param string $option An option name.
116 * @param mixed $args Option-dependent arguments.
119 function add_screen_option( $option, $args = array() ) {
120 $current_screen = get_current_screen();
122 if ( ! $current_screen )
125 $current_screen->add_option( $option, $args );
129 * Displays a screen icon.
131 * @uses get_screen_icon()
134 * @param string|WP_Screen $screen Optional. Accepts a screen object (and defaults to the current screen object)
135 * which it uses to determine an icon HTML ID. Or, if a string is provided, it is used to form the icon HTML ID.
137 function screen_icon( $screen = '' ) {
138 echo get_screen_icon( $screen );
142 * Gets a screen icon.
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.
150 function get_screen_icon( $screen = '' ) {
151 if ( empty( $screen ) )
152 $screen = get_current_screen();
153 elseif ( is_string( $screen ) )
158 if ( empty( $icon_id ) ) {
159 if ( ! empty( $screen->parent_base ) )
160 $icon_id = $screen->parent_base;
162 $icon_id = $screen->base;
164 if ( 'page' == $screen->post_type )
165 $icon_id = 'edit-pages';
167 if ( $screen->post_type )
168 $class .= ' ' . sanitize_html_class( 'icon32-posts-' . $screen->post_type );
171 return '<div id="icon-' . esc_attr( $icon_id ) . '" class="' . $class . '"><br /></div>';
175 * Get the current screen object
179 * @return object Current screen object
181 function get_current_screen() {
182 global $current_screen;
184 if ( ! isset( $current_screen ) )
187 return $current_screen;
191 * Set the current screen object
194 * @uses $current_screen
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.
199 function set_current_screen( $hook_name = '' ) {
200 WP_Screen::get( $hook_name )->set_current_screen();
204 * A class representing the admin screen.
209 final class WP_Screen {
211 * Any action associated with the screen. 'add' for *-add.php and *-new.php screens. Empty otherwise.
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'.
230 * The unique ID of the screen.
239 * Whether the screen is in the network admin.
248 * Whether the screen is in the user admin.
257 * The base menu parent.
258 * This is derived from $parent_file by removing the query string and any .php extension.
259 * $parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a $parent_base of 'edit'.
268 * The parent_file for the screen per the admin menu system.
269 * Some $parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'.
278 * The post type associated with the screen, if any.
279 * The 'edit.php?post_type=page' screen has a post type of 'page'.
280 * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'.
289 * The taxonomy associated with the screen, if any.
290 * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'.
298 * The help tab data associated with the screen, if any.
304 private $_help_tabs = array();
307 * The help sidebar data associated with screen, if any.
313 private $_help_sidebar = '';
316 * Stores old string-based help.
318 private static $_old_compat_help = array();
321 * The screen options associated with screen, if any.
327 private $_options = array();
330 * The screen object registry.
336 private static $_registry = array();
339 * Stores the result of the public show_screen_options function.
345 private $_show_screen_options;
348 * Stores the 'screen_settings' section of screen options.
354 private $_screen_settings;
357 * Fetches a screen object.
362 * @param string $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
363 * Defaults to the current $hook_suffix global.
364 * @return WP_Screen Screen object.
366 public static function get( $hook_name = '' ) {
368 if ( is_a( $hook_name, 'WP_Screen' ) )
371 $post_type = $taxonomy = null;
372 $is_network = $is_user = false;
378 $id = $GLOBALS['hook_suffix'];
380 // For those pesky meta boxes.
381 if ( $hook_name && post_type_exists( $hook_name ) ) {
383 $id = 'post'; // changes later. ends up being $base.
385 if ( '.php' == substr( $id, -4 ) )
386 $id = substr( $id, 0, -4 );
388 if ( 'post-new' == $id || 'link-add' == $id || 'media-new' == $id || 'user-new' == $id ) {
389 $id = substr( $id, 0, -4 );
394 if ( ! $post_type && $hook_name ) {
395 if ( '-network' == substr( $id, -8 ) ) {
396 $id = substr( $id, 0, -8 );
398 } elseif ( '-user' == substr( $id, -5 ) ) {
399 $id = substr( $id, 0, -5 );
403 $id = sanitize_key( $id );
404 if ( 'edit-comments' != $id && 'edit-tags' != $id && 'edit-' == substr( $id, 0, 5 ) ) {
405 $maybe = substr( $id, 5 );
406 if ( taxonomy_exists( $maybe ) ) {
409 } elseif ( post_type_exists( $maybe ) ) {
415 $is_network = is_network_admin();
416 $is_user = is_user_admin();
419 if ( 'index' == $id )
424 // If this is the current screen, see if we can be more accurate for post types and taxonomies.
425 if ( ! $hook_name ) {
426 if ( isset( $_REQUEST['post_type'] ) )
427 $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false;
428 if ( isset( $_REQUEST['taxonomy'] ) )
429 $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false;
433 if ( isset( $_GET['post'] ) )
434 $post_id = (int) $_GET['post'];
435 elseif ( isset( $_POST['post_ID'] ) )
436 $post_id = (int) $_POST['post_ID'];
441 $post = get_post( $post_id );
443 $post_type = $post->post_type;
447 if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) )
455 if ( null === $post_type )
460 if ( null === $post_type )
462 $id .= '-' . $post_type;
465 if ( null === $taxonomy )
466 $taxonomy = 'post_tag';
467 $id = 'edit-' . $taxonomy;
474 } elseif ( $is_user ) {
479 if ( isset( self::$_registry[ $id ] ) ) {
480 $screen = self::$_registry[ $id ];
481 if ( $screen === get_current_screen() )
484 $screen = new WP_Screen();
488 $screen->base = $base;
489 $screen->action = $action;
490 $screen->post_type = (string) $post_type;
491 $screen->taxonomy = (string) $taxonomy;
492 $screen->is_user = $is_user;
493 $screen->is_network = $is_network;
495 self::$_registry[ $id ] = $screen;
501 * Makes the screen object the current screen.
503 * @see set_current_screen()
506 function set_current_screen() {
507 global $current_screen, $taxnow, $typenow;
508 $current_screen = $this;
509 $taxnow = $this->taxonomy;
510 $typenow = $this->post_type;
511 do_action( 'current_screen', $current_screen );
520 private function __construct() {}
523 * Sets the old string-based contextual help for the screen.
525 * For backwards compatibility.
529 * @param WP_Screen $screen A screen object.
530 * @param string $help Help text.
532 static function add_old_compat_help( $screen, $help ) {
533 self::$_old_compat_help[ $screen->id ] = $help;
537 * Set the parent information for the screen.
538 * This is called in admin-header.php after the menu parent for the screen has been determined.
542 * @param string $parent_file The parent file of the screen. Typically the $parent_file global.
544 function set_parentage( $parent_file ) {
545 $this->parent_file = $parent_file;
546 list( $this->parent_base ) = explode( '?', $parent_file );
547 $this->parent_base = str_replace( '.php', '', $this->parent_base );
551 * Adds an option for the screen.
552 * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options.
556 * @param string $option Option ID
557 * @param mixed $args Option-dependent arguments.
559 public function add_option( $option, $args = array() ) {
560 $this->_options[ $option ] = $args;
564 * Gets the arguments for an option for the screen.
570 public function get_option( $option, $key = false ) {
571 if ( ! isset( $this->_options[ $option ] ) )
574 if ( isset( $this->_options[ $option ][ $key ] ) )
575 return $this->_options[ $option ][ $key ];
578 return $this->_options[ $option ];
582 * Add a help tab to the contextual help for the screen.
583 * Call this on the load-$pagenow hook for the relevant screen.
588 * - string - title - Title for the tab.
589 * - string - id - Tab ID. Must be HTML-safe.
590 * - string - content - Help tab content in plain text or HTML. Optional.
591 * - callback - callback - A callback to generate the tab content. Optional.
594 public function add_help_tab( $args ) {
601 $args = wp_parse_args( $args, $defaults );
603 $args['id'] = sanitize_html_class( $args['id'] );
605 // Ensure we have an ID and title.
606 if ( ! $args['id'] || ! $args['title'] )
609 $this->_help_tabs[] = $args;
613 * Removes a help tab from the contextual help for the screen.
617 * @param string $id The help tab ID.
619 public function remove_help_tab( $id ) {
620 unset( $this->_help_tabs[ $id ] );
624 * Removes all help tabs from the contextual help for the screen.
628 public function remove_help_tabs() {
629 $this->_help_tabs = array();
633 * Add a sidebar to the contextual help for the screen.
634 * 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.
638 * @param string $content Sidebar content in plain text or HTML.
640 public function set_help_sidebar( $content ) {
641 $this->_help_sidebar = $content;
645 * Render the screen's help section.
647 * This will trigger the deprecated filters for backwards compatibility.
651 public function render_screen_meta() {
653 // Call old contextual_help_list filter.
654 self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this );
656 $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : '';
657 $old_help = apply_filters( 'contextual_help', $old_help, $this->id, $this );
659 // Default help only if there is no old-style block of text and no new-style help tabs.
660 if ( empty( $old_help ) && empty( $this->_help_tabs ) ) {
661 $default_help = apply_filters( 'default_contextual_help', '' );
663 $old_help = '<p>' . $default_help . '</p>';
667 $this->add_help_tab( array(
668 'id' => 'old-contextual-help',
669 'title' => __('Overview'),
670 'content' => $old_help,
674 $has_sidebar = ! empty( $this->_help_sidebar );
676 $help_class = 'hidden';
677 if ( ! $has_sidebar )
678 $help_class .= ' no-sidebar';
682 <div id="screen-meta" class="metabox-prefs">
684 <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>">
685 <div id="contextual-help-back"></div>
686 <div id="contextual-help-columns">
687 <div class="contextual-help-tabs">
689 <?php foreach ( $this->_help_tabs as $i => $tab ):
690 $link_id = "tab-link-{$tab['id']}";
691 $panel_id = "tab-panel-{$tab['id']}";
692 $classes = ( $i == 0 ) ? 'active' : '';
695 <li id="<?php echo esc_attr( $link_id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
696 <a href="<?php echo esc_url( "#$panel_id" ); ?>">
697 <?php echo esc_html( $tab['title'] ); ?>
704 <?php if ( $has_sidebar ) : ?>
705 <div class="contextual-help-sidebar">
706 <?php echo self::$this->_help_sidebar; ?>
710 <div class="contextual-help-tabs-wrap">
711 <?php foreach ( $this->_help_tabs as $i => $tab ):
712 $panel_id = "tab-panel-{$tab['id']}";
713 $classes = ( $i == 0 ) ? 'active' : '';
714 $classes .= ' help-tab-content';
717 <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
719 // Print tab content.
720 echo $tab['content'];
722 // If it exists, fire tab callback.
723 if ( ! empty( $tab['callback'] ) )
724 call_user_func_array( $tab['callback'], array( $this, $tab ) );
732 // Add screen options
733 if ( $this->show_screen_options() )
734 $this->render_screen_options();
738 if ( ! $this->_help_tabs && ! $this->show_screen_options() )
741 <div id="screen-meta-links">
742 <?php if ( $this->_help_tabs ) : ?>
743 <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
744 <a href="#contextual-help-wrap" id="contextual-help-link" class="show-settings"><?php _e( 'Help' ); ?></a>
747 if ( $this->show_screen_options() ) : ?>
748 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
749 <a href="#screen-options-wrap" id="show-settings-link" class="show-settings"><?php _e( 'Screen Options' ); ?></a>
756 public function show_screen_options() {
757 global $wp_meta_boxes;
759 if ( is_bool( $this->_show_screen_options ) )
760 return $this->_show_screen_options;
762 $columns = get_column_headers( $this );
764 $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' );
766 $this->_screen_settings = apply_filters( 'screen_settings', '', $this );
768 switch ( $this->id ) {
770 $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";
774 if ( $this->_screen_settings || $this->_options )
777 $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
778 return $this->_show_screen_options;
782 * Render the screen options tab.
786 public function render_screen_options() {
787 global $wp_meta_boxes, $wp_list_table;
789 $columns = get_column_headers( $this );
790 $hidden = get_hidden_columns( $this );
793 <div id="screen-options-wrap" class="hidden">
794 <form id="adv-settings" action="" method="post">
796 if ( isset( $wp_meta_boxes[ $this->id ] ) ) : ?>
797 <h5><?php _ex('Show on screen', 'Metaboxes') ?></h5>
798 <div class="metabox-prefs">
800 meta_box_prefs( $this );
802 if ( 'dashboard' === $this->id && current_user_can( 'edit_theme_options' ) ) {
803 if ( isset( $_GET['welcome'] ) ) {
804 $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
805 update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
807 $welcome_checked = get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
808 if ( 2 == $welcome_checked && wp_get_current_user()->user_email != get_option( 'admin_email' ) )
809 $welcome_checked = false;
811 echo '<label for="wp_welcome_panel-hide">';
812 echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />';
813 echo __( 'Welcome' ) . "</label>\n";
819 if ( ! empty( $columns ) ) : ?>
820 <h5><?php echo ( isset( $columns['_title'] ) ? $columns['_title'] : _x('Show on screen', 'Columns') ) ?></h5>
821 <div class="metabox-prefs">
823 $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname');
825 foreach ( $columns as $column => $title ) {
826 // Can't hide these for they are special
827 if ( in_array( $column, $special ) )
829 if ( empty( $title ) )
832 if ( 'comments' == $column )
833 $title = __( 'Comments' );
834 $id = "$column-hide";
835 echo '<label for="' . $id . '">';
836 echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />';
837 echo "$title</label>\n";
844 $this->render_screen_layout();
845 $this->render_per_page_options();
846 echo $this->_screen_settings;
849 <div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div>
856 * Render the option for number of columns on the page
860 function render_screen_layout() {
861 global $screen_layout_columns;
863 // Back compat for plugins using the filter instead of add_screen_option()
864 $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this );
866 if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) )
867 $this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) );
869 if ( ! $this->get_option('layout_columns') ) {
870 $screen_layout_columns = 0;
874 $screen_layout_columns = get_user_option("screen_layout_$this->id");
875 $num = $this->get_option( 'layout_columns', 'max' );
877 if ( ! $screen_layout_columns || 'auto' == $screen_layout_columns ) {
878 if ( $this->get_option( 'layout_columns', 'default' ) )
879 $screen_layout_columns = $this->get_option( 'layout_columns', 'default' );
883 <h5><?php _e('Screen Layout'); ?></h5>
884 <div class='columns-prefs'><?php
885 _e('Number of Columns:');
886 for ( $i = 1; $i <= $num; ++$i ):
889 <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>'
890 <?php checked( $screen_layout_columns, $i ); ?> />
891 <?php echo esc_html( $i ); ?>
900 * Render the items per page option
904 function render_per_page_options() {
905 if ( ! $this->get_option( 'per_page' ) )
908 $per_page_label = $this->get_option( 'per_page', 'label' );
910 $option = $this->get_option( 'per_page', 'option' );
912 $option = str_replace( '-', '_', "{$this->id}_per_page" );
914 $per_page = (int) get_user_option( $option );
915 if ( empty( $per_page ) || $per_page < 1 ) {
916 $per_page = $this->get_option( 'per_page', 'default' );
921 if ( 'edit_comments_per_page' == $option ) {
922 $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
923 $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
924 } elseif ( 'categories_per_page' == $option ) {
925 $per_page = apply_filters( 'edit_categories_per_page', $per_page );
927 $per_page = apply_filters( $option, $per_page );
931 if ( isset( $this->post_type ) )
932 $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
935 <h5><?php _ex('Show on screen', 'Screen Options') ?></h5>
936 <div class='screen-options'>
937 <?php if ( !empty($per_page_label) ): ?>
938 <input type='text' class='screen-per-page' name='wp_screen_options[value]'
939 id='<?php echo esc_attr( $option ); ?>' maxlength='3'
940 value='<?php echo esc_attr( $per_page ); ?>' />
941 <label for='<?php echo esc_attr( $option ); ?>'>
942 <?php echo esc_html( $per_page_label ); ?>
946 echo get_submit_button( __( 'Apply' ), 'button', 'screen-options-apply', false ); ?>
947 <input type='hidden' name='wp_screen_options[option]' value='<?php echo esc_attr($option); ?>' />