PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ch3tag/mothers
PHP | 918 lines | 453 code | 141 blank | 324 comment | 81 complexity | 3ffa45d7f49bcfc706cee6934890d0df MD5 | raw file
  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="search" 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. /* translators: 1: month name, 2: 4-digit year */
  335. sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
  336. );
  337. }
  338. ?>
  339. </select>
  340. <?php
  341. }
  342. /**
  343. * Display a view switcher
  344. *
  345. * @since 3.1.0
  346. * @access protected
  347. */
  348. function view_switcher( $current_mode ) {
  349. $modes = array(
  350. 'list' => __( 'List View' ),
  351. 'excerpt' => __( 'Excerpt View' )
  352. );
  353. ?>
  354. <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
  355. <div class="view-switch">
  356. <?php
  357. foreach ( $modes as $mode => $title ) {
  358. $class = ( $current_mode == $mode ) ? 'class="current"' : '';
  359. 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";
  360. }
  361. ?>
  362. </div>
  363. <?php
  364. }
  365. /**
  366. * Display a comment count bubble
  367. *
  368. * @since 3.1.0
  369. * @access protected
  370. *
  371. * @param int $post_id
  372. * @param int $pending_comments
  373. */
  374. function comments_bubble( $post_id, $pending_comments ) {
  375. $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
  376. if ( $pending_comments )
  377. echo '<strong>';
  378. 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>";
  379. if ( $pending_comments )
  380. echo '</strong>';
  381. }
  382. /**
  383. * Get the current page number
  384. *
  385. * @since 3.1.0
  386. * @access protected
  387. *
  388. * @return int
  389. */
  390. function get_pagenum() {
  391. $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
  392. if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
  393. $pagenum = $this->_pagination_args['total_pages'];
  394. return max( 1, $pagenum );
  395. }
  396. /**
  397. * Get number of items to display on a single page
  398. *
  399. * @since 3.1.0
  400. * @access protected
  401. *
  402. * @return int
  403. */
  404. function get_items_per_page( $option, $default = 20 ) {
  405. $per_page = (int) get_user_option( $option );
  406. if ( empty( $per_page ) || $per_page < 1 )
  407. $per_page = $default;
  408. return (int) apply_filters( $option, $per_page );
  409. }
  410. /**
  411. * Display the pagination.
  412. *
  413. * @since 3.1.0
  414. * @access protected
  415. */
  416. function pagination( $which ) {
  417. if ( empty( $this->_pagination_args ) )
  418. return;
  419. extract( $this->_pagination_args, EXTR_SKIP );
  420. $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
  421. $current = $this->get_pagenum();
  422. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  423. $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
  424. $page_links = array();
  425. $disable_first = $disable_last = '';
  426. if ( $current == 1 )
  427. $disable_first = ' disabled';
  428. if ( $current == $total_pages )
  429. $disable_last = ' disabled';
  430. $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
  431. 'first-page' . $disable_first,
  432. esc_attr__( 'Go to the first page' ),
  433. esc_url( remove_query_arg( 'paged', $current_url ) ),
  434. '&laquo;'
  435. );
  436. $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
  437. 'prev-page' . $disable_first,
  438. esc_attr__( 'Go to the previous page' ),
  439. esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
  440. '&lsaquo;'
  441. );
  442. if ( 'bottom' == $which )
  443. $html_current_page = $current;
  444. else
  445. $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='paged' value='%s' size='%d' />",
  446. esc_attr__( 'Current page' ),
  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. $pagination_links_class = 'pagination-links';
  465. if ( ! empty( $infinite_scroll ) )
  466. $pagination_links_class = ' hide-if-js';
  467. $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
  468. if ( $total_pages )
  469. $page_class = $total_pages < 2 ? ' one-page' : '';
  470. else
  471. $page_class = ' no-pages';
  472. $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
  473. echo $this->_pagination;
  474. }
  475. /**
  476. * Get a list of columns. The format is:
  477. * 'internal-name' => 'Title'
  478. *
  479. * @since 3.1.0
  480. * @access protected
  481. * @abstract
  482. *
  483. * @return array
  484. */
  485. function get_columns() {
  486. die( 'function WP_List_Table::get_columns() must be over-ridden in a sub-class.' );
  487. }
  488. /**
  489. * Get a list of sortable columns. The format is:
  490. * 'internal-name' => 'orderby'
  491. * or
  492. * 'internal-name' => array( 'orderby', true )
  493. *
  494. * The second format will make the initial sorting order be descending
  495. *
  496. * @since 3.1.0
  497. * @access protected
  498. *
  499. * @return array
  500. */
  501. function get_sortable_columns() {
  502. return array();
  503. }
  504. /**
  505. * Get a list of all, hidden and sortable columns, with filter applied
  506. *
  507. * @since 3.1.0
  508. * @access protected
  509. *
  510. * @return array
  511. */
  512. function get_column_info() {
  513. if ( isset( $this->_column_headers ) )
  514. return $this->_column_headers;
  515. $screen = get_current_screen();
  516. $columns = get_column_headers( $screen );
  517. $hidden = get_hidden_columns( $screen );
  518. $_sortable = apply_filters( "manage_{$screen->id}_sortable_columns", $this->get_sortable_columns() );
  519. $sortable = array();
  520. foreach ( $_sortable as $id => $data ) {
  521. if ( empty( $data ) )
  522. continue;
  523. $data = (array) $data;
  524. if ( !isset( $data[1] ) )
  525. $data[1] = false;
  526. $sortable[$id] = $data;
  527. }
  528. $this->_column_headers = array( $columns, $hidden, $sortable );
  529. return $this->_column_headers;
  530. }
  531. /**
  532. * Return number of visible columns
  533. *
  534. * @since 3.1.0
  535. * @access public
  536. *
  537. * @return int
  538. */
  539. function get_column_count() {
  540. list ( $columns, $hidden ) = $this->get_column_info();
  541. $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
  542. return count( $columns ) - count( $hidden );
  543. }
  544. /**
  545. * Print column headers, accounting for hidden and sortable columns.
  546. *
  547. * @since 3.1.0
  548. * @access protected
  549. *
  550. * @param bool $with_id Whether to set the id attribute or not
  551. */
  552. function print_column_headers( $with_id = true ) {
  553. $screen = get_current_screen();
  554. list( $columns, $hidden, $sortable ) = $this->get_column_info();
  555. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  556. $current_url = remove_query_arg( 'paged', $current_url );
  557. if ( isset( $_GET['orderby'] ) )
  558. $current_orderby = $_GET['orderby'];
  559. else
  560. $current_orderby = '';
  561. if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] )
  562. $current_order = 'desc';
  563. else
  564. $current_order = 'asc';
  565. foreach ( $columns as $column_key => $column_display_name ) {
  566. $class = array( 'manage-column', "column-$column_key" );
  567. $style = '';
  568. if ( in_array( $column_key, $hidden ) )
  569. $style = 'display:none;';
  570. $style = ' style="' . $style . '"';
  571. if ( 'cb' == $column_key )
  572. $class[] = 'check-column';
  573. elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
  574. $class[] = 'num';
  575. if ( isset( $sortable[$column_key] ) ) {
  576. list( $orderby, $desc_first ) = $sortable[$column_key];
  577. if ( $current_orderby == $orderby ) {
  578. $order = 'asc' == $current_order ? 'desc' : 'asc';
  579. $class[] = 'sorted';
  580. $class[] = $current_order;
  581. } else {
  582. $order = $desc_first ? 'desc' : 'asc';
  583. $class[] = 'sortable';
  584. $class[] = $desc_first ? 'asc' : 'desc';
  585. }
  586. $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>';
  587. }
  588. $id = $with_id ? "id='$column_key'" : '';
  589. if ( !empty( $class ) )
  590. $class = "class='" . join( ' ', $class ) . "'";
  591. echo "<th scope='col' $id $class $style>$column_display_name</th>";
  592. }
  593. }
  594. /**
  595. * Display the table
  596. *
  597. * @since 3.1.0
  598. * @access public
  599. */
  600. function display() {
  601. extract( $this->_args );
  602. $this->display_tablenav( 'top' );
  603. ?>
  604. <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
  605. <thead>
  606. <tr>
  607. <?php $this->print_column_headers(); ?>
  608. </tr>
  609. </thead>
  610. <tfoot>
  611. <tr>
  612. <?php $this->print_column_headers( false ); ?>
  613. </tr>
  614. </tfoot>
  615. <tbody id="the-list"<?php if ( $singular ) echo " class='list:$singular'"; ?>>
  616. <?php $this->display_rows_or_placeholder(); ?>
  617. </tbody>
  618. </table>
  619. <?php
  620. $this->display_tablenav( 'bottom' );
  621. }
  622. /**
  623. * Get a list of CSS classes for the <table> tag
  624. *
  625. * @since 3.1.0
  626. * @access protected
  627. *
  628. * @return array
  629. */
  630. function get_table_classes() {
  631. return array( 'widefat', 'fixed', $this->_args['plural'] );
  632. }
  633. /**
  634. * Generate the table navigation above or below the table
  635. *
  636. * @since 3.1.0
  637. * @access protected
  638. */
  639. function display_tablenav( $which ) {
  640. if ( 'top' == $which )
  641. wp_nonce_field( 'bulk-' . $this->_args['plural'] );
  642. ?>
  643. <div class="tablenav <?php echo esc_attr( $which ); ?>">
  644. <div class="alignleft actions">
  645. <?php $this->bulk_actions( $which ); ?>
  646. </div>
  647. <?php
  648. $this->extra_tablenav( $which );
  649. $this->pagination( $which );
  650. ?>
  651. <br class="clear" />
  652. </div>
  653. <?php
  654. }
  655. /**
  656. * Extra controls to be displayed between bulk actions and pagination
  657. *
  658. * @since 3.1.0
  659. * @access protected
  660. */
  661. function extra_tablenav( $which ) {}
  662. /**
  663. * Generate the <tbody> part of the table
  664. *
  665. * @since 3.1.0
  666. * @access protected
  667. */
  668. function display_rows_or_placeholder() {
  669. if ( $this->has_items() ) {
  670. $this->display_rows();
  671. } else {
  672. list( $columns, $hidden ) = $this->get_column_info();
  673. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
  674. $this->no_items();
  675. echo '</td></tr>';
  676. }
  677. }
  678. /**
  679. * Generate the table rows
  680. *
  681. * @since 3.1.0
  682. * @access protected
  683. */
  684. function display_rows() {
  685. foreach ( $this->items as $item )
  686. $this->single_row( $item );
  687. }
  688. /**
  689. * Generates content for a single row of the table
  690. *
  691. * @since 3.1.0
  692. * @access protected
  693. *
  694. * @param object $item The current item
  695. */
  696. function single_row( $item ) {
  697. static $row_class = '';
  698. $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
  699. echo '<tr' . $row_class . '>';
  700. echo $this->single_row_columns( $item );
  701. echo '</tr>';
  702. }
  703. /**
  704. * Generates the columns for a single row of the table
  705. *
  706. * @since 3.1.0
  707. * @access protected
  708. *
  709. * @param object $item The current item
  710. */
  711. function single_row_columns( $item ) {
  712. list( $columns, $hidden ) = $this->get_column_info();
  713. foreach ( $columns as $column_name => $column_display_name ) {
  714. $class = "class='$column_name column-$column_name'";
  715. $style = '';
  716. if ( in_array( $column_name, $hidden ) )
  717. $style = ' style="display:none;"';
  718. $attributes = "$class$style";
  719. if ( 'cb' == $column_name ) {
  720. echo '<th scope="row" class="check-column">';
  721. echo $this->column_cb( $item );
  722. echo '</th>';
  723. }
  724. elseif ( method_exists( $this, 'column_' . $column_name ) ) {
  725. echo "<td $attributes>";
  726. echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
  727. echo "</td>";
  728. }
  729. else {
  730. echo "<td $attributes>";
  731. echo $this->column_default( $item, $column_name );
  732. echo "</td>";
  733. }
  734. }
  735. }
  736. /**
  737. * Handle an incoming ajax request (called from admin-ajax.php)
  738. *
  739. * @since 3.1.0
  740. * @access public
  741. */
  742. function ajax_response() {
  743. $this->prepare_items();
  744. extract( $this->_args );
  745. extract( $this->_pagination_args, EXTR_SKIP );
  746. ob_start();
  747. if ( ! empty( $_REQUEST['no_placeholder'] ) )
  748. $this->display_rows();
  749. else
  750. $this->display_rows_or_placeholder();
  751. $rows = ob_get_clean();
  752. $response = array( 'rows' => $rows );
  753. if ( isset( $total_items ) )
  754. $response['total_items_i18n'] = sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) );
  755. if ( isset( $total_pages ) ) {
  756. $response['total_pages'] = $total_pages;
  757. $response['total_pages_i18n'] = number_format_i18n( $total_pages );
  758. }
  759. die( json_encode( $response ) );
  760. }
  761. /**
  762. * Send required variables to JavaScript land
  763. *
  764. * @access private
  765. */
  766. function _js_vars() {
  767. $current_screen = get_current_screen();
  768. $args = array(
  769. 'class' => get_class( $this ),
  770. 'screen' => array(
  771. 'id' => $current_screen->id,
  772. 'base' => $current_screen->base,
  773. )
  774. );
  775. printf( "<script type='text/javascript'>list_args = %s;</script>\n", json_encode( $args ) );
  776. }
  777. }