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