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