3 * Terms List Table class.
6 * @subpackage List_Table
10 class WP_Terms_List_Table extends WP_List_Table {
14 function __construct( $args = array() ) {
15 global $post_type, $taxonomy, $action, $tax;
17 parent::__construct( array(
20 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
23 $action = $this->screen->action;
24 $post_type = $this->screen->post_type;
25 $taxonomy = $this->screen->taxonomy;
27 if ( empty( $taxonomy ) )
28 $taxonomy = 'post_tag';
30 if ( ! taxonomy_exists( $taxonomy ) )
31 wp_die( __( 'Invalid taxonomy' ) );
33 $tax = get_taxonomy( $taxonomy );
35 // @todo Still needed? Maybe just the show_ui part.
36 if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
41 function ajax_user_can() {
42 return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
45 function prepare_items() {
46 $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
48 if ( 'post_tag' == $this->screen->taxonomy ) {
49 $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
50 $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page ); // Old filter
51 } elseif ( 'category' == $this->screen->taxonomy ) {
52 $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); // Old filter
55 $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
59 'page' => $this->get_pagenum(),
60 'number' => $tags_per_page,
63 if ( !empty( $_REQUEST['orderby'] ) )
64 $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
66 if ( !empty( $_REQUEST['order'] ) )
67 $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
69 $this->callback_args = $args;
71 $this->set_pagination_args( array(
72 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
73 'per_page' => $tags_per_page,
77 function has_items() {
78 // todo: populate $this->items in prepare_items()
82 function get_bulk_actions() {
84 $actions['delete'] = __( 'Delete' );
89 function current_action() {
90 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
93 return parent::current_action();
96 function get_columns() {
98 'cb' => '<input type="checkbox" />',
99 'name' => _x( 'Name', 'term name' ),
100 'description' => __( 'Description' ),
101 'slug' => __( 'Slug' ),
104 if ( 'link_category' == $this->screen->taxonomy ) {
105 $columns['links'] = __( 'Links' );
107 $post_type_object = get_post_type_object( $this->screen->post_type );
108 $columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' );
114 function get_sortable_columns() {
117 'description' => 'description',
124 function display_rows_or_placeholder() {
125 $taxonomy = $this->screen->taxonomy;
127 $args = wp_parse_args( $this->callback_args, array(
134 extract( $args, EXTR_SKIP );
136 $args['offset'] = $offset = ( $page - 1 ) * $number;
138 // convert it to table rows
143 if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
144 // We'll need the full set of terms then.
145 $args['number'] = $args['offset'] = 0;
147 $terms = get_terms( $taxonomy, $args );
149 if ( empty( $terms ) ) {
150 list( $columns, $hidden ) = $this->get_column_info();
151 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
157 if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
158 if ( !empty( $search ) ) // Ignore children on searches.
161 $children = _get_term_hierarchy( $taxonomy );
163 // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
164 $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
166 $terms = get_terms( $taxonomy, $args );
167 foreach ( $terms as $term )
168 $this->single_row( $term );
169 $count = $number; // Only displaying a single page.
173 function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
175 $end = $start + $per_page;
177 foreach ( $terms as $key => $term ) {
179 if ( $count >= $end )
182 if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
185 // If the page starts in a subtree, print the parents.
186 if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
187 $my_parents = $parent_ids = array();
190 $my_parent = get_term( $p, $taxonomy );
191 $my_parents[] = $my_parent;
192 $p = $my_parent->parent;
193 if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
197 unset( $parent_ids );
199 $num_parents = count( $my_parents );
200 while ( $my_parent = array_pop( $my_parents ) ) {
202 $this->single_row( $my_parent, $level - $num_parents );
207 if ( $count >= $start ) {
209 $this->single_row( $term, $level );
214 unset( $terms[$key] );
216 if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
217 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
221 function single_row( $tag, $level = 0 ) {
222 static $row_class = '';
223 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
225 $this->level = $level;
227 echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
228 $this->single_row_columns( $tag );
232 function column_cb( $tag ) {
233 $default_term = get_option( 'default_' . $this->screen->taxonomy );
235 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
236 return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
237 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
242 function column_name( $tag ) {
243 $taxonomy = $this->screen->taxonomy;
244 $tax = get_taxonomy( $taxonomy );
246 $default_term = get_option( 'default_' . $taxonomy );
248 $pad = str_repeat( '— ', max( 0, $this->level ) );
249 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
250 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
251 $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
253 $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit “%s”' ), $name ) ) . '">' . $name . '</a></strong><br />';
256 if ( current_user_can( $tax->cap->edit_terms ) ) {
257 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
258 $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick Edit' ) . '</a>';
260 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
261 $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>";
263 $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
265 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
266 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
268 $out .= $this->row_actions( $actions );
269 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
270 $out .= '<div class="name">' . $qe_data->name . '</div>';
271 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
272 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
277 function column_description( $tag ) {
278 return $tag->description;
281 function column_slug( $tag ) {
282 return apply_filters( 'editable_slug', $tag->slug );
285 function column_posts( $tag ) {
286 $count = number_format_i18n( $tag->count );
288 $tax = get_taxonomy( $this->screen->taxonomy );
290 $ptype_object = get_post_type_object( $this->screen->post_type );
291 if ( ! $ptype_object->show_ui )
294 if ( $tax->query_var ) {
295 $args = array( $tax->query_var => $tag->slug );
297 $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
300 if ( 'post' != $this->screen->post_type )
301 $args['post_type'] = $this->screen->post_type;
303 if ( 'attachment' == $this->screen->post_type )
304 return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
306 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
309 function column_links( $tag ) {
310 $count = number_format_i18n( $tag->count );
312 $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
316 function column_default( $tag, $column_name ) {
317 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
321 * Outputs the hidden row displayed when inline editing
325 function inline_edit() {
326 $tax = get_taxonomy( $this->screen->taxonomy );
328 if ( ! current_user_can( $tax->cap->edit_terms ) )
332 <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
333 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
335 <fieldset><div class="inline-edit-col">
336 <h4><?php _e( 'Quick Edit' ); ?></h4>
339 <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
340 <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
342 <?php if ( !global_terms_enabled() ) { ?>
344 <span class="title"><?php _e( 'Slug' ); ?></span>
345 <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
351 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
353 list( $columns ) = $this->get_column_info();
355 foreach ( $columns as $column_name => $column_display_name ) {
356 if ( isset( $core_columns[$column_name] ) )
359 do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
364 <p class="inline-edit-save submit">
365 <a accesskey="c" href="#inline-edit" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
366 <a accesskey="s" href="#inline-edit" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></a>
367 <span class="spinner"></span>
368 <span class="error" style="display:none;"></span>
369 <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
370 <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
371 <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
375 </tbody></table></form>