]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-list-table.php
WordPress 4.1.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-list-table.php
1 <?php
2 /**
3  * Base class for displaying a list of items in an ajaxified HTML table.
4  *
5  * @since 3.1.0
6  * @access private
7  *
8  * @package WordPress
9  * @subpackage List_Table
10  */
11 class WP_List_Table {
12
13         /**
14          * The current list of items
15          *
16          * @since 3.1.0
17          * @var array
18          * @access public
19          */
20         public $items;
21
22         /**
23          * Various information about the current table
24          *
25          * @since 3.1.0
26          * @var array
27          * @access private
28          */
29         private $_args;
30
31         /**
32          * Various information needed for displaying the pagination
33          *
34          * @since 3.1.0
35          * @var array
36          * @access private
37          */
38         private $_pagination_args = array();
39
40         /**
41          * The current screen
42          *
43          * @since 3.1.0
44          * @var object
45          * @access protected
46          */
47         protected $screen;
48
49         /**
50          * Cached bulk actions
51          *
52          * @since 3.1.0
53          * @var array
54          * @access private
55          */
56         private $_actions;
57
58         /**
59          * Cached pagination output
60          *
61          * @since 3.1.0
62          * @var string
63          * @access private
64          */
65         private $_pagination;
66
67         /**
68          * The view switcher modes.
69          *
70          * @since 4.1.0
71          * @var array
72          * @access protected
73          */
74         protected $modes = array();
75
76         /**
77          * Constructor.
78          *
79          * The child class should call this constructor from its own constructor to override
80          * the default $args.
81          *
82          * @since 3.1.0
83          * @access public
84          *
85          * @param array|string $args {
86          *     Array or string of arguments.
87          *
88          *     @type string $plural   Plural value used for labels and the objects being listed.
89          *                            This affects things such as CSS class-names and nonces used
90          *                            in the list table, e.g. 'posts'. Default empty.
91          *     @type string $singular Singular label for an object being listed, e.g. 'post'.
92          *                            Default empty
93          *     @type bool   $ajax     Whether the list table supports AJAX. This includes loading
94          *                            and sorting data, for example. If true, the class will call
95          *                            the {@see _js_vars()} method in the footer to provide variables
96          *                            to any scripts handling AJAX events. Default false.
97          *     @type string $screen   String containing the hook name used to determine the current
98          *                            screen. If left null, the current screen will be automatically set.
99          *                            Default null.
100          * }
101          */
102         public function __construct( $args = array() ) {
103                 $args = wp_parse_args( $args, array(
104                         'plural' => '',
105                         'singular' => '',
106                         'ajax' => false,
107                         'screen' => null,
108                 ) );
109
110                 $this->screen = convert_to_screen( $args['screen'] );
111
112                 add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
113
114                 if ( !$args['plural'] )
115                         $args['plural'] = $this->screen->base;
116
117                 $args['plural'] = sanitize_key( $args['plural'] );
118                 $args['singular'] = sanitize_key( $args['singular'] );
119
120                 $this->_args = $args;
121
122                 if ( $args['ajax'] ) {
123                         // wp_enqueue_script( 'list-table' );
124                         add_action( 'admin_footer', array( $this, '_js_vars' ) );
125                 }
126
127                 if ( empty( $this->modes ) ) {
128                         $this->modes = array(
129                                 'list'    => __( 'List View' ),
130                                 'excerpt' => __( 'Excerpt View' )
131                         );
132                 }
133         }
134
135         /**
136          * Make private properties readable for backwards compatibility.
137          *
138          * @since 4.0.0
139          * @access public
140          *
141          * @param string $name Property to get.
142          * @return mixed Property.
143          */
144         public function __get( $name ) {
145                 return $this->$name;
146         }
147
148         /**
149          * Make private properties settable for backwards compatibility.
150          *
151          * @since 4.0.0
152          * @access public
153          *
154          * @param string $name  Property to set.
155          * @param mixed  $value Property value.
156          * @return mixed Newly-set property.
157          */
158         public function __set( $name, $value ) {
159                 return $this->$name = $value;
160         }
161
162         /**
163          * Make private properties checkable for backwards compatibility.
164          *
165          * @since 4.0.0
166          * @access public
167          *
168          * @param string $name Property to check if set.
169          * @return bool Whether the property is set.
170          */
171         public function __isset( $name ) {
172                 return isset( $this->$name );
173         }
174
175         /**
176          * Make private properties un-settable for backwards compatibility.
177          *
178          * @since 4.0.0
179          * @access public
180          *
181          * @param string $name Property to unset.
182          */
183         public function __unset( $name ) {
184                 unset( $this->$name );
185         }
186
187         /**
188          * Make private/protected methods readable for backwards compatibility.
189          *
190          * @since 4.0.0
191          * @access public
192          *
193          * @param callable $name      Method to call.
194          * @param array    $arguments Arguments to pass when calling.
195          * @return mixed|bool Return value of the callback, false otherwise.
196          */
197         public function __call( $name, $arguments ) {
198                 return call_user_func_array( array( $this, $name ), $arguments );
199         }
200
201         /**
202          * Checks the current user's permissions
203          *
204          * @since 3.1.0
205          * @access public
206          * @abstract
207          */
208         public function ajax_user_can() {
209                 die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
210         }
211
212         /**
213          * Prepares the list of items for displaying.
214          * @uses WP_List_Table::set_pagination_args()
215          *
216          * @since 3.1.0
217          * @access public
218          * @abstract
219          */
220         public function prepare_items() {
221                 die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
222         }
223
224         /**
225          * An internal method that sets all the necessary pagination arguments
226          *
227          * @param array $args An associative array with information about the pagination
228          * @access protected
229          */
230         protected function set_pagination_args( $args ) {
231                 $args = wp_parse_args( $args, array(
232                         'total_items' => 0,
233                         'total_pages' => 0,
234                         'per_page' => 0,
235                 ) );
236
237                 if ( !$args['total_pages'] && $args['per_page'] > 0 )
238                         $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
239
240                 // Redirect if page number is invalid and headers are not already sent.
241                 if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
242                         wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
243                         exit;
244                 }
245
246                 $this->_pagination_args = $args;
247         }
248
249         /**
250          * Access the pagination args.
251          *
252          * @since 3.1.0
253          * @access public
254          *
255          * @param string $key Pagination argument to retrieve. Common values include 'total_items',
256          *                    'total_pages', 'per_page', or 'infinite_scroll'.
257          * @return int Number of items that correspond to the given pagination argument.
258          */
259         public function get_pagination_arg( $key ) {
260                 if ( 'page' == $key )
261                         return $this->get_pagenum();
262
263                 if ( isset( $this->_pagination_args[$key] ) )
264                         return $this->_pagination_args[$key];
265         }
266
267         /**
268          * Whether the table has items to display or not
269          *
270          * @since 3.1.0
271          * @access public
272          *
273          * @return bool
274          */
275         public function has_items() {
276                 return !empty( $this->items );
277         }
278
279         /**
280          * Message to be displayed when there are no items
281          *
282          * @since 3.1.0
283          * @access public
284          */
285         public function no_items() {
286                 _e( 'No items found.' );
287         }
288
289         /**
290          * Display the search box.
291          *
292          * @since 3.1.0
293          * @access public
294          *
295          * @param string $text The search button text
296          * @param string $input_id The search input id
297          */
298         public function search_box( $text, $input_id ) {
299                 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
300                         return;
301
302                 $input_id = $input_id . '-search-input';
303
304                 if ( ! empty( $_REQUEST['orderby'] ) )
305                         echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
306                 if ( ! empty( $_REQUEST['order'] ) )
307                         echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
308                 if ( ! empty( $_REQUEST['post_mime_type'] ) )
309                         echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
310                 if ( ! empty( $_REQUEST['detached'] ) )
311                         echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
312 ?>
313 <p class="search-box">
314         <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
315         <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
316         <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
317 </p>
318 <?php
319         }
320
321         /**
322          * Get an associative array ( id => link ) with the list
323          * of views available on this table.
324          *
325          * @since 3.1.0
326          * @access protected
327          *
328          * @return array
329          */
330         protected function get_views() {
331                 return array();
332         }
333
334         /**
335          * Display the list of views available on this table.
336          *
337          * @since 3.1.0
338          * @access public
339          */
340         public function views() {
341                 $views = $this->get_views();
342                 /**
343                  * Filter the list of available list table views.
344                  *
345                  * The dynamic portion of the hook name, `$this->screen->id`, refers
346                  * to the ID of the current screen, usually a string.
347                  *
348                  * @since 3.5.0
349                  *
350                  * @param array $views An array of available list table views.
351                  */
352                 $views = apply_filters( "views_{$this->screen->id}", $views );
353
354                 if ( empty( $views ) )
355                         return;
356
357                 echo "<ul class='subsubsub'>\n";
358                 foreach ( $views as $class => $view ) {
359                         $views[ $class ] = "\t<li class='$class'>$view";
360                 }
361                 echo implode( " |</li>\n", $views ) . "</li>\n";
362                 echo "</ul>";
363         }
364
365         /**
366          * Get an associative array ( option_name => option_title ) with the list
367          * of bulk actions available on this table.
368          *
369          * @since 3.1.0
370          * @access protected
371          *
372          * @return array
373          */
374         protected function get_bulk_actions() {
375                 return array();
376         }
377
378         /**
379          * Display the bulk actions dropdown.
380          *
381          * @since 3.1.0
382          * @access protected
383          *
384          * @param string $which The location of the bulk actions: 'top' or 'bottom'.
385          *                      This is designated as optional for backwards-compatibility.
386          */
387         protected function bulk_actions( $which = '' ) {
388                 if ( is_null( $this->_actions ) ) {
389                         $no_new_actions = $this->_actions = $this->get_bulk_actions();
390                         /**
391                          * Filter the list table Bulk Actions drop-down.
392                          *
393                          * The dynamic portion of the hook name, `$this->screen->id`, refers
394                          * to the ID of the current screen, usually a string.
395                          *
396                          * This filter can currently only be used to remove bulk actions.
397                          *
398                          * @since 3.5.0
399                          *
400                          * @param array $actions An array of the available bulk actions.
401                          */
402                         $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
403                         $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
404                         $two = '';
405                 } else {
406                         $two = '2';
407                 }
408
409                 if ( empty( $this->_actions ) )
410                         return;
411
412                 echo "<label for='bulk-action-selector-" . esc_attr( $which ) . "' class='screen-reader-text'>" . __( 'Select bulk action' ) . "</label>";
413                 echo "<select name='action$two' id='bulk-action-selector-" . esc_attr( $which ) . "'>\n";
414                 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
415
416                 foreach ( $this->_actions as $name => $title ) {
417                         $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
418
419                         echo "\t<option value='$name'$class>$title</option>\n";
420                 }
421
422                 echo "</select>\n";
423
424                 submit_button( __( 'Apply' ), 'action', false, false, array( 'id' => "doaction$two" ) );
425                 echo "\n";
426         }
427
428         /**
429          * Get the current action selected from the bulk actions dropdown.
430          *
431          * @since 3.1.0
432          * @access public
433          *
434          * @return string|bool The action name or False if no action was selected
435          */
436         public function current_action() {
437                 if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) )
438                         return false;
439
440                 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
441                         return $_REQUEST['action'];
442
443                 if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
444                         return $_REQUEST['action2'];
445
446                 return false;
447         }
448
449         /**
450          * Generate row actions div
451          *
452          * @since 3.1.0
453          * @access protected
454          *
455          * @param array $actions The list of actions
456          * @param bool $always_visible Whether the actions should be always visible
457          * @return string
458          */
459         protected function row_actions( $actions, $always_visible = false ) {
460                 $action_count = count( $actions );
461                 $i = 0;
462
463                 if ( !$action_count )
464                         return '';
465
466                 $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
467                 foreach ( $actions as $action => $link ) {
468                         ++$i;
469                         ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
470                         $out .= "<span class='$action'>$link$sep</span>";
471                 }
472                 $out .= '</div>';
473
474                 return $out;
475         }
476
477         /**
478          * Display a monthly dropdown for filtering items
479          *
480          * @since 3.1.0
481          * @access protected
482          *
483          * @param string $post_type
484          */
485         protected function months_dropdown( $post_type ) {
486                 global $wpdb, $wp_locale;
487
488                 $months = $wpdb->get_results( $wpdb->prepare( "
489                         SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
490                         FROM $wpdb->posts
491                         WHERE post_type = %s
492                         ORDER BY post_date DESC
493                 ", $post_type ) );
494
495                 /**
496                  * Filter the 'Months' drop-down results.
497                  *
498                  * @since 3.7.0
499                  *
500                  * @param object $months    The months drop-down query results.
501                  * @param string $post_type The post type.
502                  */
503                 $months = apply_filters( 'months_dropdown_results', $months, $post_type );
504
505                 $month_count = count( $months );
506
507                 if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
508                         return;
509
510                 $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
511 ?>
512                 <label for="filter-by-date" class="screen-reader-text"><?php _e( 'Filter by date' ); ?></label>
513                 <select name="m" id="filter-by-date">
514                         <option<?php selected( $m, 0 ); ?> value="0"><?php _e( 'All dates' ); ?></option>
515 <?php
516                 foreach ( $months as $arc_row ) {
517                         if ( 0 == $arc_row->year )
518                                 continue;
519
520                         $month = zeroise( $arc_row->month, 2 );
521                         $year = $arc_row->year;
522
523                         printf( "<option %s value='%s'>%s</option>\n",
524                                 selected( $m, $year . $month, false ),
525                                 esc_attr( $arc_row->year . $month ),
526                                 /* translators: 1: month name, 2: 4-digit year */
527                                 sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
528                         );
529                 }
530 ?>
531                 </select>
532 <?php
533         }
534
535         /**
536          * Display a view switcher
537          *
538          * @since 3.1.0
539          * @access protected
540          *
541          * @param string $current_mode
542          */
543         protected function view_switcher( $current_mode ) {
544 ?>
545                 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
546                 <div class="view-switch">
547 <?php
548                         foreach ( $this->modes as $mode => $title ) {
549                                 $classes = array( 'view-' . $mode );
550                                 if ( $current_mode == $mode )
551                                         $classes[] = 'current';
552                                 printf(
553                                         "<a href='%s' class='%s' id='view-switch-$mode'><span class='screen-reader-text'>%s</span></a>\n",
554                                         esc_url( add_query_arg( 'mode', $mode ) ),
555                                         implode( ' ', $classes ),
556                                         $title
557                                 );
558                         }
559                 ?>
560                 </div>
561 <?php
562         }
563
564         /**
565          * Display a comment count bubble
566          *
567          * @since 3.1.0
568          * @access protected
569          *
570          * @param int $post_id          The post ID.
571          * @param int $pending_comments Number of pending comments.
572          */
573         protected function comments_bubble( $post_id, $pending_comments ) {
574                 $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
575
576                 if ( $pending_comments )
577                         echo '<strong>';
578
579                 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>";
580
581                 if ( $pending_comments )
582                         echo '</strong>';
583         }
584
585         /**
586          * Get the current page number
587          *
588          * @since 3.1.0
589          * @access public
590          *
591          * @return int
592          */
593         public function get_pagenum() {
594                 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
595
596                 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
597                         $pagenum = $this->_pagination_args['total_pages'];
598
599                 return max( 1, $pagenum );
600         }
601
602         /**
603          * Get number of items to display on a single page
604          *
605          * @since 3.1.0
606          * @access protected
607          *
608          * @param string $option
609          * @param int    $default
610          * @return int
611          */
612         protected function get_items_per_page( $option, $default = 20 ) {
613                 $per_page = (int) get_user_option( $option );
614                 if ( empty( $per_page ) || $per_page < 1 )
615                         $per_page = $default;
616
617                 /**
618                  * Filter the number of items to be displayed on each page of the list table.
619                  *
620                  * The dynamic hook name, $option, refers to the `per_page` option depending
621                  * on the type of list table in use. Possible values include: 'edit_comments_per_page',
622                  * 'sites_network_per_page', 'site_themes_network_per_page', 'themes_network_per_page',
623                  * 'users_network_per_page', 'edit_post_per_page', 'edit_page_per_page',
624                  * 'edit_{$post_type}_per_page', etc.
625                  *
626                  * @since 2.9.0
627                  *
628                  * @param int $per_page Number of items to be displayed. Default 20.
629                  */
630                 return (int) apply_filters( $option, $per_page );
631         }
632
633         /**
634          * Display the pagination.
635          *
636          * @since 3.1.0
637          * @access protected
638          *
639          * @param string $which
640          */
641         protected function pagination( $which ) {
642                 if ( empty( $this->_pagination_args ) ) {
643                         return;
644                 }
645
646                 $total_items = $this->_pagination_args['total_items'];
647                 $total_pages = $this->_pagination_args['total_pages'];
648                 $infinite_scroll = false;
649                 if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
650                         $infinite_scroll = $this->_pagination_args['infinite_scroll'];
651                 }
652
653                 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
654
655                 $current = $this->get_pagenum();
656
657                 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
658
659                 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
660
661                 $page_links = array();
662
663                 $disable_first = $disable_last = '';
664                 if ( $current == 1 ) {
665                         $disable_first = ' disabled';
666                 }
667                 if ( $current == $total_pages ) {
668                         $disable_last = ' disabled';
669                 }
670                 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
671                         'first-page' . $disable_first,
672                         esc_attr__( 'Go to the first page' ),
673                         esc_url( remove_query_arg( 'paged', $current_url ) ),
674                         '&laquo;'
675                 );
676
677                 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
678                         'prev-page' . $disable_first,
679                         esc_attr__( 'Go to the previous page' ),
680                         esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
681                         '&lsaquo;'
682                 );
683
684                 if ( 'bottom' == $which ) {
685                         $html_current_page = $current;
686                 } else {
687                         $html_current_page = sprintf( "%s<input class='current-page' id='current-page-selector' title='%s' type='text' name='paged' value='%s' size='%d' />",
688                                 '<label for="current-page-selector" class="screen-reader-text">' . __( 'Select Page' ) . '</label>',
689                                 esc_attr__( 'Current page' ),
690                                 $current,
691                                 strlen( $total_pages )
692                         );
693                 }
694                 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
695                 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
696
697                 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
698                         'next-page' . $disable_last,
699                         esc_attr__( 'Go to the next page' ),
700                         esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
701                         '&rsaquo;'
702                 );
703
704                 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
705                         'last-page' . $disable_last,
706                         esc_attr__( 'Go to the last page' ),
707                         esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
708                         '&raquo;'
709                 );
710
711                 $pagination_links_class = 'pagination-links';
712                 if ( ! empty( $infinite_scroll ) ) {
713                         $pagination_links_class = ' hide-if-js';
714                 }
715                 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
716
717                 if ( $total_pages ) {
718                         $page_class = $total_pages < 2 ? ' one-page' : '';
719                 } else {
720                         $page_class = ' no-pages';
721                 }
722                 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
723
724                 echo $this->_pagination;
725         }
726
727         /**
728          * Get a list of columns. The format is:
729          * 'internal-name' => 'Title'
730          *
731          * @since 3.1.0
732          * @access public
733          * @abstract
734          *
735          * @return array
736          */
737         public function get_columns() {
738                 die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
739         }
740
741         /**
742          * Get a list of sortable columns. The format is:
743          * 'internal-name' => 'orderby'
744          * or
745          * 'internal-name' => array( 'orderby', true )
746          *
747          * The second format will make the initial sorting order be descending
748          *
749          * @since 3.1.0
750          * @access protected
751          *
752          * @return array
753          */
754         protected function get_sortable_columns() {
755                 return array();
756         }
757
758         /**
759          * Get a list of all, hidden and sortable columns, with filter applied
760          *
761          * @since 3.1.0
762          * @access protected
763          *
764          * @return array
765          */
766         protected function get_column_info() {
767                 if ( isset( $this->_column_headers ) )
768                         return $this->_column_headers;
769
770                 $columns = get_column_headers( $this->screen );
771                 $hidden = get_hidden_columns( $this->screen );
772
773                 $sortable_columns = $this->get_sortable_columns();
774                 /**
775                  * Filter the list table sortable columns for a specific screen.
776                  *
777                  * The dynamic portion of the hook name, `$this->screen->id`, refers
778                  * to the ID of the current screen, usually a string.
779                  *
780                  * @since 3.5.0
781                  *
782                  * @param array $sortable_columns An array of sortable columns.
783                  */
784                 $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
785
786                 $sortable = array();
787                 foreach ( $_sortable as $id => $data ) {
788                         if ( empty( $data ) )
789                                 continue;
790
791                         $data = (array) $data;
792                         if ( !isset( $data[1] ) )
793                                 $data[1] = false;
794
795                         $sortable[$id] = $data;
796                 }
797
798                 $this->_column_headers = array( $columns, $hidden, $sortable );
799
800                 return $this->_column_headers;
801         }
802
803         /**
804          * Return number of visible columns
805          *
806          * @since 3.1.0
807          * @access public
808          *
809          * @return int
810          */
811         public function get_column_count() {
812                 list ( $columns, $hidden ) = $this->get_column_info();
813                 $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
814                 return count( $columns ) - count( $hidden );
815         }
816
817         /**
818          * Print column headers, accounting for hidden and sortable columns.
819          *
820          * @since 3.1.0
821          * @access public
822          *
823          * @param bool $with_id Whether to set the id attribute or not
824          */
825         public function print_column_headers( $with_id = true ) {
826                 list( $columns, $hidden, $sortable ) = $this->get_column_info();
827
828                 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
829                 $current_url = remove_query_arg( 'paged', $current_url );
830
831                 if ( isset( $_GET['orderby'] ) )
832                         $current_orderby = $_GET['orderby'];
833                 else
834                         $current_orderby = '';
835
836                 if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
837                         $current_order = 'desc';
838                 else
839                         $current_order = 'asc';
840
841                 if ( ! empty( $columns['cb'] ) ) {
842                         static $cb_counter = 1;
843                         $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
844                                 . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
845                         $cb_counter++;
846                 }
847
848                 foreach ( $columns as $column_key => $column_display_name ) {
849                         $class = array( 'manage-column', "column-$column_key" );
850
851                         $style = '';
852                         if ( in_array( $column_key, $hidden ) )
853                                 $style = 'display:none;';
854
855                         $style = ' style="' . $style . '"';
856
857                         if ( 'cb' == $column_key )
858                                 $class[] = 'check-column';
859                         elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
860                                 $class[] = 'num';
861
862                         if ( isset( $sortable[$column_key] ) ) {
863                                 list( $orderby, $desc_first ) = $sortable[$column_key];
864
865                                 if ( $current_orderby == $orderby ) {
866                                         $order = 'asc' == $current_order ? 'desc' : 'asc';
867                                         $class[] = 'sorted';
868                                         $class[] = $current_order;
869                                 } else {
870                                         $order = $desc_first ? 'desc' : 'asc';
871                                         $class[] = 'sortable';
872                                         $class[] = $desc_first ? 'asc' : 'desc';
873                                 }
874
875                                 $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>';
876                         }
877
878                         $id = $with_id ? "id='$column_key'" : '';
879
880                         if ( !empty( $class ) )
881                                 $class = "class='" . join( ' ', $class ) . "'";
882
883                         echo "<th scope='col' $id $class $style>$column_display_name</th>";
884                 }
885         }
886
887         /**
888          * Display the table
889          *
890          * @since 3.1.0
891          * @access public
892          */
893         public function display() {
894                 $singular = $this->_args['singular'];
895
896                 $this->display_tablenav( 'top' );
897
898 ?>
899 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
900         <thead>
901         <tr>
902                 <?php $this->print_column_headers(); ?>
903         </tr>
904         </thead>
905
906         <tfoot>
907         <tr>
908                 <?php $this->print_column_headers( false ); ?>
909         </tr>
910         </tfoot>
911
912         <tbody id="the-list"<?php
913                 if ( $singular ) {
914                         echo " data-wp-lists='list:$singular'";
915                 } ?>>
916                 <?php $this->display_rows_or_placeholder(); ?>
917         </tbody>
918 </table>
919 <?php
920                 $this->display_tablenav( 'bottom' );
921         }
922
923         /**
924          * Get a list of CSS classes for the list table table tag.
925          *
926          * @since 3.1.0
927          * @access protected
928          *
929          * @return array List of CSS classes for the table tag.
930          */
931         protected function get_table_classes() {
932                 return array( 'widefat', 'fixed', $this->_args['plural'] );
933         }
934
935         /**
936          * Generate the table navigation above or below the table
937          *
938          * @since 3.1.0
939          * @access protected
940          * @param string $which
941          */
942         protected function display_tablenav( $which ) {
943                 if ( 'top' == $which )
944                         wp_nonce_field( 'bulk-' . $this->_args['plural'] );
945 ?>
946         <div class="tablenav <?php echo esc_attr( $which ); ?>">
947
948                 <div class="alignleft actions bulkactions">
949                         <?php $this->bulk_actions( $which ); ?>
950                 </div>
951 <?php
952                 $this->extra_tablenav( $which );
953                 $this->pagination( $which );
954 ?>
955
956                 <br class="clear" />
957         </div>
958 <?php
959         }
960
961         /**
962          * Extra controls to be displayed between bulk actions and pagination
963          *
964          * @since 3.1.0
965          * @access protected
966          *
967          * @param string $which
968          */
969         protected function extra_tablenav( $which ) {}
970
971         /**
972          * Generate the tbody element for the list table.
973          *
974          * @since 3.1.0
975          * @access public
976          */
977         public function display_rows_or_placeholder() {
978                 if ( $this->has_items() ) {
979                         $this->display_rows();
980                 } else {
981                         echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
982                         $this->no_items();
983                         echo '</td></tr>';
984                 }
985         }
986
987         /**
988          * Generate the table rows
989          *
990          * @since 3.1.0
991          * @access public
992          */
993         public function display_rows() {
994                 foreach ( $this->items as $item )
995                         $this->single_row( $item );
996         }
997
998         /**
999          * Generates content for a single row of the table
1000          *
1001          * @since 3.1.0
1002          * @access public
1003          *
1004          * @param object $item The current item
1005          */
1006         public function single_row( $item ) {
1007                 static $row_class = '';
1008                 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
1009
1010                 echo '<tr' . $row_class . '>';
1011                 $this->single_row_columns( $item );
1012                 echo '</tr>';
1013         }
1014
1015         /**
1016          * Generates the columns for a single row of the table
1017          *
1018          * @since 3.1.0
1019          * @access protected
1020          *
1021          * @param object $item The current item
1022          */
1023         protected function single_row_columns( $item ) {
1024                 list( $columns, $hidden ) = $this->get_column_info();
1025
1026                 foreach ( $columns as $column_name => $column_display_name ) {
1027                         $class = "class='$column_name column-$column_name'";
1028
1029                         $style = '';
1030                         if ( in_array( $column_name, $hidden ) )
1031                                 $style = ' style="display:none;"';
1032
1033                         $attributes = "$class$style";
1034
1035                         if ( 'cb' == $column_name ) {
1036                                 echo '<th scope="row" class="check-column">';
1037                                 echo $this->column_cb( $item );
1038                                 echo '</th>';
1039                         }
1040                         elseif ( method_exists( $this, 'column_' . $column_name ) ) {
1041                                 echo "<td $attributes>";
1042                                 echo call_user_func( array( $this, 'column_' . $column_name ), $item );
1043                                 echo "</td>";
1044                         }
1045                         else {
1046                                 echo "<td $attributes>";
1047                                 echo $this->column_default( $item, $column_name );
1048                                 echo "</td>";
1049                         }
1050                 }
1051         }
1052
1053         /**
1054          * Handle an incoming ajax request (called from admin-ajax.php)
1055          *
1056          * @since 3.1.0
1057          * @access public
1058          */
1059         public function ajax_response() {
1060                 $this->prepare_items();
1061
1062                 ob_start();
1063                 if ( ! empty( $_REQUEST['no_placeholder'] ) ) {
1064                         $this->display_rows();
1065                 } else {
1066                         $this->display_rows_or_placeholder();
1067                 }
1068
1069                 $rows = ob_get_clean();
1070
1071                 $response = array( 'rows' => $rows );
1072
1073                 if ( isset( $this->_pagination_args['total_items'] ) ) {
1074                         $response['total_items_i18n'] = sprintf(
1075                                 _n( '1 item', '%s items', $this->_pagination_args['total_items'] ),
1076                                 number_format_i18n( $this->_pagination_args['total_items'] )
1077                         );
1078                 }
1079                 if ( isset( $this->_pagination_args['total_pages'] ) ) {
1080                         $response['total_pages'] = $this->_pagination_args['total_pages'];
1081                         $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] );
1082                 }
1083
1084                 die( wp_json_encode( $response ) );
1085         }
1086
1087         /**
1088          * Send required variables to JavaScript land
1089          *
1090          * @access public
1091          */
1092         public function _js_vars() {
1093                 $args = array(
1094                         'class'  => get_class( $this ),
1095                         'screen' => array(
1096                                 'id'   => $this->screen->id,
1097                                 'base' => $this->screen->base,
1098                         )
1099                 );
1100
1101                 printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );
1102         }
1103 }