You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1256 lines
34 KiB

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