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="search" 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 /* translators: 1: month name, 2: 4-digit year */
390 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
399 * Display a view switcher
404 function view_switcher( $current_mode ) {
406 'list' => __( 'List View' ),
407 'excerpt' => __( 'Excerpt View' )
411 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
412 <div class="view-switch">
414 foreach ( $modes as $mode => $title ) {
415 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
416 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";
424 * Display a comment count bubble
429 * @param int $post_id
430 * @param int $pending_comments
432 function comments_bubble( $post_id, $pending_comments ) {
433 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
435 if ( $pending_comments )
438 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>";
440 if ( $pending_comments )
445 * Get the current page number
452 function get_pagenum() {
453 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
455 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
456 $pagenum = $this->_pagination_args['total_pages'];
458 return max( 1, $pagenum );
462 * Get number of items to display on a single page
469 function get_items_per_page( $option, $default = 20 ) {
470 $per_page = (int) get_user_option( $option );
471 if ( empty( $per_page ) || $per_page < 1 )
472 $per_page = $default;
474 return (int) apply_filters( $option, $per_page );
478 * Display the pagination.
483 function pagination( $which ) {
484 if ( empty( $this->_pagination_args ) )
487 extract( $this->_pagination_args, EXTR_SKIP );
489 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
491 $current = $this->get_pagenum();
493 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
495 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
497 $page_links = array();
499 $disable_first = $disable_last = '';
501 $disable_first = ' disabled';
502 if ( $current == $total_pages )
503 $disable_last = ' disabled';
505 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
506 'first-page' . $disable_first,
507 esc_attr__( 'Go to the first page' ),
508 esc_url( remove_query_arg( 'paged', $current_url ) ),
512 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
513 'prev-page' . $disable_first,
514 esc_attr__( 'Go to the previous page' ),
515 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
519 if ( 'bottom' == $which )
520 $html_current_page = $current;
522 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
523 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 $pagination_links_class = 'pagination-links';
546 if ( ! empty( $infinite_scroll ) )
547 $pagination_links_class = ' hide-if-js';
548 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
551 $page_class = $total_pages < 2 ? ' one-page' : '';
553 $page_class = ' no-pages';
555 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
557 echo $this->_pagination;
561 * Get a list of columns. The format is:
562 * 'internal-name' => 'Title'
570 function get_columns() {
571 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
575 * Get a list of sortable columns. The format is:
576 * 'internal-name' => 'orderby'
578 * 'internal-name' => array( 'orderby', true )
580 * The second format will make the initial sorting order be descending
587 function get_sortable_columns() {
592 * Get a list of all, hidden and sortable columns, with filter applied
599 function get_column_info() {
600 if ( isset( $this->_column_headers ) )
601 return $this->_column_headers;
603 $screen = get_current_screen();
605 $columns = get_column_headers( $screen );
606 $hidden = get_hidden_columns( $screen );
608 $_sortable = apply_filters( "manage_{$screen->id}_sortable_columns", $this->get_sortable_columns() );
611 foreach ( $_sortable as $id => $data ) {
612 if ( empty( $data ) )
615 $data = (array) $data;
616 if ( !isset( $data[1] ) )
619 $sortable[$id] = $data;
622 $this->_column_headers = array( $columns, $hidden, $sortable );
624 return $this->_column_headers;
628 * Return number of visible columns
635 function get_column_count() {
636 list ( $columns, $hidden ) = $this->get_column_info();
637 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
638 return count( $columns ) - count( $hidden );
642 * Print column headers, accounting for hidden and sortable columns.
647 * @param bool $with_id Whether to set the id attribute or not
649 function print_column_headers( $with_id = true ) {
650 $screen = get_current_screen();
652 list( $columns, $hidden, $sortable ) = $this->get_column_info();
654 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
655 $current_url = remove_query_arg( 'paged', $current_url );
657 if ( isset( $_GET['orderby'] ) )
658 $current_orderby = $_GET['orderby'];
660 $current_orderby = '';
662 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
663 $current_order = 'desc';
665 $current_order = 'asc';
667 foreach ( $columns as $column_key => $column_display_name ) {
668 $class = array( 'manage-column', "column-$column_key" );
671 if ( in_array( $column_key, $hidden ) )
672 $style = 'display:none;';
674 $style = ' style="' . $style . '"';
676 if ( 'cb' == $column_key )
677 $class[] = 'check-column';
678 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
681 if ( isset( $sortable[$column_key] ) ) {
682 list( $orderby, $desc_first ) = $sortable[$column_key];
684 if ( $current_orderby == $orderby ) {
685 $order = 'asc' == $current_order ? 'desc' : 'asc';
687 $class[] = $current_order;
689 $order = $desc_first ? 'desc' : 'asc';
690 $class[] = 'sortable';
691 $class[] = $desc_first ? 'asc' : 'desc';
694 $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>';
697 $id = $with_id ? "id='$column_key'" : '';
699 if ( !empty( $class ) )
700 $class = "class='" . join( ' ', $class ) . "'";
702 echo "<th scope='col' $id $class $style>$column_display_name</th>";
713 extract( $this->_args );
715 $this->display_tablenav( 'top' );
718 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
721 <?php $this->print_column_headers(); ?>
727 <?php $this->print_column_headers( false ); ?>
731 <tbody id="the-list"<?php if ( $singular ) echo " class='list:$singular'"; ?>>
732 <?php $this->display_rows_or_placeholder(); ?>
736 $this->display_tablenav( 'bottom' );
740 * Get a list of CSS classes for the <table> tag
747 function get_table_classes() {
748 return array( 'widefat', 'fixed', $this->_args['plural'] );
752 * Generate the table navigation above or below the table
757 function display_tablenav( $which ) {
758 if ( 'top' == $which )
759 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
761 <div class="tablenav <?php echo esc_attr( $which ); ?>">
763 <div class="alignleft actions">
764 <?php $this->bulk_actions( $which ); ?>
767 $this->extra_tablenav( $which );
768 $this->pagination( $which );
777 * Extra controls to be displayed between bulk actions and pagination
782 function extra_tablenav( $which ) {}
785 * Generate the <tbody> part of the table
790 function display_rows_or_placeholder() {
791 if ( $this->has_items() ) {
792 $this->display_rows();
794 list( $columns, $hidden ) = $this->get_column_info();
795 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
802 * Generate the table rows
807 function display_rows() {
808 foreach ( $this->items as $item )
809 $this->single_row( $item );
813 * Generates content for a single row of the table
818 * @param object $item The current item
820 function single_row( $item ) {
821 static $row_class = '';
822 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
824 echo '<tr' . $row_class . '>';
825 echo $this->single_row_columns( $item );
830 * Generates the columns for a single row of the table
835 * @param object $item The current item
837 function single_row_columns( $item ) {
838 list( $columns, $hidden ) = $this->get_column_info();
840 foreach ( $columns as $column_name => $column_display_name ) {
841 $class = "class='$column_name column-$column_name'";
844 if ( in_array( $column_name, $hidden ) )
845 $style = ' style="display:none;"';
847 $attributes = "$class$style";
849 if ( 'cb' == $column_name ) {
850 echo '<th scope="row" class="check-column">';
851 echo $this->column_cb( $item );
854 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
855 echo "<td $attributes>";
856 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
860 echo "<td $attributes>";
861 echo $this->column_default( $item, $column_name );
868 * Handle an incoming ajax request (called from admin-ajax.php)
873 function ajax_response() {
874 $this->prepare_items();
876 extract( $this->_args );
877 extract( $this->_pagination_args, EXTR_SKIP );
880 if ( ! empty( $_REQUEST['no_placeholder'] ) )
881 $this->display_rows();
883 $this->display_rows_or_placeholder();
885 $rows = ob_get_clean();
887 $response = array( 'rows' => $rows );
889 if ( isset( $total_items ) )
890 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
892 if ( isset( $total_pages ) ) {
893 $response['total_pages'] = $total_pages;
894 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
897 die( json_encode( $response ) );
901 * Send required variables to JavaScript land
905 function _js_vars() {
906 $current_screen = get_current_screen();
909 'class' => get_class( $this ),
911 'id' => $current_screen->id,
912 'base' => $current_screen->base,
916 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );