3 * Base class for displaying a list of items in an ajaxified HTML table.
9 * @subpackage List_Table
14 * The current list of items
23 * Various information about the current table
32 * Various information needed for displaying the pagination
37 protected $_pagination_args = array();
58 * Cached pagination output
67 * The view switcher modes.
73 protected $modes = array();
76 * Stores the value returned by ->get_column_info()
80 protected $_column_headers;
82 protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' );
84 protected $compat_methods = array( 'set_pagination_args', 'get_views', 'get_bulk_actions', 'bulk_actions',
85 'row_actions', 'months_dropdown', 'view_switcher', 'comments_bubble', 'get_items_per_page', 'pagination',
86 'get_sortable_columns', 'get_column_info', 'get_table_classes', 'display_tablenav', 'extra_tablenav',
87 'single_row_columns' );
92 * The child class should call this constructor from its own constructor to override
98 * @param array|string $args {
99 * Array or string of arguments.
101 * @type string $plural Plural value used for labels and the objects being listed.
102 * This affects things such as CSS class-names and nonces used
103 * in the list table, e.g. 'posts'. Default empty.
104 * @type string $singular Singular label for an object being listed, e.g. 'post'.
106 * @type bool $ajax Whether the list table supports AJAX. This includes loading
107 * and sorting data, for example. If true, the class will call
108 * the {@see _js_vars()} method in the footer to provide variables
109 * to any scripts handling AJAX events. Default false.
110 * @type string $screen String containing the hook name used to determine the current
111 * screen. If left null, the current screen will be automatically set.
115 public function __construct( $args = array() ) {
116 $args = wp_parse_args( $args, array(
123 $this->screen = convert_to_screen( $args['screen'] );
125 add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
127 if ( !$args['plural'] )
128 $args['plural'] = $this->screen->base;
130 $args['plural'] = sanitize_key( $args['plural'] );
131 $args['singular'] = sanitize_key( $args['singular'] );
133 $this->_args = $args;
135 if ( $args['ajax'] ) {
136 // wp_enqueue_script( 'list-table' );
137 add_action( 'admin_footer', array( $this, '_js_vars' ) );
140 if ( empty( $this->modes ) ) {
141 $this->modes = array(
142 'list' => __( 'List View' ),
143 'excerpt' => __( 'Excerpt View' )
149 * Make private properties readable for backwards compatibility.
154 * @param string $name Property to get.
155 * @return mixed Property.
157 public function __get( $name ) {
158 if ( in_array( $name, $this->compat_fields ) ) {
164 * Make private properties settable for backwards compatibility.
169 * @param string $name Property to check if set.
170 * @param mixed $value Property value.
171 * @return mixed Newly-set property.
173 public function __set( $name, $value ) {
174 if ( in_array( $name, $this->compat_fields ) ) {
175 return $this->$name = $value;
180 * Make private properties checkable for backwards compatibility.
185 * @param string $name Property to check if set.
186 * @return bool Whether the property is set.
188 public function __isset( $name ) {
189 if ( in_array( $name, $this->compat_fields ) ) {
190 return isset( $this->$name );
195 * Make private properties un-settable for backwards compatibility.
200 * @param string $name Property to unset.
202 public function __unset( $name ) {
203 if ( in_array( $name, $this->compat_fields ) ) {
204 unset( $this->$name );
209 * Make private/protected methods readable for backwards compatibility.
214 * @param callable $name Method to call.
215 * @param array $arguments Arguments to pass when calling.
216 * @return mixed|bool Return value of the callback, false otherwise.
218 public function __call( $name, $arguments ) {
219 if ( in_array( $name, $this->compat_methods ) ) {
220 return call_user_func_array( array( $this, $name ), $arguments );
226 * Checks the current user's permissions
232 public function ajax_user_can() {
233 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
237 * Prepares the list of items for displaying.
238 * @uses WP_List_Table::set_pagination_args()
244 public function prepare_items() {
245 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
249 * An internal method that sets all the necessary pagination arguments
251 * @param array $args An associative array with information about the pagination
254 * @param array|string $args
256 protected function set_pagination_args( $args ) {
257 $args = wp_parse_args( $args, array(
263 if ( !$args['total_pages'] && $args['per_page'] > 0 )
264 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
266 // Redirect if page number is invalid and headers are not already sent.
267 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
268 wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
272 $this->_pagination_args = $args;
276 * Access the pagination args.
281 * @param string $key Pagination argument to retrieve. Common values include 'total_items',
282 * 'total_pages', 'per_page', or 'infinite_scroll'.
283 * @return int Number of items that correspond to the given pagination argument.
285 public function get_pagination_arg( $key ) {
286 if ( 'page' == $key )
287 return $this->get_pagenum();
289 if ( isset( $this->_pagination_args[$key] ) )
290 return $this->_pagination_args[$key];
294 * Whether the table has items to display or not
301 public function has_items() {
302 return !empty( $this->items );
306 * Message to be displayed when there are no items
311 public function no_items() {
312 _e( 'No items found.' );
316 * Display the search box.
321 * @param string $text The search button text
322 * @param string $input_id The search input id
324 public function search_box( $text, $input_id ) {
325 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
328 $input_id = $input_id . '-search-input';
330 if ( ! empty( $_REQUEST['orderby'] ) )
331 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
332 if ( ! empty( $_REQUEST['order'] ) )
333 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
334 if ( ! empty( $_REQUEST['post_mime_type'] ) )
335 echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
336 if ( ! empty( $_REQUEST['detached'] ) )
337 echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
339 <p class="search-box">
340 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
341 <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
342 <?php submit_button( $text, 'button', '', false, array('id' => 'search-submit') ); ?>
348 * Get an associative array ( id => link ) with the list
349 * of views available on this table.
356 protected function get_views() {
361 * Display the list of views available on this table.
366 public function views() {
367 $views = $this->get_views();
369 * Filter the list of available list table views.
371 * The dynamic portion of the hook name, `$this->screen->id`, refers
372 * to the ID of the current screen, usually a string.
376 * @param array $views An array of available list table views.
378 $views = apply_filters( "views_{$this->screen->id}", $views );
380 if ( empty( $views ) )
383 echo "<ul class='subsubsub'>\n";
384 foreach ( $views as $class => $view ) {
385 $views[ $class ] = "\t<li class='$class'>$view";
387 echo implode( " |</li>\n", $views ) . "</li>\n";
392 * Get an associative array ( option_name => option_title ) with the list
393 * of bulk actions available on this table.
400 protected function get_bulk_actions() {
405 * Display the bulk actions dropdown.
410 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
411 * This is designated as optional for backwards-compatibility.
413 protected function bulk_actions( $which = '' ) {
414 if ( is_null( $this->_actions ) ) {
415 $no_new_actions = $this->_actions = $this->get_bulk_actions();
417 * Filter the list table Bulk Actions drop-down.
419 * The dynamic portion of the hook name, `$this->screen->id`, refers
420 * to the ID of the current screen, usually a string.
422 * This filter can currently only be used to remove bulk actions.
426 * @param array $actions An array of the available bulk actions.
428 $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
429 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
435 if ( empty( $this->_actions ) )
438 echo "<label for='bulk-action-selector-" . esc_attr( $which ) . "' class='screen-reader-text'>" . __( 'Select bulk action' ) . "</label>";
439 echo "<select name='action$two' id='bulk-action-selector-" . esc_attr( $which ) . "'>\n";
440 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
442 foreach ( $this->_actions as $name => $title ) {
443 $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
445 echo "\t<option value='$name'$class>$title</option>\n";
450 submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
455 * Get the current action selected from the bulk actions dropdown.
460 * @return string|false The action name or False if no action was selected
462 public function current_action() {
463 if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) )
466 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
467 return $_REQUEST['action'];
469 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
470 return $_REQUEST['action2'];
476 * Generate row actions div
481 * @param array $actions The list of actions
482 * @param bool $always_visible Whether the actions should be always visible
485 protected function row_actions( $actions, $always_visible = false ) {
486 $action_count = count( $actions );
489 if ( !$action_count )
492 $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
493 foreach ( $actions as $action => $link ) {
495 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
496 $out .= "<span class='$action'>$link$sep</span>";
500 $out .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
506 * Display a monthly dropdown for filtering items
512 * @global WP_Locale $wp_locale
514 * @param string $post_type
516 protected function months_dropdown( $post_type ) {
517 global $wpdb, $wp_locale;
520 * Filter whether to remove the 'Months' drop-down from the post list table.
524 * @param bool $disable Whether to disable the drop-down. Default false.
525 * @param string $post_type The post type.
527 if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) {
531 $months = $wpdb->get_results( $wpdb->prepare( "
532 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
535 ORDER BY post_date DESC
539 * Filter the 'Months' drop-down results.
543 * @param object $months The months drop-down query results.
544 * @param string $post_type The post type.
546 $months = apply_filters( 'months_dropdown_results', $months, $post_type );
548 $month_count = count( $months );
550 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
553 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
555 <label for="filter-by-date" class="screen-reader-text"><?php _e( 'Filter by date' ); ?></label>
556 <select name="m" id="filter-by-date">
557 <option<?php selected( $m, 0 ); ?> value="0"><?php _e( 'All dates' ); ?></option>
559 foreach ( $months as $arc_row ) {
560 if ( 0 == $arc_row->year )
563 $month = zeroise( $arc_row->month, 2 );
564 $year = $arc_row->year;
566 printf( "<option %s value='%s'>%s</option>\n",
567 selected( $m, $year . $month, false ),
568 esc_attr( $arc_row->year . $month ),
569 /* translators: 1: month name, 2: 4-digit year */
570 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
579 * Display a view switcher
584 * @param string $current_mode
586 protected function view_switcher( $current_mode ) {
588 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
589 <div class="view-switch">
591 foreach ( $this->modes as $mode => $title ) {
592 $classes = array( 'view-' . $mode );
593 if ( $current_mode == $mode )
594 $classes[] = 'current';
596 "<a href='%s' class='%s' id='view-switch-$mode'><span class='screen-reader-text'>%s</span></a>\n",
597 esc_url( add_query_arg( 'mode', $mode ) ),
598 implode( ' ', $classes ),
608 * Display a comment count bubble
613 * @param int $post_id The post ID.
614 * @param int $pending_comments Number of pending comments.
616 protected function comments_bubble( $post_id, $pending_comments ) {
617 $approved_comments = get_comments_number();
619 $approved_comments_number = number_format_i18n( $approved_comments );
620 $pending_comments_number = number_format_i18n( $pending_comments );
622 $approved_only_phrase = sprintf( _n( '%s comment', '%s comments', $approved_comments ), $approved_comments_number );
623 $approved_phrase = sprintf( _n( '%s approved comment', '%s approved comments', $approved_comments ), $approved_comments_number );
624 $pending_phrase = sprintf( _n( '%s pending comment', '%s pending comments', $pending_comments ), $pending_comments_number );
626 // No comments at all.
627 if ( ! $approved_comments && ! $pending_comments ) {
628 printf( '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
631 // Approved comments have different display depending on some conditions.
632 } elseif ( $approved_comments ) {
633 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>',
634 esc_url( add_query_arg( array( 'p' => $post_id, 'comment_status' => 'approved' ), admin_url( 'edit-comments.php' ) ) ),
635 $approved_comments_number,
636 $pending_comments ? $approved_phrase : $approved_only_phrase
639 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>',
640 $approved_comments_number,
641 $pending_comments ? __( 'No approved comments' ) : __( 'No comments' )
645 if ( $pending_comments ) {
646 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>',
647 esc_url( add_query_arg( array( 'p' => $post_id, 'comment_status' => 'moderated' ), admin_url( 'edit-comments.php' ) ) ),
648 $pending_comments_number,
655 * Get the current page number
662 public function get_pagenum() {
663 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
665 if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
666 $pagenum = $this->_pagination_args['total_pages'];
668 return max( 1, $pagenum );
672 * Get number of items to display on a single page
677 * @param string $option
678 * @param int $default
681 protected function get_items_per_page( $option, $default = 20 ) {
682 $per_page = (int) get_user_option( $option );
683 if ( empty( $per_page ) || $per_page < 1 )
684 $per_page = $default;
687 * Filter the number of items to be displayed on each page of the list table.
689 * The dynamic hook name, $option, refers to the `per_page` option depending
690 * on the type of list table in use. Possible values include: 'edit_comments_per_page',
691 * 'sites_network_per_page', 'site_themes_network_per_page', 'themes_network_per_page',
692 * 'users_network_per_page', 'edit_post_per_page', 'edit_page_per_page',
693 * 'edit_{$post_type}_per_page', etc.
697 * @param int $per_page Number of items to be displayed. Default 20.
699 return (int) apply_filters( $option, $per_page );
703 * Display the pagination.
708 * @param string $which
710 protected function pagination( $which ) {
711 if ( empty( $this->_pagination_args ) ) {
715 $total_items = $this->_pagination_args['total_items'];
716 $total_pages = $this->_pagination_args['total_pages'];
717 $infinite_scroll = false;
718 if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
719 $infinite_scroll = $this->_pagination_args['infinite_scroll'];
722 $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
724 $current = $this->get_pagenum();
726 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
728 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
730 $page_links = array();
732 $total_pages_before = '<span class="paging-input">';
733 $total_pages_after = '</span>';
735 $disable_first = $disable_last = $disable_prev = $disable_next = false;
737 if ( $current == 1 ) {
738 $disable_first = true;
739 $disable_prev = true;
741 if ( $current == 2 ) {
742 $disable_first = true;
744 if ( $current == $total_pages ) {
745 $disable_last = true;
746 $disable_next = true;
748 if ( $current == $total_pages - 1 ) {
749 $disable_last = true;
752 if ( $disable_first ) {
753 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">«</span>';
755 $page_links[] = sprintf( "<a class='first-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
756 esc_url( remove_query_arg( 'paged', $current_url ) ),
762 if ( $disable_prev ) {
763 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">‹</span>';
765 $page_links[] = sprintf( "<a class='prev-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
766 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
767 __( 'Previous page' ),
772 if ( 'bottom' == $which ) {
773 $html_current_page = $current;
774 $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input">';
776 $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' />",
777 '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
779 strlen( $total_pages )
782 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
783 $page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;
785 if ( $disable_next ) {
786 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">›</span>';
788 $page_links[] = sprintf( "<a class='next-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
789 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
795 if ( $disable_last ) {
796 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">»</span>';
798 $page_links[] = sprintf( "<a class='last-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
799 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
805 $pagination_links_class = 'pagination-links';
806 if ( ! empty( $infinite_scroll ) ) {
807 $pagination_links_class = ' hide-if-js';
809 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
811 if ( $total_pages ) {
812 $page_class = $total_pages < 2 ? ' one-page' : '';
814 $page_class = ' no-pages';
816 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
818 echo $this->_pagination;
822 * Get a list of columns. The format is:
823 * 'internal-name' => 'Title'
831 public function get_columns() {
832 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
836 * Get a list of sortable columns. The format is:
837 * 'internal-name' => 'orderby'
839 * 'internal-name' => array( 'orderby', true )
841 * The second format will make the initial sorting order be descending
848 protected function get_sortable_columns() {
853 * Gets the name of the default primary column.
858 * @return string Name of the default primary column, in this case, an empty string.
860 protected function get_default_primary_column_name() {
861 $columns = $this->get_columns();
864 // We need a primary defined so responsive views show something,
865 // so let's fall back to the first non-checkbox column.
866 foreach( $columns as $col => $column_name ) {
867 if ( 'cb' === $col ) {
879 * Gets the name of the primary column.
884 * @return string The name of the primary column.
886 protected function get_primary_column_name() {
887 $columns = $this->get_columns();
888 $default = $this->get_default_primary_column_name();
890 // If the primary column doesn't exist fall back to the
891 // first non-checkbox column.
892 if ( ! isset( $columns[ $default ] ) ) {
893 $default = WP_List_Table::get_default_primary_column_name();
897 * Filter the name of the primary column for the current list table.
901 * @param string $default Column name default for the specific list table, e.g. 'name'.
902 * @param string $context Screen ID for specific list table, e.g. 'plugins'.
904 $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
906 if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
914 * Get a list of all, hidden and sortable columns, with filter applied
921 protected function get_column_info() {
922 // $_column_headers is already set / cached
923 if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) {
924 // Back-compat for list tables that have been manually setting $_column_headers for horse reasons.
925 // In 4.3, we added a fourth argument for primary column.
926 $column_headers = array( array(), array(), array(), $this->get_primary_column_name() );
927 foreach ( $this->_column_headers as $key => $value ) {
928 $column_headers[ $key ] = $value;
931 return $column_headers;
934 $columns = get_column_headers( $this->screen );
935 $hidden = get_hidden_columns( $this->screen );
937 $sortable_columns = $this->get_sortable_columns();
939 * Filter the list table sortable columns for a specific screen.
941 * The dynamic portion of the hook name, `$this->screen->id`, refers
942 * to the ID of the current screen, usually a string.
946 * @param array $sortable_columns An array of sortable columns.
948 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
951 foreach ( $_sortable as $id => $data ) {
952 if ( empty( $data ) )
955 $data = (array) $data;
956 if ( !isset( $data[1] ) )
959 $sortable[$id] = $data;
962 $primary = $this->get_primary_column_name();
963 $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
965 return $this->_column_headers;
969 * Return number of visible columns
976 public function get_column_count() {
977 list ( $columns, $hidden ) = $this->get_column_info();
978 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
979 return count( $columns ) - count( $hidden );
983 * Print column headers, accounting for hidden and sortable columns.
988 * @staticvar int $cb_counter
990 * @param bool $with_id Whether to set the id attribute or not
992 public function print_column_headers( $with_id = true ) {
993 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
995 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
996 $current_url = remove_query_arg( 'paged', $current_url );
998 if ( isset( $_GET['orderby'] ) )
999 $current_orderby = $_GET['orderby'];
1001 $current_orderby = '';
1003 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
1004 $current_order = 'desc';
1006 $current_order = 'asc';
1008 if ( ! empty( $columns['cb'] ) ) {
1009 static $cb_counter = 1;
1010 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
1011 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
1015 foreach ( $columns as $column_key => $column_display_name ) {
1016 $class = array( 'manage-column', "column-$column_key" );
1018 if ( in_array( $column_key, $hidden ) ) {
1019 $class[] = 'hidden';
1022 if ( 'cb' == $column_key )
1023 $class[] = 'check-column';
1024 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
1027 if ( $column_key === $primary ) {
1028 $class[] = 'column-primary';
1031 if ( isset( $sortable[$column_key] ) ) {
1032 list( $orderby, $desc_first ) = $sortable[$column_key];
1034 if ( $current_orderby == $orderby ) {
1035 $order = 'asc' == $current_order ? 'desc' : 'asc';
1036 $class[] = 'sorted';
1037 $class[] = $current_order;
1039 $order = $desc_first ? 'desc' : 'asc';
1040 $class[] = 'sortable';
1041 $class[] = $desc_first ? 'asc' : 'desc';
1044 $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>';
1047 $tag = ( 'cb' === $column_key ) ? 'td' : 'th';
1048 $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
1049 $id = $with_id ? "id='$column_key'" : '';
1051 if ( !empty( $class ) )
1052 $class = "class='" . join( ' ', $class ) . "'";
1054 echo "<$tag $scope $id $class>$column_display_name</$tag>";
1064 public function display() {
1065 $singular = $this->_args['singular'];
1067 $this->display_tablenav( 'top' );
1069 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
1072 <?php $this->print_column_headers(); ?>
1076 <tbody id="the-list"<?php
1078 echo " data-wp-lists='list:$singular'";
1080 <?php $this->display_rows_or_placeholder(); ?>
1085 <?php $this->print_column_headers( false ); ?>
1091 $this->display_tablenav( 'bottom' );
1095 * Get a list of CSS classes for the list table table tag.
1100 * @return array List of CSS classes for the table tag.
1102 protected function get_table_classes() {
1103 return array( 'widefat', 'fixed', 'striped', $this->_args['plural'] );
1107 * Generate the table navigation above or below the table
1111 * @param string $which
1113 protected function display_tablenav( $which ) {
1114 if ( 'top' == $which )
1115 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
1117 <div class="tablenav <?php echo esc_attr( $which ); ?>">
1119 <div class="alignleft actions bulkactions">
1120 <?php $this->bulk_actions( $which ); ?>
1123 $this->extra_tablenav( $which );
1124 $this->pagination( $which );
1127 <br class="clear" />
1133 * Extra controls to be displayed between bulk actions and pagination
1138 * @param string $which
1140 protected function extra_tablenav( $which ) {}
1143 * Generate the tbody element for the list table.
1148 public function display_rows_or_placeholder() {
1149 if ( $this->has_items() ) {
1150 $this->display_rows();
1152 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
1159 * Generate the table rows
1164 public function display_rows() {
1165 foreach ( $this->items as $item )
1166 $this->single_row( $item );
1170 * Generates content for a single row of the table
1175 * @param object $item The current item
1177 public function single_row( $item ) {
1179 $this->single_row_columns( $item );
1185 * @param object $item
1186 * @param string $column_name
1188 protected function column_default( $item, $column_name ) {}
1192 * @param object $item
1194 protected function column_cb( $item ) {}
1197 * Generates the columns for a single row of the table
1202 * @param object $item The current item
1204 protected function single_row_columns( $item ) {
1205 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
1207 foreach ( $columns as $column_name => $column_display_name ) {
1208 $classes = "$column_name column-$column_name";
1209 if ( $primary === $column_name ) {
1210 $classes .= ' has-row-actions column-primary';
1213 if ( in_array( $column_name, $hidden ) ) {
1214 $classes .= ' hidden';
1217 // Comments column uses HTML in the display name with screen reader text.
1218 // Instead of using esc_attr(), we strip tags to get closer to a user-friendly string.
1219 $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
1221 $attributes = "class='$classes' $data";
1223 if ( 'cb' == $column_name ) {
1224 echo '<th scope="row" class="check-column">';
1225 echo $this->column_cb( $item );
1227 } elseif ( method_exists( $this, '_column_' . $column_name ) ) {
1228 echo call_user_func(
1229 array( $this, '_column_' . $column_name ),
1235 } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
1236 echo "<td $attributes>";
1237 echo call_user_func( array( $this, 'column_' . $column_name ), $item );
1238 echo $this->handle_row_actions( $item, $column_name, $primary );
1241 echo "<td $attributes>";
1242 echo $this->column_default( $item, $column_name );
1243 echo $this->handle_row_actions( $item, $column_name, $primary );
1250 * Generates and display row actions links for the list table.
1255 * @param object $item The item being acted upon.
1256 * @param string $column_name Current column name.
1257 * @param string $primary Primary column name.
1258 * @return string The row actions output. In this case, an empty string.
1260 protected function handle_row_actions( $item, $column_name, $primary ) {
1261 return $column_name == $primary ? '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>' : '';
1265 * Handle an incoming ajax request (called from admin-ajax.php)
1270 public function ajax_response() {
1271 $this->prepare_items();
1274 if ( ! empty( $_REQUEST['no_placeholder'] ) ) {
1275 $this->display_rows();
1277 $this->display_rows_or_placeholder();
1280 $rows = ob_get_clean();
1282 $response = array( 'rows' => $rows );
1284 if ( isset( $this->_pagination_args['total_items'] ) ) {
1285 $response['total_items_i18n'] = sprintf(
1286 _n( '%s item', '%s items', $this->_pagination_args['total_items'] ),
1287 number_format_i18n( $this->_pagination_args['total_items'] )
1290 if ( isset( $this->_pagination_args['total_pages'] ) ) {
1291 $response['total_pages'] = $this->_pagination_args['total_pages'];
1292 $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] );
1295 die( wp_json_encode( $response ) );
1299 * Send required variables to JavaScript land
1303 public function _js_vars() {
1305 'class' => get_class( $this ),
1307 'id' => $this->screen->id,
1308 'base' => $this->screen->base,
1312 printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );