Geen omschrijving

class-wp-screen.php 36KB

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