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