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();
248 * Filter the list of available list table views.
250 * The dynamic portion of the hook name, $this->screen->id, refers
251 * to the ID of the current screen, usually a string.
255 * @param array $views An array of available list table views.
257 $views = apply_filters( "views_{$this->screen->id}", $views );
259 if ( empty( $views ) )
262 echo "<ul class='subsubsub'>\n";
263 foreach ( $views as $class => $view ) {
264 $views[ $class ] = "\t<li class='$class'>$view";
266 echo implode( " |</li>\n", $views ) . "</li>\n";
271 * Get an associative array ( option_name => option_title ) with the list
272 * of bulk actions available on this table.
279 function get_bulk_actions() {
284 * Display the bulk actions dropdown.
289 function bulk_actions() {
290 if ( is_null( $this->_actions ) ) {
291 $no_new_actions = $this->_actions = $this->get_bulk_actions();
293 * Filter the list table Bulk Actions drop-down.
295 * The dynamic portion of the hook name, $this->screen->id, refers
296 * to the ID of the current screen, usually a string.
298 * This filter can currently only be used to remove bulk actions.
302 * @param array $actions An array of the available bulk actions.
304 $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
305 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
311 if ( empty( $this->_actions ) )
314 echo "<select name='action$two'>\n";
315 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
317 foreach ( $this->_actions as $name => $title ) {
318 $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
320 echo "\t<option value='$name'$class>$title</option>\n";
325 submit_button( __( 'Apply' ), 'action', false, false, array( 'id' => "doaction$two" ) );
330 * Get the current action selected from the bulk actions dropdown.
335 * @return string|bool The action name or False if no action was selected
337 function current_action() {
338 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
339 return $_REQUEST['action'];
341 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
342 return $_REQUEST['action2'];
348 * Generate row actions div
353 * @param array $actions The list of actions
354 * @param bool $always_visible Whether the actions should be always visible
357 function row_actions( $actions, $always_visible = false ) {
358 $action_count = count( $actions );
361 if ( !$action_count )
364 $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
365 foreach ( $actions as $action => $link ) {
367 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
368 $out .= "<span class='$action'>$link$sep</span>";
376 * Display a monthly dropdown for filtering items
381 function months_dropdown( $post_type ) {
382 global $wpdb, $wp_locale;
384 $months = $wpdb->get_results( $wpdb->prepare( "
385 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
388 ORDER BY post_date DESC
392 * Filter the 'Months' drop-down results.
396 * @param object $months The months drop-down query results.
397 * @param string $post_type The post type.
399 $months = apply_filters( 'months_dropdown_results', $months, $post_type );
401 $month_count = count( $months );
403 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
406 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
409 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
411 foreach ( $months as $arc_row ) {
412 if ( 0 == $arc_row->year )
415 $month = zeroise( $arc_row->month, 2 );
416 $year = $arc_row->year;
418 printf( "<option %s value='%s'>%s</option>\n",
419 selected( $m, $year . $month, false ),
420 esc_attr( $arc_row->year . $month ),
421 /* translators: 1: month name, 2: 4-digit year */
422 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
431 * Display a view switcher
436 function view_switcher( $current_mode ) {
438 'list' => __( 'List View' ),
439 'excerpt' => __( 'Excerpt View' )
443 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
444 <div class="view-switch">
446 foreach ( $modes as $mode => $title ) {
447 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
448 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";
456 * Display a comment count bubble
461 * @param int $post_id
462 * @param int $pending_comments
464 function comments_bubble( $post_id, $pending_comments ) {
465 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
467 if ( $pending_comments )
470 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>";
472 if ( $pending_comments )
477 * Get the current page number
484 function get_pagenum() {
485 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
487 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
488 $pagenum = $this->_pagination_args['total_pages'];
490 return max( 1, $pagenum );
494 * Get number of items to display on a single page
501 function get_items_per_page( $option, $default = 20 ) {
502 $per_page = (int) get_user_option( $option );
503 if ( empty( $per_page ) || $per_page < 1 )
504 $per_page = $default;
507 * Filter the number of items to be displayed on each page of the list table.
509 * The dynamic hook name, $option, refers to the per page option depending
510 * on the type of list table in use. Possible values may include:
511 * 'edit_comments_per_page', 'sites_network_per_page', 'site_themes_network_per_page',
512 * 'themes_netework_per_page', 'users_network_per_page', 'edit_{$post_type}', etc.
516 * @param int $per_page Number of items to be displayed. Default 20.
518 return (int) apply_filters( $option, $per_page );
522 * Display the pagination.
527 function pagination( $which ) {
528 if ( empty( $this->_pagination_args ) )
531 extract( $this->_pagination_args, EXTR_SKIP );
533 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
535 $current = $this->get_pagenum();
537 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
539 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
541 $page_links = array();
543 $disable_first = $disable_last = '';
545 $disable_first = ' disabled';
546 if ( $current == $total_pages )
547 $disable_last = ' disabled';
549 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
550 'first-page' . $disable_first,
551 esc_attr__( 'Go to the first page' ),
552 esc_url( remove_query_arg( 'paged', $current_url ) ),
556 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
557 'prev-page' . $disable_first,
558 esc_attr__( 'Go to the previous page' ),
559 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
563 if ( 'bottom' == $which )
564 $html_current_page = $current;
566 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
567 esc_attr__( 'Current page' ),
569 strlen( $total_pages )
572 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
573 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
575 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
576 'next-page' . $disable_last,
577 esc_attr__( 'Go to the next page' ),
578 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
582 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
583 'last-page' . $disable_last,
584 esc_attr__( 'Go to the last page' ),
585 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
589 $pagination_links_class = 'pagination-links';
590 if ( ! empty( $infinite_scroll ) )
591 $pagination_links_class = ' hide-if-js';
592 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
595 $page_class = $total_pages < 2 ? ' one-page' : '';
597 $page_class = ' no-pages';
599 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
601 echo $this->_pagination;
605 * Get a list of columns. The format is:
606 * 'internal-name' => 'Title'
614 function get_columns() {
615 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
619 * Get a list of sortable columns. The format is:
620 * 'internal-name' => 'orderby'
622 * 'internal-name' => array( 'orderby', true )
624 * The second format will make the initial sorting order be descending
631 function get_sortable_columns() {
636 * Get a list of all, hidden and sortable columns, with filter applied
643 function get_column_info() {
644 if ( isset( $this->_column_headers ) )
645 return $this->_column_headers;
647 $columns = get_column_headers( $this->screen );
648 $hidden = get_hidden_columns( $this->screen );
650 $sortable_columns = $this->get_sortable_columns();
652 * Filter the list table sortable columns for a specific screen.
654 * The dynamic portion of the hook name, $this->screen->id, refers
655 * to the ID of the current screen, usually a string.
659 * @param array $sortable_columns An array of sortable columns.
661 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
664 foreach ( $_sortable as $id => $data ) {
665 if ( empty( $data ) )
668 $data = (array) $data;
669 if ( !isset( $data[1] ) )
672 $sortable[$id] = $data;
675 $this->_column_headers = array( $columns, $hidden, $sortable );
677 return $this->_column_headers;
681 * Return number of visible columns
688 function get_column_count() {
689 list ( $columns, $hidden ) = $this->get_column_info();
690 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
691 return count( $columns ) - count( $hidden );
695 * Print column headers, accounting for hidden and sortable columns.
700 * @param bool $with_id Whether to set the id attribute or not
702 function print_column_headers( $with_id = true ) {
703 list( $columns, $hidden, $sortable ) = $this->get_column_info();
705 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
706 $current_url = remove_query_arg( 'paged', $current_url );
708 if ( isset( $_GET['orderby'] ) )
709 $current_orderby = $_GET['orderby'];
711 $current_orderby = '';
713 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
714 $current_order = 'desc';
716 $current_order = 'asc';
718 if ( ! empty( $columns['cb'] ) ) {
719 static $cb_counter = 1;
720 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
721 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
725 foreach ( $columns as $column_key => $column_display_name ) {
726 $class = array( 'manage-column', "column-$column_key" );
729 if ( in_array( $column_key, $hidden ) )
730 $style = 'display:none;';
732 $style = ' style="' . $style . '"';
734 if ( 'cb' == $column_key )
735 $class[] = 'check-column';
736 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
739 if ( isset( $sortable[$column_key] ) ) {
740 list( $orderby, $desc_first ) = $sortable[$column_key];
742 if ( $current_orderby == $orderby ) {
743 $order = 'asc' == $current_order ? 'desc' : 'asc';
745 $class[] = $current_order;
747 $order = $desc_first ? 'desc' : 'asc';
748 $class[] = 'sortable';
749 $class[] = $desc_first ? 'asc' : 'desc';
752 $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>';
755 $id = $with_id ? "id='$column_key'" : '';
757 if ( !empty( $class ) )
758 $class = "class='" . join( ' ', $class ) . "'";
760 echo "<th scope='col' $id $class $style>$column_display_name</th>";
771 extract( $this->_args );
773 $this->display_tablenav( 'top' );
776 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
779 <?php $this->print_column_headers(); ?>
785 <?php $this->print_column_headers( false ); ?>
789 <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
790 <?php $this->display_rows_or_placeholder(); ?>
794 $this->display_tablenav( 'bottom' );
798 * Get a list of CSS classes for the <table> tag
805 function get_table_classes() {
806 return array( 'widefat', 'fixed', $this->_args['plural'] );
810 * Generate the table navigation above or below the table
815 function display_tablenav( $which ) {
816 if ( 'top' == $which )
817 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
819 <div class="tablenav <?php echo esc_attr( $which ); ?>">
821 <div class="alignleft actions bulkactions">
822 <?php $this->bulk_actions(); ?>
825 $this->extra_tablenav( $which );
826 $this->pagination( $which );
835 * Extra controls to be displayed between bulk actions and pagination
840 function extra_tablenav( $which ) {}
843 * Generate the <tbody> part of the table
848 function display_rows_or_placeholder() {
849 if ( $this->has_items() ) {
850 $this->display_rows();
852 list( $columns, $hidden ) = $this->get_column_info();
853 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
860 * Generate the table rows
865 function display_rows() {
866 foreach ( $this->items as $item )
867 $this->single_row( $item );
871 * Generates content for a single row of the table
876 * @param object $item The current item
878 function single_row( $item ) {
879 static $row_class = '';
880 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
882 echo '<tr' . $row_class . '>';
883 $this->single_row_columns( $item );
888 * Generates the columns for a single row of the table
893 * @param object $item The current item
895 function single_row_columns( $item ) {
896 list( $columns, $hidden ) = $this->get_column_info();
898 foreach ( $columns as $column_name => $column_display_name ) {
899 $class = "class='$column_name column-$column_name'";
902 if ( in_array( $column_name, $hidden ) )
903 $style = ' style="display:none;"';
905 $attributes = "$class$style";
907 if ( 'cb' == $column_name ) {
908 echo '<th scope="row" class="check-column">';
909 echo $this->column_cb( $item );
912 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
913 echo "<td $attributes>";
914 echo call_user_func( array( $this, 'column_' . $column_name ), $item );
918 echo "<td $attributes>";
919 echo $this->column_default( $item, $column_name );
926 * Handle an incoming ajax request (called from admin-ajax.php)
931 function ajax_response() {
932 $this->prepare_items();
934 extract( $this->_args );
935 extract( $this->_pagination_args, EXTR_SKIP );
938 if ( ! empty( $_REQUEST['no_placeholder'] ) )
939 $this->display_rows();
941 $this->display_rows_or_placeholder();
943 $rows = ob_get_clean();
945 $response = array( 'rows' => $rows );
947 if ( isset( $total_items ) )
948 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
950 if ( isset( $total_pages ) ) {
951 $response['total_pages'] = $total_pages;
952 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
955 die( json_encode( $response ) );
959 * Send required variables to JavaScript land
963 function _js_vars() {
965 'class' => get_class( $this ),
967 'id' => $this->screen->id,
968 'base' => $this->screen->base,
972 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );