3 * List Table API: WP_Terms_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying terms in a list table.
18 class WP_Terms_List_Table extends WP_List_Table {
20 public $callback_args;
30 * @see WP_List_Table::__construct() for more information on default arguments.
32 * @global string $post_type
33 * @global string $taxonomy
34 * @global string $action
37 * @param array $args An associative array of arguments.
39 public function __construct( $args = array() ) {
40 global $post_type, $taxonomy, $action, $tax;
42 parent::__construct( array(
45 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
48 $action = $this->screen->action;
49 $post_type = $this->screen->post_type;
50 $taxonomy = $this->screen->taxonomy;
52 if ( empty( $taxonomy ) )
53 $taxonomy = 'post_tag';
55 if ( ! taxonomy_exists( $taxonomy ) )
56 wp_die( __( 'Invalid taxonomy' ) );
58 $tax = get_taxonomy( $taxonomy );
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 ) ) ) )
70 public function ajax_user_can() {
71 return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
77 public function prepare_items() {
78 $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
80 if ( 'post_tag' === $this->screen->taxonomy ) {
82 * Filter the number of terms displayed per page for the Tags list table.
86 * @param int $tags_per_page Number of tags to be displayed. Default 20.
88 $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
91 * Filter the number of terms displayed per page for the Tags list table.
94 * @deprecated 2.8.0 Use edit_tags_per_page instead.
96 * @param int $tags_per_page Number of tags to be displayed. Default 20.
98 $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page );
99 } elseif ( 'category' === $this->screen->taxonomy ) {
101 * Filter the number of terms displayed per page for the Categories list table.
105 * @param int $tags_per_page Number of categories to be displayed. Default 20.
107 $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
110 $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
114 'page' => $this->get_pagenum(),
115 'number' => $tags_per_page,
118 if ( !empty( $_REQUEST['orderby'] ) )
119 $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
121 if ( !empty( $_REQUEST['order'] ) )
122 $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
124 $this->callback_args = $args;
126 $this->set_pagination_args( array(
127 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
128 'per_page' => $tags_per_page,
136 public function has_items() {
137 // todo: populate $this->items in prepare_items()
144 public function no_items() {
145 echo get_taxonomy( $this->screen->taxonomy )->labels->not_found;
152 protected function get_bulk_actions() {
154 $actions['delete'] = __( 'Delete' );
163 public function current_action() {
164 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' === $_REQUEST['action'] || 'delete' === $_REQUEST['action2'] ) )
165 return 'bulk-delete';
167 return parent::current_action();
174 public function get_columns() {
176 'cb' => '<input type="checkbox" />',
177 'name' => _x( 'Name', 'term name' ),
178 'description' => __( 'Description' ),
179 'slug' => __( 'Slug' ),
182 if ( 'link_category' === $this->screen->taxonomy ) {
183 $columns['links'] = __( 'Links' );
185 $columns['posts'] = _x( 'Count', 'Number/count of items' );
195 protected function get_sortable_columns() {
198 'description' => 'description',
208 public function display_rows_or_placeholder() {
209 $taxonomy = $this->screen->taxonomy;
211 $args = wp_parse_args( $this->callback_args, array(
218 $page = $args['page'];
220 // Set variable because $args['number'] can be subsequently overridden.
221 $number = $args['number'];
223 $args['offset'] = $offset = ( $page - 1 ) * $number;
225 // Convert it to table rows.
228 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
229 // We'll need the full set of terms then.
230 $args['number'] = $args['offset'] = 0;
232 $terms = get_terms( $taxonomy, $args );
234 if ( empty( $terms ) || ! is_array( $terms ) ) {
235 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
241 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
242 if ( ! empty( $args['search'] ) ) {// Ignore children on searches.
245 $children = _get_term_hierarchy( $taxonomy );
247 // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
248 $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
250 foreach ( $terms as $term ) {
251 $this->single_row( $term );
257 * @param string $taxonomy
258 * @param array $terms
259 * @param array $children
261 * @param int $per_page
266 private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
268 $end = $start + $per_page;
270 foreach ( $terms as $key => $term ) {
272 if ( $count >= $end )
275 if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
278 // If the page starts in a subtree, print the parents.
279 if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
280 $my_parents = $parent_ids = array();
283 $my_parent = get_term( $p, $taxonomy );
284 $my_parents[] = $my_parent;
285 $p = $my_parent->parent;
286 if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
290 unset( $parent_ids );
292 $num_parents = count( $my_parents );
293 while ( $my_parent = array_pop( $my_parents ) ) {
295 $this->single_row( $my_parent, $level - $num_parents );
300 if ( $count >= $start ) {
302 $this->single_row( $term, $level );
307 unset( $terms[$key] );
309 if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
310 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
315 * @global string $taxonomy
319 public function single_row( $tag, $level = 0 ) {
321 $tag = sanitize_term( $tag, $taxonomy );
323 $this->level = $level;
325 echo '<tr id="tag-' . $tag->term_id . '">';
326 $this->single_row_columns( $tag );
334 public function column_cb( $tag ) {
335 $default_term = get_option( 'default_' . $this->screen->taxonomy );
337 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
338 return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
339 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
348 public function column_name( $tag ) {
349 $taxonomy = $this->screen->taxonomy;
351 $pad = str_repeat( '— ', max( 0, $this->level ) );
354 * Filter display of the term name in the terms list table.
356 * The default output may include padding due to the term's
357 * current level in the term hierarchy.
361 * @see WP_Terms_List_Table::column_name()
363 * @param string $pad_tag_name The term name, padded if not top-level.
364 * @param object $tag Term object.
366 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
368 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
370 $uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI'];
372 $edit_link = add_query_arg(
374 urlencode( wp_unslash( $uri ) ),
375 get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
379 '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong><br />',
380 esc_url( $edit_link ),
381 /* translators: %s: taxonomy term name */
382 esc_attr( sprintf( __( '“%s” (Edit)' ), $tag->name ) ),
386 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
387 $out .= '<div class="name">' . $qe_data->name . '</div>';
389 /** This filter is documented in wp-admin/edit-tag-form.php */
390 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '</div>';
391 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
397 * Gets the name of the default primary column.
402 * @return string Name of the default primary column, in this case, 'name'.
404 protected function get_default_primary_column_name() {
409 * Generates and displays row action links.
414 * @param object $tag Tag being acted upon.
415 * @param string $column_name Current column name.
416 * @param string $primary Primary column name.
417 * @return string Row actions output for terms.
419 protected function handle_row_actions( $tag, $column_name, $primary ) {
420 if ( $primary !== $column_name ) {
424 $taxonomy = $this->screen->taxonomy;
425 $tax = get_taxonomy( $taxonomy );
426 $default_term = get_option( 'default_' . $taxonomy );
428 $uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI'];
430 $edit_link = add_query_arg(
432 urlencode( wp_unslash( $uri ) ),
433 get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
437 if ( current_user_can( $tax->cap->edit_terms ) ) {
438 $actions['edit'] = sprintf(
439 '<a href="%s" aria-label="%s">%s</a>',
440 esc_url( $edit_link ),
441 /* translators: %s: taxonomy term name */
442 esc_attr( sprintf( __( 'Edit “%s”' ), $tag->name ) ),
445 $actions['inline hide-if-no-js'] = sprintf(
446 '<a href="#" class="editinline aria-button-if-js" aria-label="%s">%s</a>',
447 /* translators: %s: taxonomy term name */
448 esc_attr( sprintf( __( 'Quick edit “%s” inline' ), $tag->name ) ),
449 __( 'Quick Edit' )
452 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term ) {
453 $actions['delete'] = sprintf(
454 '<a href="%s" class="delete-tag aria-button-if-js" aria-label="%s">%s</a>',
455 wp_nonce_url( "edit-tags.php?action=delete&taxonomy=$taxonomy&tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ),
456 /* translators: %s: taxonomy term name */
457 esc_attr( sprintf( __( 'Delete “%s”' ), $tag->name ) ),
461 if ( $tax->public ) {
462 $actions['view'] = sprintf(
463 '<a href="%s" aria-label="%s">%s</a>',
464 get_term_link( $tag ),
465 /* translators: %s: taxonomy term name */
466 esc_attr( sprintf( __( 'View “%s” archive' ), $tag->name ) ),
472 * Filter the action links displayed for each term in the Tags list table.
475 * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
477 * @param array $actions An array of action links to be displayed. Default
478 * 'Edit', 'Quick Edit', 'Delete', and 'View'.
479 * @param object $tag Term object.
481 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
484 * Filter the action links displayed for each term in the terms list table.
486 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
490 * @param array $actions An array of action links to be displayed. Default
491 * 'Edit', 'Quick Edit', 'Delete', and 'View'.
492 * @param object $tag Term object.
494 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
496 return $this->row_actions( $actions );
503 public function column_description( $tag ) {
504 return $tag->description;
511 public function column_slug( $tag ) {
512 /** This filter is documented in wp-admin/edit-tag-form.php */
513 return apply_filters( 'editable_slug', $tag->slug, $tag );
520 public function column_posts( $tag ) {
521 $count = number_format_i18n( $tag->count );
523 $tax = get_taxonomy( $this->screen->taxonomy );
525 $ptype_object = get_post_type_object( $this->screen->post_type );
526 if ( ! $ptype_object->show_ui )
529 if ( $tax->query_var ) {
530 $args = array( $tax->query_var => $tag->slug );
532 $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
535 if ( 'post' != $this->screen->post_type )
536 $args['post_type'] = $this->screen->post_type;
538 if ( 'attachment' === $this->screen->post_type )
539 return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
541 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
548 public function column_links( $tag ) {
549 $count = number_format_i18n( $tag->count );
551 $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
557 * @param string $column_name
560 public function column_default( $tag, $column_name ) {
562 * Filter the displayed columns in the terms list table.
564 * The dynamic portion of the hook name, `$this->screen->taxonomy`,
565 * refers to the slug of the current taxonomy.
569 * @param string $string Blank string.
570 * @param string $column_name Name of the column.
571 * @param int $term_id Term ID.
573 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
577 * Outputs the hidden row displayed when inline editing
581 public function inline_edit() {
582 $tax = get_taxonomy( $this->screen->taxonomy );
584 if ( ! current_user_can( $tax->cap->edit_terms ) )
588 <form method="get"><table style="display: none"><tbody id="inlineedit">
589 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
592 <legend class="inline-edit-legend"><?php _e( 'Quick Edit' ); ?></legend>
593 <div class="inline-edit-col">
595 <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
596 <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
598 <?php if ( !global_terms_enabled() ) { ?>
600 <span class="title"><?php _e( 'Slug' ); ?></span>
601 <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
607 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
609 list( $columns ) = $this->get_column_info();
611 foreach ( $columns as $column_name => $column_display_name ) {
612 if ( isset( $core_columns[$column_name] ) )
615 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
616 do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
621 <p class="inline-edit-save submit">
622 <button type="button" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></button>
623 <button type="button" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></button>
624 <span class="spinner"></span>
625 <span class="error" style="display:none;"></span>
626 <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
627 <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
628 <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
632 </tbody></table></form>