/wp-admin/includes/class-wp-screen.php

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