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

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