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 * Filters 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 * Filters 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 * Filters 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() {
155 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) ) {
156 $actions['delete'] = __( 'Delete' );
166 public function current_action() {
167 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' === $_REQUEST['action'] || 'delete' === $_REQUEST['action2'] ) )
168 return 'bulk-delete';
170 return parent::current_action();
177 public function get_columns() {
179 'cb' => '<input type="checkbox" />',
180 'name' => _x( 'Name', 'term name' ),
181 'description' => __( 'Description' ),
182 'slug' => __( 'Slug' ),
185 if ( 'link_category' === $this->screen->taxonomy ) {
186 $columns['links'] = __( 'Links' );
188 $columns['posts'] = _x( 'Count', 'Number/count of items' );
198 protected function get_sortable_columns() {
201 'description' => 'description',
211 public function display_rows_or_placeholder() {
212 $taxonomy = $this->screen->taxonomy;
214 $args = wp_parse_args( $this->callback_args, array(
221 $page = $args['page'];
223 // Set variable because $args['number'] can be subsequently overridden.
224 $number = $args['number'];
226 $args['offset'] = $offset = ( $page - 1 ) * $number;
228 // Convert it to table rows.
231 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
232 // We'll need the full set of terms then.
233 $args['number'] = $args['offset'] = 0;
235 $terms = get_terms( $taxonomy, $args );
237 if ( empty( $terms ) || ! is_array( $terms ) ) {
238 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
244 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
245 if ( ! empty( $args['search'] ) ) {// Ignore children on searches.
248 $children = _get_term_hierarchy( $taxonomy );
250 // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
251 $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
253 foreach ( $terms as $term ) {
254 $this->single_row( $term );
260 * @param string $taxonomy
261 * @param array $terms
262 * @param array $children
264 * @param int $per_page
269 private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
271 $end = $start + $per_page;
273 foreach ( $terms as $key => $term ) {
275 if ( $count >= $end )
278 if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
281 // If the page starts in a subtree, print the parents.
282 if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
283 $my_parents = $parent_ids = array();
286 $my_parent = get_term( $p, $taxonomy );
287 $my_parents[] = $my_parent;
288 $p = $my_parent->parent;
289 if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
293 unset( $parent_ids );
295 $num_parents = count( $my_parents );
296 while ( $my_parent = array_pop( $my_parents ) ) {
298 $this->single_row( $my_parent, $level - $num_parents );
303 if ( $count >= $start ) {
305 $this->single_row( $term, $level );
310 unset( $terms[$key] );
312 if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
313 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
318 * @global string $taxonomy
319 * @param WP_Term $tag Term object.
322 public function single_row( $tag, $level = 0 ) {
324 $tag = sanitize_term( $tag, $taxonomy );
326 $this->level = $level;
328 echo '<tr id="tag-' . $tag->term_id . '">';
329 $this->single_row_columns( $tag );
334 * @param WP_Term $tag Term object.
337 public function column_cb( $tag ) {
338 if ( current_user_can( 'delete_term', $tag->term_id ) ) {
339 return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
340 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
347 * @param WP_Term $tag Term object.
350 public function column_name( $tag ) {
351 $taxonomy = $this->screen->taxonomy;
353 $pad = str_repeat( '— ', max( 0, $this->level ) );
356 * Filters display of the term name in the terms list table.
358 * The default output may include padding due to the term's
359 * current level in the term hierarchy.
363 * @see WP_Terms_List_Table::column_name()
365 * @param string $pad_tag_name The term name, padded if not top-level.
366 * @param WP_Term $tag Term object.
368 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
370 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
372 $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
374 $edit_link = add_query_arg(
376 urlencode( wp_unslash( $uri ) ),
377 get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
381 '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong><br />',
382 esc_url( $edit_link ),
383 /* translators: %s: taxonomy term name */
384 esc_attr( sprintf( __( '“%s” (Edit)' ), $tag->name ) ),
388 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
389 $out .= '<div class="name">' . $qe_data->name . '</div>';
391 /** This filter is documented in wp-admin/edit-tag-form.php */
392 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '</div>';
393 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
399 * Gets the name of the default primary column.
404 * @return string Name of the default primary column, in this case, 'name'.
406 protected function get_default_primary_column_name() {
411 * Generates and displays row action links.
416 * @param WP_Term $tag Tag being acted upon.
417 * @param string $column_name Current column name.
418 * @param string $primary Primary column name.
419 * @return string Row actions output for terms.
421 protected function handle_row_actions( $tag, $column_name, $primary ) {
422 if ( $primary !== $column_name ) {
426 $taxonomy = $this->screen->taxonomy;
427 $tax = get_taxonomy( $taxonomy );
428 $uri = wp_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( 'edit_term', $tag->term_id ) ) {
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( 'delete_term', $tag->term_id ) ) {
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 * Filters 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 WP_Term $tag Term object.
481 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
484 * Filters 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 WP_Term $tag Term object.
494 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
496 return $this->row_actions( $actions );
500 * @param WP_Term $tag Term object.
503 public function column_description( $tag ) {
504 return $tag->description;
508 * @param WP_Term $tag Term object.
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 );
517 * @param WP_Term $tag Term object.
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>";
545 * @param WP_Term $tag Term object.
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>";
556 * @param WP_Term $tag Term object.
557 * @param string $column_name
560 public function column_default( $tag, $column_name ) {
562 * Filters 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 alignleft"><?php _e( 'Cancel' ); ?></button>
623 <button type="button" class="save button 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>