class-wp-screen.php 34 KB

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