3 * Base class for displaying a list of items in an ajaxified HTML table.
6 * @subpackage List_Table
13 * The current list of items
22 * Various information about the current table
31 * Various information needed for displaying the pagination
37 var $_pagination_args = array();
58 * Cached pagination output
67 * Constructor. The child class should call this constructor from its own constructor
69 * @param array $args An associative array with information about the current table
72 function __construct( $args = array() ) {
73 $args = wp_parse_args( $args, array(
80 $this->screen = convert_to_screen( $args['screen'] );
82 add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
84 if ( !$args['plural'] )
85 $args['plural'] = $this->screen->base;
87 $args['plural'] = sanitize_key( $args['plural'] );
88 $args['singular'] = sanitize_key( $args['singular'] );
92 if ( $args['ajax'] ) {
93 // wp_enqueue_script( 'list-table' );
94 add_action( 'admin_footer', array( $this, '_js_vars' ) );
99 * Checks the current user's permissions
106 function ajax_user_can() {
107 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
111 * Prepares the list of items for displaying.
112 * @uses WP_List_Table::set_pagination_args()
118 function prepare_items() {
119 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
123 * An internal method that sets all the necessary pagination arguments
125 * @param array $args An associative array with information about the pagination
128 function set_pagination_args( $args ) {
129 $args = wp_parse_args( $args, array(
135 if ( !$args['total_pages'] && $args['per_page'] > 0 )
136 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
138 // redirect if page number is invalid and headers are not already sent
139 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
140 wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
144 $this->_pagination_args = $args;
148 * Access the pagination args
156 function get_pagination_arg( $key ) {
157 if ( 'page' == $key )
158 return $this->get_pagenum();
160 if ( isset( $this->_pagination_args[$key] ) )
161 return $this->_pagination_args[$key];
165 * Whether the table has items to display or not
172 function has_items() {
173 return !empty( $this->items );
177 * Message to be displayed when there are no items
182 function no_items() {
183 _e( 'No items found.' );
187 * Display the search box.
192 * @param string $text The search button text
193 * @param string $input_id The search input id
195 function search_box( $text, $input_id ) {
196 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
199 $input_id = $input_id . '-search-input';
201 if ( ! empty( $_REQUEST['orderby'] ) )
202 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
203 if ( ! empty( $_REQUEST['order'] ) )
204 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
205 if ( ! empty( $_REQUEST['post_mime_type'] ) )
206 echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
207 if ( ! empty( $_REQUEST['detached'] ) )
208 echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
210 <p class="search-box">
211 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
212 <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
213 <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
219 * Get an associative array ( id => link ) with the list
220 * of views available on this table.
227 function get_views() {
232 * Display the list of views available on this table.
238 $views = $this->get_views();
240 * Filter the list of available list table views.
242 * The dynamic portion of the hook name, $this->screen->id, refers
243 * to the ID of the current screen, usually a string.
247 * @param array $views An array of available list table views.
249 $views = apply_filters( "views_{$this->screen->id}", $views );
251 if ( empty( $views ) )
254 echo "<ul class='subsubsub'>\n";
255 foreach ( $views as $class => $view ) {
256 $views[ $class ] = "\t<li class='$class'>$view";
258 echo implode( " |</li>\n", $views ) . "</li>\n";
263 * Get an associative array ( option_name => option_title ) with the list
264 * of bulk actions available on this table.
271 function get_bulk_actions() {
276 * Display the bulk actions dropdown.
281 function bulk_actions() {
282 if ( is_null( $this->_actions ) ) {
283 $no_new_actions = $this->_actions = $this->get_bulk_actions();
285 * Filter the list table Bulk Actions drop-down.
287 * The dynamic portion of the hook name, $this->screen->id, refers
288 * to the ID of the current screen, usually a string.
290 * This filter can currently only be used to remove bulk actions.
294 * @param array $actions An array of the available bulk actions.
296 $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
297 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
303 if ( empty( $this->_actions ) )
306 echo "<select name='action$two'>\n";
307 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
309 foreach ( $this->_actions as $name => $title ) {
310 $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
312 echo "\t<option value='$name'$class>$title</option>\n";
317 submit_button( __( 'Apply' ), 'action', false, false, array( 'id' => "doaction$two" ) );
322 * Get the current action selected from the bulk actions dropdown.
327 * @return string|bool The action name or False if no action was selected
329 function current_action() {
330 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
331 return $_REQUEST['action'];
333 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
334 return $_REQUEST['action2'];
340 * Generate row actions div
345 * @param array $actions The list of actions
346 * @param bool $always_visible Whether the actions should be always visible
349 function row_actions( $actions, $always_visible = false ) {
350 $action_count = count( $actions );
353 if ( !$action_count )
356 $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
357 foreach ( $actions as $action => $link ) {
359 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
360 $out .= "<span class='$action'>$link$sep</span>";
368 * Display a monthly dropdown for filtering items
373 function months_dropdown( $post_type ) {
374 global $wpdb, $wp_locale;
376 $months = $wpdb->get_results( $wpdb->prepare( "
377 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
380 ORDER BY post_date DESC
384 * Filter the 'Months' drop-down results.
388 * @param object $months The months drop-down query results.
389 * @param string $post_type The post type.
391 $months = apply_filters( 'months_dropdown_results', $months, $post_type );
393 $month_count = count( $months );
395 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
398 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
401 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
403 foreach ( $months as $arc_row ) {
404 if ( 0 == $arc_row->year )
407 $month = zeroise( $arc_row->month, 2 );
408 $year = $arc_row->year;
410 printf( "<option %s value='%s'>%s</option>\n",
411 selected( $m, $year . $month, false ),
412 esc_attr( $arc_row->year . $month ),
413 /* translators: 1: month name, 2: 4-digit year */
414 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
423 * Display a view switcher
428 function view_switcher( $current_mode ) {
430 'list' => __( 'List View' ),
431 'excerpt' => __( 'Excerpt View' )
435 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
436 <div class="view-switch">
438 foreach ( $modes as $mode => $title ) {
439 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
440 echo "<a href='" . esc_url( add_query_arg( 'mode', $mode, $_SERVER['REQUEST_URI'] ) ) . "' $class><img id='view-switch-$mode' src='" . esc_url( includes_url( 'images/blank.gif' ) ) . "' width='20' height='20' title='$title' alt='$title' /></a>\n";
448 * Display a comment count bubble
453 * @param int $post_id
454 * @param int $pending_comments
456 function comments_bubble( $post_id, $pending_comments ) {
457 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
459 if ( $pending_comments )
462 echo "<a href='" . esc_url( add_query_arg( 'p', $post_id, admin_url( 'edit-comments.php' ) ) ) . "' title='" . esc_attr( $pending_phrase ) . "' class='post-com-count'><span class='comment-count'>" . number_format_i18n( get_comments_number() ) . "</span></a>";
464 if ( $pending_comments )
469 * Get the current page number
476 function get_pagenum() {
477 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
479 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
480 $pagenum = $this->_pagination_args['total_pages'];
482 return max( 1, $pagenum );
486 * Get number of items to display on a single page
493 function get_items_per_page( $option, $default = 20 ) {
494 $per_page = (int) get_user_option( $option );
495 if ( empty( $per_page ) || $per_page < 1 )
496 $per_page = $default;
499 * Filter the number of items to be displayed on each page of the list table.
501 * The dynamic hook name, $option, refers to the per page option depending
502 * on the type of list table in use. Possible values may include:
503 * 'edit_comments_per_page', 'sites_network_per_page', 'site_themes_network_per_page',
504 * 'themes_netework_per_page', 'users_network_per_page', 'edit_{$post_type}', etc.
508 * @param int $per_page Number of items to be displayed. Default 20.
510 return (int) apply_filters( $option, $per_page );
514 * Display the pagination.
519 function pagination( $which ) {
520 if ( empty( $this->_pagination_args ) )
523 extract( $this->_pagination_args, EXTR_SKIP );
525 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
527 $current = $this->get_pagenum();
529 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
531 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
533 $page_links = array();
535 $disable_first = $disable_last = '';
537 $disable_first = ' disabled';
538 if ( $current == $total_pages )
539 $disable_last = ' disabled';
541 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
542 'first-page' . $disable_first,
543 esc_attr__( 'Go to the first page' ),
544 esc_url( remove_query_arg( 'paged', $current_url ) ),
548 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
549 'prev-page' . $disable_first,
550 esc_attr__( 'Go to the previous page' ),
551 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
555 if ( 'bottom' == $which )
556 $html_current_page = $current;
558 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
559 esc_attr__( 'Current page' ),
561 strlen( $total_pages )
564 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
565 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
567 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
568 'next-page' . $disable_last,
569 esc_attr__( 'Go to the next page' ),
570 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
574 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
575 'last-page' . $disable_last,
576 esc_attr__( 'Go to the last page' ),
577 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
581 $pagination_links_class = 'pagination-links';
582 if ( ! empty( $infinite_scroll ) )
583 $pagination_links_class = ' hide-if-js';
584 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
587 $page_class = $total_pages < 2 ? ' one-page' : '';
589 $page_class = ' no-pages';
591 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
593 echo $this->_pagination;
597 * Get a list of columns. The format is:
598 * 'internal-name' => 'Title'
606 function get_columns() {
607 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
611 * Get a list of sortable columns. The format is:
612 * 'internal-name' => 'orderby'
614 * 'internal-name' => array( 'orderby', true )
616 * The second format will make the initial sorting order be descending
623 function get_sortable_columns() {
628 * Get a list of all, hidden and sortable columns, with filter applied
635 function get_column_info() {
636 if ( isset( $this->_column_headers ) )
637 return $this->_column_headers;
639 $columns = get_column_headers( $this->screen );
640 $hidden = get_hidden_columns( $this->screen );
642 $sortable_columns = $this->get_sortable_columns();
644 * Filter the list table sortable columns for a specific screen.
646 * The dynamic portion of the hook name, $this->screen->id, refers
647 * to the ID of the current screen, usually a string.
651 * @param array $sortable_columns An array of sortable columns.
653 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
656 foreach ( $_sortable as $id => $data ) {
657 if ( empty( $data ) )
660 $data = (array) $data;
661 if ( !isset( $data[1] ) )
664 $sortable[$id] = $data;
667 $this->_column_headers = array( $columns, $hidden, $sortable );
669 return $this->_column_headers;
673 * Return number of visible columns
680 function get_column_count() {
681 list ( $columns, $hidden ) = $this->get_column_info();
682 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
683 return count( $columns ) - count( $hidden );
687 * Print column headers, accounting for hidden and sortable columns.
692 * @param bool $with_id Whether to set the id attribute or not
694 function print_column_headers( $with_id = true ) {
695 list( $columns, $hidden, $sortable ) = $this->get_column_info();
697 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
698 $current_url = remove_query_arg( 'paged', $current_url );
700 if ( isset( $_GET['orderby'] ) )
701 $current_orderby = $_GET['orderby'];
703 $current_orderby = '';
705 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
706 $current_order = 'desc';
708 $current_order = 'asc';
710 if ( ! empty( $columns['cb'] ) ) {
711 static $cb_counter = 1;
712 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
713 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
717 foreach ( $columns as $column_key => $column_display_name ) {
718 $class = array( 'manage-column', "column-$column_key" );
721 if ( in_array( $column_key, $hidden ) )
722 $style = 'display:none;';
724 $style = ' style="' . $style . '"';
726 if ( 'cb' == $column_key )
727 $class[] = 'check-column';
728 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
731 if ( isset( $sortable[$column_key] ) ) {
732 list( $orderby, $desc_first ) = $sortable[$column_key];
734 if ( $current_orderby == $orderby ) {
735 $order = 'asc' == $current_order ? 'desc' : 'asc';
737 $class[] = $current_order;
739 $order = $desc_first ? 'desc' : 'asc';
740 $class[] = 'sortable';
741 $class[] = $desc_first ? 'asc' : 'desc';
744 $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>';
747 $id = $with_id ? "id='$column_key'" : '';
749 if ( !empty( $class ) )
750 $class = "class='" . join( ' ', $class ) . "'";
752 echo "<th scope='col' $id $class $style>$column_display_name</th>";
763 extract( $this->_args );
765 $this->display_tablenav( 'top' );
768 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
771 <?php $this->print_column_headers(); ?>
777 <?php $this->print_column_headers( false ); ?>
781 <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
782 <?php $this->display_rows_or_placeholder(); ?>
786 $this->display_tablenav( 'bottom' );
790 * Get a list of CSS classes for the <table> tag
797 function get_table_classes() {
798 return array( 'widefat', 'fixed', $this->_args['plural'] );
802 * Generate the table navigation above or below the table
807 function display_tablenav( $which ) {
808 if ( 'top' == $which )
809 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
811 <div class="tablenav <?php echo esc_attr( $which ); ?>">
813 <div class="alignleft actions bulkactions">
814 <?php $this->bulk_actions(); ?>
817 $this->extra_tablenav( $which );
818 $this->pagination( $which );
827 * Extra controls to be displayed between bulk actions and pagination
832 function extra_tablenav( $which ) {}
835 * Generate the <tbody> part of the table
840 function display_rows_or_placeholder() {
841 if ( $this->has_items() ) {
842 $this->display_rows();
844 list( $columns, $hidden ) = $this->get_column_info();
845 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
852 * Generate the table rows
857 function display_rows() {
858 foreach ( $this->items as $item )
859 $this->single_row( $item );
863 * Generates content for a single row of the table
868 * @param object $item The current item
870 function single_row( $item ) {
871 static $row_class = '';
872 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
874 echo '<tr' . $row_class . '>';
875 $this->single_row_columns( $item );
880 * Generates the columns for a single row of the table
885 * @param object $item The current item
887 function single_row_columns( $item ) {
888 list( $columns, $hidden ) = $this->get_column_info();
890 foreach ( $columns as $column_name => $column_display_name ) {
891 $class = "class='$column_name column-$column_name'";
894 if ( in_array( $column_name, $hidden ) )
895 $style = ' style="display:none;"';
897 $attributes = "$class$style";
899 if ( 'cb' == $column_name ) {
900 echo '<th scope="row" class="check-column">';
901 echo $this->column_cb( $item );
904 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
905 echo "<td $attributes>";
906 echo call_user_func( array( $this, 'column_' . $column_name ), $item );
910 echo "<td $attributes>";
911 echo $this->column_default( $item, $column_name );
918 * Handle an incoming ajax request (called from admin-ajax.php)
923 function ajax_response() {
924 $this->prepare_items();
926 extract( $this->_args );
927 extract( $this->_pagination_args, EXTR_SKIP );
930 if ( ! empty( $_REQUEST['no_placeholder'] ) )
931 $this->display_rows();
933 $this->display_rows_or_placeholder();
935 $rows = ob_get_clean();
937 $response = array( 'rows' => $rows );
939 if ( isset( $total_items ) )
940 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
942 if ( isset( $total_pages ) ) {
943 $response['total_pages'] = $total_pages;
944 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
947 die( json_encode( $response ) );
951 * Send required variables to JavaScript land
955 function _js_vars() {
957 'class' => get_class( $this ),
959 'id' => $this->screen->id,
960 'base' => $this->screen->base,
964 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );