[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/includes/ -> class-wp-list-table.php (source)

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


Generated: Mon Jun 24 08:20:01 2019 Cross-referenced by PHPXref 0.7