Aucune description

class-wp-list-table.php 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. <?php
  2. /**
  3. * Administration API: WP_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Base class for displaying a list of items in an ajaxified HTML table.
  11. *
  12. * @since 3.1.0
  13. * @access private
  14. */
  15. class WP_List_Table {
  16. /**
  17. * The current list of items.
  18. *
  19. * @since 3.1.0
  20. * @var array
  21. */
  22. public $items;
  23. /**
  24. * Various information about the current table.
  25. *
  26. * @since 3.1.0
  27. * @var array
  28. */
  29. protected $_args;
  30. /**
  31. * Various information needed for displaying the pagination.
  32. *
  33. * @since 3.1.0
  34. * @var array
  35. */
  36. protected $_pagination_args = array();
  37. /**
  38. * The current screen.
  39. *
  40. * @since 3.1.0
  41. * @var WP_Screen
  42. */
  43. protected $screen;
  44. /**
  45. * Cached bulk actions.
  46. *
  47. * @since 3.1.0
  48. * @var array
  49. */
  50. private $_actions;
  51. /**
  52. * Cached pagination output.
  53. *
  54. * @since 3.1.0
  55. * @var string
  56. */
  57. private $_pagination;
  58. /**
  59. * The view switcher modes.
  60. *
  61. * @since 4.1.0
  62. * @var array
  63. */
  64. protected $modes = array();
  65. /**
  66. * Stores the value returned by ->get_column_info().
  67. *
  68. * @since 4.1.0
  69. * @var array
  70. */
  71. protected $_column_headers;
  72. /**
  73. * {@internal Missing Summary}
  74. *
  75. * @var array
  76. */
  77. protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' );
  78. /**
  79. * {@internal Missing Summary}
  80. *
  81. * @var array
  82. */
  83. protected $compat_methods = array(
  84. 'set_pagination_args',
  85. 'get_views',
  86. 'get_bulk_actions',
  87. 'bulk_actions',
  88. 'row_actions',
  89. 'months_dropdown',
  90. 'view_switcher',
  91. 'comments_bubble',
  92. 'get_items_per_page',
  93. 'pagination',
  94. 'get_sortable_columns',
  95. 'get_column_info',
  96. 'get_table_classes',
  97. 'display_tablenav',
  98. 'extra_tablenav',
  99. 'single_row_columns',
  100. );
  101. /**
  102. * Constructor.
  103. *
  104. * The child class should call this constructor from its own constructor to override
  105. * the default $args.
  106. *
  107. * @since 3.1.0
  108. *
  109. * @param array|string $args {
  110. * Array or string of arguments.
  111. *
  112. * @type string $plural Plural value used for labels and the objects being listed.
  113. * This affects things such as CSS class-names and nonces used
  114. * in the list table, e.g. 'posts'. Default empty.
  115. * @type string $singular Singular label for an object being listed, e.g. 'post'.
  116. * Default empty
  117. * @type bool $ajax Whether the list table supports Ajax. This includes loading
  118. * and sorting data, for example. If true, the class will call
  119. * the _js_vars() method in the footer to provide variables
  120. * to any scripts handling Ajax events. Default false.
  121. * @type string $screen String containing the hook name used to determine the current
  122. * screen. If left null, the current screen will be automatically set.
  123. * Default null.
  124. * }
  125. */
  126. public function __construct( $args = array() ) {
  127. $args = wp_parse_args(
  128. $args,
  129. array(
  130. 'plural' => '',
  131. 'singular' => '',
  132. 'ajax' => false,
  133. 'screen' => null,
  134. )
  135. );
  136. $this->screen = convert_to_screen( $args['screen'] );
  137. add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
  138. if ( ! $args['plural'] ) {
  139. $args['plural'] = $this->screen->base;
  140. }
  141. $args['plural'] = sanitize_key( $args['plural'] );
  142. $args['singular'] = sanitize_key( $args['singular'] );
  143. $this->_args = $args;
  144. if ( $args['ajax'] ) {
  145. // wp_enqueue_script( 'list-table' );
  146. add_action( 'admin_footer', array( $this, '_js_vars' ) );
  147. }
  148. if ( empty( $this->modes ) ) {
  149. $this->modes = array(
  150. 'list' => __( 'Compact view' ),
  151. 'excerpt' => __( 'Extended view' ),
  152. );
  153. }
  154. }
  155. /**
  156. * Make private properties readable for backward compatibility.
  157. *
  158. * @since 4.0.0
  159. *
  160. * @param string $name Property to get.
  161. * @return mixed Property.
  162. */
  163. public function __get( $name ) {
  164. if ( in_array( $name, $this->compat_fields, true ) ) {
  165. return $this->$name;
  166. }
  167. }
  168. /**
  169. * Make private properties settable for backward compatibility.
  170. *
  171. * @since 4.0.0
  172. *
  173. * @param string $name Property to check if set.
  174. * @param mixed $value Property value.
  175. * @return mixed Newly-set property.
  176. */
  177. public function __set( $name, $value ) {
  178. if ( in_array( $name, $this->compat_fields, true ) ) {
  179. return $this->$name = $value;
  180. }
  181. }
  182. /**
  183. * Make private properties checkable for backward compatibility.
  184. *
  185. * @since 4.0.0
  186. *
  187. * @param string $name Property to check if set.
  188. * @return bool Whether the property is set.
  189. */
  190. public function __isset( $name ) {
  191. if ( in_array( $name, $this->compat_fields, true ) ) {
  192. return isset( $this->$name );
  193. }
  194. }
  195. /**
  196. * Make private properties un-settable for backward compatibility.
  197. *
  198. * @since 4.0.0
  199. *
  200. * @param string $name Property to unset.
  201. */
  202. public function __unset( $name ) {
  203. if ( in_array( $name, $this->compat_fields, true ) ) {
  204. unset( $this->$name );
  205. }
  206. }
  207. /**
  208. * Make private/protected methods readable for backward compatibility.
  209. *
  210. * @since 4.0.0
  211. *
  212. * @param string $name Method to call.
  213. * @param array $arguments Arguments to pass when calling.
  214. * @return mixed|bool Return value of the callback, false otherwise.
  215. */
  216. public function __call( $name, $arguments ) {
  217. if ( in_array( $name, $this->compat_methods, true ) ) {
  218. return $this->$name( ...$arguments );
  219. }
  220. return false;
  221. }
  222. /**
  223. * Checks the current user's permissions
  224. *
  225. * @since 3.1.0
  226. * @abstract
  227. */
  228. public function ajax_user_can() {
  229. die( 'function WP_List_Table::ajax_user_can() must be overridden in a subclass.' );
  230. }
  231. /**
  232. * Prepares the list of items for displaying.
  233. *
  234. * @uses WP_List_Table::set_pagination_args()
  235. *
  236. * @since 3.1.0
  237. * @abstract
  238. */
  239. public function prepare_items() {
  240. die( 'function WP_List_Table::prepare_items() must be overridden in a subclass.' );
  241. }
  242. /**
  243. * An internal method that sets all the necessary pagination arguments
  244. *
  245. * @since 3.1.0
  246. *
  247. * @param array|string $args Array or string of arguments with information about the pagination.
  248. */
  249. protected function set_pagination_args( $args ) {
  250. $args = wp_parse_args(
  251. $args,
  252. array(
  253. 'total_items' => 0,
  254. 'total_pages' => 0,
  255. 'per_page' => 0,
  256. )
  257. );
  258. if ( ! $args['total_pages'] && $args['per_page'] > 0 ) {
  259. $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
  260. }
  261. // Redirect if page number is invalid and headers are not already sent.
  262. if ( ! headers_sent() && ! wp_doing_ajax() && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
  263. wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
  264. exit;
  265. }
  266. $this->_pagination_args = $args;
  267. }
  268. /**
  269. * Access the pagination args.
  270. *
  271. * @since 3.1.0
  272. *
  273. * @param string $key Pagination argument to retrieve. Common values include 'total_items',
  274. * 'total_pages', 'per_page', or 'infinite_scroll'.
  275. * @return int Number of items that correspond to the given pagination argument.
  276. */
  277. public function get_pagination_arg( $key ) {
  278. if ( 'page' === $key ) {
  279. return $this->get_pagenum();
  280. }
  281. if ( isset( $this->_pagination_args[ $key ] ) ) {
  282. return $this->_pagination_args[ $key ];
  283. }
  284. }
  285. /**
  286. * Whether the table has items to display or not
  287. *
  288. * @since 3.1.0
  289. *
  290. * @return bool
  291. */
  292. public function has_items() {
  293. return ! empty( $this->items );
  294. }
  295. /**
  296. * Message to be displayed when there are no items
  297. *
  298. * @since 3.1.0
  299. */
  300. public function no_items() {
  301. _e( 'No items found.' );
  302. }
  303. /**
  304. * Displays the search box.
  305. *
  306. * @since 3.1.0
  307. *
  308. * @param string $text The 'submit' button label.
  309. * @param string $input_id ID attribute value for the search input field.
  310. */
  311. public function search_box( $text, $input_id ) {
  312. if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
  313. return;
  314. }
  315. $input_id = $input_id . '-search-input';
  316. if ( ! empty( $_REQUEST['orderby'] ) ) {
  317. echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
  318. }
  319. if ( ! empty( $_REQUEST['order'] ) ) {
  320. echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
  321. }
  322. if ( ! empty( $_REQUEST['post_mime_type'] ) ) {
  323. echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
  324. }
  325. if ( ! empty( $_REQUEST['detached'] ) ) {
  326. echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
  327. }
  328. ?>
  329. <p class="search-box">
  330. <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
  331. <input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" />
  332. <?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
  333. </p>
  334. <?php
  335. }
  336. /**
  337. * Gets the list of views available on this table.
  338. *
  339. * The format is an associative array:
  340. * - `'id' => 'link'`
  341. *
  342. * @since 3.1.0
  343. *
  344. * @return array
  345. */
  346. protected function get_views() {
  347. return array();
  348. }
  349. /**
  350. * Displays the list of views available on this table.
  351. *
  352. * @since 3.1.0
  353. */
  354. public function views() {
  355. $views = $this->get_views();
  356. /**
  357. * Filters the list of available list table views.
  358. *
  359. * The dynamic portion of the hook name, `$this->screen->id`, refers
  360. * to the ID of the current screen.
  361. *
  362. * @since 3.1.0
  363. *
  364. * @param string[] $views An array of available list table views.
  365. */
  366. $views = apply_filters( "views_{$this->screen->id}", $views );
  367. if ( empty( $views ) ) {
  368. return;
  369. }
  370. $this->screen->render_screen_reader_content( 'heading_views' );
  371. echo "<ul class='subsubsub'>\n";
  372. foreach ( $views as $class => $view ) {
  373. $views[ $class ] = "\t<li class='$class'>$view";
  374. }
  375. echo implode( " |</li>\n", $views ) . "</li>\n";
  376. echo '</ul>';
  377. }
  378. /**
  379. * Retrieves the list of bulk actions available for this table.
  380. *
  381. * The format is an associative array where each element represents either a top level option value and label, or
  382. * an array representing an optgroup and its options.
  383. *
  384. * For a standard option, the array element key is the field value and the array element value is the field label.
  385. *
  386. * For an optgroup, the array element key is the label and the array element value is an associative array of
  387. * options as above.
  388. *
  389. * Example:
  390. *
  391. * [
  392. * 'edit' => 'Edit',
  393. * 'delete' => 'Delete',
  394. * 'Change State' => [
  395. * 'feature' => 'Featured',
  396. * 'sale' => 'On Sale',
  397. * ]
  398. * ]
  399. *
  400. * @since 3.1.0
  401. * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
  402. *
  403. * @return array
  404. */
  405. protected function get_bulk_actions() {
  406. return array();
  407. }
  408. /**
  409. * Displays the bulk actions dropdown.
  410. *
  411. * @since 3.1.0
  412. *
  413. * @param string $which The location of the bulk actions: 'top' or 'bottom'.
  414. * This is designated as optional for backward compatibility.
  415. */
  416. protected function bulk_actions( $which = '' ) {
  417. if ( is_null( $this->_actions ) ) {
  418. $this->_actions = $this->get_bulk_actions();
  419. /**
  420. * Filters the items in the bulk actions menu of the list table.
  421. *
  422. * The dynamic portion of the hook name, `$this->screen->id`, refers
  423. * to the ID of the current screen.
  424. *
  425. * @since 3.1.0
  426. * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
  427. *
  428. * @param array $actions An array of the available bulk actions.
  429. */
  430. $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  431. $two = '';
  432. } else {
  433. $two = '2';
  434. }
  435. if ( empty( $this->_actions ) ) {
  436. return;
  437. }
  438. echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
  439. echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">\n";
  440. echo '<option value="-1">' . __( 'Bulk actions' ) . "</option>\n";
  441. foreach ( $this->_actions as $key => $value ) {
  442. if ( is_array( $value ) ) {
  443. echo "\t" . '<optgroup label="' . esc_attr( $key ) . '">' . "\n";
  444. foreach ( $value as $name => $title ) {
  445. $class = ( 'edit' === $name ) ? ' class="hide-if-no-js"' : '';
  446. echo "\t\t" . '<option value="' . esc_attr( $name ) . '"' . $class . '>' . $title . "</option>\n";
  447. }
  448. echo "\t" . "</optgroup>\n";
  449. } else {
  450. $class = ( 'edit' === $key ) ? ' class="hide-if-no-js"' : '';
  451. echo "\t" . '<option value="' . esc_attr( $key ) . '"' . $class . '>' . $value . "</option>\n";
  452. }
  453. }
  454. echo "</select>\n";
  455. submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
  456. echo "\n";
  457. }
  458. /**
  459. * Gets the current action selected from the bulk actions dropdown.
  460. *
  461. * @since 3.1.0
  462. *
  463. * @return string|false The action name. False if no action was selected.
  464. */
  465. public function current_action() {
  466. if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) {
  467. return false;
  468. }
  469. if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) {
  470. return $_REQUEST['action'];
  471. }
  472. return false;
  473. }
  474. /**
  475. * Generates the required HTML for a list of row action links.
  476. *
  477. * @since 3.1.0
  478. *
  479. * @param string[] $actions An array of action links.
  480. * @param bool $always_visible Whether the actions should be always visible.
  481. * @return string The HTML for the row actions.
  482. */
  483. protected function row_actions( $actions, $always_visible = false ) {
  484. $action_count = count( $actions );
  485. if ( ! $action_count ) {
  486. return '';
  487. }
  488. $mode = get_user_setting( 'posts_list_mode', 'list' );
  489. if ( 'excerpt' === $mode ) {
  490. $always_visible = true;
  491. }
  492. $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
  493. $i = 0;
  494. foreach ( $actions as $action => $link ) {
  495. ++$i;
  496. $sep = ( $i < $action_count ) ? ' | ' : '';
  497. $out .= "<span class='$action'>$link$sep</span>";
  498. }
  499. $out .= '</div>';
  500. $out .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
  501. return $out;
  502. }
  503. /**
  504. * Displays a dropdown for filtering items in the list table by month.
  505. *
  506. * @since 3.1.0
  507. *
  508. * @global wpdb $wpdb WordPress database abstraction object.
  509. * @global WP_Locale $wp_locale WordPress date and time locale object.
  510. *
  511. * @param string $post_type The post type.
  512. */
  513. protected function months_dropdown( $post_type ) {
  514. global $wpdb, $wp_locale;
  515. /**
  516. * Filters whether to remove the 'Months' drop-down from the post list table.
  517. *
  518. * @since 4.2.0
  519. *
  520. * @param bool $disable Whether to disable the drop-down. Default false.
  521. * @param string $post_type The post type.
  522. */
  523. if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) {
  524. return;
  525. }
  526. /**
  527. * Filters to short-circuit performing the months dropdown query.
  528. *
  529. * @since 5.7.0
  530. *
  531. * @param object[]|false $months 'Months' drop-down results. Default false.
  532. * @param string $post_type The post type.
  533. */
  534. $months = apply_filters( 'pre_months_dropdown_query', false, $post_type );
  535. if ( ! is_array( $months ) ) {
  536. $extra_checks = "AND post_status != 'auto-draft'";
  537. if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) {
  538. $extra_checks .= " AND post_status != 'trash'";
  539. } elseif ( isset( $_GET['post_status'] ) ) {
  540. $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] );
  541. }
  542. $months = $wpdb->get_results(
  543. $wpdb->prepare(
  544. "
  545. SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
  546. FROM $wpdb->posts
  547. WHERE post_type = %s
  548. $extra_checks
  549. ORDER BY post_date DESC
  550. ",
  551. $post_type
  552. )
  553. );
  554. }
  555. /**
  556. * Filters the 'Months' drop-down results.
  557. *
  558. * @since 3.7.0
  559. *
  560. * @param object[] $months Array of the months drop-down query results.
  561. * @param string $post_type The post type.
  562. */
  563. $months = apply_filters( 'months_dropdown_results', $months, $post_type );
  564. $month_count = count( $months );
  565. if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) {
  566. return;
  567. }
  568. $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
  569. ?>
  570. <label for="filter-by-date" class="screen-reader-text"><?php echo get_post_type_object( $post_type )->labels->filter_by_date; ?></label>
  571. <select name="m" id="filter-by-date">
  572. <option<?php selected( $m, 0 ); ?> value="0"><?php _e( 'All dates' ); ?></option>
  573. <?php
  574. foreach ( $months as $arc_row ) {
  575. if ( 0 == $arc_row->year ) {
  576. continue;
  577. }
  578. $month = zeroise( $arc_row->month, 2 );
  579. $year = $arc_row->year;
  580. printf(
  581. "<option %s value='%s'>%s</option>\n",
  582. selected( $m, $year . $month, false ),
  583. esc_attr( $arc_row->year . $month ),
  584. /* translators: 1: Month name, 2: 4-digit year. */
  585. sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
  586. );
  587. }
  588. ?>
  589. </select>
  590. <?php
  591. }
  592. /**
  593. * Displays a view switcher.
  594. *
  595. * @since 3.1.0
  596. *
  597. * @param string $current_mode
  598. */
  599. protected function view_switcher( $current_mode ) {
  600. ?>
  601. <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
  602. <div class="view-switch">
  603. <?php
  604. foreach ( $this->modes as $mode => $title ) {
  605. $classes = array( 'view-' . $mode );
  606. $aria_current = '';
  607. if ( $current_mode === $mode ) {
  608. $classes[] = 'current';
  609. $aria_current = ' aria-current="page"';
  610. }
  611. printf(
  612. "<a href='%s' class='%s' id='view-switch-$mode'$aria_current><span class='screen-reader-text'>%s</span></a>\n",
  613. esc_url( remove_query_arg( 'attachment-filter', add_query_arg( 'mode', $mode ) ) ),
  614. implode( ' ', $classes ),
  615. $title
  616. );
  617. }
  618. ?>
  619. </div>
  620. <?php
  621. }
  622. /**
  623. * Displays a comment count bubble.
  624. *
  625. * @since 3.1.0
  626. *
  627. * @param int $post_id The post ID.
  628. * @param int $pending_comments Number of pending comments.
  629. */
  630. protected function comments_bubble( $post_id, $pending_comments ) {
  631. $approved_comments = get_comments_number();
  632. $approved_comments_number = number_format_i18n( $approved_comments );
  633. $pending_comments_number = number_format_i18n( $pending_comments );
  634. $approved_only_phrase = sprintf(
  635. /* translators: %s: Number of comments. */
  636. _n( '%s comment', '%s comments', $approved_comments ),
  637. $approved_comments_number
  638. );
  639. $approved_phrase = sprintf(
  640. /* translators: %s: Number of comments. */
  641. _n( '%s approved comment', '%s approved comments', $approved_comments ),
  642. $approved_comments_number
  643. );
  644. $pending_phrase = sprintf(
  645. /* translators: %s: Number of comments. */
  646. _n( '%s pending comment', '%s pending comments', $pending_comments ),
  647. $pending_comments_number
  648. );
  649. if ( ! $approved_comments && ! $pending_comments ) {
  650. // No comments at all.
  651. printf(
  652. '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">%s</span>',
  653. __( 'No comments' )
  654. );
  655. } elseif ( $approved_comments && 'trash' === get_post_status( $post_id ) ) {
  656. // Don't link the comment bubble for a trashed post.
  657. printf(
  658. '<span class="post-com-count post-com-count-approved"><span class="comment-count-approved" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
  659. $approved_comments_number,
  660. $pending_comments ? $approved_phrase : $approved_only_phrase
  661. );
  662. } elseif ( $approved_comments ) {
  663. // Link the comment bubble to approved comments.
  664. printf(
  665. '<a href="%s" class="post-com-count post-com-count-approved"><span class="comment-count-approved" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  666. esc_url(
  667. add_query_arg(
  668. array(
  669. 'p' => $post_id,
  670. 'comment_status' => 'approved',
  671. ),
  672. admin_url( 'edit-comments.php' )
  673. )
  674. ),
  675. $approved_comments_number,
  676. $pending_comments ? $approved_phrase : $approved_only_phrase
  677. );
  678. } else {
  679. // Don't link the comment bubble when there are no approved comments.
  680. printf(
  681. '<span class="post-com-count post-com-count-no-comments"><span class="comment-count comment-count-no-comments" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
  682. $approved_comments_number,
  683. $pending_comments ? __( 'No approved comments' ) : __( 'No comments' )
  684. );
  685. }
  686. if ( $pending_comments ) {
  687. printf(
  688. '<a href="%s" class="post-com-count post-com-count-pending"><span class="comment-count-pending" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  689. esc_url(
  690. add_query_arg(
  691. array(
  692. 'p' => $post_id,
  693. 'comment_status' => 'moderated',
  694. ),
  695. admin_url( 'edit-comments.php' )
  696. )
  697. ),
  698. $pending_comments_number,
  699. $pending_phrase
  700. );
  701. } else {
  702. printf(
  703. '<span class="post-com-count post-com-count-pending post-com-count-no-pending"><span class="comment-count comment-count-no-pending" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
  704. $pending_comments_number,
  705. $approved_comments ? __( 'No pending comments' ) : __( 'No comments' )
  706. );
  707. }
  708. }
  709. /**
  710. * Gets the current page number.
  711. *
  712. * @since 3.1.0
  713. *
  714. * @return int
  715. */
  716. public function get_pagenum() {
  717. $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
  718. if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) {
  719. $pagenum = $this->_pagination_args['total_pages'];
  720. }
  721. return max( 1, $pagenum );
  722. }
  723. /**
  724. * Gets the number of items to display on a single page.
  725. *
  726. * @since 3.1.0
  727. *
  728. * @param string $option
  729. * @param int $default
  730. * @return int
  731. */
  732. protected function get_items_per_page( $option, $default = 20 ) {
  733. $per_page = (int) get_user_option( $option );
  734. if ( empty( $per_page ) || $per_page < 1 ) {
  735. $per_page = $default;
  736. }
  737. /**
  738. * Filters the number of items to be displayed on each page of the list table.
  739. *
  740. * The dynamic hook name, `$option`, refers to the `per_page` option depending
  741. * on the type of list table in use. Possible filter names include:
  742. *
  743. * - `edit_comments_per_page`
  744. * - `sites_network_per_page`
  745. * - `site_themes_network_per_page`
  746. * - `themes_network_per_page'`
  747. * - `users_network_per_page`
  748. * - `edit_post_per_page`
  749. * - `edit_page_per_page'`
  750. * - `edit_{$post_type}_per_page`
  751. * - `edit_post_tag_per_page`
  752. * - `edit_category_per_page`
  753. * - `edit_{$taxonomy}_per_page`
  754. * - `site_users_network_per_page`
  755. * - `users_per_page`
  756. *
  757. * @since 2.9.0
  758. *
  759. * @param int $per_page Number of items to be displayed. Default 20.
  760. */
  761. return (int) apply_filters( "{$option}", $per_page );
  762. }
  763. /**
  764. * Displays the pagination.
  765. *
  766. * @since 3.1.0
  767. *
  768. * @param string $which
  769. */
  770. protected function pagination( $which ) {
  771. if ( empty( $this->_pagination_args ) ) {
  772. return;
  773. }
  774. $total_items = $this->_pagination_args['total_items'];
  775. $total_pages = $this->_pagination_args['total_pages'];
  776. $infinite_scroll = false;
  777. if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
  778. $infinite_scroll = $this->_pagination_args['infinite_scroll'];
  779. }
  780. if ( 'top' === $which && $total_pages > 1 ) {
  781. $this->screen->render_screen_reader_content( 'heading_pagination' );
  782. }
  783. $output = '<span class="displaying-num">' . sprintf(
  784. /* translators: %s: Number of items. */
  785. _n( '%s item', '%s items', $total_items ),
  786. number_format_i18n( $total_items )
  787. ) . '</span>';
  788. $current = $this->get_pagenum();
  789. $removable_query_args = wp_removable_query_args();
  790. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  791. $current_url = remove_query_arg( $removable_query_args, $current_url );
  792. $page_links = array();
  793. $total_pages_before = '<span class="paging-input">';
  794. $total_pages_after = '</span></span>';
  795. $disable_first = false;
  796. $disable_last = false;
  797. $disable_prev = false;
  798. $disable_next = false;
  799. if ( 1 == $current ) {
  800. $disable_first = true;
  801. $disable_prev = true;
  802. }
  803. if ( 2 == $current ) {
  804. $disable_first = true;
  805. }
  806. if ( $total_pages == $current ) {
  807. $disable_last = true;
  808. $disable_next = true;
  809. }
  810. if ( $total_pages - 1 == $current ) {
  811. $disable_last = true;
  812. }
  813. if ( $disable_first ) {
  814. $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
  815. } else {
  816. $page_links[] = sprintf(
  817. "<a class='first-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
  818. esc_url( remove_query_arg( 'paged', $current_url ) ),
  819. __( 'First page' ),
  820. '&laquo;'
  821. );
  822. }
  823. if ( $disable_prev ) {
  824. $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
  825. } else {
  826. $page_links[] = sprintf(
  827. "<a class='prev-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
  828. esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ) ),
  829. __( 'Previous page' ),
  830. '&lsaquo;'
  831. );
  832. }
  833. if ( 'bottom' === $which ) {
  834. $html_current_page = $current;
  835. $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">';
  836. } else {
  837. $html_current_page = sprintf(
  838. "%s<input class='current-page' id='current-page-selector' type='text' name='paged' value='%s' size='%d' aria-describedby='table-paging' /><span class='tablenav-paging-text'>",
  839. '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
  840. $current,
  841. strlen( $total_pages )
  842. );
  843. }
  844. $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
  845. $page_links[] = $total_pages_before . sprintf(
  846. /* translators: 1: Current page, 2: Total pages. */
  847. _x( '%1$s of %2$s', 'paging' ),
  848. $html_current_page,
  849. $html_total_pages
  850. ) . $total_pages_after;
  851. if ( $disable_next ) {
  852. $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
  853. } else {
  854. $page_links[] = sprintf(
  855. "<a class='next-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
  856. esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ) ),
  857. __( 'Next page' ),
  858. '&rsaquo;'
  859. );
  860. }
  861. if ( $disable_last ) {
  862. $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
  863. } else {
  864. $page_links[] = sprintf(
  865. "<a class='last-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
  866. esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
  867. __( 'Last page' ),
  868. '&raquo;'
  869. );
  870. }
  871. $pagination_links_class = 'pagination-links';
  872. if ( ! empty( $infinite_scroll ) ) {
  873. $pagination_links_class .= ' hide-if-js';
  874. }
  875. $output .= "\n<span class='$pagination_links_class'>" . implode( "\n", $page_links ) . '</span>';
  876. if ( $total_pages ) {
  877. $page_class = $total_pages < 2 ? ' one-page' : '';
  878. } else {
  879. $page_class = ' no-pages';
  880. }
  881. $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
  882. echo $this->_pagination;
  883. }
  884. /**
  885. * Gets a list of columns.
  886. *
  887. * The format is:
  888. * - `'internal-name' => 'Title'`
  889. *
  890. * @since 3.1.0
  891. * @abstract
  892. *
  893. * @return array
  894. */
  895. public function get_columns() {
  896. die( 'function WP_List_Table::get_columns() must be overridden in a subclass.' );
  897. }
  898. /**
  899. * Gets a list of sortable columns.
  900. *
  901. * The format is:
  902. * - `'internal-name' => 'orderby'`
  903. * - `'internal-name' => array( 'orderby', 'asc' )` - The second element sets the initial sorting order.
  904. * - `'internal-name' => array( 'orderby', true )` - The second element makes the initial order descending.
  905. *
  906. * @since 3.1.0
  907. *
  908. * @return array
  909. */
  910. protected function get_sortable_columns() {
  911. return array();
  912. }
  913. /**
  914. * Gets the name of the default primary column.
  915. *
  916. * @since 4.3.0
  917. *
  918. * @return string Name of the default primary column, in this case, an empty string.
  919. */
  920. protected function get_default_primary_column_name() {
  921. $columns = $this->get_columns();
  922. $column = '';
  923. if ( empty( $columns ) ) {
  924. return $column;
  925. }
  926. // We need a primary defined so responsive views show something,
  927. // so let's fall back to the first non-checkbox column.
  928. foreach ( $columns as $col => $column_name ) {
  929. if ( 'cb' === $col ) {
  930. continue;
  931. }
  932. $column = $col;
  933. break;
  934. }
  935. return $column;
  936. }
  937. /**
  938. * Public wrapper for WP_List_Table::get_default_primary_column_name().
  939. *
  940. * @since 4.4.0
  941. *
  942. * @return string Name of the default primary column.
  943. */
  944. public function get_primary_column() {
  945. return $this->get_primary_column_name();
  946. }
  947. /**
  948. * Gets the name of the primary column.
  949. *
  950. * @since 4.3.0
  951. *
  952. * @return string The name of the primary column.
  953. */
  954. protected function get_primary_column_name() {
  955. $columns = get_column_headers( $this->screen );
  956. $default = $this->get_default_primary_column_name();
  957. // If the primary column doesn't exist,
  958. // fall back to the first non-checkbox column.
  959. if ( ! isset( $columns[ $default ] ) ) {
  960. $default = self::get_default_primary_column_name();
  961. }
  962. /**
  963. * Filters the name of the primary column for the current list table.
  964. *
  965. * @since 4.3.0
  966. *
  967. * @param string $default Column name default for the specific list table, e.g. 'name'.
  968. * @param string $context Screen ID for specific list table, e.g. 'plugins'.
  969. */
  970. $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
  971. if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
  972. $column = $default;
  973. }
  974. return $column;
  975. }
  976. /**
  977. * Gets a list of all, hidden, and sortable columns, with filter applied.
  978. *
  979. * @since 3.1.0
  980. *
  981. * @return array
  982. */
  983. protected function get_column_info() {
  984. // $_column_headers is already set / cached.
  985. if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) {
  986. /*
  987. * Backward compatibility for `$_column_headers` format prior to WordPress 4.3.
  988. *
  989. * In WordPress 4.3 the primary column name was added as a fourth item in the
  990. * column headers property. This ensures the primary column name is included
  991. * in plugins setting the property directly in the three item format.
  992. */
  993. $column_headers = array( array(), array(), array(), $this->get_primary_column_name() );
  994. foreach ( $this->_column_headers as $key => $value ) {
  995. $column_headers[ $key ] = $value;
  996. }
  997. return $column_headers;
  998. }
  999. $columns = get_column_headers( $this->screen );
  1000. $hidden = get_hidden_columns( $this->screen );
  1001. $sortable_columns = $this->get_sortable_columns();
  1002. /**
  1003. * Filters the list table sortable columns for a specific screen.
  1004. *
  1005. * The dynamic portion of the hook name, `$this->screen->id`, refers
  1006. * to the ID of the current screen.
  1007. *
  1008. * @since 3.1.0
  1009. *
  1010. * @param array $sortable_columns An array of sortable columns.
  1011. */
  1012. $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
  1013. $sortable = array();
  1014. foreach ( $_sortable as $id => $data ) {
  1015. if ( empty( $data ) ) {
  1016. continue;
  1017. }
  1018. $data = (array) $data;
  1019. if ( ! isset( $data[1] ) ) {
  1020. $data[1] = false;
  1021. }
  1022. $sortable[ $id ] = $data;
  1023. }
  1024. $primary = $this->get_primary_column_name();
  1025. $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
  1026. return $this->_column_headers;
  1027. }
  1028. /**
  1029. * Returns the number of visible columns.
  1030. *
  1031. * @since 3.1.0
  1032. *
  1033. * @return int
  1034. */
  1035. public function get_column_count() {
  1036. list ( $columns, $hidden ) = $this->get_column_info();
  1037. $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
  1038. return count( $columns ) - count( $hidden );
  1039. }
  1040. /**
  1041. * Prints column headers, accounting for hidden and sortable columns.
  1042. *
  1043. * @since 3.1.0
  1044. *
  1045. * @param bool $with_id Whether to set the ID attribute or not
  1046. */
  1047. public function print_column_headers( $with_id = true ) {
  1048. list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
  1049. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1050. $current_url = remove_query_arg( 'paged', $current_url );
  1051. if ( isset( $_GET['orderby'] ) ) {
  1052. $current_orderby = $_GET['orderby'];
  1053. } else {
  1054. $current_orderby = '';
  1055. }
  1056. if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) {
  1057. $current_order = 'desc';
  1058. } else {
  1059. $current_order = 'asc';
  1060. }
  1061. if ( ! empty( $columns['cb'] ) ) {
  1062. static $cb_counter = 1;
  1063. $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
  1064. . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
  1065. $cb_counter++;
  1066. }
  1067. foreach ( $columns as $column_key => $column_display_name ) {
  1068. $class = array( 'manage-column', "column-$column_key" );
  1069. if ( in_array( $column_key, $hidden, true ) ) {
  1070. $class[] = 'hidden';
  1071. }
  1072. if ( 'cb' === $column_key ) {
  1073. $class[] = 'check-column';
  1074. } elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ), true ) ) {
  1075. $class[] = 'num';
  1076. }
  1077. if ( $column_key === $primary ) {
  1078. $class[] = 'column-primary';
  1079. }
  1080. if ( isset( $sortable[ $column_key ] ) ) {
  1081. list( $orderby, $desc_first ) = $sortable[ $column_key ];
  1082. if ( $current_orderby === $orderby ) {
  1083. $order = 'asc' === $current_order ? 'desc' : 'asc';
  1084. $class[] = 'sorted';
  1085. $class[] = $current_order;
  1086. } else {
  1087. $order = strtolower( $desc_first );
  1088. if ( ! in_array( $order, array( 'desc', 'asc' ), true ) ) {
  1089. $order = $desc_first ? 'desc' : 'asc';
  1090. }
  1091. $class[] = 'sortable';
  1092. $class[] = 'desc' === $order ? 'asc' : 'desc';
  1093. }
  1094. $column_display_name = sprintf(
  1095. '<a href="%s"><span>%s</span><span class="sorting-indicator"></span></a>',
  1096. esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ),
  1097. $column_display_name
  1098. );
  1099. }
  1100. $tag = ( 'cb' === $column_key ) ? 'td' : 'th';
  1101. $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
  1102. $id = $with_id ? "id='$column_key'" : '';
  1103. if ( ! empty( $class ) ) {
  1104. $class = "class='" . implode( ' ', $class ) . "'";
  1105. }
  1106. echo "<$tag $scope $id $class>$column_display_name</$tag>";
  1107. }
  1108. }
  1109. /**
  1110. * Displays the table.
  1111. *
  1112. * @since 3.1.0
  1113. */
  1114. public function display() {
  1115. $singular = $this->_args['singular'];
  1116. $this->display_tablenav( 'top' );
  1117. $this->screen->render_screen_reader_content( 'heading_list' );
  1118. ?>
  1119. <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
  1120. <thead>
  1121. <tr>
  1122. <?php $this->print_column_headers(); ?>
  1123. </tr>
  1124. </thead>
  1125. <tbody id="the-list"
  1126. <?php
  1127. if ( $singular ) {
  1128. echo " data-wp-lists='list:$singular'";
  1129. }
  1130. ?>
  1131. >
  1132. <?php $this->display_rows_or_placeholder(); ?>
  1133. </tbody>
  1134. <tfoot>
  1135. <tr>
  1136. <?php $this->print_column_headers( false ); ?>
  1137. </tr>
  1138. </tfoot>
  1139. </table>
  1140. <?php
  1141. $this->display_tablenav( 'bottom' );
  1142. }
  1143. /**
  1144. * Gets a list of CSS classes for the WP_List_Table table tag.
  1145. *
  1146. * @since 3.1.0
  1147. *
  1148. * @return string[] Array of CSS classes for the table tag.
  1149. */
  1150. protected function get_table_classes() {
  1151. $mode = get_user_setting( 'posts_list_mode', 'list' );
  1152. $mode_class = esc_attr( 'table-view-' . $mode );
  1153. return array( 'widefat', 'fixed', 'striped', $mode_class, $this->_args['plural'] );
  1154. }
  1155. /**
  1156. * Generates the table navigation above or below the table
  1157. *
  1158. * @since 3.1.0
  1159. * @param string $which
  1160. */
  1161. protected function display_tablenav( $which ) {
  1162. if ( 'top' === $which ) {
  1163. wp_nonce_field( 'bulk-' . $this->_args['plural'] );
  1164. }
  1165. ?>
  1166. <div class="tablenav <?php echo esc_attr( $which ); ?>">
  1167. <?php if ( $this->has_items() ) : ?>
  1168. <div class="alignleft actions bulkactions">
  1169. <?php $this->bulk_actions( $which ); ?>
  1170. </div>
  1171. <?php
  1172. endif;
  1173. $this->extra_tablenav( $which );
  1174. $this->pagination( $which );
  1175. ?>
  1176. <br class="clear" />
  1177. </div>
  1178. <?php
  1179. }
  1180. /**
  1181. * Extra controls to be displayed between bulk actions and pagination.
  1182. *
  1183. * @since 3.1.0
  1184. *
  1185. * @param string $which
  1186. */
  1187. protected function extra_tablenav( $which ) {}
  1188. /**
  1189. * Generates the tbody element for the list table.
  1190. *
  1191. * @since 3.1.0
  1192. */
  1193. public function display_rows_or_placeholder() {
  1194. if ( $this->has_items() ) {
  1195. $this->display_rows();
  1196. } else {
  1197. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
  1198. $this->no_items();
  1199. echo '</td></tr>';
  1200. }
  1201. }
  1202. /**
  1203. * Generates the table rows.
  1204. *
  1205. * @since 3.1.0
  1206. */
  1207. public function display_rows() {
  1208. foreach ( $this->items as $item ) {
  1209. $this->single_row( $item );
  1210. }
  1211. }
  1212. /**
  1213. * Generates content for a single row of the table.
  1214. *
  1215. * @since 3.1.0
  1216. *
  1217. * @param object|array $item The current item
  1218. */
  1219. public function single_row( $item ) {
  1220. echo '<tr>';
  1221. $this->single_row_columns( $item );
  1222. echo '</tr>';
  1223. }
  1224. /**
  1225. * @param object|array $item
  1226. * @param string $column_name
  1227. */
  1228. protected function column_default( $item, $column_name ) {}
  1229. /**
  1230. * @param object|array $item
  1231. */
  1232. protected function column_cb( $item ) {}
  1233. /**
  1234. * Generates the columns for a single row of the table.
  1235. *
  1236. * @since 3.1.0
  1237. *
  1238. * @param object|array $item The current item.
  1239. */
  1240. protected function single_row_columns( $item ) {
  1241. list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
  1242. foreach ( $columns as $column_name => $column_display_name ) {
  1243. $classes = "$column_name column-$column_name";
  1244. if ( $primary === $column_name ) {
  1245. $classes .= ' has-row-actions column-primary';
  1246. }
  1247. if ( in_array( $column_name, $hidden, true ) ) {
  1248. $classes .= ' hidden';
  1249. }
  1250. // Comments column uses HTML in the display name with screen reader text.
  1251. // Strip tags to get closer to a user-friendly string.
  1252. $data = 'data-colname="' . esc_attr( wp_strip_all_tags( $column_display_name ) ) . '"';
  1253. $attributes = "class='$classes' $data";
  1254. if ( 'cb' === $column_name ) {
  1255. echo '<th scope="row" class="check-column">';
  1256. echo $this->column_cb( $item );
  1257. echo '</th>';
  1258. } elseif ( method_exists( $this, '_column_' . $column_name ) ) {
  1259. echo call_user_func(
  1260. array( $this, '_column_' . $column_name ),
  1261. $item,
  1262. $classes,
  1263. $data,
  1264. $primary
  1265. );
  1266. } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
  1267. echo "<td $attributes>";
  1268. echo call_user_func( array( $this, 'column_' . $column_name ), $item );
  1269. echo $this->handle_row_actions( $item, $column_name, $primary );
  1270. echo '</td>';
  1271. } else {
  1272. echo "<td $attributes>";
  1273. echo $this->column_default( $item, $column_name );
  1274. echo $this->handle_row_actions( $item, $column_name, $primary );
  1275. echo '</td>';
  1276. }
  1277. }
  1278. }
  1279. /**
  1280. * Generates and display row actions links for the list table.
  1281. *
  1282. * @since 4.3.0
  1283. *
  1284. * @param object|array $item The item being acted upon.
  1285. * @param string $column_name Current column name.
  1286. * @param string $primary Primary column name.
  1287. * @return string The row actions HTML, or an empty string
  1288. * if the current column is not the primary column.
  1289. */
  1290. protected function handle_row_actions( $item, $column_name, $primary ) {
  1291. return $column_name === $primary ? '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>' : '';
  1292. }
  1293. /**
  1294. * Handles an incoming ajax request (called from admin-ajax.php)
  1295. *
  1296. * @since 3.1.0
  1297. */
  1298. public function ajax_response() {
  1299. $this->prepare_items();
  1300. ob_start();
  1301. if ( ! empty( $_REQUEST['no_placeholder'] ) ) {
  1302. $this->display_rows();
  1303. } else {
  1304. $this->display_rows_or_placeholder();
  1305. }
  1306. $rows = ob_get_clean();
  1307. $response = array( 'rows' => $rows );
  1308. if ( isset( $this->_pagination_args['total_items'] ) ) {
  1309. $response['total_items_i18n'] = sprintf(
  1310. /* translators: Number of items. */
  1311. _n( '%s item', '%s items', $this->_pagination_args['total_items'] ),
  1312. number_format_i18n( $this->_pagination_args['total_items'] )
  1313. );
  1314. }
  1315. if ( isset( $this->_pagination_args['total_pages'] ) ) {
  1316. $response['total_pages'] = $this->_pagination_args['total_pages'];
  1317. $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] );
  1318. }
  1319. die( wp_json_encode( $response ) );
  1320. }
  1321. /**
  1322. * Sends required variables to JavaScript land.
  1323. *
  1324. * @since 3.1.0
  1325. */
  1326. public function _js_vars() {
  1327. $args = array(
  1328. 'class' => get_class( $this ),
  1329. 'screen' => array(
  1330. 'id' => $this->screen->id,
  1331. 'base' => $this->screen->base,
  1332. ),
  1333. );
  1334. printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );
  1335. }
  1336. }