Brak opisu

class-wp-screen.php 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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 $taxnow
  361. * @global string $typenow
  362. */
  363. public function set_current_screen() {
  364. global $current_screen, $taxnow, $typenow;
  365. $current_screen = $this;
  366. $taxnow = $this->taxonomy;
  367. $typenow = $this->post_type;
  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. * This filter is currently only used on the Widgets screen to enable
  906. * accessibility mode.
  907. *
  908. * @since 3.0.0
  909. *
  910. * @param string $screen_settings Screen settings.
  911. * @param WP_Screen $this WP_Screen object.
  912. */
  913. $this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this );
  914. if ( $this->_screen_settings || $this->_options ) {
  915. $show_screen = true;
  916. }
  917. /**
  918. * Filters whether to show the Screen Options tab.
  919. *
  920. * @since 3.2.0
  921. *
  922. * @param bool $show_screen Whether to show Screen Options tab.
  923. * Default true.
  924. * @param WP_Screen $this Current WP_Screen instance.
  925. */
  926. $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
  927. return $this->_show_screen_options;
  928. }
  929. /**
  930. * Render the screen options tab.
  931. *
  932. * @since 3.3.0
  933. *
  934. * @param array $options {
  935. * Options for the tab.
  936. *
  937. * @type bool $wrap Whether the screen-options-wrap div will be included. Defaults to true.
  938. * }
  939. */
  940. public function render_screen_options( $options = array() ) {
  941. $options = wp_parse_args(
  942. $options,
  943. array(
  944. 'wrap' => true,
  945. )
  946. );
  947. $wrapper_start = '';
  948. $wrapper_end = '';
  949. $form_start = '';
  950. $form_end = '';
  951. // Output optional wrapper.
  952. if ( $options['wrap'] ) {
  953. $wrapper_start = '<div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="' . esc_attr__( 'Screen Options Tab' ) . '">';
  954. $wrapper_end = '</div>';
  955. }
  956. // Don't output the form and nonce for the widgets accessibility mode links.
  957. if ( 'widgets' !== $this->base ) {
  958. $form_start = "\n<form id='adv-settings' method='post'>\n";
  959. $form_end = "\n" . wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false, false ) . "\n</form>\n";
  960. }
  961. echo $wrapper_start . $form_start;
  962. $this->render_meta_boxes_preferences();
  963. $this->render_list_table_columns_preferences();
  964. $this->render_screen_layout();
  965. $this->render_per_page_options();
  966. $this->render_view_mode();
  967. echo $this->_screen_settings;
  968. /**
  969. * Filters whether to show the Screen Options submit button.
  970. *
  971. * @since 4.4.0
  972. *
  973. * @param bool $show_button Whether to show Screen Options submit button.
  974. * Default false.
  975. * @param WP_Screen $screen Current WP_Screen instance.
  976. */
  977. $show_button = apply_filters( 'screen_options_show_submit', false, $this );
  978. if ( $show_button ) {
  979. submit_button( __( 'Apply' ), 'primary', 'screen-options-apply', true );
  980. }
  981. echo $form_end . $wrapper_end;
  982. }
  983. /**
  984. * Render the meta boxes preferences.
  985. *
  986. * @since 4.4.0
  987. *
  988. * @global array $wp_meta_boxes
  989. */
  990. public function render_meta_boxes_preferences() {
  991. global $wp_meta_boxes;
  992. if ( ! isset( $wp_meta_boxes[ $this->id ] ) ) {
  993. return;
  994. }
  995. ?>
  996. <fieldset class="metabox-prefs">
  997. <legend><?php _e( 'Screen elements' ); ?></legend>
  998. <p>
  999. <?php _e( 'Some screen elements can be shown or hidden by using the checkboxes.' ); ?>
  1000. <?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.' ); ?>
  1001. </p>
  1002. <?php
  1003. meta_box_prefs( $this );
  1004. if ( 'dashboard' === $this->id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) {
  1005. if ( isset( $_GET['welcome'] ) ) {
  1006. $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
  1007. update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
  1008. } else {
  1009. $welcome_checked = (int) get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
  1010. if ( 2 === $welcome_checked && wp_get_current_user()->user_email !== get_option( 'admin_email' ) ) {
  1011. $welcome_checked = false;
  1012. }
  1013. }
  1014. echo '<label for="wp_welcome_panel-hide">';
  1015. echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />';
  1016. echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n";
  1017. }
  1018. ?>
  1019. </fieldset>
  1020. <?php
  1021. }
  1022. /**
  1023. * Render the list table columns preferences.
  1024. *
  1025. * @since 4.4.0
  1026. */
  1027. public function render_list_table_columns_preferences() {
  1028. $columns = get_column_headers( $this );
  1029. $hidden = get_hidden_columns( $this );
  1030. if ( ! $columns ) {
  1031. return;
  1032. }
  1033. $legend = ! empty( $columns['_title'] ) ? $columns['_title'] : __( 'Columns' );
  1034. ?>
  1035. <fieldset class="metabox-prefs">
  1036. <legend><?php echo $legend; ?></legend>
  1037. <?php
  1038. $special = array( '_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname' );
  1039. foreach ( $columns as $column => $title ) {
  1040. // Can't hide these for they are special.
  1041. if ( in_array( $column, $special, true ) ) {
  1042. continue;
  1043. }
  1044. if ( empty( $title ) ) {
  1045. continue;
  1046. }
  1047. /*
  1048. * The Comments column uses HTML in the display name with some screen
  1049. * reader text. Make sure to strip tags from the Comments column
  1050. * title and any other custom column title plugins might add.
  1051. */
  1052. $title = wp_strip_all_tags( $title );
  1053. $id = "$column-hide";
  1054. echo '<label>';
  1055. echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( ! in_array( $column, $hidden, true ), true, false ) . ' />';
  1056. echo "$title</label>\n";
  1057. }
  1058. ?>
  1059. </fieldset>
  1060. <?php
  1061. }
  1062. /**
  1063. * Render the option for number of columns on the page
  1064. *
  1065. * @since 3.3.0
  1066. */
  1067. public function render_screen_layout() {
  1068. if ( ! $this->get_option( 'layout_columns' ) ) {
  1069. return;
  1070. }
  1071. $screen_layout_columns = $this->get_columns();
  1072. $num = $this->get_option( 'layout_columns', 'max' );
  1073. ?>
  1074. <fieldset class='columns-prefs'>
  1075. <legend class="screen-layout"><?php _e( 'Layout' ); ?></legend>
  1076. <?php for ( $i = 1; $i <= $num; ++$i ) : ?>
  1077. <label class="columns-prefs-<?php echo $i; ?>">
  1078. <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>' <?php checked( $screen_layout_columns, $i ); ?> />
  1079. <?php
  1080. printf(
  1081. /* translators: %s: Number of columns on the page. */
  1082. _n( '%s column', '%s columns', $i ),
  1083. number_format_i18n( $i )
  1084. );
  1085. ?>
  1086. </label>
  1087. <?php endfor; ?>
  1088. </fieldset>
  1089. <?php
  1090. }
  1091. /**
  1092. * Render the items per page option
  1093. *
  1094. * @since 3.3.0
  1095. */
  1096. public function render_per_page_options() {
  1097. if ( null === $this->get_option( 'per_page' ) ) {
  1098. return;
  1099. }
  1100. $per_page_label = $this->get_option( 'per_page', 'label' );
  1101. if ( null === $per_page_label ) {
  1102. $per_page_label = __( 'Number of items per page:' );
  1103. }
  1104. $option = $this->get_option( 'per_page', 'option' );
  1105. if ( ! $option ) {
  1106. $option = str_replace( '-', '_', "{$this->id}_per_page" );
  1107. }
  1108. $per_page = (int) get_user_option( $option );
  1109. if ( empty( $per_page ) || $per_page < 1 ) {
  1110. $per_page = $this->get_option( 'per_page', 'default' );
  1111. if ( ! $per_page ) {
  1112. $per_page = 20;
  1113. }
  1114. }
  1115. if ( 'edit_comments_per_page' === $option ) {
  1116. $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
  1117. /** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
  1118. $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
  1119. } elseif ( 'categories_per_page' === $option ) {
  1120. /** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */
  1121. $per_page = apply_filters( 'edit_categories_per_page', $per_page );
  1122. } else {
  1123. /** This filter is documented in wp-admin/includes/class-wp-list-table.php */
  1124. $per_page = apply_filters( "{$option}", $per_page );
  1125. }
  1126. // Back compat.
  1127. if ( isset( $this->post_type ) ) {
  1128. /** This filter is documented in wp-admin/includes/post.php */
  1129. $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
  1130. }
  1131. // This needs a submit button.
  1132. add_filter( 'screen_options_show_submit', '__return_true' );
  1133. ?>
  1134. <fieldset class="screen-options">
  1135. <legend><?php _e( 'Pagination' ); ?></legend>
  1136. <?php if ( $per_page_label ) : ?>
  1137. <label for="<?php echo esc_attr( $option ); ?>"><?php echo $per_page_label; ?></label>
  1138. <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]"
  1139. id="<?php echo esc_attr( $option ); ?>" maxlength="3"
  1140. value="<?php echo esc_attr( $per_page ); ?>" />
  1141. <?php endif; ?>
  1142. <input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" />
  1143. </fieldset>
  1144. <?php
  1145. }
  1146. /**
  1147. * Render the list table view mode preferences.
  1148. *
  1149. * @since 4.4.0
  1150. *
  1151. * @global string $mode List table view mode.
  1152. */
  1153. public function render_view_mode() {
  1154. global $mode;
  1155. $screen = get_current_screen();
  1156. // Currently only enabled for posts and comments lists.
  1157. if ( 'edit' !== $screen->base && 'edit-comments' !== $screen->base ) {
  1158. return;
  1159. }
  1160. $view_mode_post_types = get_post_types( array( 'show_ui' => true ) );
  1161. /**
  1162. * Filters the post types that have different view mode options.
  1163. *
  1164. * @since 4.4.0
  1165. *
  1166. * @param string[] $view_mode_post_types Array of post types that can change view modes.
  1167. * Default post types with show_ui on.
  1168. */
  1169. $view_mode_post_types = apply_filters( 'view_mode_post_types', $view_mode_post_types );
  1170. if ( 'edit' === $screen->base && ! in_array( $this->post_type, $view_mode_post_types, true ) ) {
  1171. return;
  1172. }
  1173. if ( ! isset( $mode ) ) {
  1174. $mode = get_user_setting( 'posts_list_mode', 'list' );
  1175. }
  1176. // This needs a submit button.
  1177. add_filter( 'screen_options_show_submit', '__return_true' );
  1178. ?>
  1179. <fieldset class="metabox-prefs view-mode">
  1180. <legend><?php _e( 'View mode' ); ?></legend>
  1181. <label for="list-view-mode">
  1182. <input id="list-view-mode" type="radio" name="mode" value="list" <?php checked( 'list', $mode ); ?> />
  1183. <?php _e( 'Compact view' ); ?>
  1184. </label>
  1185. <label for="excerpt-view-mode">
  1186. <input id="excerpt-view-mode" type="radio" name="mode" value="excerpt" <?php checked( 'excerpt', $mode ); ?> />
  1187. <?php _e( 'Extended view' ); ?>
  1188. </label>
  1189. </fieldset>
  1190. <?php
  1191. }
  1192. /**
  1193. * Render screen reader text.
  1194. *
  1195. * @since 4.4.0
  1196. *
  1197. * @param string $key The screen reader text array named key.
  1198. * @param string $tag Optional. The HTML tag to wrap the screen reader text. Default h2.
  1199. */
  1200. public function render_screen_reader_content( $key = '', $tag = 'h2' ) {
  1201. if ( ! isset( $this->_screen_reader_content[ $key ] ) ) {
  1202. return;
  1203. }
  1204. echo "<$tag class='screen-reader-text'>" . $this->_screen_reader_content[ $key ] . "</$tag>";
  1205. }
  1206. }