class-wp-list-table.php 43 KB

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