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;
94 $args['plural'] = sanitize_key( $args['plural'] );
95 $args['singular'] = sanitize_key( $args['singular'] );
99 if ( $args['ajax'] ) {
100 // wp_enqueue_script( 'list-table' );
101 add_action( 'admin_footer', array( &$this, '_js_vars' ) );
106 * Checks the current user's permissions
113 function ajax_user_can() {
114 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
118 * Prepares the list of items for displaying.
119 * @uses WP_List_Table::set_pagination_args()
125 function prepare_items() {
126 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
130 * An internal method that sets all the necessary pagination arguments
132 * @param array $args An associative array with information about the pagination
135 function set_pagination_args( $args ) {
136 $args = wp_parse_args( $args, array(
142 if ( !$args['total_pages'] && $args['per_page'] > 0 )
143 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
145 // redirect if page number is invalid and headers are not already sent
146 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
147 wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
151 $this->_pagination_args = $args;
155 * Access the pagination args
163 function get_pagination_arg( $key ) {
164 if ( 'page' == $key )
165 return $this->get_pagenum();
167 if ( isset( $this->_pagination_args[$key] ) )
168 return $this->_pagination_args[$key];
172 * Whether the table has items to display or not
179 function has_items() {
180 return !empty( $this->items );
184 * Message to be displayed when there are no items
189 function no_items() {
190 _e( 'No items found.' );
194 * Display the search box.
199 * @param string $text The search button text
200 * @param string $input_id The search input id
202 function search_box( $text, $input_id ) {
203 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
206 $input_id = $input_id . '-search-input';
208 if ( ! empty( $_REQUEST['orderby'] ) )
209 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
210 if ( ! empty( $_REQUEST['order'] ) )
211 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
213 <p class="search-box">
214 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
215 <input type="text" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
216 <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
222 * Get an associative array ( id => link ) with the list
223 * of views available on this table.
230 function get_views() {
235 * Display the list of views available on this table.
241 $screen = get_current_screen();
243 $views = $this->get_views();
244 $views = apply_filters( 'views_' . $screen->id, $views );
246 if ( empty( $views ) )
249 echo "<ul class='subsubsub'>\n";
250 foreach ( $views as $class => $view ) {
251 $views[ $class ] = "\t<li class='$class'>$view";
253 echo implode( " |</li>\n", $views ) . "</li>\n";
258 * Get an associative array ( option_name => option_title ) with the list
259 * of bulk actions available on this table.
266 function get_bulk_actions() {
271 * Display the bulk actions dropdown.
276 function bulk_actions() {
277 $screen = get_current_screen();
279 if ( is_null( $this->_actions ) ) {
280 $no_new_actions = $this->_actions = $this->get_bulk_actions();
281 // This filter can currently only be used to remove actions.
282 $this->_actions = apply_filters( 'bulk_actions-' . $screen->id, $this->_actions );
283 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
289 if ( empty( $this->_actions ) )
292 echo "<select name='action$two'>\n";
293 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
295 foreach ( $this->_actions as $name => $title ) {
296 $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
298 echo "\t<option value='$name'$class>$title</option>\n";
303 submit_button( __( 'Apply' ), 'button-secondary action', false, false, array( 'id' => "doaction$two" ) );
308 * Get the current action selected from the bulk actions dropdown.
313 * @return string|bool The action name or False if no action was selected
315 function current_action() {
316 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
317 return $_REQUEST['action'];
319 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
320 return $_REQUEST['action2'];
326 * Generate row actions div
331 * @param array $actions The list of actions
332 * @param bool $always_visible Whether the actions should be always visible
335 function row_actions( $actions, $always_visible = false ) {
336 $action_count = count( $actions );
339 if ( !$action_count )
342 $out = '<div class="' . ( $always_visible ? 'row-actions-visible' : 'row-actions' ) . '">';
343 foreach ( $actions as $action => $link ) {
345 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
346 $out .= "<span class='$action'>$link$sep</span>";
354 * Display a monthly dropdown for filtering items
359 function months_dropdown( $post_type ) {
360 global $wpdb, $wp_locale;
362 $months = $wpdb->get_results( $wpdb->prepare( "
363 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
366 ORDER BY post_date DESC
369 $month_count = count( $months );
371 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
374 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
377 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
379 foreach ( $months as $arc_row ) {
380 if ( 0 == $arc_row->year )
383 $month = zeroise( $arc_row->month, 2 );
384 $year = $arc_row->year;
386 printf( "<option %s value='%s'>%s</option>\n",
387 selected( $m, $year . $month, false ),
388 esc_attr( $arc_row->year . $month ),
389 $wp_locale->get_month( $month ) . " $year"
398 * Display a view switcher
403 function view_switcher( $current_mode ) {
405 'list' => __( 'List View' ),
406 'excerpt' => __( 'Excerpt View' )
410 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
411 <div class="view-switch">
413 foreach ( $modes as $mode => $title ) {
414 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
415 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";
423 * Display a comment count bubble
428 * @param int $post_id
429 * @param int $pending_comments
431 function comments_bubble( $post_id, $pending_comments ) {
432 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
434 if ( $pending_comments )
437 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>";
439 if ( $pending_comments )
444 * Get the current page number
451 function get_pagenum() {
452 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
454 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
455 $pagenum = $this->_pagination_args['total_pages'];
457 return max( 1, $pagenum );
461 * Get number of items to display on a single page
468 function get_items_per_page( $option, $default = 20 ) {
469 $per_page = (int) get_user_option( $option );
470 if ( empty( $per_page ) || $per_page < 1 )
471 $per_page = $default;
473 return (int) apply_filters( $option, $per_page );
477 * Display the pagination.
482 function pagination( $which ) {
483 if ( empty( $this->_pagination_args ) )
486 extract( $this->_pagination_args );
488 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
490 $current = $this->get_pagenum();
492 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
494 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
496 $page_links = array();
498 $disable_first = $disable_last = '';
500 $disable_first = ' disabled';
501 if ( $current == $total_pages )
502 $disable_last = ' disabled';
504 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
505 'first-page' . $disable_first,
506 esc_attr__( 'Go to the first page' ),
507 esc_url( remove_query_arg( 'paged', $current_url ) ),
511 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
512 'prev-page' . $disable_first,
513 esc_attr__( 'Go to the previous page' ),
514 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
518 if ( 'bottom' == $which )
519 $html_current_page = $current;
521 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='%s' value='%s' size='%d' />",
522 esc_attr__( 'Current page' ),
525 strlen( $total_pages )
528 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
529 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
531 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
532 'next-page' . $disable_last,
533 esc_attr__( 'Go to the next page' ),
534 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
538 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
539 'last-page' . $disable_last,
540 esc_attr__( 'Go to the last page' ),
541 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
545 $output .= "\n<span class='pagination-links'>" . join( "\n", $page_links ) . '</span>';
548 $page_class = $total_pages < 2 ? ' one-page' : '';
550 $page_class = ' no-pages';
552 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
554 echo $this->_pagination;
558 * Get a list of columns. The format is:
559 * 'internal-name' => 'Title'
567 function get_columns() {
568 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
572 * Get a list of sortable columns. The format is:
573 * 'internal-name' => 'orderby'
575 * 'internal-name' => array( 'orderby', true )
577 * The second format will make the initial sorting order be descending
584 function get_sortable_columns() {
589 * Get a list of all, hidden and sortable columns, with filter applied
596 function get_column_info() {
597 if ( isset( $this->_column_headers ) )
598 return $this->_column_headers;
600 $screen = get_current_screen();
602 $columns = get_column_headers( $screen );
603 $hidden = get_hidden_columns( $screen );
605 $_sortable = apply_filters( "manage_{$screen->id}_sortable_columns", $this->get_sortable_columns() );
608 foreach ( $_sortable as $id => $data ) {
609 if ( empty( $data ) )
612 $data = (array) $data;
613 if ( !isset( $data[1] ) )
616 $sortable[$id] = $data;
619 $this->_column_headers = array( $columns, $hidden, $sortable );
621 return $this->_column_headers;
625 * Return number of visible columns
632 function get_column_count() {
633 list ( $columns, $hidden ) = $this->get_column_info();
634 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
635 return count( $columns ) - count( $hidden );
639 * Print column headers, accounting for hidden and sortable columns.
644 * @param bool $with_id Whether to set the id attribute or not
646 function print_column_headers( $with_id = true ) {
647 $screen = get_current_screen();
649 list( $columns, $hidden, $sortable ) = $this->get_column_info();
651 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
652 $current_url = remove_query_arg( 'paged', $current_url );
654 if ( isset( $_GET['orderby'] ) )
655 $current_orderby = $_GET['orderby'];
657 $current_orderby = '';
659 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
660 $current_order = 'desc';
662 $current_order = 'asc';
664 foreach ( $columns as $column_key => $column_display_name ) {
665 $class = array( 'manage-column', "column-$column_key" );
668 if ( in_array( $column_key, $hidden ) )
669 $style = 'display:none;';
671 $style = ' style="' . $style . '"';
673 if ( 'cb' == $column_key )
674 $class[] = 'check-column';
675 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
678 if ( isset( $sortable[$column_key] ) ) {
679 list( $orderby, $desc_first ) = $sortable[$column_key];
681 if ( $current_orderby == $orderby ) {
682 $order = 'asc' == $current_order ? 'desc' : 'asc';
684 $class[] = $current_order;
686 $order = $desc_first ? 'desc' : 'asc';
687 $class[] = 'sortable';
688 $class[] = $desc_first ? 'asc' : 'desc';
691 $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>';
694 $id = $with_id ? "id='$column_key'" : '';
696 if ( !empty( $class ) )
697 $class = "class='" . join( ' ', $class ) . "'";
699 echo "<th scope='col' $id $class $style>$column_display_name</th>";
710 extract( $this->_args );
712 $this->display_tablenav( 'top' );
715 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
718 <?php $this->print_column_headers(); ?>
724 <?php $this->print_column_headers( false ); ?>
728 <tbody id="the-list"<?php if ( $singular ) echo " class='list:$singular'"; ?>>
729 <?php $this->display_rows_or_placeholder(); ?>
733 $this->display_tablenav( 'bottom' );
737 * Get a list of CSS classes for the <table> tag
744 function get_table_classes() {
745 return array( 'widefat', 'fixed', $this->_args['plural'] );
749 * Generate the table navigation above or below the table
754 function display_tablenav( $which ) {
755 if ( 'top' == $which )
756 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
758 <div class="tablenav <?php echo esc_attr( $which ); ?>">
760 <div class="alignleft actions">
761 <?php $this->bulk_actions( $which ); ?>
764 $this->extra_tablenav( $which );
765 $this->pagination( $which );
774 * Extra controls to be displayed between bulk actions and pagination
779 function extra_tablenav( $which ) {}
782 * Generate the <tbody> part of the table
787 function display_rows_or_placeholder() {
788 if ( $this->has_items() ) {
789 $this->display_rows();
791 list( $columns, $hidden ) = $this->get_column_info();
792 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
799 * Generate the table rows
804 function display_rows() {
805 foreach ( $this->items as $item )
806 $this->single_row( $item );
810 * Generates content for a single row of the table
815 * @param object $item The current item
817 function single_row( $item ) {
818 static $row_class = '';
819 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
821 echo '<tr' . $row_class . '>';
822 echo $this->single_row_columns( $item );
827 * Generates the columns for a single row of the table
832 * @param object $item The current item
834 function single_row_columns( $item ) {
835 list( $columns, $hidden ) = $this->get_column_info();
837 foreach ( $columns as $column_name => $column_display_name ) {
838 $class = "class='$column_name column-$column_name'";
841 if ( in_array( $column_name, $hidden ) )
842 $style = ' style="display:none;"';
844 $attributes = "$class$style";
846 if ( 'cb' == $column_name ) {
847 echo '<th scope="row" class="check-column">';
848 echo $this->column_cb( $item );
851 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
852 echo "<td $attributes>";
853 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
857 echo "<td $attributes>";
858 echo $this->column_default( $item, $column_name );
865 * Handle an incoming ajax request (called from admin-ajax.php)
870 function ajax_response() {
871 $this->prepare_items();
873 extract( $this->_args );
874 extract( $this->_pagination_args );
877 if ( ! empty( $_REQUEST['no_placeholder'] ) )
878 $this->display_rows();
880 $this->display_rows_or_placeholder();
882 $rows = ob_get_clean();
884 $response = array( 'rows' => $rows );
886 if ( isset( $total_items ) )
887 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
889 if ( isset( $total_pages ) ) {
890 $response['total_pages'] = $total_pages;
891 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
894 die( json_encode( $response ) );
898 * Send required variables to JavaScript land
902 function _js_vars() {
903 $current_screen = get_current_screen();
906 'class' => get_class( $this ),
908 'id' => $current_screen->id,
909 'base' => $current_screen->base,
913 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );