3 * Terms List Table class.
6 * @subpackage List_Table
10 class WP_Terms_List_Table extends WP_List_Table {
12 public $callback_args;
20 * @see WP_List_Table::__construct() for more information on default arguments.
22 * @param array $args An associative array of arguments.
24 public function __construct( $args = array() ) {
25 global $post_type, $taxonomy, $action, $tax;
27 parent::__construct( array(
30 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
33 $action = $this->screen->action;
34 $post_type = $this->screen->post_type;
35 $taxonomy = $this->screen->taxonomy;
37 if ( empty( $taxonomy ) )
38 $taxonomy = 'post_tag';
40 if ( ! taxonomy_exists( $taxonomy ) )
41 wp_die( __( 'Invalid taxonomy' ) );
43 $tax = get_taxonomy( $taxonomy );
45 // @todo Still needed? Maybe just the show_ui part.
46 if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
51 public function ajax_user_can() {
52 return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
55 public function prepare_items() {
56 $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
58 if ( 'post_tag' == $this->screen->taxonomy ) {
60 * Filter the number of terms displayed per page for the Tags list table.
64 * @param int $tags_per_page Number of tags to be displayed. Default 20.
66 $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
69 * Filter the number of terms displayed per page for the Tags list table.
72 * @deprecated 2.8.0 Use edit_tags_per_page instead.
74 * @param int $tags_per_page Number of tags to be displayed. Default 20.
76 $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page );
77 } elseif ( 'category' == $this->screen->taxonomy ) {
79 * Filter the number of terms displayed per page for the Categories list table.
83 * @param int $tags_per_page Number of categories to be displayed. Default 20.
85 $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
88 $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
92 'page' => $this->get_pagenum(),
93 'number' => $tags_per_page,
96 if ( !empty( $_REQUEST['orderby'] ) )
97 $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
99 if ( !empty( $_REQUEST['order'] ) )
100 $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
102 $this->callback_args = $args;
104 $this->set_pagination_args( array(
105 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
106 'per_page' => $tags_per_page,
110 public function has_items() {
111 // todo: populate $this->items in prepare_items()
115 protected function get_bulk_actions() {
117 $actions['delete'] = __( 'Delete' );
122 public function current_action() {
123 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
124 return 'bulk-delete';
126 return parent::current_action();
129 public function get_columns() {
131 'cb' => '<input type="checkbox" />',
132 'name' => _x( 'Name', 'term name' ),
133 'description' => __( 'Description' ),
134 'slug' => __( 'Slug' ),
137 if ( 'link_category' == $this->screen->taxonomy ) {
138 $columns['links'] = __( 'Links' );
140 $columns['posts'] = _x( 'Count', 'Number/count of items' );
146 protected function get_sortable_columns() {
149 'description' => 'description',
156 public function display_rows_or_placeholder() {
157 $taxonomy = $this->screen->taxonomy;
159 $args = wp_parse_args( $this->callback_args, array(
166 $page = $args['page'];
168 // Set variable because $args['number'] can be subsequently overridden.
169 $number = $args['number'];
171 $args['offset'] = $offset = ( $page - 1 ) * $number;
173 // Convert it to table rows.
176 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
177 // We'll need the full set of terms then.
178 $args['number'] = $args['offset'] = 0;
180 $terms = get_terms( $taxonomy, $args );
182 if ( empty( $terms ) ) {
183 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
189 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
190 if ( ! empty( $args['search'] ) ) {// Ignore children on searches.
193 $children = _get_term_hierarchy( $taxonomy );
195 // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
196 $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
198 $terms = get_terms( $taxonomy, $args );
199 foreach ( $terms as $term ) {
200 $this->single_row( $term );
205 private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
207 $end = $start + $per_page;
209 foreach ( $terms as $key => $term ) {
211 if ( $count >= $end )
214 if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
217 // If the page starts in a subtree, print the parents.
218 if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
219 $my_parents = $parent_ids = array();
222 $my_parent = get_term( $p, $taxonomy );
223 $my_parents[] = $my_parent;
224 $p = $my_parent->parent;
225 if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
229 unset( $parent_ids );
231 $num_parents = count( $my_parents );
232 while ( $my_parent = array_pop( $my_parents ) ) {
234 $this->single_row( $my_parent, $level - $num_parents );
239 if ( $count >= $start ) {
241 $this->single_row( $term, $level );
246 unset( $terms[$key] );
248 if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
249 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
253 public function single_row( $tag, $level = 0 ) {
255 $tag = sanitize_term( $tag, $taxonomy );
257 static $row_class = '';
258 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
260 $this->level = $level;
262 echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
263 $this->single_row_columns( $tag );
267 public function column_cb( $tag ) {
268 $default_term = get_option( 'default_' . $this->screen->taxonomy );
270 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
271 return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
272 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
277 public function column_name( $tag ) {
278 $taxonomy = $this->screen->taxonomy;
279 $tax = get_taxonomy( $taxonomy );
281 $default_term = get_option( 'default_' . $taxonomy );
283 $pad = str_repeat( '— ', max( 0, $this->level ) );
286 * Filter display of the term name in the terms list table.
288 * The default output may include padding due to the term's
289 * current level in the term hierarchy.
293 * @see WP_Terms_List_Table::column_name()
295 * @param string $pad_tag_name The term name, padded if not top-level.
296 * @param object $tag Term object.
298 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
300 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
301 $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
303 $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit “%s”' ), $name ) ) . '">' . $name . '</a></strong><br />';
306 if ( current_user_can( $tax->cap->edit_terms ) ) {
307 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
308 $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick Edit' ) . '</a>';
310 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
311 $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>";
313 $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
316 * Filter the action links displayed for each term in the Tags list table.
319 * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
321 * @param array $actions An array of action links to be displayed. Default
322 * 'Edit', 'Quick Edit', 'Delete', and 'View'.
323 * @param object $tag Term object.
325 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
328 * Filter the action links displayed for each term in the terms list table.
330 * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
334 * @param array $actions An array of action links to be displayed. Default
335 * 'Edit', 'Quick Edit', 'Delete', and 'View'.
336 * @param object $tag Term object.
338 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
340 $out .= $this->row_actions( $actions );
341 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
342 $out .= '<div class="name">' . $qe_data->name . '</div>';
344 /** This filter is documented in wp-admin/edit-tag-form.php */
345 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
346 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
351 public function column_description( $tag ) {
352 return $tag->description;
355 public function column_slug( $tag ) {
356 /** This filter is documented in wp-admin/edit-tag-form.php */
357 return apply_filters( 'editable_slug', $tag->slug );
360 public function column_posts( $tag ) {
361 $count = number_format_i18n( $tag->count );
363 $tax = get_taxonomy( $this->screen->taxonomy );
365 $ptype_object = get_post_type_object( $this->screen->post_type );
366 if ( ! $ptype_object->show_ui )
369 if ( $tax->query_var ) {
370 $args = array( $tax->query_var => $tag->slug );
372 $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
375 if ( 'post' != $this->screen->post_type )
376 $args['post_type'] = $this->screen->post_type;
378 if ( 'attachment' == $this->screen->post_type )
379 return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
381 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
384 public function column_links( $tag ) {
385 $count = number_format_i18n( $tag->count );
387 $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
391 public function column_default( $tag, $column_name ) {
393 * Filter the displayed columns in the terms list table.
395 * The dynamic portion of the hook name, $this->screen->taxonomy,
396 * refers to the slug of the current taxonomy.
400 * @param string $string Blank string.
401 * @param string $column_name Name of the column.
402 * @param int $term_id Term ID.
404 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
408 * Outputs the hidden row displayed when inline editing
412 public function inline_edit() {
413 $tax = get_taxonomy( $this->screen->taxonomy );
415 if ( ! current_user_can( $tax->cap->edit_terms ) )
419 <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
420 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
422 <fieldset><div class="inline-edit-col">
423 <h4><?php _e( 'Quick Edit' ); ?></h4>
426 <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
427 <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
429 <?php if ( !global_terms_enabled() ) { ?>
431 <span class="title"><?php _e( 'Slug' ); ?></span>
432 <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
438 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
440 list( $columns ) = $this->get_column_info();
442 foreach ( $columns as $column_name => $column_display_name ) {
443 if ( isset( $core_columns[$column_name] ) )
446 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
447 do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
452 <p class="inline-edit-save submit">
453 <a accesskey="c" href="#inline-edit" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
454 <a accesskey="s" href="#inline-edit" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></a>
455 <span class="spinner"></span>
456 <span class="error" style="display:none;"></span>
457 <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
458 <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
459 <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
463 </tbody></table></form>