Changeset 16127 for trunk/wp-admin/includes/list-table.php
- Timestamp:
- 11/01/2010 08:56:27 AM (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/list-table.php
r16125 r16127 1 1 <?php 2 2 /** 3 * Base class and helper functions for displaying a list of items in an ajaxified HTML table.3 * Helper functions for displaying a list of items in an ajaxified HTML table. 4 4 * 5 5 * @package WordPress … … 7 7 * @since 3.1.0 8 8 */ 9 10 /**11 * Base class for displaying a list of items in an ajaxified HTML table.12 *13 * @package WordPress14 * @subpackage List_Table15 * @since 3.1.016 */17 class WP_List_Table {18 19 /**20 * The current list of items21 *22 * @since 3.1.023 * @var array24 * @access protected25 */26 var $items;27 28 /**29 * Various information about the current table30 *31 * @since 3.1.032 * @var array33 * @access private34 */35 var $_args;36 37 /**38 * Various information needed for displaying the pagination39 *40 * @since 3.1.041 * @var array42 * @access private43 */44 var $_pagination_args = array();45 46 /**47 * The current screen48 *49 * @since 3.1.050 * @var object51 * @access private52 */53 var $_screen;54 55 /**56 * Cached bulk actions57 *58 * @since 3.1.059 * @var array60 * @access private61 */62 var $_actions;63 64 /**65 * Cached pagination output66 *67 * @since 3.1.068 * @var string69 * @access private70 */71 var $_pagination;72 73 /**74 * Constructor. The child class should call this constructor from it's own constructor75 *76 * @param array $args An associative array with information about the current table77 * @access protected78 */79 function WP_List_Table( $args ) {80 $args = wp_parse_args( $args, array(81 'screen' => '',82 'plural' => '',83 'singular' => '',84 'ajax' => true85 ) );86 87 $this->_screen = $args['screen'];88 89 if ( is_string( $this->_screen ) )90 $this->_screen = convert_to_screen( $this->_screen );91 92 add_filter( 'manage_' . $this->_screen->id . '_columns', array( &$this, 'get_columns' ), 0 );93 94 if ( !$args['plural'] )95 $args['plural'] = $this->_screen->base;96 97 $this->_args = $args;98 99 if ( $args['ajax'] ) {100 wp_enqueue_script( 'list-table' );101 add_action( 'admin_footer', array( &$this, '_js_vars' ) );102 }103 }104 105 /**106 * Checks the current user's permissions107 * @uses wp_die()108 *109 * @since 3.1.0110 * @access public111 */112 function check_permissions() {113 die( 'function WP_List_Table::check_permissions() must be over-ridden in a sub-class.' );114 }115 116 /**117 * Prepares the list of items for displaying.118 * @uses WP_List_Table::set_pagination_args()119 *120 * @since 3.1.0121 * @access public122 */123 function prepare_items() {124 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );125 }126 127 /**128 * An internal method that sets all the necessary pagination arguments129 *130 * @param array $args An associative array with information about the pagination131 * @access protected132 */133 function set_pagination_args( $args ) {134 $args = wp_parse_args( $args, array(135 'query_var' => 'paged',136 'total_items' => 0,137 'total_pages' => 0,138 'per_page' => 0,139 ) );140 141 if ( !$args['total_pages'] && $args['per_page'] > 0 )142 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );143 144 $this->_pagination_args = $args;145 }146 147 /**148 * Access the pagination args149 *150 * @since 3.1.0151 * @access public152 *153 * @param string $key154 * @return array155 */156 function get_pagination_arg( $key ) {157 if ( 'page' == $key )158 return $this->get_pagenum();159 160 if ( isset( $this->_pagination_args[$key] ) )161 return $this->_pagination_args[$key];162 }163 164 /**165 * Whether the table has items to display or not166 *167 * @since 3.1.0168 * @access public169 *170 * @return bool171 */172 function has_items() {173 return !empty( $this->items );174 }175 176 /**177 * Message to be displayed when there are no items178 *179 * @since 3.1.0180 * @access public181 */182 function no_items() {183 _e( 'No items found.' );184 }185 186 /**187 * Get an associative array ( id => link ) with the list188 * of views available on this table.189 *190 * @since 3.1.0191 * @access protected192 *193 * @return array194 */195 function get_views() {196 return array();197 }198 199 /**200 * Display the bulk actions dropdown.201 *202 * @since 3.1.0203 * @access public204 */205 function views() {206 $views = $this->get_views();207 $views = apply_filters( 'views_' . $this->_screen->base, $views );208 209 if ( empty( $views ) )210 return;211 212 echo "<ul class='subsubsub'>\n";213 echo implode( " |</li>\n", $views ) . "</li>\n";214 echo "</ul>";215 }216 217 /**218 * Get an associative array ( option_name => option_title ) with the list219 * of bulk actions available on this table.220 *221 * @since 3.1.0222 * @access protected223 *224 * @return array225 */226 function get_bulk_actions() {227 return array();228 }229 230 /**231 * Display the bulk actions dropdown.232 *233 * @since 3.1.0234 * @access public235 */236 function bulk_actions() {237 238 if ( is_null( $this->_actions ) ) {239 $this->_actions = $this->get_bulk_actions();240 $this->_actions = apply_filters( 'bulk_actions-' . $this->_screen->base, $this->_actions );241 $two = '';242 }243 else {244 $two = '2';245 }246 247 if ( empty( $this->_actions ) )248 return;249 250 echo "<select name='action$two'>\n";251 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";252 foreach ( $this->_actions as $name => $title )253 echo "\t<option value='$name'>$title</option>\n";254 echo "</select>\n";255 256 submit_button( __( 'Apply' ), 'button-secondary action', "doaction$two", false );257 echo "\n";258 }259 260 /**261 * Get the current action selected from the bulk actions dropdown.262 *263 * @since 3.1.0264 * @access public265 *266 * @return string|bool The action name or False if no action was selected267 */268 function current_action() {269 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )270 return $_REQUEST['action'];271 272 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )273 return $_REQUEST['action2'];274 275 return false;276 }277 278 /**279 * Generate row actions div280 *281 * @since 3.1.0282 * @access protected283 *284 * @param array $actions The list of actions285 * @param bool $always_visible Wether the actions should be always visible286 * @return string287 */288 function row_actions( $actions, $always_visible = false ) {289 $action_count = count( $actions );290 $i = 0;291 292 if ( !$action_count )293 return '';294 295 $out = '<div class="' . ( $always_visible ? 'row-actions-visible' : 'row-actions' ) . '">';296 foreach ( $actions as $action => $link ) {297 ++$i;298 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';299 $out .= "<span class='$action'>$link$sep</span>";300 }301 $out .= '</div>';302 303 return $out;304 }305 306 /**307 * Display a monthly dropdown for filtering items308 *309 * @since 3.1.0310 * @access protected311 */312 function months_dropdown( $post_type ) {313 global $wpdb, $wp_locale;314 315 $months = $wpdb->get_results( $wpdb->prepare( "316 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month317 FROM $wpdb->posts318 WHERE post_type = %s319 ORDER BY post_date DESC320 ", $post_type ) );321 322 $month_count = count( $months );323 324 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )325 return;326 327 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;328 ?>329 <select name='m'>330 <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>331 <?php332 foreach ( $months as $arc_row ) {333 if ( 0 == $arc_row->year )334 continue;335 336 $month = zeroise( $arc_row->month, 2 );337 $year = $arc_row->year;338 339 printf( "<option %s value='%s'>%s</option>\n",340 selected( $m, $year . $month, false ),341 esc_attr( $arc_row->year . $month ),342 $wp_locale->get_month( $month ) . " $year"343 );344 }345 ?>346 </select>347 <?php348 }349 350 /**351 * Display a view switcher352 *353 * @since 3.1.0354 * @access protected355 */356 function view_switcher( $current_mode ) {357 $modes = array(358 'list' => __( 'List View' ),359 'excerpt' => __( 'Excerpt View' )360 );361 362 ?>363 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />364 <div class="view-switch">365 <?php366 foreach ( $modes as $mode => $title ) {367 $class = ( $current_mode == $mode ) ? 'class="current"' : '';368 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";369 }370 ?>371 </div>372 <?php373 }374 375 /**376 * Display a comment count bubble377 *378 * @since 3.1.0379 * @access protected380 *381 * @param int $post_id382 * @param int $pending_comments383 */384 function comments_bubble( $post_id, $pending_comments ) {385 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );386 387 if ( $pending_comments )388 echo '<strong>';389 390 $link = "<a href='" . add_query_arg( 'p', $post_id, admin_url('edit-comments.php') ) . "' title='$pending_phrase' class='post-com-count'><span class='comment-count'>%s</span></a>";391 392 comments_number(393 sprintf( $link, /* translators: comment count link */ _x( '0', 'comment count' ) ),394 sprintf( $link, /* translators: comment count link */ _x( '1', 'comment count' ) ),395 sprintf( $link, /* translators: comment count link: % will be substituted by comment count */ _x( '%', 'comment count' ) )396 );397 398 if ( $pending_comments )399 echo '</strong>';400 }401 402 /**403 * Get the current page number404 *405 * @since 3.1.0406 * @access protected407 *408 * @return int409 */410 function get_pagenum( $query_var = 'paged' ) {411 $pagenum = isset( $_REQUEST[$query_var] ) ? absint( $_REQUEST[$query_var] ) : 0;412 413 return max( 1, $pagenum );414 }415 416 /**417 * Get number of items to display on a single page418 *419 * @since 3.1.0420 * @access protected421 *422 * @return int423 */424 function get_items_per_page( $option, $default = 20 ) {425 $per_page = (int) get_user_option( $option );426 if ( empty( $per_page ) || $per_page < 1 )427 $per_page = $default;428 429 return (int) apply_filters( $option, $per_page );430 }431 432 /**433 * Display the pagination.434 *435 * @since 3.1.0436 * @access protected437 */438 function pagination() {439 if ( $this->_pagination ) {440 echo $this->_pagination;441 return;442 }443 444 if ( empty( $this->_pagination_args ) )445 return;446 447 extract( $this->_pagination_args );448 449 if ( $total_pages < 2 )450 return;451 452 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';453 454 $current = $this->get_pagenum( $query_var );455 456 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];457 458 $page_links = array();459 460 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",461 'first-page',462 esc_attr__( 'Go to the first page' ),463 esc_url( remove_query_arg( $query_var, $current_url ) ),464 '««'465 );466 467 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",468 'prev-page',469 esc_attr__( 'Go to the previous page' ),470 esc_url( add_query_arg( $query_var, max( 1, $current-1 ), $current_url ) ),471 '«'472 );473 474 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='%s' value='%s' size='%d' />",475 esc_attr__( 'Current page' ),476 esc_attr( $query_var ),477 number_format_i18n( $current ),478 strlen( $total_pages )479 );480 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );481 $page_links[] = sprintf( _x( '%s of %s', 'paging' ), $html_current_page, $html_total_pages );482 483 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",484 'next-page',485 esc_attr__( 'Go to the next page' ),486 esc_url( add_query_arg( $query_var, min( $total_pages, $current+1 ), $current_url ) ),487 '»'488 );489 490 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",491 'last-page',492 esc_attr__( 'Go to the last page' ),493 esc_url( add_query_arg( $query_var, $total_pages, $current_url ) ),494 '»»'495 );496 497 $output .= join( "\n", $page_links );498 499 $this->_pagination = "<div class='tablenav-pages'>$output</div>";500 501 echo $this->_pagination;502 }503 504 /**505 * Get a list of columns. The format is internal_name => title506 *507 * @since 3.1.0508 * @access protected509 *510 * @return array511 */512 function get_columns() {513 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );514 }515 516 /**517 * Get a list of sortable columns. The format is internal_name => orderby518 *519 * @since 3.1.0520 * @access protected521 *522 * @return array523 */524 function get_sortable_columns() {525 return array();526 }527 528 /**529 * Get a list of all, hidden and sortable columns, with filter applied530 *531 * @since 3.1.0532 * @access protected533 *534 * @return array535 */536 function get_column_info() {537 if ( !isset( $this->_column_headers ) ) {538 $columns = get_column_headers( $this->_screen );539 $hidden = get_hidden_columns( $this->_screen );540 $sortable = apply_filters( 'manage_' . $this->_screen->id . '_sortable_columns', $this->get_sortable_columns() );541 542 $this->_column_headers = array( $columns, $hidden, $sortable );543 }544 545 return $this->_column_headers;546 }547 548 /**549 * Print column headers, accounting for hidden and sortable columns.550 *551 * @since 3.1.0552 * @access protected553 *554 * @param bool $with_id Whether to set the id attribute or not555 */556 function print_column_headers( $with_id = true ) {557 $screen = $this->_screen;558 559 list( $columns, $hidden, $sortable ) = $this->get_column_info();560 561 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];562 563 if ( isset( $_GET['orderby'] ) )564 $current_orderby = $_GET['orderby'];565 else566 $current_orderby = '';567 568 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )569 $current_order = 'desc';570 else571 $current_order = 'asc';572 573 foreach ( $columns as $column_key => $column_display_name ) {574 $class = array( 'manage-column', "column-$column_key" );575 576 $style = '';577 if ( in_array( $column_key, $hidden ) )578 $style = 'display:none;';579 580 $style = ' style="' . $style . '"';581 582 if ( 'cb' == $column_key )583 $class[] = 'check-column';584 elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )585 $class[] = 'num';586 587 if ( isset( $sortable[$column_key] ) ) {588 $orderby = $sortable[$column_key];589 if ( $current_orderby == $orderby ) {590 $order = 'asc' == $current_order ? 'desc' : 'asc';591 $class[] = "sorted-$current_order";592 } else {593 $order = 'asc';594 $class[] = 'sortable';595 }596 $column_display_name = '<a href="' . esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ) . '">' . $column_display_name . '</a>';597 $column_display_name .= '<div class="sorting-indicator"></div>';598 }599 600 $id = $with_id ? "id='$column_key'" : '';601 602 if ( !empty( $class ) )603 $class = "class='" . join( ' ', $class ) . "'";604 605 echo "<th scope='col' $id $class $style>$column_display_name</th>";606 }607 }608 609 /**610 * Display the table or a message if there are no items611 *612 * @since 3.1.0613 * @access public614 */615 function display() {616 if ( $this->has_items() ) {617 $this->display_table();618 } else {619 ?>620 <br class="clear" />621 <p><?php $this->no_items(); ?></p>622 <?php623 }624 }625 626 /**627 * Get a list of CSS classes for the <table> tag628 *629 * @since 3.1.0630 * @access protected631 *632 * @return array633 */634 function get_table_classes() {635 extract( $this->_args );636 637 return array( 'widefat', 'fixed', $plural );638 }639 640 /**641 * Display the full table642 *643 * @since 3.1.0644 * @access public645 */646 function display_table() {647 extract( $this->_args );648 649 $this->display_tablenav( 'top' );650 651 ?>652 <table class="<?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">653 <thead>654 <tr>655 <?php $this->print_column_headers(); ?>656 </tr>657 </thead>658 659 <tfoot>660 <tr>661 <?php $this->print_column_headers( false ); ?>662 </tr>663 </tfoot>664 665 <tbody id="the-list"<?php if ( $singular ) echo " class='list:$singular'"; ?>>666 <?php $this->display_rows(); ?>667 </tbody>668 </table>669 <?php670 671 $this->display_tablenav( 'bottom' );672 }673 674 /**675 * Generate the table navigation above or below the table676 *677 * @since 3.1.0678 * @access protected679 */680 function display_tablenav( $which ) {681 if ( 'top' == $which )682 wp_nonce_field( 'bulk-' . $this->_args['plural'] );683 ?>684 <div class="tablenav">685 686 <div class="alignleft actions">687 <?php $this->bulk_actions( $which ); ?>688 </div>689 690 <?php691 $this->extra_tablenav( $which );692 $this->pagination( $which );693 ?>694 695 <br class="clear" />696 </div>697 698 <br class="clear" />699 <?php700 }701 702 /**703 * Extra controls to be displayed between bulk actions and pagination704 *705 * @since 3.1.0706 * @access protected707 */708 function extra_tablenav( $which ) {}709 710 /**711 * Generate the <tbody> part of the table712 *713 * @since 3.1.0714 * @access protected715 */716 function display_rows() {717 foreach ( $this->items as $item )718 $this->single_row( $item );719 }720 721 /**722 * Generates content for a single row of the table723 *724 * @since 3.1.0725 * @access protected726 *727 * @param $object $item The current item728 */729 function single_row( $item ) {730 static $row_class = '';731 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );732 733 echo '<tr' . $row_class . '>';734 echo $this->single_row_columns( $item );735 echo '</tr>';736 }737 738 /**739 * Generates the columns for a single row of the table740 *741 * @since 3.1.0742 * @access protected743 *744 * @param $object $item The current item745 */746 function single_row_columns( $item ) {747 list( $columns, $hidden ) = $this->get_column_info();748 749 foreach ( $columns as $column_name => $column_display_name ) {750 $class = "class=\"$column_name column-$column_name\"";751 752 $style = '';753 if ( in_array( $column_name, $hidden ) )754 $style = ' style="display:none;"';755 756 $attributes = "$class$style";757 758 if ( 'cb' == $column_name ) {759 echo '<th scope="row" class="check-column">';760 echo $this->column_cb( $item );761 echo '</th>';762 }763 elseif ( method_exists( $this, 'column_' . $column_name ) ) {764 echo "<td $attributes>";765 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );766 echo "</td>";767 }768 else {769 echo "<td $attributes>";770 echo $this->column_default( $item, $column_name );771 echo "</td>";772 }773 }774 }775 776 /**777 * Handle an incoming ajax request (called from admin-ajax.php)778 *779 * @since 3.1.0780 * @access public781 */782 function ajax_response() {783 $this->check_permissions();784 $this->prepare_items();785 786 extract( $this->_args );787 extract( $this->_pagination_args );788 789 ob_start();790 $this->display_rows();791 $rows = ob_get_clean();792 793 $response = array( 'rows' => $rows );794 795 if ( isset( $total_items ) )796 $response['total_items'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );797 798 if ( isset( $total_pages ) )799 $response['total_pages'] = $total_pages;800 801 die( json_encode( $response ) );802 }803 804 /**805 * Send required variables to JavaScript land806 *807 * @access private808 */809 function _js_vars() {810 extract( $this->_args );811 812 $class = get_class( $this );813 814 printf( "<script type='text/javascript'>list_args = %s;</script>\n",815 json_encode( compact( 'screen', 'class' ) )816 );817 }818 }819 9 820 10 /** … … 828 18 function get_list_table( $class ) { 829 19 $class = apply_filters( "get_list_table_$class", $class ); 830 20 831 21 require_list_table( $class ); 832 22
Note: See TracChangeset
for help on using the changeset viewer.