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 WP_List_Table( $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 $this->_pagination_args = $args;
146 * Access the pagination args
154 function get_pagination_arg( $key ) {
155 if ( 'page' == $key )
156 return $this->get_pagenum();
158 if ( isset( $this->_pagination_args[$key] ) )
159 return $this->_pagination_args[$key];
163 * Whether the table has items to display or not
170 function has_items() {
171 return !empty( $this->items );
175 * Message to be displayed when there are no items
180 function no_items() {
181 _e( 'No items found.' );
185 * Display the search box.
190 * @param string $text The search button text
191 * @param string $input_id The search input id
193 function search_box( $text, $input_id ) {
194 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
197 $input_id = $input_id . '-search-input';
199 if ( ! empty( $_REQUEST['orderby'] ) )
200 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
201 if ( ! empty( $_REQUEST['order'] ) )
202 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
204 <p class="search-box">
205 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
206 <input type="text" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
207 <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
213 * Get an associative array ( id => link ) with the list
214 * of views available on this table.
221 function get_views() {
226 * Display the bulk actions dropdown.
232 $screen = get_current_screen();
234 $views = $this->get_views();
235 $views = apply_filters( 'views_' . $screen->id, $views );
237 if ( empty( $views ) )
240 echo "<ul class='subsubsub'>\n";
241 foreach ( $views as $class => $view ) {
242 $views[ $class ] = "\t<li class='$class'>$view";
244 echo implode( " |</li>\n", $views ) . "</li>\n";
249 * Get an associative array ( option_name => option_title ) with the list
250 * of bulk actions available on this table.
257 function get_bulk_actions() {
262 * Display the bulk actions dropdown.
267 function bulk_actions() {
268 $screen = get_current_screen();
270 if ( is_null( $this->_actions ) ) {
271 $no_new_actions = $this->_actions = $this->get_bulk_actions();
272 // This filter can currently only be used to remove actions.
273 $this->_actions = apply_filters( 'bulk_actions-' . $screen->id, $this->_actions );
274 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
280 if ( empty( $this->_actions ) )
283 echo "<select name='action$two'>\n";
284 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
285 foreach ( $this->_actions as $name => $title )
286 echo "\t<option value='$name'>$title</option>\n";
289 submit_button( __( 'Apply' ), 'button-secondary action', false, false, array( 'id' => "doaction$two" ) );
294 * Get the current action selected from the bulk actions dropdown.
299 * @return string|bool The action name or False if no action was selected
301 function current_action() {
302 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
303 return $_REQUEST['action'];
305 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
306 return $_REQUEST['action2'];
312 * Generate row actions div
317 * @param array $actions The list of actions
318 * @param bool $always_visible Wether the actions should be always visible
321 function row_actions( $actions, $always_visible = false ) {
322 $action_count = count( $actions );
325 if ( !$action_count )
328 $out = '<div class="' . ( $always_visible ? 'row-actions-visible' : 'row-actions' ) . '">';
329 foreach ( $actions as $action => $link ) {
331 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
332 $out .= "<span class='$action'>$link$sep</span>";
340 * Display a monthly dropdown for filtering items
345 function months_dropdown( $post_type ) {
346 global $wpdb, $wp_locale;
348 $months = $wpdb->get_results( $wpdb->prepare( "
349 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
352 ORDER BY post_date DESC
355 $month_count = count( $months );
357 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
360 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
363 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
365 foreach ( $months as $arc_row ) {
366 if ( 0 == $arc_row->year )
369 $month = zeroise( $arc_row->month, 2 );
370 $year = $arc_row->year;
372 printf( "<option %s value='%s'>%s</option>\n",
373 selected( $m, $year . $month, false ),
374 esc_attr( $arc_row->year . $month ),
375 $wp_locale->get_month( $month ) . " $year"
384 * Display a view switcher
389 function view_switcher( $current_mode ) {
391 'list' => __( 'List View' ),
392 'excerpt' => __( 'Excerpt View' )
396 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
397 <div class="view-switch">
399 foreach ( $modes as $mode => $title ) {
400 $class = ( $current_mode == $mode ) ? 'class="current"' : '';
401 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";
409 * Display a comment count bubble
414 * @param int $post_id
415 * @param int $pending_comments
417 function comments_bubble( $post_id, $pending_comments ) {
418 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
420 if ( $pending_comments )
423 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>";
425 if ( $pending_comments )
430 * Get the current page number
437 function get_pagenum() {
438 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
440 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
441 $pagenum = $this->_pagination_args['total_pages'];
443 return max( 1, $pagenum );
447 * Get number of items to display on a single page
454 function get_items_per_page( $option, $default = 20 ) {
455 $per_page = (int) get_user_option( $option );
456 if ( empty( $per_page ) || $per_page < 1 )
457 $per_page = $default;
459 return (int) apply_filters( $option, $per_page );
463 * Display the pagination.
468 function pagination( $which ) {
469 if ( empty( $this->_pagination_args ) )
472 extract( $this->_pagination_args );
474 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
476 $current = $this->get_pagenum();
478 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
480 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
482 $page_links = array();
484 $disable_first = $disable_last = '';
486 $disable_first = ' disabled';
487 if ( $current == $total_pages )
488 $disable_last = ' disabled';
490 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
491 'first-page' . $disable_first,
492 esc_attr__( 'Go to the first page' ),
493 esc_url( remove_query_arg( 'paged', $current_url ) ),
497 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
498 'prev-page' . $disable_first,
499 esc_attr__( 'Go to the previous page' ),
500 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
504 if ( 'bottom' == $which )
505 $html_current_page = $current;
507 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='%s' value='%s' size='%d' />",
508 esc_attr__( 'Current page' ),
511 strlen( $total_pages )
514 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
515 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
517 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
518 'next-page' . $disable_last,
519 esc_attr__( 'Go to the next page' ),
520 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
524 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
525 'last-page' . $disable_last,
526 esc_attr__( 'Go to the last page' ),
527 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
531 $output .= "\n" . join( "\n", $page_links );
533 $page_class = $total_pages < 2 ? ' one-page' : '';
535 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
537 echo $this->_pagination;
541 * Get a list of columns. The format is:
542 * 'internal-name' => 'Title'
550 function get_columns() {
551 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
555 * Get a list of sortable columns. The format is:
556 * 'internal-name' => 'orderby'
558 * 'internal-name' => array( 'orderby', true )
560 * The second format will make the initial sorting order be descending
567 function get_sortable_columns() {
572 * Get a list of all, hidden and sortable columns, with filter applied
579 function get_column_info() {
580 if ( isset( $this->_column_headers ) )
581 return $this->_column_headers;
583 $screen = get_current_screen();
585 $columns = get_column_headers( $screen );
586 $hidden = get_hidden_columns( $screen );
588 $_sortable = apply_filters( "manage_{$screen->id}_sortable_columns", $this->get_sortable_columns() );
591 foreach ( $_sortable as $id => $data ) {
592 if ( empty( $data ) )
595 $data = (array) $data;
596 if ( !isset( $data[1] ) )
599 $sortable[$id] = $data;
602 $this->_column_headers = array( $columns, $hidden, $sortable );
604 return $this->_column_headers;
608 * Return number of visible columns
615 function get_column_count() {
616 list ( $columns, $hidden ) = $this->get_column_info();
617 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
618 return count( $columns ) - count( $hidden );
622 * Print column headers, accounting for hidden and sortable columns.
627 * @param bool $with_id Whether to set the id attribute or not
629 function print_column_headers( $with_id = true ) {
630 $screen = get_current_screen();
632 list( $columns, $hidden, $sortable ) = $this->get_column_info();
634 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
635 $current_url = remove_query_arg( 'paged', $current_url );
637 if ( isset( $_GET['orderby'] ) )
638 $current_orderby = $_GET['orderby'];
640 $current_orderby = '';
642 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
643 $current_order = 'desc';
645 $current_order = 'asc';
647 foreach ( $columns as $column_key => $column_display_name ) {
648 $class = array( 'manage-column', "column-$column_key" );
651 if ( in_array( $column_key, $hidden ) )
652 $style = 'display:none;';
654 $style = ' style="' . $style . '"';
656 if ( 'cb' == $column_key )
657 $class[] = 'check-column';
658 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
661 if ( isset( $sortable[$column_key] ) ) {
662 list( $orderby, $desc_first ) = $sortable[$column_key];
664 if ( $current_orderby == $orderby ) {
665 $order = 'asc' == $current_order ? 'desc' : 'asc';
667 $class[] = $current_order;
669 $order = $desc_first ? 'desc' : 'asc';
670 $class[] = 'sortable';
671 $class[] = $desc_first ? 'asc' : 'desc';
674 $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>';
677 $id = $with_id ? "id='$column_key'" : '';
679 if ( !empty( $class ) )
680 $class = "class='" . join( ' ', $class ) . "'";
682 echo "<th scope='col' $id $class $style>$column_display_name</th>";
693 extract( $this->_args );
695 $this->display_tablenav( 'top' );
698 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
701 <?php $this->print_column_headers(); ?>
707 <?php $this->print_column_headers( false ); ?>
711 <tbody id="the-list"<?php if ( $singular ) echo " class='list:$singular'"; ?>>
712 <?php $this->display_rows_or_placeholder(); ?>
716 $this->display_tablenav( 'bottom' );
720 * Get a list of CSS classes for the <table> tag
727 function get_table_classes() {
728 return array( 'widefat', 'fixed', $this->_args['plural'] );
732 * Generate the table navigation above or below the table
737 function display_tablenav( $which ) {
738 if ( 'top' == $which )
739 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
741 <div class="tablenav <?php echo esc_attr( $which ); ?>">
743 <div class="alignleft actions">
744 <?php $this->bulk_actions( $which ); ?>
747 $this->extra_tablenav( $which );
748 $this->pagination( $which );
757 * Extra controls to be displayed between bulk actions and pagination
762 function extra_tablenav( $which ) {}
765 * Generate the <tbody> part of the table
770 function display_rows_or_placeholder() {
771 if ( $this->has_items() ) {
772 $this->display_rows();
774 list( $columns, $hidden ) = $this->get_column_info();
775 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
782 * Generate the table rows
787 function display_rows() {
788 foreach ( $this->items as $item )
789 $this->single_row( $item );
793 * Generates content for a single row of the table
798 * @param object $item The current item
800 function single_row( $item ) {
801 static $row_class = '';
802 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
804 echo '<tr' . $row_class . '>';
805 echo $this->single_row_columns( $item );
810 * Generates the columns for a single row of the table
815 * @param object $item The current item
817 function single_row_columns( $item ) {
818 list( $columns, $hidden ) = $this->get_column_info();
820 foreach ( $columns as $column_name => $column_display_name ) {
821 $class = "class='$column_name column-$column_name'";
824 if ( in_array( $column_name, $hidden ) )
825 $style = ' style="display:none;"';
827 $attributes = "$class$style";
829 if ( 'cb' == $column_name ) {
830 echo '<th scope="row" class="check-column">';
831 echo $this->column_cb( $item );
834 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
835 echo "<td $attributes>";
836 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
840 echo "<td $attributes>";
841 echo $this->column_default( $item, $column_name );
848 * Handle an incoming ajax request (called from admin-ajax.php)
853 function ajax_response() {
854 $this->prepare_items();
856 extract( $this->_args );
857 extract( $this->_pagination_args );
860 if ( ! empty( $_REQUEST['no_placeholder'] ) )
861 $this->display_rows();
863 $this->display_rows_or_placeholder();
865 $rows = ob_get_clean();
867 $response = array( 'rows' => $rows );
869 if ( isset( $total_items ) )
870 $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
872 if ( isset( $total_pages ) ) {
873 $response['total_pages'] = $total_pages;
874 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
877 die( json_encode( $response ) );
881 * Send required variables to JavaScript land
885 function _js_vars() {
887 'class' => get_class( $this ),
888 'screen' => get_current_screen()
891 printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );