class-wp-screen.php 34 KB

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