[ Index ]

PHP Cross Reference of WordPress

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 $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 overridden in a subclass.' );
 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 overridden in a subclass.' );
 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 );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 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       * Generates the required HTML for a list of row action links.
 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 The HTML for the row actions.
 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       * Displays a dropdown for filtering items in the list table by month.
 535       *
 536       * @since 3.1.0
 537       *
 538       * @global wpdb      $wpdb      WordPress database abstraction object.
 539       * @global WP_Locale $wp_locale WordPress date and time locale object.
 540       *
 541       * @param string $post_type The 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              $aria_current = '';
 636  
 637              if ( $current_mode === $mode ) {
 638                  $classes[]    = 'current';
 639                  $aria_current = ' aria-current="page"';
 640              }
 641              printf(
 642                  "<a href='%s' class='%s' id='view-switch-$mode'$aria_current><span class='screen-reader-text'>%s</span></a>\n",
 643                  esc_url( add_query_arg( 'mode', $mode ) ),
 644                  implode( ' ', $classes ),
 645                  $title
 646              );
 647          }
 648          ?>
 649          </div>
 650          <?php
 651      }
 652  
 653      /**
 654       * Display a comment count bubble
 655       *
 656       * @since 3.1.0
 657       *
 658       * @param int $post_id          The post ID.
 659       * @param int $pending_comments Number of pending comments.
 660       */
 661  	protected function comments_bubble( $post_id, $pending_comments ) {
 662          $approved_comments = get_comments_number();
 663  
 664          $approved_comments_number = number_format_i18n( $approved_comments );
 665          $pending_comments_number  = number_format_i18n( $pending_comments );
 666  
 667          $approved_only_phrase = sprintf(
 668              /* translators: %s: Number of comments. */
 669              _n( '%s comment', '%s comments', $approved_comments ),
 670              $approved_comments_number
 671          );
 672  
 673          $approved_phrase = sprintf(
 674              /* translators: %s: Number of comments. */
 675              _n( '%s approved comment', '%s approved comments', $approved_comments ),
 676              $approved_comments_number
 677          );
 678  
 679          $pending_phrase = sprintf(
 680              /* translators: %s: Number of comments. */
 681              _n( '%s pending comment', '%s pending comments', $pending_comments ),
 682              $pending_comments_number
 683          );
 684  
 685          // No comments at all.
 686          if ( ! $approved_comments && ! $pending_comments ) {
 687              printf(
 688                  '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">%s</span>',
 689                  __( 'No comments' )
 690              );
 691              // Approved comments have different display depending on some conditions.
 692          } elseif ( $approved_comments ) {
 693              printf(
 694                  '<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>',
 695                  esc_url(
 696                      add_query_arg(
 697                          array(
 698                              'p'              => $post_id,
 699                              'comment_status' => 'approved',
 700                          ),
 701                          admin_url( 'edit-comments.php' )
 702                      )
 703                  ),
 704                  $approved_comments_number,
 705                  $pending_comments ? $approved_phrase : $approved_only_phrase
 706              );
 707          } else {
 708              printf(
 709                  '<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>',
 710                  $approved_comments_number,
 711                  $pending_comments ? __( 'No approved comments' ) : __( 'No comments' )
 712              );
 713          }
 714  
 715          if ( $pending_comments ) {
 716              printf(
 717                  '<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>',
 718                  esc_url(
 719                      add_query_arg(
 720                          array(
 721                              'p'              => $post_id,
 722                              'comment_status' => 'moderated',
 723                          ),
 724                          admin_url( 'edit-comments.php' )
 725                      )
 726                  ),
 727                  $pending_comments_number,
 728                  $pending_phrase
 729              );
 730          } else {
 731              printf(
 732                  '<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>',
 733                  $pending_comments_number,
 734                  $approved_comments ? __( 'No pending comments' ) : __( 'No comments' )
 735              );
 736          }
 737      }
 738  
 739      /**
 740       * Get the current page number
 741       *
 742       * @since 3.1.0
 743       *
 744       * @return int
 745       */
 746  	public function get_pagenum() {
 747          $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
 748  
 749          if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) {
 750              $pagenum = $this->_pagination_args['total_pages'];
 751          }
 752  
 753          return max( 1, $pagenum );
 754      }
 755  
 756      /**
 757       * Get number of items to display on a single page
 758       *
 759       * @since 3.1.0
 760       *
 761       * @param string $option
 762       * @param int    $default
 763       * @return int
 764       */
 765  	protected function get_items_per_page( $option, $default = 20 ) {
 766          $per_page = (int) get_user_option( $option );
 767          if ( empty( $per_page ) || $per_page < 1 ) {
 768              $per_page = $default;
 769          }
 770  
 771          /**
 772           * Filters the number of items to be displayed on each page of the list table.
 773           *
 774           * The dynamic hook name, $option, refers to the `per_page` option depending
 775           * on the type of list table in use. Possible values include: 'edit_comments_per_page',
 776           * 'sites_network_per_page', 'site_themes_network_per_page', 'themes_network_per_page',
 777           * 'users_network_per_page', 'edit_post_per_page', 'edit_page_per_page',
 778           * 'edit_{$post_type}_per_page', etc.
 779           *
 780           * @since 2.9.0
 781           *
 782           * @param int $per_page Number of items to be displayed. Default 20.
 783           */
 784          return (int) apply_filters( "{$option}", $per_page );
 785      }
 786  
 787      /**
 788       * Display the pagination.
 789       *
 790       * @since 3.1.0
 791       *
 792       * @param string $which
 793       */
 794  	protected function pagination( $which ) {
 795          if ( empty( $this->_pagination_args ) ) {
 796              return;
 797          }
 798  
 799          $total_items     = $this->_pagination_args['total_items'];
 800          $total_pages     = $this->_pagination_args['total_pages'];
 801          $infinite_scroll = false;
 802          if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
 803              $infinite_scroll = $this->_pagination_args['infinite_scroll'];
 804          }
 805  
 806          if ( 'top' === $which && $total_pages > 1 ) {
 807              $this->screen->render_screen_reader_content( 'heading_pagination' );
 808          }
 809  
 810          $output = '<span class="displaying-num">' . sprintf(
 811              /* translators: %s: Number of items. */
 812              _n( '%s item', '%s items', $total_items ),
 813              number_format_i18n( $total_items )
 814          ) . '</span>';
 815  
 816          $current              = $this->get_pagenum();
 817          $removable_query_args = wp_removable_query_args();
 818  
 819          $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
 820  
 821          $current_url = remove_query_arg( $removable_query_args, $current_url );
 822  
 823          $page_links = array();
 824  
 825          $total_pages_before = '<span class="paging-input">';
 826          $total_pages_after  = '</span></span>';
 827  
 828          $disable_first = false;
 829          $disable_last  = false;
 830          $disable_prev  = false;
 831          $disable_next  = false;
 832  
 833          if ( $current == 1 ) {
 834              $disable_first = true;
 835              $disable_prev  = true;
 836          }
 837          if ( $current == 2 ) {
 838              $disable_first = true;
 839          }
 840          if ( $current == $total_pages ) {
 841              $disable_last = true;
 842              $disable_next = true;
 843          }
 844          if ( $current == $total_pages - 1 ) {
 845              $disable_last = true;
 846          }
 847  
 848          if ( $disable_first ) {
 849              $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
 850          } else {
 851              $page_links[] = sprintf(
 852                  "<a class='first-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 853                  esc_url( remove_query_arg( 'paged', $current_url ) ),
 854                  __( 'First page' ),
 855                  '&laquo;'
 856              );
 857          }
 858  
 859          if ( $disable_prev ) {
 860              $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
 861          } else {
 862              $page_links[] = sprintf(
 863                  "<a class='prev-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 864                  esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ) ),
 865                  __( 'Previous page' ),
 866                  '&lsaquo;'
 867              );
 868          }
 869  
 870          if ( 'bottom' === $which ) {
 871              $html_current_page  = $current;
 872              $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">';
 873          } else {
 874              $html_current_page = sprintf(
 875                  "%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'>",
 876                  '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
 877                  $current,
 878                  strlen( $total_pages )
 879              );
 880          }
 881          $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
 882          $page_links[]     = $total_pages_before . sprintf(
 883              /* translators: 1: Current page, 2: Total pages. */
 884              _x( '%1$s of %2$s', 'paging' ),
 885              $html_current_page,
 886              $html_total_pages
 887          ) . $total_pages_after;
 888  
 889          if ( $disable_next ) {
 890              $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
 891          } else {
 892              $page_links[] = sprintf(
 893                  "<a class='next-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 894                  esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ) ),
 895                  __( 'Next page' ),
 896                  '&rsaquo;'
 897              );
 898          }
 899  
 900          if ( $disable_last ) {
 901              $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
 902          } else {
 903              $page_links[] = sprintf(
 904                  "<a class='last-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 905                  esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
 906                  __( 'Last page' ),
 907                  '&raquo;'
 908              );
 909          }
 910  
 911          $pagination_links_class = 'pagination-links';
 912          if ( ! empty( $infinite_scroll ) ) {
 913              $pagination_links_class .= ' hide-if-js';
 914          }
 915          $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
 916  
 917          if ( $total_pages ) {
 918              $page_class = $total_pages < 2 ? ' one-page' : '';
 919          } else {
 920              $page_class = ' no-pages';
 921          }
 922          $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
 923  
 924          echo $this->_pagination;
 925      }
 926  
 927      /**
 928       * Get a list of columns. The format is:
 929       * 'internal-name' => 'Title'
 930       *
 931       * @since 3.1.0
 932       * @abstract
 933       *
 934       * @return array
 935       */
 936  	public function get_columns() {
 937          die( 'function WP_List_Table::get_columns() must be overridden in a subclass.' );
 938      }
 939  
 940      /**
 941       * Get a list of sortable columns. The format is:
 942       * 'internal-name' => 'orderby'
 943       * or
 944       * 'internal-name' => array( 'orderby', true )
 945       *
 946       * The second format will make the initial sorting order be descending
 947       *
 948       * @since 3.1.0
 949       *
 950       * @return array
 951       */
 952  	protected function get_sortable_columns() {
 953          return array();
 954      }
 955  
 956      /**
 957       * Gets the name of the default primary column.
 958       *
 959       * @since 4.3.0
 960       *
 961       * @return string Name of the default primary column, in this case, an empty string.
 962       */
 963  	protected function get_default_primary_column_name() {
 964          $columns = $this->get_columns();
 965          $column  = '';
 966  
 967          if ( empty( $columns ) ) {
 968              return $column;
 969          }
 970  
 971          // We need a primary defined so responsive views show something,
 972          // so let's fall back to the first non-checkbox column.
 973          foreach ( $columns as $col => $column_name ) {
 974              if ( 'cb' === $col ) {
 975                  continue;
 976              }
 977  
 978              $column = $col;
 979              break;
 980          }
 981  
 982          return $column;
 983      }
 984  
 985      /**
 986       * Public wrapper for WP_List_Table::get_default_primary_column_name().
 987       *
 988       * @since 4.4.0
 989       *
 990       * @return string Name of the default primary column.
 991       */
 992  	public function get_primary_column() {
 993          return $this->get_primary_column_name();
 994      }
 995  
 996      /**
 997       * Gets the name of the primary column.
 998       *
 999       * @since 4.3.0
1000       *
1001       * @return string The name of the primary column.
1002       */
1003  	protected function get_primary_column_name() {
1004          $columns = get_column_headers( $this->screen );
1005          $default = $this->get_default_primary_column_name();
1006  
1007          // If the primary column doesn't exist fall back to the
1008          // first non-checkbox column.
1009          if ( ! isset( $columns[ $default ] ) ) {
1010              $default = WP_List_Table::get_default_primary_column_name();
1011          }
1012  
1013          /**
1014           * Filters the name of the primary column for the current list table.
1015           *
1016           * @since 4.3.0
1017           *
1018           * @param string $default Column name default for the specific list table, e.g. 'name'.
1019           * @param string $context Screen ID for specific list table, e.g. 'plugins'.
1020           */
1021          $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
1022  
1023          if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
1024              $column = $default;
1025          }
1026  
1027          return $column;
1028      }
1029  
1030      /**
1031       * Get a list of all, hidden and sortable columns, with filter applied
1032       *
1033       * @since 3.1.0
1034       *
1035       * @return array
1036       */
1037  	protected function get_column_info() {
1038          // $_column_headers is already set / cached
1039          if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) {
1040              // Back-compat for list tables that have been manually setting $_column_headers for horse reasons.
1041              // In 4.3, we added a fourth argument for primary column.
1042              $column_headers = array( array(), array(), array(), $this->get_primary_column_name() );
1043              foreach ( $this->_column_headers as $key => $value ) {
1044                  $column_headers[ $key ] = $value;
1045              }
1046  
1047              return $column_headers;
1048          }
1049  
1050          $columns = get_column_headers( $this->screen );
1051          $hidden  = get_hidden_columns( $this->screen );
1052  
1053          $sortable_columns = $this->get_sortable_columns();
1054          /**
1055           * Filters the list table sortable columns for a specific screen.
1056           *
1057           * The dynamic portion of the hook name, `$this->screen->id`, refers
1058           * to the ID of the current screen, usually a string.
1059           *
1060           * @since 3.5.0
1061           *
1062           * @param array $sortable_columns An array of sortable columns.
1063           */
1064          $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
1065  
1066          $sortable = array();
1067          foreach ( $_sortable as $id => $data ) {
1068              if ( empty( $data ) ) {
1069                  continue;
1070              }
1071  
1072              $data = (array) $data;
1073              if ( ! isset( $data[1] ) ) {
1074                  $data[1] = false;
1075              }
1076  
1077              $sortable[ $id ] = $data;
1078          }
1079  
1080          $primary               = $this->get_primary_column_name();
1081          $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
1082  
1083          return $this->_column_headers;
1084      }
1085  
1086      /**
1087       * Return number of visible columns
1088       *
1089       * @since 3.1.0
1090       *
1091       * @return int
1092       */
1093  	public function get_column_count() {
1094          list ( $columns, $hidden ) = $this->get_column_info();
1095          $hidden                    = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
1096          return count( $columns ) - count( $hidden );
1097      }
1098  
1099      /**
1100       * Print column headers, accounting for hidden and sortable columns.
1101       *
1102       * @since 3.1.0
1103       *
1104       * @staticvar int $cb_counter
1105       *
1106       * @param bool $with_id Whether to set the id attribute or not
1107       */
1108  	public function print_column_headers( $with_id = true ) {
1109          list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
1110  
1111          $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
1112          $current_url = remove_query_arg( 'paged', $current_url );
1113  
1114          if ( isset( $_GET['orderby'] ) ) {
1115              $current_orderby = $_GET['orderby'];
1116          } else {
1117              $current_orderby = '';
1118          }
1119  
1120          if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) {
1121              $current_order = 'desc';
1122          } else {
1123              $current_order = 'asc';
1124          }
1125  
1126          if ( ! empty( $columns['cb'] ) ) {
1127              static $cb_counter = 1;
1128              $columns['cb']     = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
1129                  . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
1130              $cb_counter++;
1131          }
1132  
1133          foreach ( $columns as $column_key => $column_display_name ) {
1134              $class = array( 'manage-column', "column-$column_key" );
1135  
1136              if ( in_array( $column_key, $hidden ) ) {
1137                  $class[] = 'hidden';
1138              }
1139  
1140              if ( 'cb' === $column_key ) {
1141                  $class[] = 'check-column';
1142              } elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) ) {
1143                  $class[] = 'num';
1144              }
1145  
1146              if ( $column_key === $primary ) {
1147                  $class[] = 'column-primary';
1148              }
1149  
1150              if ( isset( $sortable[ $column_key ] ) ) {
1151                  list( $orderby, $desc_first ) = $sortable[ $column_key ];
1152  
1153                  if ( $current_orderby === $orderby ) {
1154                      $order   = 'asc' === $current_order ? 'desc' : 'asc';
1155                      $class[] = 'sorted';
1156                      $class[] = $current_order;
1157                  } else {
1158                      $order   = $desc_first ? 'desc' : 'asc';
1159                      $class[] = 'sortable';
1160                      $class[] = $desc_first ? 'asc' : 'desc';
1161                  }
1162  
1163                  $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>';
1164              }
1165  
1166              $tag   = ( 'cb' === $column_key ) ? 'td' : 'th';
1167              $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
1168              $id    = $with_id ? "id='$column_key'" : '';
1169  
1170              if ( ! empty( $class ) ) {
1171                  $class = "class='" . join( ' ', $class ) . "'";
1172              }
1173  
1174              echo "<$tag $scope $id $class>$column_display_name</$tag>";
1175          }
1176      }
1177  
1178      /**
1179       * Displays the table.
1180       *
1181       * @since 3.1.0
1182       */
1183  	public function display() {
1184          $singular = $this->_args['singular'];
1185  
1186          $this->display_tablenav( 'top' );
1187  
1188          $this->screen->render_screen_reader_content( 'heading_list' );
1189          ?>
1190  <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
1191      <thead>
1192      <tr>
1193          <?php $this->print_column_headers(); ?>
1194      </tr>
1195      </thead>
1196  
1197      <tbody id="the-list"
1198          <?php
1199          if ( $singular ) {
1200              echo " data-wp-lists='list:$singular'";
1201          }
1202          ?>
1203          >
1204          <?php $this->display_rows_or_placeholder(); ?>
1205      </tbody>
1206  
1207      <tfoot>
1208      <tr>
1209          <?php $this->print_column_headers( false ); ?>
1210      </tr>
1211      </tfoot>
1212  
1213  </table>
1214          <?php
1215          $this->display_tablenav( 'bottom' );
1216      }
1217  
1218      /**
1219       * Get a list of CSS classes for the WP_List_Table table tag.
1220       *
1221       * @since 3.1.0
1222       *
1223       * @return string[] Array of CSS classes for the table tag.
1224       */
1225  	protected function get_table_classes() {
1226          return array( 'widefat', 'fixed', 'striped', $this->_args['plural'] );
1227      }
1228  
1229      /**
1230       * Generate the table navigation above or below the table
1231       *
1232       * @since 3.1.0
1233       * @param string $which
1234       */
1235  	protected function display_tablenav( $which ) {
1236          if ( 'top' === $which ) {
1237              wp_nonce_field( 'bulk-' . $this->_args['plural'] );
1238          }
1239          ?>
1240      <div class="tablenav <?php echo esc_attr( $which ); ?>">
1241  
1242          <?php if ( $this->has_items() ) : ?>
1243          <div class="alignleft actions bulkactions">
1244              <?php $this->bulk_actions( $which ); ?>
1245          </div>
1246              <?php
1247          endif;
1248          $this->extra_tablenav( $which );
1249          $this->pagination( $which );
1250          ?>
1251  
1252          <br class="clear" />
1253      </div>
1254          <?php
1255      }
1256  
1257      /**
1258       * Extra controls to be displayed between bulk actions and pagination
1259       *
1260       * @since 3.1.0
1261       *
1262       * @param string $which
1263       */
1264  	protected function extra_tablenav( $which ) {}
1265  
1266      /**
1267       * Generate the tbody element for the list table.
1268       *
1269       * @since 3.1.0
1270       */
1271  	public function display_rows_or_placeholder() {
1272          if ( $this->has_items() ) {
1273              $this->display_rows();
1274          } else {
1275              echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
1276              $this->no_items();
1277              echo '</td></tr>';
1278          }
1279      }
1280  
1281      /**
1282       * Generate the table rows
1283       *
1284       * @since 3.1.0
1285       */
1286  	public function display_rows() {
1287          foreach ( $this->items as $item ) {
1288              $this->single_row( $item );
1289          }
1290      }
1291  
1292      /**
1293       * Generates content for a single row of the table
1294       *
1295       * @since 3.1.0
1296       *
1297       * @param object $item The current item
1298       */
1299  	public function single_row( $item ) {
1300          echo '<tr>';
1301          $this->single_row_columns( $item );
1302          echo '</tr>';
1303      }
1304  
1305      /**
1306       * @param object $item
1307       * @param string $column_name
1308       */
1309  	protected function column_default( $item, $column_name ) {}
1310  
1311      /**
1312       * @param object $item
1313       */
1314  	protected function column_cb( $item ) {}
1315  
1316      /**
1317       * Generates the columns for a single row of the table
1318       *
1319       * @since 3.1.0
1320       *
1321       * @param object $item The current item
1322       */
1323  	protected function single_row_columns( $item ) {
1324          list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
1325  
1326          foreach ( $columns as $column_name => $column_display_name ) {
1327              $classes = "$column_name column-$column_name";
1328              if ( $primary === $column_name ) {
1329                  $classes .= ' has-row-actions column-primary';
1330              }
1331  
1332              if ( in_array( $column_name, $hidden ) ) {
1333                  $classes .= ' hidden';
1334              }
1335  
1336              // Comments column uses HTML in the display name with screen reader text.
1337              // Instead of using esc_attr(), we strip tags to get closer to a user-friendly string.
1338              $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
1339  
1340              $attributes = "class='$classes' $data";
1341  
1342              if ( 'cb' === $column_name ) {
1343                  echo '<th scope="row" class="check-column">';
1344                  echo $this->column_cb( $item );
1345                  echo '</th>';
1346              } elseif ( method_exists( $this, '_column_' . $column_name ) ) {
1347                  echo call_user_func(
1348                      array( $this, '_column_' . $column_name ),
1349                      $item,
1350                      $classes,
1351                      $data,
1352                      $primary
1353                  );
1354              } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
1355                  echo "<td $attributes>";
1356                  echo call_user_func( array( $this, 'column_' . $column_name ), $item );
1357                  echo $this->handle_row_actions( $item, $column_name, $primary );
1358                  echo '</td>';
1359              } else {
1360                  echo "<td $attributes>";
1361                  echo $this->column_default( $item, $column_name );
1362                  echo $this->handle_row_actions( $item, $column_name, $primary );
1363                  echo '</td>';
1364              }
1365          }
1366      }
1367  
1368      /**
1369       * Generates and display row actions links for the list table.
1370       *
1371       * @since 4.3.0
1372       *
1373       * @param object $item        The item being acted upon.
1374       * @param string $column_name Current column name.
1375       * @param string $primary     Primary column name.
1376       * @return string The row actions HTML, or an empty string if the current column is the primary column.
1377       */
1378  	protected function handle_row_actions( $item, $column_name, $primary ) {
1379          return $column_name === $primary ? '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>' : '';
1380      }
1381  
1382      /**
1383       * Handle an incoming ajax request (called from admin-ajax.php)
1384       *
1385       * @since 3.1.0
1386       */
1387  	public function ajax_response() {
1388          $this->prepare_items();
1389  
1390          ob_start();
1391          if ( ! empty( $_REQUEST['no_placeholder'] ) ) {
1392              $this->display_rows();
1393          } else {
1394              $this->display_rows_or_placeholder();
1395          }
1396  
1397          $rows = ob_get_clean();
1398  
1399          $response = array( 'rows' => $rows );
1400  
1401          if ( isset( $this->_pagination_args['total_items'] ) ) {
1402              $response['total_items_i18n'] = sprintf(
1403                  /* translators: Number of items. */
1404                  _n( '%s item', '%s items', $this->_pagination_args['total_items'] ),
1405                  number_format_i18n( $this->_pagination_args['total_items'] )
1406              );
1407          }
1408          if ( isset( $this->_pagination_args['total_pages'] ) ) {
1409              $response['total_pages']      = $this->_pagination_args['total_pages'];
1410              $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] );
1411          }
1412  
1413          die( wp_json_encode( $response ) );
1414      }
1415  
1416      /**
1417       * Send required variables to JavaScript land
1418       */
1419  	public function _js_vars() {
1420          $args = array(
1421              'class'  => get_class( $this ),
1422              'screen' => array(
1423                  'id'   => $this->screen->id,
1424                  'base' => $this->screen->base,
1425              ),
1426          );
1427  
1428          printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );
1429      }
1430  }


Generated: Wed Nov 20 01:00:03 2019 Cross-referenced by PHPXref 0.7.1