PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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