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 it's 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(
87 $screen = get_current_screen();
89 add_filter( "manage_{$screen->id}_columns", array( &$this, 'get_columns' ), 0 );
91 if ( !$args['plural'] )
92 $args['plural'] = $screen->base;
96 if ( $args['ajax'] ) {
97 // wp_enqueue_script( 'list-table' );
98 add_action( 'admin_footer', array( &$this, '_js_vars' ) );
103 * Checks the current user's permissions
110 function ajax_user_can() {
111 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
115 * Prepares the list of items for displaying.
116 * @uses WP_List_Table::set_pagination_args()
122 function prepare_items() {
123 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
127 * An internal method that sets all the necessary pagination arguments
129 * @param array $args An associative array with information about the pagination
132 function set_pagination_args( $args ) {
133 $args = wp_parse_args( $args, array(
139 if ( !$args['total_pages'] && $args['per_page'] > 0 )
140 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
142 // redirect if page number is invalid and headers are not already sent
143 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
144 wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
148 $this->_pagination_args = $args;
152 * Access the pagination args
160 function get_pagination_arg( $key ) {
161 if ( 'page' == $key )
162 return $this->get_pagenum();
164 if ( isset( $this->_pagination_args[$key] ) )
165 return $this->_pagination_args[$key];
169 * Whether the table has items to display or not
176 function has_items() {
177 return !empty( $this->items );
181 * Message to be displayed when there are no items
186 function no_items() {
187 _e( 'No items found.' );
191 * Display the search box.
196 * @param string $text The search button text
197 * @param string $input_id The search input id
199 function search_box( $text, $input_id ) {
200 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
203 $input_id = $input_id . '-search-input';
205 if ( ! empty( $_REQUEST['orderby'] ) )
206 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
207 if ( ! empty( $_REQUEST['order'] ) )
208 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
210 <p class="search-box">
211 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
212 <input type="text" 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 bulk actions dropdown.
238 $screen = get_current_screen();
240 $views = $this->get_views();
241 $views = apply_filters( 'views_' . $screen->id, $views );
243 if ( empty( $views ) )
246 echo "<ul class='subsubsub'>\n";
247 foreach ( $views as $class => $view ) {
248 $views[ $class ] = "\t<li class='$class'>$view";
250 echo implode( " |</li>\n", $views ) . "</li>\n";
255 * Get an associative array ( option_name => option_title ) with the list
256 * of bulk actions available on this table.
263 function get_bulk_actions() {
268 * Display the bulk actions dropdown.
273 function bulk_actions() {
274 $screen = get_current_screen();
276 if ( is_null( $this->_actions ) ) {
277 $no_new_actions = $this->_actions = $this->get_bulk_actions();
278 // This filter can currently only be used to remove actions.
279 $this->_actions = apply_filters( 'bulk_actions-' . $screen->id, $this->_actions );
280 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
286 if ( empty( $this->_actions ) )
289 echo "<select name='action$two'>\n";
290 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
292 foreach ( $this->_actions as $name => $title ) {
293 $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
295 echo "\t<option value='$name'$class>$title</option>\n";
300 submit_button( __( 'Apply' ), 'button-secondary action', false, false, array( 'id' => "doaction$two" ) );
305 * Get the current action selected from the bulk actions dropdown.
310 * @return string|bool The action name or False if no action was selected
312 function current_action() {
313 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
314 return $_REQUEST['action'];
316 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
317 return $_REQUEST['action2'];
323 * Generate row actions div
328 * @param array $actions The list of actions
329 * @param bool $always_visible Wether the actions should be always visible
332 function row_actions( $actions, $always_visible = false ) {
333 $action_count = count( $actions );
336 if ( !$action_count )
339 $out = '<div class="' . ( $always_visible ? 'row-actions-visible' : 'row-actions' ) . '">';
340 foreach ( $actions as $action => $link ) {
342 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
343 $out .= "<span class='$action'>$link$sep</span>";
351 * Display a monthly dropdown for filtering items
356 function months_dropdown( $post_type ) {
357 global $wpdb, $wp_locale;
359 $months = $wpdb->get_results( $wpdb->prepare( "
360 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
363 ORDER BY post_date DESC
366 $month_count = count( $months );
368 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
371 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
374 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
376 foreach ( $months as $arc_row ) {
377 if ( 0 == $arc_row->year )
380 $month = zeroise( $arc_row->month, 2 );
381 $year = $arc_row->year;
383 printf( "<option %s value='%s'>%s</option>\n",
384 selected( $m, $year . $month, false ),
385 esc_attr( $arc_row->year . $month ),
386 $wp_locale->get_month( $month ) . " $year"
395 * Display a view switcher
400 function view_switcher( $current_mode ) {
402 'list' => __( 'List View' ),
403 'excerpt' => __( 'Excerpt View' )
407 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
408 <div class="view-switch">
410 foreach ( $modes as $mode => $title ) {
411 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
412 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";
420 * Display a comment count bubble
425 * @param int $post_id
426 * @param int $pending_comments
428 function comments_bubble( $post_id, $pending_comments ) {
429 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
431 if ( $pending_comments )
434 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>";
436 if ( $pending_comments )
441 * Get the current page number
448 function get_pagenum() {
449 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
451 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
452 $pagenum = $this->_pagination_args['total_pages'];
454 return max( 1, $pagenum );
458 * Get number of items to display on a single page
465 function get_items_per_page( $option, $default = 20 ) {
466 $per_page = (int) get_user_option( $option );
467 if ( empty( $per_page ) || $per_page < 1 )
468 $per_page = $default;
470 return (int) apply_filters( $option, $per_page );
474 * Display the pagination.
479 function pagination( $which ) {
480 if ( empty( $this->_pagination_args ) )
483 extract( $this->_pagination_args );
485 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
487 $current = $this->get_pagenum();
489 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
491 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
493 $page_links = array();
495 $disable_first = $disable_last = '';
497 $disable_first = ' disabled';
498 if ( $current == $total_pages )
499 $disable_last = ' disabled';
501 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
502 'first-page' . $disable_first,
503 esc_attr__( 'Go to the first page' ),
504 esc_url( remove_query_arg( 'paged', $current_url ) ),
508 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
509 'prev-page' . $disable_first,
510 esc_attr__( 'Go to the previous page' ),
511 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
515 if ( 'bottom' == $which )
516 $html_current_page = $current;
518 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='%s' value='%s' size='%d' />",
519 esc_attr__( 'Current page' ),
522 strlen( $total_pages )
525 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
526 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
528 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
529 'next-page' . $disable_last,
530 esc_attr__( 'Go to the next page' ),
531 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
535 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
536 'last-page' . $disable_last,
537 esc_attr__( 'Go to the last page' ),
538 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
542 $output .= "\n<span class='pagination-links'>" . join( "\n", $page_links ) . '</span>';
545 $page_class = $total_pages < 2 ? ' one-page' : '';
547 $page_class = ' no-pages';
549 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
551 echo $this->_pagination;
555 * Get a list of columns. The format is:
556 * 'internal-name' => 'Title'
564 function get_columns() {
565 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
569 * Get a list of sortable columns. The format is:
570 * 'internal-name' => 'orderby'
572 * 'internal-name' => array( 'orderby', true )
574 * The second format will make the initial sorting order be descending
581 function get_sortable_columns() {
586 * Get a list of all, hidden and sortable columns, with filter applied
593 function get_column_info() {
594 if ( isset( $this->_column_headers ) )
595 return $this->_column_headers;
597 $screen = get_current_screen();
599 $columns = get_column_headers( $screen );
600 $hidden = get_hidden_columns( $screen );
602 $_sortable = apply_filters( "manage_{$screen->id}_sortable_columns", $this->get_sortable_columns() );
605 foreach ( $_sortable as $id => $data ) {
606 if ( empty( $data ) )
609 $data = (array) $data;
610 if ( !isset( $data[1] ) )
613 $sortable[$id] = $data;
616 $this->_column_headers = array( $columns, $hidden, $sortable );
618 return $this->_column_headers;
622 * Return number of visible columns
629 function get_column_count() {
630 list ( $columns, $hidden ) = $this->get_column_info();
631 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
632 return count( $columns ) - count( $hidden );
636 * Print column headers, accounting for hidden and sortable columns.
641 * @param bool $with_id Whether to set the id attribute or not
643 function print_column_headers( $with_id = true ) {
644 $screen = get_current_screen();
646 list( $columns, $hidden, $sortable ) = $this->get_column_info();
648 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
649 $current_url = remove_query_arg( 'paged', $current_url );
651 if ( isset( $_GET['orderby'] ) )
652 $current_orderby = $_GET['orderby'];
654 $current_orderby = '';
656 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
657 $current_order = 'desc';
659 $current_order = 'asc';
661 foreach ( $columns as $column_key => $column_display_name ) {
662 $class = array( 'manage-column', "column-$column_key" );
665 if ( in_array( $column_key, $hidden ) )
666 $style = 'display:none;';
668 $style = ' style="' . $style . '"';
670 if ( 'cb' == $column_key )
671 $class[] = 'check-column';
672 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
675 if ( isset( $sortable[$column_key] ) ) {
676 list( $orderby, $desc_first ) = $sortable[$column_key];
678 if ( $current_orderby == $orderby ) {
679 $order = 'asc' == $current_order ? 'desc' : 'asc';
681 $class[] = $current_order;
683 $order = $desc_first ? 'desc' : 'asc';
684 $class[] = 'sortable';
685 $class[] = $desc_first ? 'asc' : 'desc';
688 $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>';
691 $id = $with_id ? "id='$column_key'" : '';
693 if ( !empty( $class ) )
694 $class = "class='" . join( ' ', $class ) . "'";
696 echo "<th scope='col' $id $class $style>$column_display_name</th>";
707 extract( $this->_args );
709 $this->display_tablenav( 'top' );
712 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
715 <?php $this->print_column_headers(); ?>
721 <?php $this->print_column_headers( false ); ?>
725 <tbody id="the-list"<?php if ( $singular ) echo " class='list:$singular'"; ?>>
726 <?php $this->display_rows_or_placeholder(); ?>
730 $this->display_tablenav( 'bottom' );
734 * Get a list of CSS classes for the <table> tag
741 function get_table_classes() {
742 return array( 'widefat', 'fixed', $this->_args['plural'] );
746 * Generate the table navigation above or below the table
751 function display_tablenav( $which ) {
752 if ( 'top' == $which )
753 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
755 <div class="tablenav <?php echo esc_attr( $which ); ?>">
757 <div class="alignleft actions">
758 <?php $this->bulk_actions( $which ); ?>
761 $this->extra_tablenav( $which );
762 $this->pagination( $which );
771 * Extra controls to be displayed between bulk actions and pagination
776 function extra_tablenav( $which ) {}
779 * Generate the <tbody> part of the table
784 function display_rows_or_placeholder() {
785 if ( $this->has_items() ) {
786 $this->display_rows();
788 list( $columns, $hidden ) = $this->get_column_info();
789 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
796 * Generate the table rows
801 function display_rows() {
802 foreach ( $this->items as $item )
803 $this->single_row( $item );
807 * Generates content for a single row of the table
812 * @param object $item The current item
814 function single_row( $item ) {
815 static $row_class = '';
816 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
818 echo '<tr' . $row_class . '>';
819 echo $this->single_row_columns( $item );
824 * Generates the columns for a single row of the table
829 * @param object $item The current item
831 function single_row_columns( $item ) {
832 list( $columns, $hidden ) = $this->get_column_info();
834 foreach ( $columns as $column_name => $column_display_name ) {
835 $class = "class='$column_name column-$column_name'";
838 if ( in_array( $column_name, $hidden ) )
839 $style = ' style="display:none;"';
841 $attributes = "$class$style";
843 if ( 'cb' == $column_name ) {
844 echo '<th scope="row" class="check-column">';
845 echo $this->column_cb( $item );
848 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
849 echo "<td $attributes>";
850 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
854 echo "<td $attributes>";
855 echo $this->column_default( $item, $column_name );
862 * Handle an incoming ajax request (called from admin-ajax.php)
867 function ajax_response() {
868 $this->prepare_items();
870 extract( $this->_args );
871 extract( $this->_pagination_args );
874 if ( ! empty( $_REQUEST['no_placeholder'] ) )
875 $this->display_rows();
877 $this->display_rows_or_placeholder();
879 $rows = ob_get_clean();
881 $response = array( 'rows' => $rows );
883 if ( isset( $total_items ) )
884 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
886 if ( isset( $total_pages ) ) {
887 $response['total_pages'] = $total_pages;
888 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
891 die( json_encode( $response ) );
895 * Send required variables to JavaScript land
899 function _js_vars() {
901 'class' => get_class( $this ),
902 'screen' => get_current_screen()
905 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );