class-wp-screen.php 35 KB

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