home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / includes / class-wp-screen.php < prev    next >
Encoding:
PHP Script  |  2017-10-23  |  33.5 KB  |  1,243 lines

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