3 * Terms List Table class.
6 * @subpackage List_Table
10 class WP_Terms_List_Table extends WP_List_Table {
12 public $callback_args;
22 * @see WP_List_Table::__construct() for more information on default arguments.
24 * @param array $args An associative array of arguments.
26 public function __construct( $args = array() ) {
27 global $post_type, $taxonomy, $action, $tax;
29 parent::__construct( array(
32 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
35 $action = $this->screen->action;
36 $post_type = $this->screen->post_type;
37 $taxonomy = $this->screen->taxonomy;
39 if ( empty( $taxonomy ) )
40 $taxonomy = 'post_tag';
42 if ( ! taxonomy_exists( $taxonomy ) )
43 wp_die( __( 'Invalid taxonomy' ) );
45 $tax = get_taxonomy( $taxonomy );
47 // @todo Still needed? Maybe just the show_ui part.
48 if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
53 public function ajax_user_can() {
54 return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
57 public function prepare_items() {
58 $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
60 if ( 'post_tag' == $this->screen->taxonomy ) {
62 * Filter the number of terms displayed per page for the Tags list table.
66 * @param int $tags_per_page Number of tags to be displayed. Default 20.
68 $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
71 * Filter the number of terms displayed per page for the Tags list table.
74 * @deprecated 2.8.0 Use edit_tags_per_page instead.
76 * @param int $tags_per_page Number of tags to be displayed. Default 20.
78 $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page );
79 } elseif ( 'category' == $this->screen->taxonomy ) {
81 * Filter the number of terms displayed per page for the Categories list table.
85 * @param int $tags_per_page Number of categories to be displayed. Default 20.
87 $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
90 $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
94 'page' => $this->get_pagenum(),
95 'number' => $tags_per_page,
98 if ( !empty( $_REQUEST['orderby'] ) )
99 $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
101 if ( !empty( $_REQUEST['order'] ) )
102 $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
104 $this->callback_args = $args;
106 $this->set_pagination_args( array(
107 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
108 'per_page' => $tags_per_page,
112 public function has_items() {
113 // todo: populate $this->items in prepare_items()
117 public function no_items() {
118 echo get_taxonomy( $this->screen->taxonomy )->labels->not_found;
121 protected function get_bulk_actions() {
123 $actions['delete'] = __( 'Delete' );
128 public function current_action() {
129 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
130 return 'bulk-delete';
132 return parent::current_action();
135 public function get_columns() {
137 'cb' => '<input type="checkbox" />',
138 'name' => _x( 'Name', 'term name' ),
139 'description' => __( 'Description' ),
140 'slug' => __( 'Slug' ),
143 if ( 'link_category' == $this->screen->taxonomy ) {
144 $columns['links'] = __( 'Links' );
146 $columns['posts'] = _x( 'Count', 'Number/count of items' );
152 protected function get_sortable_columns() {
155 'description' => 'description',
162 public function display_rows_or_placeholder() {
163 $taxonomy = $this->screen->taxonomy;
165 $args = wp_parse_args( $this->callback_args, array(
172 $page = $args['page'];
174 // Set variable because $args['number'] can be subsequently overridden.
175 $number = $args['number'];
177 $args['offset'] = $offset = ( $page - 1 ) * $number;
179 // Convert it to table rows.
182 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
183 // We'll need the full set of terms then.
184 $args['number'] = $args['offset'] = 0;
186 $terms = get_terms( $taxonomy, $args );
188 if ( empty( $terms ) ) {
189 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
195 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
196 if ( ! empty( $args['search'] ) ) {// Ignore children on searches.
199 $children = _get_term_hierarchy( $taxonomy );
201 // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
202 $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
204 $terms = get_terms( $taxonomy, $args );
205 foreach ( $terms as $term ) {
206 $this->single_row( $term );
212 * @param string $taxonomy
213 * @param array $terms
214 * @param array $children
216 * @param int $per_page
221 private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
223 $end = $start + $per_page;
225 foreach ( $terms as $key => $term ) {
227 if ( $count >= $end )
230 if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
233 // If the page starts in a subtree, print the parents.
234 if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
235 $my_parents = $parent_ids = array();
238 $my_parent = get_term( $p, $taxonomy );
239 $my_parents[] = $my_parent;
240 $p = $my_parent->parent;
241 if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
245 unset( $parent_ids );
247 $num_parents = count( $my_parents );
248 while ( $my_parent = array_pop( $my_parents ) ) {
250 $this->single_row( $my_parent, $level - $num_parents );
255 if ( $count >= $start ) {
257 $this->single_row( $term, $level );
262 unset( $terms[$key] );
264 if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
265 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
270 * @global string $taxonomy
274 public function single_row( $tag, $level = 0 ) {
276 $tag = sanitize_term( $tag, $taxonomy );
278 $this->level = $level;
280 echo '<tr id="tag-' . $tag->term_id . '">';
281 $this->single_row_columns( $tag );
289 public function column_cb( $tag ) {
290 $default_term = get_option( 'default_' . $this->screen->taxonomy );
292 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
293 return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
294 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
303 public function column_name( $tag ) {
304 $taxonomy = $this->screen->taxonomy;
305 $tax = get_taxonomy( $taxonomy );
307 $default_term = get_option( 'default_' . $taxonomy );
309 $pad = str_repeat( '— ', max( 0, $this->level ) );
312 * Filter display of the term name in the terms list table.
314 * The default output may include padding due to the term's
315 * current level in the term hierarchy.
319 * @see WP_Terms_List_Table::column_name()
321 * @param string $pad_tag_name The term name, padded if not top-level.
322 * @param object $tag Term object.
324 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
326 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
327 $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
329 $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit “%s”' ), $name ) ) . '">' . $name . '</a></strong><br />';
332 if ( current_user_can( $tax->cap->edit_terms ) ) {
333 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
334 $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick Edit' ) . '</a>';
336 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
337 $actions['delete'] = "<a class='delete-tag' href='" . wp_nonce_url( "edit-tags.php?action=delete&taxonomy=$taxonomy&tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ) . "'>" . __( 'Delete' ) . "</a>";
339 $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
342 * Filter the action links displayed for each term in the Tags list table.
345 * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
347 * @param array $actions An array of action links to be displayed. Default
348 * 'Edit', 'Quick Edit', 'Delete', and 'View'.
349 * @param object $tag Term object.
351 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
354 * Filter the action links displayed for each term in the terms list table.
356 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
360 * @param array $actions An array of action links to be displayed. Default
361 * 'Edit', 'Quick Edit', 'Delete', and 'View'.
362 * @param object $tag Term object.
364 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
366 $out .= $this->row_actions( $actions );
367 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
368 $out .= '<div class="name">' . $qe_data->name . '</div>';
370 /** This filter is documented in wp-admin/edit-tag-form.php */
371 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
372 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
381 public function column_description( $tag ) {
382 return $tag->description;
389 public function column_slug( $tag ) {
390 /** This filter is documented in wp-admin/edit-tag-form.php */
391 return apply_filters( 'editable_slug', $tag->slug );
398 public function column_posts( $tag ) {
399 $count = number_format_i18n( $tag->count );
401 $tax = get_taxonomy( $this->screen->taxonomy );
403 $ptype_object = get_post_type_object( $this->screen->post_type );
404 if ( ! $ptype_object->show_ui )
407 if ( $tax->query_var ) {
408 $args = array( $tax->query_var => $tag->slug );
410 $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
413 if ( 'post' != $this->screen->post_type )
414 $args['post_type'] = $this->screen->post_type;
416 if ( 'attachment' == $this->screen->post_type )
417 return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
419 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
426 public function column_links( $tag ) {
427 $count = number_format_i18n( $tag->count );
429 $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
435 * @param string $column_name
438 public function column_default( $tag, $column_name ) {
440 * Filter the displayed columns in the terms list table.
442 * The dynamic portion of the hook name, `$this->screen->taxonomy`,
443 * refers to the slug of the current taxonomy.
447 * @param string $string Blank string.
448 * @param string $column_name Name of the column.
449 * @param int $term_id Term ID.
451 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
455 * Outputs the hidden row displayed when inline editing
459 public function inline_edit() {
460 $tax = get_taxonomy( $this->screen->taxonomy );
462 if ( ! current_user_can( $tax->cap->edit_terms ) )
466 <form method="get"><table style="display: none"><tbody id="inlineedit">
467 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
469 <fieldset><div class="inline-edit-col">
470 <h4><?php _e( 'Quick Edit' ); ?></h4>
473 <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
474 <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
476 <?php if ( !global_terms_enabled() ) { ?>
478 <span class="title"><?php _e( 'Slug' ); ?></span>
479 <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
485 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
487 list( $columns ) = $this->get_column_info();
489 foreach ( $columns as $column_name => $column_display_name ) {
490 if ( isset( $core_columns[$column_name] ) )
493 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
494 do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
499 <p class="inline-edit-save submit">
500 <a href="#inline-edit" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
501 <a href="#inline-edit" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></a>
502 <span class="spinner"></span>
503 <span class="error" style="display:none;"></span>
504 <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
505 <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
506 <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
510 </tbody></table></form>