PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-list-table.php

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 920 lines | 459 code | 137 blank | 324 comment | 84 complexity | 61406670e11a640a514dc2e5f2d1dbff MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Base class for displaying a list of items in an ajaxified HTML table.
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Base class for displaying a list of items in an ajaxified HTML table.
  11. *
  12. * @package WordPress
  13. * @subpackage List_Table
  14. * @since 3.1.0
  15. * @access private
  16. */
  17. class WP_List_Table {
  18. /**
  19. * The current list of items
  20. *
  21. * @since 3.1.0
  22. * @var array
  23. * @access protected
  24. */
  25. var $items;
  26. /**
  27. * Various information about the current table
  28. *
  29. * @since 3.1.0
  30. * @var array
  31. * @access private
  32. */
  33. var $_args;
  34. /**
  35. * Various information needed for displaying the pagination
  36. *
  37. * @since 3.1.0
  38. * @var array
  39. * @access private
  40. */
  41. var $_pagination_args = array();
  42. /**
  43. * The current screen
  44. *
  45. * @since 3.1.0
  46. * @var object
  47. * @access protected
  48. */
  49. var $screen;
  50. /**
  51. * Cached bulk actions
  52. *
  53. * @since 3.1.0
  54. * @var array
  55. * @access private
  56. */
  57. var $_actions;
  58. /**
  59. * Cached pagination output
  60. *
  61. * @since 3.1.0
  62. * @var string
  63. * @access private
  64. */
  65. var $_pagination;
  66. /**
  67. * Constructor. The child class should call this constructor from it's own constructor
  68. *
  69. * @param array $args An associative array with information about the current table
  70. * @access protected
  71. */
  72. function __construct( $args = array() ) {
  73. $args = wp_parse_args( $args, array(
  74. 'plural' => '',
  75. 'singular' => '',
  76. 'ajax' => false,
  77. 'screen' => null,
  78. ) );
  79. $this->screen = convert_to_screen( $args['screen'] );
  80. add_filter( "manage_{$this->screen->id}_columns", array( &$this, 'get_columns' ), 0 );
  81. if ( !$args['plural'] )
  82. $args['plural'] = $this->screen->base;
  83. $args['plural'] = sanitize_key( $args['plural'] );
  84. $args['singular'] = sanitize_key( $args['singular'] );
  85. $this->_args = $args;
  86. if ( $args['ajax'] ) {
  87. // wp_enqueue_script( 'list-table' );
  88. add_action( 'admin_footer', array( &$this, '_js_vars' ) );
  89. }
  90. }
  91. /**
  92. * Checks the current user's permissions
  93. * @uses wp_die()
  94. *
  95. * @since 3.1.0
  96. * @access public
  97. * @abstract
  98. */
  99. function ajax_user_can() {
  100. die( 'function WP_List_Table::ajax_user_can() must be over-ridden in a sub-class.' );
  101. }
  102. /**
  103. * Prepares the list of items for displaying.
  104. * @uses WP_List_Table::set_pagination_args()
  105. *
  106. * @since 3.1.0
  107. * @access public
  108. * @abstract
  109. */
  110. function prepare_items() {
  111. die( 'function WP_List_Table::prepare_items() must be over-ridden in a sub-class.' );
  112. }
  113. /**
  114. * An internal method that sets all the necessary pagination arguments
  115. *
  116. * @param array $args An associative array with information about the pagination
  117. * @access protected
  118. */
  119. function set_pagination_args( $args ) {
  120. $args = wp_parse_args( $args, array(
  121. 'total_items' => 0,
  122. 'total_pages' => 0,
  123. 'per_page' => 0,
  124. ) );
  125. if ( !$args['total_pages'] && $args['per_page'] > 0 )
  126. $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
  127. // redirect if page number is invalid and headers are not already sent
  128. if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
  129. wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
  130. exit;
  131. }
  132. $this->_pagination_args = $args;
  133. }
  134. /**
  135. * Access the pagination args
  136. *
  137. * @since 3.1.0
  138. * @access public
  139. *
  140. * @param string $key
  141. * @return array
  142. */
  143. function get_pagination_arg( $key ) {
  144. if ( 'page' == $key )
  145. return $this->get_pagenum();
  146. if ( isset( $this->_pagination_args[$key] ) )
  147. return $this->_pagination_args[$key];
  148. }
  149. /**
  150. * Whether the table has items to display or not
  151. *
  152. * @since 3.1.0
  153. * @access public
  154. *
  155. * @return bool
  156. */
  157. function has_items() {
  158. return !empty( $this->items );
  159. }
  160. /**
  161. * Message to be displayed when there are no items
  162. *
  163. * @since 3.1.0
  164. * @access public
  165. */
  166. function no_items() {
  167. _e( 'No items found.' );
  168. }
  169. /**
  170. * Display the search box.
  171. *
  172. * @since 3.1.0
  173. * @access public
  174. *
  175. * @param string $text The search button text
  176. * @param string $input_id The search input id
  177. */
  178. function search_box( $text, $input_id ) {
  179. if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
  180. return;
  181. $input_id = $input_id . '-search-input';
  182. if ( ! empty( $_REQUEST['orderby'] ) )
  183. echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
  184. if ( ! empty( $_REQUEST['order'] ) )
  185. echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
  186. if ( ! empty( $_REQUEST['post_mime_type'] ) )
  187. echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
  188. if ( ! empty( $_REQUEST['detached'] ) )
  189. echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
  190. ?>
  191. <p class="search-box">
  192. <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
  193. <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
  194. <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
  195. </p>
  196. <?php
  197. }
  198. /**
  199. * Get an associative array ( id => link ) with the list
  200. * of views available on this table.
  201. *
  202. * @since 3.1.0
  203. * @access protected
  204. *
  205. * @return array
  206. */
  207. function get_views() {
  208. return array();
  209. }
  210. /**
  211. * Display the list of views available on this table.
  212. *
  213. * @since 3.1.0
  214. * @access public
  215. */
  216. function views() {
  217. $views = $this->get_views();
  218. $views = apply_filters( 'views_' . $this->screen->id, $views );
  219. if ( empty( $views ) )
  220. return;
  221. echo "<ul class='subsubsub'>\n";
  222. foreach ( $views as $class => $view ) {
  223. $views[ $class ] = "\t<li class='$class'>$view";
  224. }
  225. echo implode( " |</li>\n", $views ) . "</li>\n";
  226. echo "</ul>";
  227. }
  228. /**
  229. * Get an associative array ( option_name => option_title ) with the list
  230. * of bulk actions available on this table.
  231. *
  232. * @since 3.1.0
  233. * @access protected
  234. *
  235. * @return array
  236. */
  237. function get_bulk_actions() {
  238. return array();
  239. }
  240. /**
  241. * Display the bulk actions dropdown.
  242. *
  243. * @since 3.1.0
  244. * @access public
  245. */
  246. function bulk_actions() {
  247. if ( is_null( $this->_actions ) ) {
  248. $no_new_actions = $this->_actions = $this->get_bulk_actions();
  249. // This filter can currently only be used to remove actions.
  250. $this->_actions = apply_filters( 'bulk_actions-' . $this->screen->id, $this->_actions );
  251. $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
  252. $two = '';
  253. } else {
  254. $two = '2';
  255. }
  256. if ( empty( $this->_actions ) )
  257. return;
  258. echo "<select name='action$two'>\n";
  259. echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions' ) . "</option>\n";
  260. foreach ( $this->_actions as $name => $title ) {
  261. $class = 'edit' == $name ? ' class="hide-if-no-js"' : '';
  262. echo "\t<option value='$name'$class>$title</option>\n";
  263. }
  264. echo "</select>\n";
  265. submit_button( __( 'Apply' ), 'action', false, false, array( 'id' => "doaction$two" ) );
  266. echo "\n";
  267. }
  268. /**
  269. * Get the current action selected from the bulk actions dropdown.
  270. *
  271. * @since 3.1.0
  272. * @access public
  273. *
  274. * @return string|bool The action name or False if no action was selected
  275. */
  276. function current_action() {
  277. if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
  278. return $_REQUEST['action'];
  279. if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
  280. return $_REQUEST['action2'];
  281. return false;
  282. }
  283. /**
  284. * Generate row actions div
  285. *
  286. * @since 3.1.0
  287. * @access protected
  288. *
  289. * @param array $actions The list of actions
  290. * @param bool $always_visible Whether the actions should be always visible
  291. * @return string
  292. */
  293. function row_actions( $actions, $always_visible = false ) {
  294. $action_count = count( $actions );
  295. $i = 0;
  296. if ( !$action_count )
  297. return '';
  298. $out = '<div class="' . ( $always_visible ? 'row-actions-visible' : 'row-actions' ) . '">';
  299. foreach ( $actions as $action => $link ) {
  300. ++$i;
  301. ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
  302. $out .= "<span class='$action'>$link$sep</span>";
  303. }
  304. $out .= '</div>';
  305. return $out;
  306. }
  307. /**
  308. * Display a monthly dropdown for filtering items
  309. *
  310. * @since 3.1.0
  311. * @access protected
  312. */
  313. function months_dropdown( $post_type ) {
  314. global $wpdb, $wp_locale;
  315. $months = $wpdb->get_results( $wpdb->prepare( "
  316. SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
  317. FROM $wpdb->posts
  318. WHERE post_type = %s
  319. ORDER BY post_date DESC
  320. ", $post_type ) );
  321. $month_count = count( $months );
  322. if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
  323. return;
  324. $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
  325. ?>
  326. <select name='m'>
  327. <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
  328. <?php
  329. foreach ( $months as $arc_row ) {
  330. if ( 0 == $arc_row->year )
  331. continue;
  332. $month = zeroise( $arc_row->month, 2 );
  333. $year = $arc_row->year;
  334. printf( "<option %s value='%s'>%s</option>\n",
  335. selected( $m, $year . $month, false ),
  336. esc_attr( $arc_row->year . $month ),
  337. /* translators: 1: month name, 2: 4-digit year */
  338. sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
  339. );
  340. }
  341. ?>
  342. </select>
  343. <?php
  344. }
  345. /**
  346. * Display a view switcher
  347. *
  348. * @since 3.1.0
  349. * @access protected
  350. */
  351. function view_switcher( $current_mode ) {
  352. $modes = array(
  353. 'list' => __( 'List View' ),
  354. 'excerpt' => __( 'Excerpt View' )
  355. );
  356. ?>
  357. <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
  358. <div class="view-switch">
  359. <?php
  360. foreach ( $modes as $mode => $title ) {
  361. $class = ( $current_mode == $mode ) ? 'class="current"' : '';
  362. echo "<a href='" . esc_url( add_query_arg( 'mode', $mode, $_SERVER['REQUEST_URI'] ) ) . "' $class><img id='view-switch-$mode' src='" . esc_url( includes_url( 'images/blank.gif' ) ) . "' width='20' height='20' title='$title' alt='$title' /></a>\n";
  363. }
  364. ?>
  365. </div>
  366. <?php
  367. }
  368. /**
  369. * Display a comment count bubble
  370. *
  371. * @since 3.1.0
  372. * @access protected
  373. *
  374. * @param int $post_id
  375. * @param int $pending_comments
  376. */
  377. function comments_bubble( $post_id, $pending_comments ) {
  378. $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
  379. if ( $pending_comments )
  380. echo '<strong>';
  381. echo "<a href='" . esc_url( add_query_arg( 'p', $post_id, admin_url( 'edit-comments.php' ) ) ) . "' title='" . esc_attr( $pending_phrase ) . "' class='post-com-count'><span class='comment-count'>" . number_format_i18n( get_comments_number() ) . "</span></a>";
  382. if ( $pending_comments )
  383. echo '</strong>';
  384. }
  385. /**
  386. * Get the current page number
  387. *
  388. * @since 3.1.0
  389. * @access protected
  390. *
  391. * @return int
  392. */
  393. function get_pagenum() {
  394. $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
  395. if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
  396. $pagenum = $this->_pagination_args['total_pages'];
  397. return max( 1, $pagenum );
  398. }
  399. /**
  400. * Get number of items to display on a single page
  401. *
  402. * @since 3.1.0
  403. * @access protected
  404. *
  405. * @return int
  406. */
  407. function get_items_per_page( $option, $default = 20 ) {
  408. $per_page = (int) get_user_option( $option );
  409. if ( empty( $per_page ) || $per_page < 1 )
  410. $per_page = $default;
  411. return (int) apply_filters( $option, $per_page );
  412. }
  413. /**
  414. * Display the pagination.
  415. *
  416. * @since 3.1.0
  417. * @access protected
  418. */
  419. function pagination( $which ) {
  420. if ( empty( $this->_pagination_args ) )
  421. return;
  422. extract( $this->_pagination_args, EXTR_SKIP );
  423. $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
  424. $current = $this->get_pagenum();
  425. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  426. $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
  427. $page_links = array();
  428. $disable_first = $disable_last = '';
  429. if ( $current == 1 )
  430. $disable_first = ' disabled';
  431. if ( $current == $total_pages )
  432. $disable_last = ' disabled';
  433. $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
  434. 'first-page' . $disable_first,
  435. esc_attr__( 'Go to the first page' ),
  436. esc_url( remove_query_arg( 'paged', $current_url ) ),
  437. '&laquo;'
  438. );
  439. $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
  440. 'prev-page' . $disable_first,
  441. esc_attr__( 'Go to the previous page' ),
  442. esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
  443. '&lsaquo;'
  444. );
  445. if ( 'bottom' == $which )
  446. $html_current_page = $current;
  447. else
  448. $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
  449. esc_attr__( 'Current page' ),
  450. $current,
  451. strlen( $total_pages )
  452. );
  453. $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
  454. $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
  455. $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
  456. 'next-page' . $disable_last,
  457. esc_attr__( 'Go to the next page' ),
  458. esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
  459. '&rsaquo;'
  460. );
  461. $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
  462. 'last-page' . $disable_last,
  463. esc_attr__( 'Go to the last page' ),
  464. esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
  465. '&raquo;'
  466. );
  467. $pagination_links_class = 'pagination-links';
  468. if ( ! empty( $infinite_scroll ) )
  469. $pagination_links_class = ' hide-if-js';
  470. $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
  471. if ( $total_pages )
  472. $page_class = $total_pages < 2 ? ' one-page' : '';
  473. else
  474. $page_class = ' no-pages';
  475. $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
  476. echo $this->_pagination;
  477. }
  478. /**
  479. * Get a list of columns. The format is:
  480. * 'internal-name' => 'Title'
  481. *
  482. * @since 3.1.0
  483. * @access protected
  484. * @abstract
  485. *
  486. * @return array
  487. */
  488. function get_columns() {
  489. die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
  490. }
  491. /**
  492. * Get a list of sortable columns. The format is:
  493. * 'internal-name' => 'orderby'
  494. * or
  495. * 'internal-name' => array( 'orderby', true )
  496. *
  497. * The second format will make the initial sorting order be descending
  498. *
  499. * @since 3.1.0
  500. * @access protected
  501. *
  502. * @return array
  503. */
  504. function get_sortable_columns() {
  505. return array();
  506. }
  507. /**
  508. * Get a list of all, hidden and sortable columns, with filter applied
  509. *
  510. * @since 3.1.0
  511. * @access protected
  512. *
  513. * @return array
  514. */
  515. function get_column_info() {
  516. if ( isset( $this->_column_headers ) )
  517. return $this->_column_headers;
  518. $columns = get_column_headers( $this->screen );
  519. $hidden = get_hidden_columns( $this->screen );
  520. $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $this->get_sortable_columns() );
  521. $sortable = array();
  522. foreach ( $_sortable as $id => $data ) {
  523. if ( empty( $data ) )
  524. continue;
  525. $data = (array) $data;
  526. if ( !isset( $data[1] ) )
  527. $data[1] = false;
  528. $sortable[$id] = $data;
  529. }
  530. $this->_column_headers = array( $columns, $hidden, $sortable );
  531. return $this->_column_headers;
  532. }
  533. /**
  534. * Return number of visible columns
  535. *
  536. * @since 3.1.0
  537. * @access public
  538. *
  539. * @return int
  540. */
  541. function get_column_count() {
  542. list ( $columns, $hidden ) = $this->get_column_info();
  543. $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
  544. return count( $columns ) - count( $hidden );
  545. }
  546. /**
  547. * Print column headers, accounting for hidden and sortable columns.
  548. *
  549. * @since 3.1.0
  550. * @access protected
  551. *
  552. * @param bool $with_id Whether to set the id attribute or not
  553. */
  554. function print_column_headers( $with_id = true ) {
  555. list( $columns, $hidden, $sortable ) = $this->get_column_info();
  556. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  557. $current_url = remove_query_arg( 'paged', $current_url );
  558. if ( isset( $_GET['orderby'] ) )
  559. $current_orderby = $_GET['orderby'];
  560. else
  561. $current_orderby = '';
  562. if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
  563. $current_order = 'desc';
  564. else
  565. $current_order = 'asc';
  566. if ( ! empty( $columns['cb'] ) ) {
  567. static $cb_counter = 1;
  568. $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>'
  569. . '<input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
  570. $cb_counter++;
  571. }
  572. foreach ( $columns as $column_key => $column_display_name ) {
  573. $class = array( 'manage-column', "column-$column_key" );
  574. $style = '';
  575. if ( in_array( $column_key, $hidden ) )
  576. $style = 'display:none;';
  577. $style = ' style="' . $style . '"';
  578. if ( 'cb' == $column_key )
  579. $class[] = 'check-column';
  580. elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
  581. $class[] = 'num';
  582. if ( isset( $sortable[$column_key] ) ) {
  583. list( $orderby, $desc_first ) = $sortable[$column_key];
  584. if ( $current_orderby == $orderby ) {
  585. $order = 'asc' == $current_order ? 'desc' : 'asc';
  586. $class[] = 'sorted';
  587. $class[] = $current_order;
  588. } else {
  589. $order = $desc_first ? 'desc' : 'asc';
  590. $class[] = 'sortable';
  591. $class[] = $desc_first ? 'asc' : 'desc';
  592. }
  593. $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>';
  594. }
  595. $id = $with_id ? "id='$column_key'" : '';
  596. if ( !empty( $class ) )
  597. $class = "class='" . join( ' ', $class ) . "'";
  598. echo "<th scope='col' $id $class $style>$column_display_name</th>";
  599. }
  600. }
  601. /**
  602. * Display the table
  603. *
  604. * @since 3.1.0
  605. * @access public
  606. */
  607. function display() {
  608. extract( $this->_args );
  609. $this->display_tablenav( 'top' );
  610. ?>
  611. <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
  612. <thead>
  613. <tr>
  614. <?php $this->print_column_headers(); ?>
  615. </tr>
  616. </thead>
  617. <tfoot>
  618. <tr>
  619. <?php $this->print_column_headers( false ); ?>
  620. </tr>
  621. </tfoot>
  622. <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
  623. <?php $this->display_rows_or_placeholder(); ?>
  624. </tbody>
  625. </table>
  626. <?php
  627. $this->display_tablenav( 'bottom' );
  628. }
  629. /**
  630. * Get a list of CSS classes for the <table> tag
  631. *
  632. * @since 3.1.0
  633. * @access protected
  634. *
  635. * @return array
  636. */
  637. function get_table_classes() {
  638. return array( 'widefat', 'fixed', $this->_args['plural'] );
  639. }
  640. /**
  641. * Generate the table navigation above or below the table
  642. *
  643. * @since 3.1.0
  644. * @access protected
  645. */
  646. function display_tablenav( $which ) {
  647. if ( 'top' == $which )
  648. wp_nonce_field( 'bulk-' . $this->_args['plural'] );
  649. ?>
  650. <div class="tablenav <?php echo esc_attr( $which ); ?>">
  651. <div class="alignleft actions">
  652. <?php $this->bulk_actions(); ?>
  653. </div>
  654. <?php
  655. $this->extra_tablenav( $which );
  656. $this->pagination( $which );
  657. ?>
  658. <br class="clear" />
  659. </div>
  660. <?php
  661. }
  662. /**
  663. * Extra controls to be displayed between bulk actions and pagination
  664. *
  665. * @since 3.1.0
  666. * @access protected
  667. */
  668. function extra_tablenav( $which ) {}
  669. /**
  670. * Generate the <tbody> part of the table
  671. *
  672. * @since 3.1.0
  673. * @access protected
  674. */
  675. function display_rows_or_placeholder() {
  676. if ( $this->has_items() ) {
  677. $this->display_rows();
  678. } else {
  679. list( $columns, $hidden ) = $this->get_column_info();
  680. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
  681. $this->no_items();
  682. echo '</td></tr>';
  683. }
  684. }
  685. /**
  686. * Generate the table rows
  687. *
  688. * @since 3.1.0
  689. * @access protected
  690. */
  691. function display_rows() {
  692. foreach ( $this->items as $item )
  693. $this->single_row( $item );
  694. }
  695. /**
  696. * Generates content for a single row of the table
  697. *
  698. * @since 3.1.0
  699. * @access protected
  700. *
  701. * @param object $item The current item
  702. */
  703. function single_row( $item ) {
  704. static $row_class = '';
  705. $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
  706. echo '<tr' . $row_class . '>';
  707. echo $this->single_row_columns( $item );
  708. echo '</tr>';
  709. }
  710. /**
  711. * Generates the columns for a single row of the table
  712. *
  713. * @since 3.1.0
  714. * @access protected
  715. *
  716. * @param object $item The current item
  717. */
  718. function single_row_columns( $item ) {
  719. list( $columns, $hidden ) = $this->get_column_info();
  720. foreach ( $columns as $column_name => $column_display_name ) {
  721. $class = "class='$column_name column-$column_name'";
  722. $style = '';
  723. if ( in_array( $column_name, $hidden ) )
  724. $style = ' style="display:none;"';
  725. $attributes = "$class$style";
  726. if ( 'cb' == $column_name ) {
  727. echo '<th scope="row" class="check-column">';
  728. echo $this->column_cb( $item );
  729. echo '</th>';
  730. }
  731. elseif ( method_exists( $this, 'column_' . $column_name ) ) {
  732. echo "<td $attributes>";
  733. echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
  734. echo "</td>";
  735. }
  736. else {
  737. echo "<td $attributes>";
  738. echo $this->column_default( $item, $column_name );
  739. echo "</td>";
  740. }
  741. }
  742. }
  743. /**
  744. * Handle an incoming ajax request (called from admin-ajax.php)
  745. *
  746. * @since 3.1.0
  747. * @access public
  748. */
  749. function ajax_response() {
  750. $this->prepare_items();
  751. extract( $this->_args );
  752. extract( $this->_pagination_args, EXTR_SKIP );
  753. ob_start();
  754. if ( ! empty( $_REQUEST['no_placeholder'] ) )
  755. $this->display_rows();
  756. else
  757. $this->display_rows_or_placeholder();
  758. $rows = ob_get_clean();
  759. $response = array( 'rows' => $rows );
  760. if ( isset( $total_items ) )
  761. $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
  762. if ( isset( $total_pages ) ) {
  763. $response['total_pages'] = $total_pages;
  764. $response['total_pages_i18n'] = number_format_i18n( $total_pages );
  765. }
  766. die( json_encode( $response ) );
  767. }
  768. /**
  769. * Send required variables to JavaScript land
  770. *
  771. * @access private
  772. */
  773. function _js_vars() {
  774. $args = array(
  775. 'class' => get_class( $this ),
  776. 'screen' => array(
  777. 'id' => $this->screen->id,
  778. 'base' => $this->screen->base,
  779. )
  780. );
  781. printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );
  782. }
  783. }