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
370 $month_count = count( $months );
372 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
375 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
378 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
380 foreach ( $months as $arc_row ) {
381 if ( 0 == $arc_row->year )
384 $month = zeroise( $arc_row->month, 2 );
385 $year = $arc_row->year;
387 printf( "<option %s value='%s'>%s</option>\n",
388 selected( $m, $year . $month, false ),
389 esc_attr( $arc_row->year . $month ),
390 /* translators: 1: month name, 2: 4-digit year */
391 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
400 * Display a view switcher
405 function view_switcher( $current_mode ) {
407 'list' => __( 'List View' ),
408 'excerpt' => __( 'Excerpt View' )
412 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
413 <div class="view-switch">
415 foreach ( $modes as $mode => $title ) {
416 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
417 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";
425 * Display a comment count bubble
430 * @param int $post_id
431 * @param int $pending_comments
433 function comments_bubble( $post_id, $pending_comments ) {
434 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
436 if ( $pending_comments )
439 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>";
441 if ( $pending_comments )
446 * Get the current page number
453 function get_pagenum() {
454 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
456 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
457 $pagenum = $this->_pagination_args['total_pages'];
459 return max( 1, $pagenum );
463 * Get number of items to display on a single page
470 function get_items_per_page( $option, $default = 20 ) {
471 $per_page = (int) get_user_option( $option );
472 if ( empty( $per_page ) || $per_page < 1 )
473 $per_page = $default;
475 return (int) apply_filters( $option, $per_page );
479 * Display the pagination.
484 function pagination( $which ) {
485 if ( empty( $this->_pagination_args ) )
488 extract( $this->_pagination_args, EXTR_SKIP );
490 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
492 $current = $this->get_pagenum();
494 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
496 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
498 $page_links = array();
500 $disable_first = $disable_last = '';
502 $disable_first = ' disabled';
503 if ( $current == $total_pages )
504 $disable_last = ' disabled';
506 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
507 'first-page' . $disable_first,
508 esc_attr__( 'Go to the first page' ),
509 esc_url( remove_query_arg( 'paged', $current_url ) ),
513 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
514 'prev-page' . $disable_first,
515 esc_attr__( 'Go to the previous page' ),
516 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
520 if ( 'bottom' == $which )
521 $html_current_page = $current;
523 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
524 esc_attr__( 'Current page' ),
526 strlen( $total_pages )
529 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
530 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
532 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
533 'next-page' . $disable_last,
534 esc_attr__( 'Go to the next page' ),
535 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
539 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
540 'last-page' . $disable_last,
541 esc_attr__( 'Go to the last page' ),
542 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
546 $pagination_links_class = 'pagination-links';
547 if ( ! empty( $infinite_scroll ) )
548 $pagination_links_class = ' hide-if-js';
549 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
552 $page_class = $total_pages < 2 ? ' one-page' : '';
554 $page_class = ' no-pages';
556 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
558 echo $this->_pagination;
562 * Get a list of columns. The format is:
563 * 'internal-name' => 'Title'
571 function get_columns() {
572 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
576 * Get a list of sortable columns. The format is:
577 * 'internal-name' => 'orderby'
579 * 'internal-name' => array( 'orderby', true )
581 * The second format will make the initial sorting order be descending
588 function get_sortable_columns() {
593 * Get a list of all, hidden and sortable columns, with filter applied
600 function get_column_info() {
601 if ( isset( $this->_column_headers ) )
602 return $this->_column_headers;
604 $columns = get_column_headers( $this->screen );
605 $hidden = get_hidden_columns( $this->screen );
607 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $this->get_sortable_columns() );
610 foreach ( $_sortable as $id => $data ) {
611 if ( empty( $data ) )
614 $data = (array) $data;
615 if ( !isset( $data[1] ) )
618 $sortable[$id] = $data;
621 $this->_column_headers = array( $columns, $hidden, $sortable );
623 return $this->_column_headers;
627 * Return number of visible columns
634 function get_column_count() {
635 list ( $columns, $hidden ) = $this->get_column_info();
636 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
637 return count( $columns ) - count( $hidden );
641 * Print column headers, accounting for hidden and sortable columns.
646 * @param bool $with_id Whether to set the id attribute or not
648 function print_column_headers( $with_id = true ) {
649 list( $columns, $hidden, $sortable ) = $this->get_column_info();
651 $current_url = set_url_scheme( '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 if ( ! empty( $columns['cb'] ) ) {
665 static $cb_counter = 1;
666 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
667 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
671 foreach ( $columns as $column_key => $column_display_name ) {
672 $class = array( 'manage-column', "column-$column_key" );
675 if ( in_array( $column_key, $hidden ) )
676 $style = 'display:none;';
678 $style = ' style="' . $style . '"';
680 if ( 'cb' == $column_key )
681 $class[] = 'check-column';
682 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
685 if ( isset( $sortable[$column_key] ) ) {
686 list( $orderby, $desc_first ) = $sortable[$column_key];
688 if ( $current_orderby == $orderby ) {
689 $order = 'asc' == $current_order ? 'desc' : 'asc';
691 $class[] = $current_order;
693 $order = $desc_first ? 'desc' : 'asc';
694 $class[] = 'sortable';
695 $class[] = $desc_first ? 'asc' : 'desc';
698 $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>';
701 $id = $with_id ? "id='$column_key'" : '';
703 if ( !empty( $class ) )
704 $class = "class='" . join( ' ', $class ) . "'";
706 echo "<th scope='col' $id $class $style>$column_display_name</th>";
717 extract( $this->_args );
719 $this->display_tablenav( 'top' );
722 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
725 <?php $this->print_column_headers(); ?>
731 <?php $this->print_column_headers( false ); ?>
735 <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
736 <?php $this->display_rows_or_placeholder(); ?>
740 $this->display_tablenav( 'bottom' );
744 * Get a list of CSS classes for the <table> tag
751 function get_table_classes() {
752 return array( 'widefat', 'fixed', $this->_args['plural'] );
756 * Generate the table navigation above or below the table
761 function display_tablenav( $which ) {
762 if ( 'top' == $which )
763 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
765 <div class="tablenav <?php echo esc_attr( $which ); ?>">
767 <div class="alignleft actions">
768 <?php $this->bulk_actions(); ?>
771 $this->extra_tablenav( $which );
772 $this->pagination( $which );
781 * Extra controls to be displayed between bulk actions and pagination
786 function extra_tablenav( $which ) {}
789 * Generate the <tbody> part of the table
794 function display_rows_or_placeholder() {
795 if ( $this->has_items() ) {
796 $this->display_rows();
798 list( $columns, $hidden ) = $this->get_column_info();
799 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
806 * Generate the table rows
811 function display_rows() {
812 foreach ( $this->items as $item )
813 $this->single_row( $item );
817 * Generates content for a single row of the table
822 * @param object $item The current item
824 function single_row( $item ) {
825 static $row_class = '';
826 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
828 echo '<tr' . $row_class . '>';
829 $this->single_row_columns( $item );
834 * Generates the columns for a single row of the table
839 * @param object $item The current item
841 function single_row_columns( $item ) {
842 list( $columns, $hidden ) = $this->get_column_info();
844 foreach ( $columns as $column_name => $column_display_name ) {
845 $class = "class='$column_name column-$column_name'";
848 if ( in_array( $column_name, $hidden ) )
849 $style = ' style="display:none;"';
851 $attributes = "$class$style";
853 if ( 'cb' == $column_name ) {
854 echo '<th scope="row" class="check-column">';
855 echo $this->column_cb( $item );
858 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
859 echo "<td $attributes>";
860 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
864 echo "<td $attributes>";
865 echo $this->column_default( $item, $column_name );
872 * Handle an incoming ajax request (called from admin-ajax.php)
877 function ajax_response() {
878 $this->prepare_items();
880 extract( $this->_args );
881 extract( $this->_pagination_args, EXTR_SKIP );
884 if ( ! empty( $_REQUEST['no_placeholder'] ) )
885 $this->display_rows();
887 $this->display_rows_or_placeholder();
889 $rows = ob_get_clean();
891 $response = array( 'rows' => $rows );
893 if ( isset( $total_items ) )
894 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
896 if ( isset( $total_pages ) ) {
897 $response['total_pages'] = $total_pages;
898 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
901 die( json_encode( $response ) );
905 * Send required variables to JavaScript land
909 function _js_vars() {
911 'class' => get_class( $this ),
913 'id' => $this->screen->id,
914 'base' => $this->screen->base,
918 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );