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.  
  119. if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) ) { 
  120. $actions['delete'] = __( 'Delete' ); 
  121.  
  122. return $actions; 
  123.  
  124. /** 
  125. * @return string 
  126. */ 
  127. public function current_action() { 
  128. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' === $_REQUEST['action'] || 'delete' === $_REQUEST['action2'] ) ) 
  129. return 'bulk-delete'; 
  130.  
  131. return parent::current_action(); 
  132.  
  133. /** 
  134. * @return array 
  135. */ 
  136. public function get_columns() { 
  137. $columns = array( 
  138. 'cb' => '<input type="checkbox" />',  
  139. 'name' => _x( 'Name', 'term name' ),  
  140. 'description' => __( 'Description' ),  
  141. 'slug' => __( 'Slug' ),  
  142. ); 
  143.  
  144. if ( 'link_category' === $this->screen->taxonomy ) { 
  145. $columns['links'] = __( 'Links' ); 
  146. } else { 
  147. $columns['posts'] = _x( 'Count', 'Number/count of items' ); 
  148.  
  149. return $columns; 
  150.  
  151. /** 
  152. * @return array 
  153. */ 
  154. protected function get_sortable_columns() { 
  155. return array( 
  156. 'name' => 'name',  
  157. 'description' => 'description',  
  158. 'slug' => 'slug',  
  159. 'posts' => 'count',  
  160. 'links' => 'count' 
  161. ); 
  162.  
  163. /** 
  164. * @access public 
  165. */ 
  166. public function display_rows_or_placeholder() { 
  167. $taxonomy = $this->screen->taxonomy; 
  168.  
  169. $args = wp_parse_args( $this->callback_args, array( 
  170. 'page' => 1,  
  171. 'number' => 20,  
  172. 'search' => '',  
  173. 'hide_empty' => 0 
  174. ) ); 
  175.  
  176. $page = $args['page']; 
  177.  
  178. // Set variable because $args['number'] can be subsequently overridden. 
  179. $number = $args['number']; 
  180.  
  181. $args['offset'] = $offset = ( $page - 1 ) * $number; 
  182.  
  183. // Convert it to table rows. 
  184. $count = 0; 
  185.  
  186. if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { 
  187. // We'll need the full set of terms then. 
  188. $args['number'] = $args['offset'] = 0; 
  189. $terms = get_terms( $taxonomy, $args ); 
  190.  
  191. if ( empty( $terms ) || ! is_array( $terms ) ) { 
  192. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">'; 
  193. $this->no_items(); 
  194. echo '</td></tr>'; 
  195. return; 
  196.  
  197. if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { 
  198. if ( ! empty( $args['search'] ) ) {// Ignore children on searches. 
  199. $children = array(); 
  200. } else { 
  201. $children = _get_term_hierarchy( $taxonomy ); 
  202. // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake 
  203. $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count ); 
  204. } else { 
  205. foreach ( $terms as $term ) { 
  206. $this->single_row( $term ); 
  207.  
  208. /** 
  209. * @param string $taxonomy 
  210. * @param array $terms 
  211. * @param array $children 
  212. * @param int $start 
  213. * @param int $per_page 
  214. * @param int $count 
  215. * @param int $parent 
  216. * @param int $level 
  217. */ 
  218. private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) { 
  219.  
  220. $end = $start + $per_page; 
  221.  
  222. foreach ( $terms as $key => $term ) { 
  223.  
  224. if ( $count >= $end ) 
  225. break; 
  226.  
  227. if ( $term->parent != $parent && empty( $_REQUEST['s'] ) ) 
  228. continue; 
  229.  
  230. // If the page starts in a subtree, print the parents. 
  231. if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) { 
  232. $my_parents = $parent_ids = array(); 
  233. $p = $term->parent; 
  234. while ( $p ) { 
  235. $my_parent = get_term( $p, $taxonomy ); 
  236. $my_parents[] = $my_parent; 
  237. $p = $my_parent->parent; 
  238. if ( in_array( $p, $parent_ids ) ) // Prevent parent loops. 
  239. break; 
  240. $parent_ids[] = $p; 
  241. unset( $parent_ids ); 
  242.  
  243. $num_parents = count( $my_parents ); 
  244. while ( $my_parent = array_pop( $my_parents ) ) { 
  245. echo "\t"; 
  246. $this->single_row( $my_parent, $level - $num_parents ); 
  247. $num_parents--; 
  248.  
  249. if ( $count >= $start ) { 
  250. echo "\t"; 
  251. $this->single_row( $term, $level ); 
  252.  
  253. ++$count; 
  254.  
  255. unset( $terms[$key] ); 
  256.  
  257. if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) ) 
  258. $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 ); 
  259.  
  260. /** 
  261. * @global string $taxonomy 
  262. * @param WP_Term $tag Term object. 
  263. * @param int $level 
  264. */ 
  265. public function single_row( $tag, $level = 0 ) { 
  266. global $taxonomy; 
  267. $tag = sanitize_term( $tag, $taxonomy ); 
  268.  
  269. $this->level = $level; 
  270.  
  271. echo '<tr id="tag-' . $tag->term_id . '">'; 
  272. $this->single_row_columns( $tag ); 
  273. echo '</tr>'; 
  274.  
  275. /** 
  276. * @param WP_Term $tag Term object. 
  277. * @return string 
  278. */ 
  279. public function column_cb( $tag ) { 
  280. if ( current_user_can( 'delete_term', $tag->term_id ) ) { 
  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 WP_Term $tag Term object. 
  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 WP_Term $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 = wp_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 WP_Term $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. $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI']; 
  358.  
  359. $edit_link = add_query_arg( 
  360. 'wp_http_referer',  
  361. urlencode( wp_unslash( $uri ) ),  
  362. get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) 
  363. ); 
  364.  
  365. $actions = array(); 
  366. if ( current_user_can( 'edit_term', $tag->term_id ) ) { 
  367. $actions['edit'] = sprintf( 
  368. '<a href="%s" aria-label="%s">%s</a>',  
  369. esc_url( $edit_link ),  
  370. /** translators: %s: taxonomy term name */ 
  371. esc_attr( sprintf( __( 'Edit “%s”' ), $tag->name ) ),  
  372. __( 'Edit' ) 
  373. ); 
  374. $actions['inline hide-if-no-js'] = sprintf( 
  375. '<a href="#" class="editinline aria-button-if-js" aria-label="%s">%s</a>',  
  376. /** translators: %s: taxonomy term name */ 
  377. esc_attr( sprintf( __( 'Quick edit “%s” inline' ), $tag->name ) ),  
  378. __( 'Quick Edit' ) 
  379. ); 
  380. if ( current_user_can( 'delete_term', $tag->term_id ) ) { 
  381. $actions['delete'] = sprintf( 
  382. '<a href="%s" class="delete-tag aria-button-if-js" aria-label="%s">%s</a>',  
  383. wp_nonce_url( "edit-tags.php?action=delete&taxonomy=$taxonomy&tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ),  
  384. /** translators: %s: taxonomy term name */ 
  385. esc_attr( sprintf( __( 'Delete “%s”' ), $tag->name ) ),  
  386. __( 'Delete' ) 
  387. ); 
  388. if ( $tax->public ) { 
  389. $actions['view'] = sprintf( 
  390. '<a href="%s" aria-label="%s">%s</a>',  
  391. get_term_link( $tag ),  
  392. /** translators: %s: taxonomy term name */ 
  393. esc_attr( sprintf( __( 'View “%s” archive' ), $tag->name ) ),  
  394. __( 'View' ) 
  395. ); 
  396.  
  397. /** 
  398. * Filters the action links displayed for each term in the Tags list table. 
  399. * @since 2.8.0 
  400. * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead. 
  401. * @param array $actions An array of action links to be displayed. Default 
  402. * 'Edit', 'Quick Edit', 'Delete', and 'View'. 
  403. * @param WP_Term $tag Term object. 
  404. */ 
  405. $actions = apply_filters( 'tag_row_actions', $actions, $tag ); 
  406.  
  407. /** 
  408. * Filters the action links displayed for each term in the terms list table. 
  409. * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. 
  410. * @since 3.0.0 
  411. * @param array $actions An array of action links to be displayed. Default 
  412. * 'Edit', 'Quick Edit', 'Delete', and 'View'. 
  413. * @param WP_Term $tag Term object. 
  414. */ 
  415. $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag ); 
  416.  
  417. return $this->row_actions( $actions ); 
  418.  
  419. /** 
  420. * @param WP_Term $tag Term object. 
  421. * @return string 
  422. */ 
  423. public function column_description( $tag ) { 
  424. return $tag->description; 
  425.  
  426. /** 
  427. * @param WP_Term $tag Term object. 
  428. * @return string 
  429. */ 
  430. public function column_slug( $tag ) { 
  431. /** This filter is documented in wp-admin/edit-tag-form.php */ 
  432. return apply_filters( 'editable_slug', $tag->slug, $tag ); 
  433.  
  434. /** 
  435. * @param WP_Term $tag Term object. 
  436. * @return string 
  437. */ 
  438. public function column_posts( $tag ) { 
  439. $count = number_format_i18n( $tag->count ); 
  440.  
  441. $tax = get_taxonomy( $this->screen->taxonomy ); 
  442.  
  443. $ptype_object = get_post_type_object( $this->screen->post_type ); 
  444. if ( ! $ptype_object->show_ui ) 
  445. return $count; 
  446.  
  447. if ( $tax->query_var ) { 
  448. $args = array( $tax->query_var => $tag->slug ); 
  449. } else { 
  450. $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug ); 
  451.  
  452. if ( 'post' != $this->screen->post_type ) 
  453. $args['post_type'] = $this->screen->post_type; 
  454.  
  455. if ( 'attachment' === $this->screen->post_type ) 
  456. return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>"; 
  457.  
  458. return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>"; 
  459.  
  460. /** 
  461. * @param WP_Term $tag Term object. 
  462. * @return string 
  463. */ 
  464. public function column_links( $tag ) { 
  465. $count = number_format_i18n( $tag->count ); 
  466. if ( $count ) 
  467. $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>"; 
  468. return $count; 
  469.  
  470. /** 
  471. * @param WP_Term $tag Term object. 
  472. * @param string $column_name 
  473. * @return string 
  474. */ 
  475. public function column_default( $tag, $column_name ) { 
  476. /** 
  477. * Filters the displayed columns in the terms list table. 
  478. * The dynamic portion of the hook name, `$this->screen->taxonomy`,  
  479. * refers to the slug of the current taxonomy. 
  480. * @since 2.8.0 
  481. * @param string $string Blank string. 
  482. * @param string $column_name Name of the column. 
  483. * @param int $term_id Term ID. 
  484. */ 
  485. return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id ); 
  486.  
  487. /** 
  488. * Outputs the hidden row displayed when inline editing 
  489. * @since 3.1.0 
  490. */ 
  491. public function inline_edit() { 
  492. $tax = get_taxonomy( $this->screen->taxonomy ); 
  493.  
  494. if ( ! current_user_can( $tax->cap->edit_terms ) ) 
  495. return; 
  496. ?> 
  497.  
  498. <form method="get"><table style="display: none"><tbody id="inlineedit"> 
  499. <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange"> 
  500.  
  501. <fieldset> 
  502. <legend class="inline-edit-legend"><?php _e( 'Quick Edit' ); ?></legend> 
  503. <div class="inline-edit-col"> 
  504. <label> 
  505. <span class="title"><?php _ex( 'Name', 'term name' ); ?></span> 
  506. <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span> 
  507. </label> 
  508. <?php if ( !global_terms_enabled() ) { ?> 
  509. <label> 
  510. <span class="title"><?php _e( 'Slug' ); ?></span> 
  511. <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span> 
  512. </label> 
  513. <?php } ?> 
  514. </div></fieldset> 
  515. <?php 
  516.  
  517. $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true ); 
  518.  
  519. list( $columns ) = $this->get_column_info(); 
  520.  
  521. foreach ( $columns as $column_name => $column_display_name ) { 
  522. if ( isset( $core_columns[$column_name] ) ) 
  523. continue; 
  524.  
  525. /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */ 
  526. do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy ); 
  527.  
  528. ?> 
  529.  
  530. <p class="inline-edit-save submit"> 
  531. <button type="button" class="cancel button alignleft"><?php _e( 'Cancel' ); ?></button> 
  532. <button type="button" class="save button button-primary alignright"><?php echo $tax->labels->update_item; ?></button> 
  533. <span class="spinner"></span> 
  534. <span class="error" style="display:none;"></span> 
  535. <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?> 
  536. <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" /> 
  537. <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" /> 
  538. <br class="clear" /> 
  539. </p> 
  540. </td></tr> 
  541. </tbody></table></form> 
  542. <?php