3 * Administration API: WP_List_Table class
6 * @subpackage List_Table
11 * Base class for displaying a list of items in an ajaxified HTML table.
19 * The current list of items.
28 * Various information about the current table.
37 * Various information needed for displaying the pagination.
43 protected $_pagination_args = array();
55 * Cached bulk actions.
64 * Cached pagination output.
73 * The view switcher modes.
79 protected $modes = array();
82 * Stores the value returned by ->get_column_info().
88 protected $_column_headers;
91 * {@internal Missing Summary}
96 protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' );
99 * {@internal Missing Summary}
104 protected $compat_methods = array( 'set_pagination_args', 'get_views', 'get_bulk_actions', 'bulk_actions',
105 'row_actions', 'months_dropdown', 'view_switcher', 'comments_bubble', 'get_items_per_page', 'pagination',
106 'get_sortable_columns', 'get_column_info', 'get_table_classes', 'display_tablenav', 'extra_tablenav',
107 'single_row_columns' );
112 * The child class should call this constructor from its own constructor to override
118 * @param array|string $args {
119 * Array or string of arguments.
121 * @type string $plural Plural value used for labels and the objects being listed.
122 * This affects things such as CSS class-names and nonces used
123 * in the list table, e.g. 'posts'. Default empty.
124 * @type string $singular Singular label for an object being listed, e.g. 'post'.
126 * @type bool $ajax Whether the list table supports Ajax. This includes loading
127 * and sorting data, for example. If true, the class will call
128 * the _js_vars() method in the footer to provide variables
129 * to any scripts handling Ajax events. Default false.
130 * @type string $screen String containing the hook name used to determine the current
131 * screen. If left null, the current screen will be automatically set.
135 public function __construct( $args = array() ) {
136 $args = wp_parse_args( $args, array(
143 $this->screen = convert_to_screen( $args['screen'] );
145 add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
147 if ( !$args['plural'] )
148 $args['plural'] = $this->screen->base;
150 $args['plural'] = sanitize_key( $args['plural'] );
151 $args['singular'] = sanitize_key( $args['singular'] );
153 $this->_args = $args;
155 if ( $args['ajax'] ) {
156 // wp_enqueue_script( 'list-table' );
157 add_action( 'admin_footer', array( $this, '_js_vars' ) );
160 if ( empty( $this->modes ) ) {
161 $this->modes = array(
162 'list' => __( 'List View' ),
163 'excerpt' => __( 'Excerpt View' )
169 * Make private properties readable for backward compatibility.
174 * @param string $name Property to get.
175 * @return mixed Property.
177 public function __get( $name ) {
178 if ( in_array( $name, $this->compat_fields ) ) {
184 * Make private properties settable for backward compatibility.
189 * @param string $name Property to check if set.
190 * @param mixed $value Property value.
191 * @return mixed Newly-set property.
193 public function __set( $name, $value ) {
194 if ( in_array( $name, $this->compat_fields ) ) {
195 return $this->$name = $value;
200 * Make private properties checkable for backward compatibility.
205 * @param string $name Property to check if set.
206 * @return bool Whether the property is set.
208 public function __isset( $name ) {
209 if ( in_array( $name, $this->compat_fields ) ) {
210 return isset( $this->$name );
215 * Make private properties un-settable for backward compatibility.
220 * @param string $name Property to unset.
222 public function __unset( $name ) {
223 if ( in_array( $name, $this->compat_fields ) ) {
224 unset( $this->$name );
229 * Make private/protected methods readable for backward compatibility.
234 * @param callable $name Method to call.
235 * @param array $arguments Arguments to pass when calling.
236 * @return mixed|bool Return value of the callback, false otherwise.
238 public function __call( $name, $arguments ) {
239 if ( in_array( $name, $this->compat_methods ) ) {
240 return call_user_func_array( array( $this, $name ), $arguments );
246 * Checks the current user's permissions
252 public function ajax_user_can() {
253 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
257 * Prepares the list of items for displaying.
258 * @uses WP_List_Table::set_pagination_args()
264 public function prepare_items() {
265 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
269 * An internal method that sets all the necessary pagination arguments
274 * @param array|string $args Array or string of arguments with information about the pagination.
276 protected function set_pagination_args( $args ) {
277 $args = wp_parse_args( $args, array(
283 if ( !$args['total_pages'] && $args['per_page'] > 0 )
284 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
286 // Redirect if page number is invalid and headers are not already sent.
287 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
288 wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
292 $this->_pagination_args = $args;
296 * Access the pagination args.
301 * @param string $key Pagination argument to retrieve. Common values include 'total_items',
302 * 'total_pages', 'per_page', or 'infinite_scroll'.
303 * @return int Number of items that correspond to the given pagination argument.
305 public function get_pagination_arg( $key ) {
306 if ( 'page' === $key ) {
307 return $this->get_pagenum();
310 if ( isset( $this->_pagination_args[$key] ) ) {
311 return $this->_pagination_args[$key];
316 * Whether the table has items to display or not
323 public function has_items() {
324 return !empty( $this->items );
328 * Message to be displayed when there are no items
333 public function no_items() {
334 _e( 'No items found.' );
338 * Displays the search box.
343 * @param string $text The 'submit' button label.
344 * @param string $input_id ID attribute value for the search input field.
346 public function search_box( $text, $input_id ) {
347 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
350 $input_id = $input_id . '-search-input';
352 if ( ! empty( $_REQUEST['orderby'] ) )
353 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
354 if ( ! empty( $_REQUEST['order'] ) )
355 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
356 if ( ! empty( $_REQUEST['post_mime_type'] ) )
357 echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
358 if ( ! empty( $_REQUEST['detached'] ) )
359 echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
361 <p class="search-box">
362 <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
363 <input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" />
364 <?php submit_button( $text, 'button', '', false, array( 'id' => 'search-submit' ) ); ?>
370 * Get an associative array ( id => link ) with the list
371 * of views available on this table.
378 protected function get_views() {
383 * Display the list of views available on this table.
388 public function views() {
389 $views = $this->get_views();
391 * Filters the list of available list table views.
393 * The dynamic portion of the hook name, `$this->screen->id`, refers
394 * to the ID of the current screen, usually a string.
398 * @param array $views An array of available list table views.
400 $views = apply_filters( "views_{$this->screen->id}", $views );
402 if ( empty( $views ) )
405 $this->screen->render_screen_reader_content( 'heading_views' );
407 echo "<ul class='subsubsub'>\n";
408 foreach ( $views as $class => $view ) {
409 $views[ $class ] = "\t<li class='$class'>$view";
411 echo implode( " |</li>\n", $views ) . "</li>\n";
416 * Get an associative array ( option_name => option_title ) with the list
417 * of bulk actions available on this table.
424 protected function get_bulk_actions() {
429 * Display the bulk actions dropdown.
434 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
435 * This is designated as optional for backward compatibility.
437 protected function bulk_actions( $which = '' ) {
438 if ( is_null( $this->_actions ) ) {
439 $no_new_actions = $this->_actions = $this->get_bulk_actions();
441 * Filters the list table Bulk Actions drop-down.
443 * The dynamic portion of the hook name, `$this->screen->id`, refers
444 * to the ID of the current screen, usually a string.
446 * This filter can currently only be used to remove bulk actions.
450 * @param array $actions An array of the available bulk actions.
452 $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
453 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
459 if ( empty( $this->_actions ) )
462 echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
463 echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">\n";
464 echo '<option value="-1">' . __( 'Bulk Actions' ) . "</option>\n";
466 foreach ( $this->_actions as $name => $title ) {
467 $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
469 echo "\t" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>\n";
474 submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
479 * Get the current action selected from the bulk actions dropdown.
484 * @return string|false The action name or False if no action was selected
486 public function current_action() {
487 if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) )
490 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
491 return $_REQUEST['action'];
493 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
494 return $_REQUEST['action2'];
500 * Generate row actions div
505 * @param array $actions The list of actions
506 * @param bool $always_visible Whether the actions should be always visible
509 protected function row_actions( $actions, $always_visible = false ) {
510 $action_count = count( $actions );
513 if ( !$action_count )
516 $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
517 foreach ( $actions as $action => $link ) {
519 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
520 $out .= "<span class='$action'>$link$sep</span>";
524 $out .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
530 * Display a monthly dropdown for filtering items
536 * @global WP_Locale $wp_locale
538 * @param string $post_type
540 protected function months_dropdown( $post_type ) {
541 global $wpdb, $wp_locale;
544 * Filters whether to remove the 'Months' drop-down from the post list table.
548 * @param bool $disable Whether to disable the drop-down. Default false.
549 * @param string $post_type The post type.
551 if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) {
555 $extra_checks = "AND post_status != 'auto-draft'";
556 if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) {
557 $extra_checks .= " AND post_status != 'trash'";
558 } elseif ( isset( $_GET['post_status'] ) ) {
559 $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] );
562 $months = $wpdb->get_results( $wpdb->prepare( "
563 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
567 ORDER BY post_date DESC
571 * Filters the 'Months' drop-down results.
575 * @param object $months The months drop-down query results.
576 * @param string $post_type The post type.
578 $months = apply_filters( 'months_dropdown_results', $months, $post_type );
580 $month_count = count( $months );
582 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
585 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
587 <label for="filter-by-date" class="screen-reader-text"><?php _e( 'Filter by date' ); ?></label>
588 <select name="m" id="filter-by-date">
589 <option<?php selected( $m, 0 ); ?> value="0"><?php _e( 'All dates' ); ?></option>
591 foreach ( $months as $arc_row ) {
592 if ( 0 == $arc_row->year )
595 $month = zeroise( $arc_row->month, 2 );
596 $year = $arc_row->year;
598 printf( "<option %s value='%s'>%s</option>\n",
599 selected( $m, $year . $month, false ),
600 esc_attr( $arc_row->year . $month ),
601 /* translators: 1: month name, 2: 4-digit year */
602 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
611 * Display a view switcher
616 * @param string $current_mode
618 protected function view_switcher( $current_mode ) {
620 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
621 <div class="view-switch">
623 foreach ( $this->modes as $mode => $title ) {
624 $classes = array( 'view-' . $mode );
625 if ( $current_mode === $mode )
626 $classes[] = 'current';
628 "<a href='%s' class='%s' id='view-switch-$mode'><span class='screen-reader-text'>%s</span></a>\n",
629 esc_url( add_query_arg( 'mode', $mode ) ),
630 implode( ' ', $classes ),
640 * Display a comment count bubble
645 * @param int $post_id The post ID.
646 * @param int $pending_comments Number of pending comments.
648 protected function comments_bubble( $post_id, $pending_comments ) {
649 $approved_comments = get_comments_number();
651 $approved_comments_number = number_format_i18n( $approved_comments );
652 $pending_comments_number = number_format_i18n( $pending_comments );
654 $approved_only_phrase = sprintf( _n( '%s comment', '%s comments', $approved_comments ), $approved_comments_number );
655 $approved_phrase = sprintf( _n( '%s approved comment', '%s approved comments', $approved_comments ), $approved_comments_number );
656 $pending_phrase = sprintf( _n( '%s pending comment', '%s pending comments', $pending_comments ), $pending_comments_number );
658 // No comments at all.
659 if ( ! $approved_comments && ! $pending_comments ) {
660 printf( '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
663 // Approved comments have different display depending on some conditions.
664 } elseif ( $approved_comments ) {
665 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>',
666 esc_url( add_query_arg( array( 'p' => $post_id, 'comment_status' => 'approved' ), admin_url( 'edit-comments.php' ) ) ),
667 $approved_comments_number,
668 $pending_comments ? $approved_phrase : $approved_only_phrase
671 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>',
672 $approved_comments_number,
673 $pending_comments ? __( 'No approved comments' ) : __( 'No comments' )
677 if ( $pending_comments ) {
678 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>',
679 esc_url( add_query_arg( array( 'p' => $post_id, 'comment_status' => 'moderated' ), admin_url( 'edit-comments.php' ) ) ),
680 $pending_comments_number,
684 printf( '<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>',
685 $pending_comments_number,
686 $approved_comments ? __( 'No pending comments' ) : __( 'No comments' )
692 * Get the current page number
699 public function get_pagenum() {
700 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
702 if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
703 $pagenum = $this->_pagination_args['total_pages'];
705 return max( 1, $pagenum );
709 * Get number of items to display on a single page
714 * @param string $option
715 * @param int $default
718 protected function get_items_per_page( $option, $default = 20 ) {
719 $per_page = (int) get_user_option( $option );
720 if ( empty( $per_page ) || $per_page < 1 )
721 $per_page = $default;
724 * Filters the number of items to be displayed on each page of the list table.
726 * The dynamic hook name, $option, refers to the `per_page` option depending
727 * on the type of list table in use. Possible values include: 'edit_comments_per_page',
728 * 'sites_network_per_page', 'site_themes_network_per_page', 'themes_network_per_page',
729 * 'users_network_per_page', 'edit_post_per_page', 'edit_page_per_page',
730 * 'edit_{$post_type}_per_page', etc.
734 * @param int $per_page Number of items to be displayed. Default 20.
736 return (int) apply_filters( $option, $per_page );
740 * Display the pagination.
745 * @param string $which
747 protected function pagination( $which ) {
748 if ( empty( $this->_pagination_args ) ) {
752 $total_items = $this->_pagination_args['total_items'];
753 $total_pages = $this->_pagination_args['total_pages'];
754 $infinite_scroll = false;
755 if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
756 $infinite_scroll = $this->_pagination_args['infinite_scroll'];
759 if ( 'top' === $which && $total_pages > 1 ) {
760 $this->screen->render_screen_reader_content( 'heading_pagination' );
763 $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
765 $current = $this->get_pagenum();
766 $removable_query_args = wp_removable_query_args();
768 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
770 $current_url = remove_query_arg( $removable_query_args, $current_url );
772 $page_links = array();
774 $total_pages_before = '<span class="paging-input">';
775 $total_pages_after = '</span></span>';
777 $disable_first = $disable_last = $disable_prev = $disable_next = false;
779 if ( $current == 1 ) {
780 $disable_first = true;
781 $disable_prev = true;
783 if ( $current == 2 ) {
784 $disable_first = true;
786 if ( $current == $total_pages ) {
787 $disable_last = true;
788 $disable_next = true;
790 if ( $current == $total_pages - 1 ) {
791 $disable_last = true;
794 if ( $disable_first ) {
795 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">«</span>';
797 $page_links[] = sprintf( "<a class='first-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
798 esc_url( remove_query_arg( 'paged', $current_url ) ),
804 if ( $disable_prev ) {
805 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">‹</span>';
807 $page_links[] = sprintf( "<a class='prev-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
808 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
809 __( 'Previous page' ),
814 if ( 'bottom' === $which ) {
815 $html_current_page = $current;
816 $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">';
818 $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' /><span class='tablenav-paging-text'>",
819 '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
821 strlen( $total_pages )
824 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
825 $page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;
827 if ( $disable_next ) {
828 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">›</span>';
830 $page_links[] = sprintf( "<a class='next-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
831 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
837 if ( $disable_last ) {
838 $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">»</span>';
840 $page_links[] = sprintf( "<a class='last-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
841 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
847 $pagination_links_class = 'pagination-links';
848 if ( ! empty( $infinite_scroll ) ) {
849 $pagination_links_class = ' hide-if-js';
851 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
853 if ( $total_pages ) {
854 $page_class = $total_pages < 2 ? ' one-page' : '';
856 $page_class = ' no-pages';
858 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
860 echo $this->_pagination;
864 * Get a list of columns. The format is:
865 * 'internal-name' => 'Title'
873 public function get_columns() {
874 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
878 * Get a list of sortable columns. The format is:
879 * 'internal-name' => 'orderby'
881 * 'internal-name' => array( 'orderby', true )
883 * The second format will make the initial sorting order be descending
890 protected function get_sortable_columns() {
895 * Gets the name of the default primary column.
900 * @return string Name of the default primary column, in this case, an empty string.
902 protected function get_default_primary_column_name() {
903 $columns = $this->get_columns();
906 if ( empty( $columns ) ) {
910 // We need a primary defined so responsive views show something,
911 // so let's fall back to the first non-checkbox column.
912 foreach ( $columns as $col => $column_name ) {
913 if ( 'cb' === $col ) {
925 * Public wrapper for WP_List_Table::get_default_primary_column_name().
930 * @return string Name of the default primary column.
932 public function get_primary_column() {
933 return $this->get_primary_column_name();
937 * Gets the name of the primary column.
942 * @return string The name of the primary column.
944 protected function get_primary_column_name() {
945 $columns = get_column_headers( $this->screen );
946 $default = $this->get_default_primary_column_name();
948 // If the primary column doesn't exist fall back to the
949 // first non-checkbox column.
950 if ( ! isset( $columns[ $default ] ) ) {
951 $default = WP_List_Table::get_default_primary_column_name();
955 * Filters the name of the primary column for the current list table.
959 * @param string $default Column name default for the specific list table, e.g. 'name'.
960 * @param string $context Screen ID for specific list table, e.g. 'plugins'.
962 $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
964 if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
972 * Get a list of all, hidden and sortable columns, with filter applied
979 protected function get_column_info() {
980 // $_column_headers is already set / cached
981 if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) {
982 // Back-compat for list tables that have been manually setting $_column_headers for horse reasons.
983 // In 4.3, we added a fourth argument for primary column.
984 $column_headers = array( array(), array(), array(), $this->get_primary_column_name() );
985 foreach ( $this->_column_headers as $key => $value ) {
986 $column_headers[ $key ] = $value;
989 return $column_headers;
992 $columns = get_column_headers( $this->screen );
993 $hidden = get_hidden_columns( $this->screen );
995 $sortable_columns = $this->get_sortable_columns();
997 * Filters the list table sortable columns for a specific screen.
999 * The dynamic portion of the hook name, `$this->screen->id`, refers
1000 * to the ID of the current screen, usually a string.
1004 * @param array $sortable_columns An array of sortable columns.
1006 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
1008 $sortable = array();
1009 foreach ( $_sortable as $id => $data ) {
1010 if ( empty( $data ) )
1013 $data = (array) $data;
1014 if ( !isset( $data[1] ) )
1017 $sortable[$id] = $data;
1020 $primary = $this->get_primary_column_name();
1021 $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
1023 return $this->_column_headers;
1027 * Return number of visible columns
1034 public function get_column_count() {
1035 list ( $columns, $hidden ) = $this->get_column_info();
1036 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
1037 return count( $columns ) - count( $hidden );
1041 * Print column headers, accounting for hidden and sortable columns.
1046 * @staticvar int $cb_counter
1048 * @param bool $with_id Whether to set the id attribute or not
1050 public function print_column_headers( $with_id = true ) {
1051 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
1053 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
1054 $current_url = remove_query_arg( 'paged', $current_url );
1056 if ( isset( $_GET['orderby'] ) ) {
1057 $current_orderby = $_GET['orderby'];
1059 $current_orderby = '';
1062 if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) {
1063 $current_order = 'desc';
1065 $current_order = 'asc';
1068 if ( ! empty( $columns['cb'] ) ) {
1069 static $cb_counter = 1;
1070 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
1071 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
1075 foreach ( $columns as $column_key => $column_display_name ) {
1076 $class = array( 'manage-column', "column-$column_key" );
1078 if ( in_array( $column_key, $hidden ) ) {
1079 $class[] = 'hidden';
1082 if ( 'cb' === $column_key )
1083 $class[] = 'check-column';
1084 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
1087 if ( $column_key === $primary ) {
1088 $class[] = 'column-primary';
1091 if ( isset( $sortable[$column_key] ) ) {
1092 list( $orderby, $desc_first ) = $sortable[$column_key];
1094 if ( $current_orderby === $orderby ) {
1095 $order = 'asc' === $current_order ? 'desc' : 'asc';
1096 $class[] = 'sorted';
1097 $class[] = $current_order;
1099 $order = $desc_first ? 'desc' : 'asc';
1100 $class[] = 'sortable';
1101 $class[] = $desc_first ? 'asc' : 'desc';
1104 $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>';
1107 $tag = ( 'cb' === $column_key ) ? 'td' : 'th';
1108 $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
1109 $id = $with_id ? "id='$column_key'" : '';
1111 if ( !empty( $class ) )
1112 $class = "class='" . join( ' ', $class ) . "'";
1114 echo "<$tag $scope $id $class>$column_display_name</$tag>";
1124 public function display() {
1125 $singular = $this->_args['singular'];
1127 $this->display_tablenav( 'top' );
1129 $this->screen->render_screen_reader_content( 'heading_list' );
1131 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
1134 <?php $this->print_column_headers(); ?>
1138 <tbody id="the-list"<?php
1140 echo " data-wp-lists='list:$singular'";
1142 <?php $this->display_rows_or_placeholder(); ?>
1147 <?php $this->print_column_headers( false ); ?>
1153 $this->display_tablenav( 'bottom' );
1157 * Get a list of CSS classes for the WP_List_Table table tag.
1162 * @return array List of CSS classes for the table tag.
1164 protected function get_table_classes() {
1165 return array( 'widefat', 'fixed', 'striped', $this->_args['plural'] );
1169 * Generate the table navigation above or below the table
1173 * @param string $which
1175 protected function display_tablenav( $which ) {
1176 if ( 'top' === $which ) {
1177 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
1180 <div class="tablenav <?php echo esc_attr( $which ); ?>">
1182 <?php if ( $this->has_items() ): ?>
1183 <div class="alignleft actions bulkactions">
1184 <?php $this->bulk_actions( $which ); ?>
1187 $this->extra_tablenav( $which );
1188 $this->pagination( $which );
1191 <br class="clear" />
1197 * Extra controls to be displayed between bulk actions and pagination
1202 * @param string $which
1204 protected function extra_tablenav( $which ) {}
1207 * Generate the tbody element for the list table.
1212 public function display_rows_or_placeholder() {
1213 if ( $this->has_items() ) {
1214 $this->display_rows();
1216 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
1223 * Generate the table rows
1228 public function display_rows() {
1229 foreach ( $this->items as $item )
1230 $this->single_row( $item );
1234 * Generates content for a single row of the table
1239 * @param object $item The current item
1241 public function single_row( $item ) {
1243 $this->single_row_columns( $item );
1249 * @param object $item
1250 * @param string $column_name
1252 protected function column_default( $item, $column_name ) {}
1256 * @param object $item
1258 protected function column_cb( $item ) {}
1261 * Generates the columns for a single row of the table
1266 * @param object $item The current item
1268 protected function single_row_columns( $item ) {
1269 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
1271 foreach ( $columns as $column_name => $column_display_name ) {
1272 $classes = "$column_name column-$column_name";
1273 if ( $primary === $column_name ) {
1274 $classes .= ' has-row-actions column-primary';
1277 if ( in_array( $column_name, $hidden ) ) {
1278 $classes .= ' hidden';
1281 // Comments column uses HTML in the display name with screen reader text.
1282 // Instead of using esc_attr(), we strip tags to get closer to a user-friendly string.
1283 $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
1285 $attributes = "class='$classes' $data";
1287 if ( 'cb' === $column_name ) {
1288 echo '<th scope="row" class="check-column">';
1289 echo $this->column_cb( $item );
1291 } elseif ( method_exists( $this, '_column_' . $column_name ) ) {
1292 echo call_user_func(
1293 array( $this, '_column_' . $column_name ),
1299 } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
1300 echo "<td $attributes>";
1301 echo call_user_func( array( $this, 'column_' . $column_name ), $item );
1302 echo $this->handle_row_actions( $item, $column_name, $primary );
1305 echo "<td $attributes>";
1306 echo $this->column_default( $item, $column_name );
1307 echo $this->handle_row_actions( $item, $column_name, $primary );
1314 * Generates and display row actions links for the list table.
1319 * @param object $item The item being acted upon.
1320 * @param string $column_name Current column name.
1321 * @param string $primary Primary column name.
1322 * @return string The row actions HTML, or an empty string if the current column is the primary column.
1324 protected function handle_row_actions( $item, $column_name, $primary ) {
1325 return $column_name === $primary ? '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>' : '';
1329 * Handle an incoming ajax request (called from admin-ajax.php)
1334 public function ajax_response() {
1335 $this->prepare_items();
1338 if ( ! empty( $_REQUEST['no_placeholder'] ) ) {
1339 $this->display_rows();
1341 $this->display_rows_or_placeholder();
1344 $rows = ob_get_clean();
1346 $response = array( 'rows' => $rows );
1348 if ( isset( $this->_pagination_args['total_items'] ) ) {
1349 $response['total_items_i18n'] = sprintf(
1350 _n( '%s item', '%s items', $this->_pagination_args['total_items'] ),
1351 number_format_i18n( $this->_pagination_args['total_items'] )
1354 if ( isset( $this->_pagination_args['total_pages'] ) ) {
1355 $response['total_pages'] = $this->_pagination_args['total_pages'];
1356 $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] );
1359 die( wp_json_encode( $response ) );
1363 * Send required variables to JavaScript land
1367 public function _js_vars() {
1369 'class' => get_class( $this ),
1371 'id' => $this->screen->id,
1372 'base' => $this->screen->base,
1376 printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );