WP_Terms_List_Table

Core class used to implement displaying terms in a list table.

Defined (1)

The class is defined in the following location(s).

/wp-admin/includes/class-wp-terms-list-table.php  
  1. class WP_Terms_List_Table extends WP_List_Table { 
  2.  
  3. public $callback_args; 
  4.  
  5. private $level; 
  6.  
  7. /** 
  8. * Constructor. 
  9. * @since 3.1.0 
  10. * @access public 
  11. * @see WP_List_Table::__construct() for more information on default arguments. 
  12. * @global string $post_type 
  13. * @global string $taxonomy 
  14. * @global string $action 
  15. * @global object $tax 
  16. * @param array $args An associative array of arguments. 
  17. */ 
  18. public function __construct( $args = array() ) { 
  19. global $post_type, $taxonomy, $action, $tax; 
  20.  
  21. parent::__construct( array( 
  22. 'plural' => 'tags',  
  23. 'singular' => 'tag',  
  24. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,  
  25. ) ); 
  26.  
  27. $action = $this->screen->action; 
  28. $post_type = $this->screen->post_type; 
  29. $taxonomy = $this->screen->taxonomy; 
  30.  
  31. if ( empty( $taxonomy ) ) 
  32. $taxonomy = 'post_tag'; 
  33.  
  34. if ( ! taxonomy_exists( $taxonomy ) ) 
  35. wp_die( __( 'Invalid taxonomy.' ) ); 
  36.  
  37. $tax = get_taxonomy( $taxonomy ); 
  38.  
  39. // @todo Still needed? Maybe just the show_ui part. 
  40. if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) ) 
  41. $post_type = 'post'; 
  42.  
  43.  
  44. /** 
  45. * @return bool 
  46. */ 
  47. public function ajax_user_can() { 
  48. return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms ); 
  49.  
  50. /** 
  51. * @access public 
  52. */ 
  53. public function prepare_items() { 
  54. $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' ); 
  55.  
  56. if ( 'post_tag' === $this->screen->taxonomy ) { 
  57. /** 
  58. * Filters the number of terms displayed per page for the Tags list table. 
  59. * @since 2.8.0 
  60. * @param int $tags_per_page Number of tags to be displayed. Default 20. 
  61. */ 
  62. $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page ); 
  63.  
  64. /** 
  65. * Filters the number of terms displayed per page for the Tags list table. 
  66. * @since 2.7.0 
  67. * @deprecated 2.8.0 Use edit_tags_per_page instead. 
  68. * @param int $tags_per_page Number of tags to be displayed. Default 20. 
  69. */ 
  70. $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page ); 
  71. } elseif ( 'category' === $this->screen->taxonomy ) { 
  72. /** 
  73. * Filters the number of terms displayed per page for the Categories list table. 
  74. * @since 2.8.0 
  75. * @param int $tags_per_page Number of categories to be displayed. Default 20. 
  76. */ 
  77. $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); 
  78.  
  79. $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : ''; 
  80.  
  81. $args = array( 
  82. 'search' => $search,  
  83. 'page' => $this->get_pagenum(),  
  84. 'number' => $tags_per_page,  
  85. ); 
  86.  
  87. if ( !empty( $_REQUEST['orderby'] ) ) 
  88. $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) ); 
  89.  
  90. if ( !empty( $_REQUEST['order'] ) ) 
  91. $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) ); 
  92.  
  93. $this->callback_args = $args; 
  94.  
  95. $this->set_pagination_args( array( 
  96. 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),  
  97. 'per_page' => $tags_per_page,  
  98. ) ); 
  99.  
  100. /** 
  101. * @return bool 
  102. */ 
  103. public function has_items() { 
  104. // todo: populate $this->items in prepare_items() 
  105. return true; 
  106.  
  107. /** 
  108. * @access public 
  109. */ 
  110. public function no_items() { 
  111. echo get_taxonomy( $this->screen->taxonomy )->labels->not_found; 
  112.  
  113. /** 
  114. * @return array 
  115. */ 
  116. protected function get_bulk_actions() { 
  117. $actions = array(); 
  118. $actions['delete'] = __( 'Delete' ); 
  119.  
  120. return $actions; 
  121.  
  122. /** 
  123. * @return string 
  124. */ 
  125. public function current_action() { 
  126. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' === $_REQUEST['action'] || 'delete' === $_REQUEST['action2'] ) ) 
  127. return 'bulk-delete'; 
  128.  
  129. return parent::current_action(); 
  130.  
  131. /** 
  132. * @return array 
  133. */ 
  134. public function get_columns() { 
  135. $columns = array( 
  136. 'cb' => '<input type="checkbox" />',  
  137. 'name' => _x( 'Name', 'term name' ),  
  138. 'description' => __( 'Description' ),  
  139. 'slug' => __( 'Slug' ),  
  140. ); 
  141.  
  142. if ( 'link_category' === $this->screen->taxonomy ) { 
  143. $columns['links'] = __( 'Links' ); 
  144. } else { 
  145. $columns['posts'] = _x( 'Count', 'Number/count of items' ); 
  146.  
  147. return $columns; 
  148.  
  149. /** 
  150. * @return array 
  151. */ 
  152. protected function get_sortable_columns() { 
  153. return array( 
  154. 'name' => 'name',  
  155. 'description' => 'description',  
  156. 'slug' => 'slug',  
  157. 'posts' => 'count',  
  158. 'links' => 'count' 
  159. ); 
  160.  
  161. /** 
  162. * @access public 
  163. */ 
  164. public function display_rows_or_placeholder() { 
  165. $taxonomy = $this->screen->taxonomy; 
  166.  
  167. $args = wp_parse_args( $this->callback_args, array( 
  168. 'page' => 1,  
  169. 'number' => 20,  
  170. 'search' => '',  
  171. 'hide_empty' => 0 
  172. ) ); 
  173.  
  174. $page = $args['page']; 
  175.  
  176. // Set variable because $args['number'] can be subsequently overridden. 
  177. $number = $args['number']; 
  178.  
  179. $args['offset'] = $offset = ( $page - 1 ) * $number; 
  180.  
  181. // Convert it to table rows. 
  182. $count = 0; 
  183.  
  184. if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { 
  185. // We'll need the full set of terms then. 
  186. $args['number'] = $args['offset'] = 0; 
  187. $terms = get_terms( $taxonomy, $args ); 
  188.  
  189. if ( empty( $terms ) || ! is_array( $terms ) ) { 
  190. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">'; 
  191. $this->no_items(); 
  192. echo '</td></tr>'; 
  193. return; 
  194.  
  195. if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { 
  196. if ( ! empty( $args['search'] ) ) {// Ignore children on searches. 
  197. $children = array(); 
  198. } else { 
  199. $children = _get_term_hierarchy( $taxonomy ); 
  200. // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake 
  201. $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count ); 
  202. } else { 
  203. foreach ( $terms as $term ) { 
  204. $this->single_row( $term ); 
  205.  
  206. /** 
  207. * @param string $taxonomy 
  208. * @param array $terms 
  209. * @param array $children 
  210. * @param int $start 
  211. * @param int $per_page 
  212. * @param int $count 
  213. * @param int $parent 
  214. * @param int $level 
  215. */ 
  216. private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) { 
  217.  
  218. $end = $start + $per_page; 
  219.  
  220. foreach ( $terms as $key => $term ) { 
  221.  
  222. if ( $count >= $end ) 
  223. break; 
  224.  
  225. if ( $term->parent != $parent && empty( $_REQUEST['s'] ) ) 
  226. continue; 
  227.  
  228. // If the page starts in a subtree, print the parents. 
  229. if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) { 
  230. $my_parents = $parent_ids = array(); 
  231. $p = $term->parent; 
  232. while ( $p ) { 
  233. $my_parent = get_term( $p, $taxonomy ); 
  234. $my_parents[] = $my_parent; 
  235. $p = $my_parent->parent; 
  236. if ( in_array( $p, $parent_ids ) ) // Prevent parent loops. 
  237. break; 
  238. $parent_ids[] = $p; 
  239. unset( $parent_ids ); 
  240.  
  241. $num_parents = count( $my_parents ); 
  242. while ( $my_parent = array_pop( $my_parents ) ) { 
  243. echo "\t"; 
  244. $this->single_row( $my_parent, $level - $num_parents ); 
  245. $num_parents--; 
  246.  
  247. if ( $count >= $start ) { 
  248. echo "\t"; 
  249. $this->single_row( $term, $level ); 
  250.  
  251. ++$count; 
  252.  
  253. unset( $terms[$key] ); 
  254.  
  255. if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) ) 
  256. $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 ); 
  257.  
  258. /** 
  259. * @global string $taxonomy 
  260. * @param object $tag 
  261. * @param int $level 
  262. */ 
  263. public function single_row( $tag, $level = 0 ) { 
  264. global $taxonomy; 
  265. $tag = sanitize_term( $tag, $taxonomy ); 
  266.  
  267. $this->level = $level; 
  268.  
  269. echo '<tr id="tag-' . $tag->term_id . '">'; 
  270. $this->single_row_columns( $tag ); 
  271. echo '</tr>'; 
  272.  
  273. /** 
  274. * @param object $tag 
  275. * @return string 
  276. */ 
  277. public function column_cb( $tag ) { 
  278. $default_term = get_option( 'default_' . $this->screen->taxonomy ); 
  279.  
  280. if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term ) 
  281. return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>' 
  282. . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />'; 
  283.  
  284. return ' '; 
  285.  
  286. /** 
  287. * @param object $tag 
  288. * @return string 
  289. */ 
  290. public function column_name( $tag ) { 
  291. $taxonomy = $this->screen->taxonomy; 
  292.  
  293. $pad = str_repeat( '— ', max( 0, $this->level ) ); 
  294.  
  295. /** 
  296. * Filters display of the term name in the terms list table. 
  297. * The default output may include padding due to the term's 
  298. * current level in the term hierarchy. 
  299. * @since 2.5.0 
  300. * @see WP_Terms_List_Table::column_name() 
  301. * @param string $pad_tag_name The term name, padded if not top-level. 
  302. * @param object $tag Term object. 
  303. */ 
  304. $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag ); 
  305.  
  306. $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' ); 
  307.  
  308. $uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI']; 
  309.  
  310. $edit_link = add_query_arg( 
  311. 'wp_http_referer',  
  312. urlencode( wp_unslash( $uri ) ),  
  313. get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) 
  314. ); 
  315.  
  316. $out = sprintf( 
  317. '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong><br />',  
  318. esc_url( $edit_link ),  
  319. /** translators: %s: taxonomy term name */ 
  320. esc_attr( sprintf( __( '“%s” (Edit)' ), $tag->name ) ),  
  321. $name 
  322. ); 
  323.  
  324. $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">'; 
  325. $out .= '<div class="name">' . $qe_data->name . '</div>'; 
  326.  
  327. /** This filter is documented in wp-admin/edit-tag-form.php */ 
  328. $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '</div>'; 
  329. $out .= '<div class="parent">' . $qe_data->parent . '</div></div>'; 
  330.  
  331. return $out; 
  332.  
  333. /** 
  334. * Gets the name of the default primary column. 
  335. * @since 4.3.0 
  336. * @access protected 
  337. * @return string Name of the default primary column, in this case, 'name'. 
  338. */ 
  339. protected function get_default_primary_column_name() { 
  340. return 'name'; 
  341.  
  342. /** 
  343. * Generates and displays row action links. 
  344. * @since 4.3.0 
  345. * @access protected 
  346. * @param object $tag Tag being acted upon. 
  347. * @param string $column_name Current column name. 
  348. * @param string $primary Primary column name. 
  349. * @return string Row actions output for terms. 
  350. */ 
  351. protected function handle_row_actions( $tag, $column_name, $primary ) { 
  352. if ( $primary !== $column_name ) { 
  353. return ''; 
  354.  
  355. $taxonomy = $this->screen->taxonomy; 
  356. $tax = get_taxonomy( $taxonomy ); 
  357. $default_term = get_option( 'default_' . $taxonomy ); 
  358.  
  359. $uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI']; 
  360.  
  361. $edit_link = add_query_arg( 
  362. 'wp_http_referer',  
  363. urlencode( wp_unslash( $uri ) ),  
  364. get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) 
  365. ); 
  366.  
  367. $actions = array(); 
  368. if ( current_user_can( $tax->cap->edit_terms ) ) { 
  369. $actions['edit'] = sprintf( 
  370. '<a href="%s" aria-label="%s">%s</a>',  
  371. esc_url( $edit_link ),  
  372. /** translators: %s: taxonomy term name */ 
  373. esc_attr( sprintf( __( 'Edit “%s”' ), $tag->name ) ),  
  374. __( 'Edit' ) 
  375. ); 
  376. $actions['inline hide-if-no-js'] = sprintf( 
  377. '<a href="#" class="editinline aria-button-if-js" aria-label="%s">%s</a>',  
  378. /** translators: %s: taxonomy term name */ 
  379. esc_attr( sprintf( __( 'Quick edit “%s” inline' ), $tag->name ) ),  
  380. __( 'Quick Edit' ) 
  381. ); 
  382. if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term ) { 
  383. $actions['delete'] = sprintf( 
  384. '<a href="%s" class="delete-tag aria-button-if-js" aria-label="%s">%s</a>',  
  385. wp_nonce_url( "edit-tags.php?action=delete&taxonomy=$taxonomy&tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ),  
  386. /** translators: %s: taxonomy term name */ 
  387. esc_attr( sprintf( __( 'Delete “%s”' ), $tag->name ) ),  
  388. __( 'Delete' ) 
  389. ); 
  390. if ( $tax->public ) { 
  391. $actions['view'] = sprintf( 
  392. '<a href="%s" aria-label="%s">%s</a>',  
  393. get_term_link( $tag ),  
  394. /** translators: %s: taxonomy term name */ 
  395. esc_attr( sprintf( __( 'View “%s” archive' ), $tag->name ) ),  
  396. __( 'View' ) 
  397. ); 
  398.  
  399. /** 
  400. * Filters the action links displayed for each term in the Tags list table. 
  401. * @since 2.8.0 
  402. * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead. 
  403. * @param array $actions An array of action links to be displayed. Default 
  404. * 'Edit', 'Quick Edit', 'Delete', and 'View'. 
  405. * @param object $tag Term object. 
  406. */ 
  407. $actions = apply_filters( 'tag_row_actions', $actions, $tag ); 
  408.  
  409. /** 
  410. * Filters the action links displayed for each term in the terms list table. 
  411. * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. 
  412. * @since 3.0.0 
  413. * @param array $actions An array of action links to be displayed. Default 
  414. * 'Edit', 'Quick Edit', 'Delete', and 'View'. 
  415. * @param object $tag Term object. 
  416. */ 
  417. $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag ); 
  418.  
  419. return $this->row_actions( $actions ); 
  420.  
  421. /** 
  422. * @param object $tag 
  423. * @return string 
  424. */ 
  425. public function column_description( $tag ) { 
  426. return $tag->description; 
  427.  
  428. /** 
  429. * @param object $tag 
  430. * @return string 
  431. */ 
  432. public function column_slug( $tag ) { 
  433. /** This filter is documented in wp-admin/edit-tag-form.php */ 
  434. return apply_filters( 'editable_slug', $tag->slug, $tag ); 
  435.  
  436. /** 
  437. * @param object $tag 
  438. * @return string 
  439. */ 
  440. public function column_posts( $tag ) { 
  441. $count = number_format_i18n( $tag->count ); 
  442.  
  443. $tax = get_taxonomy( $this->screen->taxonomy ); 
  444.  
  445. $ptype_object = get_post_type_object( $this->screen->post_type ); 
  446. if ( ! $ptype_object->show_ui ) 
  447. return $count; 
  448.  
  449. if ( $tax->query_var ) { 
  450. $args = array( $tax->query_var => $tag->slug ); 
  451. } else { 
  452. $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug ); 
  453.  
  454. if ( 'post' != $this->screen->post_type ) 
  455. $args['post_type'] = $this->screen->post_type; 
  456.  
  457. if ( 'attachment' === $this->screen->post_type ) 
  458. return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>"; 
  459.  
  460. return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>"; 
  461.  
  462. /** 
  463. * @param object $tag 
  464. * @return string 
  465. */ 
  466. public function column_links( $tag ) { 
  467. $count = number_format_i18n( $tag->count ); 
  468. if ( $count ) 
  469. $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>"; 
  470. return $count; 
  471.  
  472. /** 
  473. * @param object $tag 
  474. * @param string $column_name 
  475. * @return string 
  476. */ 
  477. public function column_default( $tag, $column_name ) { 
  478. /** 
  479. * Filters the displayed columns in the terms list table. 
  480. * The dynamic portion of the hook name, `$this->screen->taxonomy`,  
  481. * refers to the slug of the current taxonomy. 
  482. * @since 2.8.0 
  483. * @param string $string Blank string. 
  484. * @param string $column_name Name of the column. 
  485. * @param int $term_id Term ID. 
  486. */ 
  487. return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id ); 
  488.  
  489. /** 
  490. * Outputs the hidden row displayed when inline editing 
  491. * @since 3.1.0 
  492. */ 
  493. public function inline_edit() { 
  494. $tax = get_taxonomy( $this->screen->taxonomy ); 
  495.  
  496. if ( ! current_user_can( $tax->cap->edit_terms ) ) 
  497. return; 
  498. ?> 
  499.  
  500. <form method="get"><table style="display: none"><tbody id="inlineedit"> 
  501. <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange"> 
  502.  
  503. <fieldset> 
  504. <legend class="inline-edit-legend"><?php _e( 'Quick Edit' ); ?></legend> 
  505. <div class="inline-edit-col"> 
  506. <label> 
  507. <span class="title"><?php _ex( 'Name', 'term name' ); ?></span> 
  508. <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span> 
  509. </label> 
  510. <?php if ( !global_terms_enabled() ) { ?> 
  511. <label> 
  512. <span class="title"><?php _e( 'Slug' ); ?></span> 
  513. <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span> 
  514. </label> 
  515. <?php } ?> 
  516. </div></fieldset> 
  517. <?php 
  518.  
  519. $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true ); 
  520.  
  521. list( $columns ) = $this->get_column_info(); 
  522.  
  523. foreach ( $columns as $column_name => $column_display_name ) { 
  524. if ( isset( $core_columns[$column_name] ) ) 
  525. continue; 
  526.  
  527. /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */ 
  528. do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy ); 
  529.  
  530. ?> 
  531.  
  532. <p class="inline-edit-save submit"> 
  533. <button type="button" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></button> 
  534. <button type="button" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></button> 
  535. <span class="spinner"></span> 
  536. <span class="error" style="display:none;"></span> 
  537. <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?> 
  538. <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" /> 
  539. <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" /> 
  540. <br class="clear" /> 
  541. </p> 
  542. </td></tr> 
  543. </tbody></table></form> 
  544. <?php