3 * Base class for displaying a list of items in an ajaxified HTML table.
6 * @subpackage List_Table
11 * Base class for displaying a list of items in an ajaxified HTML table.
14 * @subpackage List_Table
21 * The current list of items
30 * Various information about the current table
39 * Various information needed for displaying the pagination
45 var $_pagination_args = array();
66 * Cached pagination output
75 * Constructor. The child class should call this constructor from its own constructor
77 * @param array $args An associative array with information about the current table
80 function __construct( $args = array() ) {
81 $args = wp_parse_args( $args, array(
88 $this->screen = convert_to_screen( $args['screen'] );
90 add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
92 if ( !$args['plural'] )
93 $args['plural'] = $this->screen->base;
95 $args['plural'] = sanitize_key( $args['plural'] );
96 $args['singular'] = sanitize_key( $args['singular'] );
100 if ( $args['ajax'] ) {
101 // wp_enqueue_script( 'list-table' );
102 add_action( 'admin_footer', array( $this, '_js_vars' ) );
107 * Checks the current user's permissions
114 function ajax_user_can() {
115 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
119 * Prepares the list of items for displaying.
120 * @uses WP_List_Table::set_pagination_args()
126 function prepare_items() {
127 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
131 * An internal method that sets all the necessary pagination arguments
133 * @param array $args An associative array with information about the pagination
136 function set_pagination_args( $args ) {
137 $args = wp_parse_args( $args, array(
143 if ( !$args['total_pages'] && $args['per_page'] > 0 )
144 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
146 // redirect if page number is invalid and headers are not already sent
147 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
148 wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
152 $this->_pagination_args = $args;
156 * Access the pagination args
164 function get_pagination_arg( $key ) {
165 if ( 'page' == $key )
166 return $this->get_pagenum();
168 if ( isset( $this->_pagination_args[$key] ) )
169 return $this->_pagination_args[$key];
173 * Whether the table has items to display or not
180 function has_items() {
181 return !empty( $this->items );
185 * Message to be displayed when there are no items
190 function no_items() {
191 _e( 'No items found.' );
195 * Display the search box.
200 * @param string $text The search button text
201 * @param string $input_id The search input id
203 function search_box( $text, $input_id ) {
204 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
207 $input_id = $input_id . '-search-input';
209 if ( ! empty( $_REQUEST['orderby'] ) )
210 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
211 if ( ! empty( $_REQUEST['order'] ) )
212 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
213 if ( ! empty( $_REQUEST['post_mime_type'] ) )
214 echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
215 if ( ! empty( $_REQUEST['detached'] ) )
216 echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
218 <p class="search-box">
219 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
220 <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
221 <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
227 * Get an associative array ( id => link ) with the list
228 * of views available on this table.
235 function get_views() {
240 * Display the list of views available on this table.
246 $views = $this->get_views();
247 $views = apply_filters( 'views_' . $this->screen->id, $views );
249 if ( empty( $views ) )
252 echo "<ul class='subsubsub'>\n";
253 foreach ( $views as $class => $view ) {
254 $views[ $class ] = "\t<li class='$class'>$view";
256 echo implode( " |</li>\n", $views ) . "</li>\n";
261 * Get an associative array ( option_name => option_title ) with the list
262 * of bulk actions available on this table.
269 function get_bulk_actions() {
274 * Display the bulk actions dropdown.
279 function bulk_actions() {
280 if ( is_null( $this->_actions ) ) {
281 $no_new_actions = $this->_actions = $this->get_bulk_actions();
282 // This filter can currently only be used to remove actions.
283 $this->_actions = apply_filters( 'bulk_actions-' . $this->screen->id, $this->_actions );
284 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
290 if ( empty( $this->_actions ) )
293 echo "<select name='action$two'>\n";
294 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
296 foreach ( $this->_actions as $name => $title ) {
297 $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
299 echo "\t<option value='$name'$class>$title</option>\n";
304 submit_button( __( 'Apply' ), 'action', false, false, array( 'id' => "doaction$two" ) );
309 * Get the current action selected from the bulk actions dropdown.
314 * @return string|bool The action name or False if no action was selected
316 function current_action() {
317 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
318 return $_REQUEST['action'];
320 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
321 return $_REQUEST['action2'];
327 * Generate row actions div
332 * @param array $actions The list of actions
333 * @param bool $always_visible Whether the actions should be always visible
336 function row_actions( $actions, $always_visible = false ) {
337 $action_count = count( $actions );
340 if ( !$action_count )
343 $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
344 foreach ( $actions as $action => $link ) {
346 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
347 $out .= "<span class='$action'>$link$sep</span>";
355 * Display a monthly dropdown for filtering items
360 function months_dropdown( $post_type ) {
361 global $wpdb, $wp_locale;
363 $months = $wpdb->get_results( $wpdb->prepare( "
364 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
367 ORDER BY post_date DESC
371 * Filter the months dropdown results.
375 * @param object $months The months dropdown query results.
376 * @param string $post_type The post type.
378 $months = apply_filters( 'months_dropdown_results', $months, $post_type );
380 $month_count = count( $months );
382 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
385 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
388 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
390 foreach ( $months as $arc_row ) {
391 if ( 0 == $arc_row->year )
394 $month = zeroise( $arc_row->month, 2 );
395 $year = $arc_row->year;
397 printf( "<option %s value='%s'>%s</option>\n",
398 selected( $m, $year . $month, false ),
399 esc_attr( $arc_row->year . $month ),
400 /* translators: 1: month name, 2: 4-digit year */
401 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
410 * Display a view switcher
415 function view_switcher( $current_mode ) {
417 'list' => __( 'List View' ),
418 'excerpt' => __( 'Excerpt View' )
422 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
423 <div class="view-switch">
425 foreach ( $modes as $mode => $title ) {
426 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
427 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";
435 * Display a comment count bubble
440 * @param int $post_id
441 * @param int $pending_comments
443 function comments_bubble( $post_id, $pending_comments ) {
444 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
446 if ( $pending_comments )
449 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>";
451 if ( $pending_comments )
456 * Get the current page number
463 function get_pagenum() {
464 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
466 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
467 $pagenum = $this->_pagination_args['total_pages'];
469 return max( 1, $pagenum );
473 * Get number of items to display on a single page
480 function get_items_per_page( $option, $default = 20 ) {
481 $per_page = (int) get_user_option( $option );
482 if ( empty( $per_page ) || $per_page < 1 )
483 $per_page = $default;
485 return (int) apply_filters( $option, $per_page );
489 * Display the pagination.
494 function pagination( $which ) {
495 if ( empty( $this->_pagination_args ) )
498 extract( $this->_pagination_args, EXTR_SKIP );
500 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
502 $current = $this->get_pagenum();
504 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
506 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
508 $page_links = array();
510 $disable_first = $disable_last = '';
512 $disable_first = ' disabled';
513 if ( $current == $total_pages )
514 $disable_last = ' disabled';
516 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
517 'first-page' . $disable_first,
518 esc_attr__( 'Go to the first page' ),
519 esc_url( remove_query_arg( 'paged', $current_url ) ),
523 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
524 'prev-page' . $disable_first,
525 esc_attr__( 'Go to the previous page' ),
526 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
530 if ( 'bottom' == $which )
531 $html_current_page = $current;
533 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
534 esc_attr__( 'Current page' ),
536 strlen( $total_pages )
539 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
540 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
542 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
543 'next-page' . $disable_last,
544 esc_attr__( 'Go to the next page' ),
545 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
549 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
550 'last-page' . $disable_last,
551 esc_attr__( 'Go to the last page' ),
552 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
556 $pagination_links_class = 'pagination-links';
557 if ( ! empty( $infinite_scroll ) )
558 $pagination_links_class = ' hide-if-js';
559 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
562 $page_class = $total_pages < 2 ? ' one-page' : '';
564 $page_class = ' no-pages';
566 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
568 echo $this->_pagination;
572 * Get a list of columns. The format is:
573 * 'internal-name' => 'Title'
581 function get_columns() {
582 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
586 * Get a list of sortable columns. The format is:
587 * 'internal-name' => 'orderby'
589 * 'internal-name' => array( 'orderby', true )
591 * The second format will make the initial sorting order be descending
598 function get_sortable_columns() {
603 * Get a list of all, hidden and sortable columns, with filter applied
610 function get_column_info() {
611 if ( isset( $this->_column_headers ) )
612 return $this->_column_headers;
614 $columns = get_column_headers( $this->screen );
615 $hidden = get_hidden_columns( $this->screen );
617 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $this->get_sortable_columns() );
620 foreach ( $_sortable as $id => $data ) {
621 if ( empty( $data ) )
624 $data = (array) $data;
625 if ( !isset( $data[1] ) )
628 $sortable[$id] = $data;
631 $this->_column_headers = array( $columns, $hidden, $sortable );
633 return $this->_column_headers;
637 * Return number of visible columns
644 function get_column_count() {
645 list ( $columns, $hidden ) = $this->get_column_info();
646 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
647 return count( $columns ) - count( $hidden );
651 * Print column headers, accounting for hidden and sortable columns.
656 * @param bool $with_id Whether to set the id attribute or not
658 function print_column_headers( $with_id = true ) {
659 list( $columns, $hidden, $sortable ) = $this->get_column_info();
661 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
662 $current_url = remove_query_arg( 'paged', $current_url );
664 if ( isset( $_GET['orderby'] ) )
665 $current_orderby = $_GET['orderby'];
667 $current_orderby = '';
669 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
670 $current_order = 'desc';
672 $current_order = 'asc';
674 if ( ! empty( $columns['cb'] ) ) {
675 static $cb_counter = 1;
676 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
677 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
681 foreach ( $columns as $column_key => $column_display_name ) {
682 $class = array( 'manage-column', "column-$column_key" );
685 if ( in_array( $column_key, $hidden ) )
686 $style = 'display:none;';
688 $style = ' style="' . $style . '"';
690 if ( 'cb' == $column_key )
691 $class[] = 'check-column';
692 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
695 if ( isset( $sortable[$column_key] ) ) {
696 list( $orderby, $desc_first ) = $sortable[$column_key];
698 if ( $current_orderby == $orderby ) {
699 $order = 'asc' == $current_order ? 'desc' : 'asc';
701 $class[] = $current_order;
703 $order = $desc_first ? 'desc' : 'asc';
704 $class[] = 'sortable';
705 $class[] = $desc_first ? 'asc' : 'desc';
708 $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>';
711 $id = $with_id ? "id='$column_key'" : '';
713 if ( !empty( $class ) )
714 $class = "class='" . join( ' ', $class ) . "'";
716 echo "<th scope='col' $id $class $style>$column_display_name</th>";
727 extract( $this->_args );
729 $this->display_tablenav( 'top' );
732 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
735 <?php $this->print_column_headers(); ?>
741 <?php $this->print_column_headers( false ); ?>
745 <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
746 <?php $this->display_rows_or_placeholder(); ?>
750 $this->display_tablenav( 'bottom' );
754 * Get a list of CSS classes for the <table> tag
761 function get_table_classes() {
762 return array( 'widefat', 'fixed', $this->_args['plural'] );
766 * Generate the table navigation above or below the table
771 function display_tablenav( $which ) {
772 if ( 'top' == $which )
773 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
775 <div class="tablenav <?php echo esc_attr( $which ); ?>">
777 <div class="alignleft actions bulkactions">
778 <?php $this->bulk_actions(); ?>
781 $this->extra_tablenav( $which );
782 $this->pagination( $which );
791 * Extra controls to be displayed between bulk actions and pagination
796 function extra_tablenav( $which ) {}
799 * Generate the <tbody> part of the table
804 function display_rows_or_placeholder() {
805 if ( $this->has_items() ) {
806 $this->display_rows();
808 list( $columns, $hidden ) = $this->get_column_info();
809 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
816 * Generate the table rows
821 function display_rows() {
822 foreach ( $this->items as $item )
823 $this->single_row( $item );
827 * Generates content for a single row of the table
832 * @param object $item The current item
834 function single_row( $item ) {
835 static $row_class = '';
836 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
838 echo '<tr' . $row_class . '>';
839 $this->single_row_columns( $item );
844 * Generates the columns for a single row of the table
849 * @param object $item The current item
851 function single_row_columns( $item ) {
852 list( $columns, $hidden ) = $this->get_column_info();
854 foreach ( $columns as $column_name => $column_display_name ) {
855 $class = "class='$column_name column-$column_name'";
858 if ( in_array( $column_name, $hidden ) )
859 $style = ' style="display:none;"';
861 $attributes = "$class$style";
863 if ( 'cb' == $column_name ) {
864 echo '<th scope="row" class="check-column">';
865 echo $this->column_cb( $item );
868 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
869 echo "<td $attributes>";
870 echo call_user_func( array( $this, 'column_' . $column_name ), $item );
874 echo "<td $attributes>";
875 echo $this->column_default( $item, $column_name );
882 * Handle an incoming ajax request (called from admin-ajax.php)
887 function ajax_response() {
888 $this->prepare_items();
890 extract( $this->_args );
891 extract( $this->_pagination_args, EXTR_SKIP );
894 if ( ! empty( $_REQUEST['no_placeholder'] ) )
895 $this->display_rows();
897 $this->display_rows_or_placeholder();
899 $rows = ob_get_clean();
901 $response = array( 'rows' => $rows );
903 if ( isset( $total_items ) )
904 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
906 if ( isset( $total_pages ) ) {
907 $response['total_pages'] = $total_pages;
908 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
911 die( json_encode( $response ) );
915 * Send required variables to JavaScript land
919 function _js_vars() {
921 'class' => get_class( $this ),
923 'id' => $this->screen->id,
924 'base' => $this->screen->base,
928 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );