class-wp-screen.php 36 KB

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