PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-list-table.php

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