PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/screen.php

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