3 * Terms List Table class.
6 * @subpackage List_Table
10 class WP_Terms_List_Table extends WP_List_Table {
14 function __construct() {
15 global $post_type, $taxonomy, $tax;
17 wp_reset_vars( array( 'action', 'taxonomy', 'post_type' ) );
19 if ( empty( $taxonomy ) )
20 $taxonomy = 'post_tag';
22 if ( !taxonomy_exists( $taxonomy ) )
23 wp_die( __( 'Invalid taxonomy' ) );
25 $tax = get_taxonomy( $taxonomy );
27 if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
30 parent::__construct( array(
36 function ajax_user_can() {
39 return current_user_can( $tax->cap->manage_terms );
42 function prepare_items() {
45 $tags_per_page = $this->get_items_per_page( 'edit_' . $taxonomy . '_per_page' );
47 if ( 'post_tag' == $taxonomy ) {
48 $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
49 $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page ); // Old filter
50 } elseif ( 'category' == $taxonomy ) {
51 $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); // Old filter
54 $search = !empty( $_REQUEST['s'] ) ? trim( stripslashes( $_REQUEST['s'] ) ) : '';
58 'page' => $this->get_pagenum(),
59 'number' => $tags_per_page,
62 if ( !empty( $_REQUEST['orderby'] ) )
63 $args['orderby'] = trim( stripslashes( $_REQUEST['orderby'] ) );
65 if ( !empty( $_REQUEST['order'] ) )
66 $args['order'] = trim( stripslashes( $_REQUEST['order'] ) );
68 $this->callback_args = $args;
70 $this->set_pagination_args( array(
71 'total_items' => wp_count_terms( $taxonomy, compact( 'search' ) ),
72 'per_page' => $tags_per_page,
76 function has_items() {
77 // todo: populate $this->items in prepare_items()
81 function get_bulk_actions() {
83 $actions['delete'] = __( 'Delete' );
88 function current_action() {
89 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
92 return parent::current_action();
95 function get_columns() {
96 global $taxonomy, $post_type;
99 'cb' => '<input type="checkbox" />',
100 'name' => _x( 'Name', 'term name' ),
101 'description' => __( 'Description' ),
102 'slug' => __( 'Slug' ),
105 if ( 'link_category' == $taxonomy ) {
106 $columns['links'] = __( 'Links' );
108 $post_type_object = get_post_type_object( $post_type );
109 $columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' );
115 function get_sortable_columns() {
118 'description' => 'description',
125 function display_rows_or_placeholder() {
128 $args = wp_parse_args( $this->callback_args, array(
135 extract( $args, EXTR_SKIP );
137 $args['offset'] = $offset = ( $page - 1 ) * $number;
139 // convert it to table rows
145 if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
146 // We'll need the full set of terms then.
147 $args['number'] = $args['offset'] = 0;
149 $terms = get_terms( $taxonomy, $args );
150 if ( !empty( $search ) ) // Ignore children on searches.
153 $children = _get_term_hierarchy( $taxonomy );
155 // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
156 $out .= $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
158 $terms = get_terms( $taxonomy, $args );
159 foreach ( $terms as $term )
160 $out .= $this->single_row( $term, 0, $taxonomy );
161 $count = $number; // Only displaying a single page.
164 if ( empty( $terms ) ) {
165 list( $columns, $hidden ) = $this->get_column_info();
166 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
174 function _rows( $taxonomy, $terms, &$children, $start = 0, $per_page = 20, &$count, $parent = 0, $level = 0 ) {
176 $end = $start + $per_page;
179 foreach ( $terms as $key => $term ) {
181 if ( $count >= $end )
184 if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
187 // If the page starts in a subtree, print the parents.
188 if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
189 $my_parents = $parent_ids = array();
192 $my_parent = get_term( $p, $taxonomy );
193 $my_parents[] = $my_parent;
194 $p = $my_parent->parent;
195 if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
199 unset( $parent_ids );
201 $num_parents = count( $my_parents );
202 while ( $my_parent = array_pop( $my_parents ) ) {
203 $output .= "\t" . $this->single_row( $my_parent, $level - $num_parents, $taxonomy );
208 if ( $count >= $start )
209 $output .= "\t" . $this->single_row( $term, $level, $taxonomy );
213 unset( $terms[$key] );
215 if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
216 $output .= $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
222 function single_row( $tag, $level = 0 ) {
223 static $row_class = '';
224 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
226 $this->level = $level;
228 echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
229 echo $this->single_row_columns( $tag );
233 function column_cb( $tag ) {
234 global $taxonomy, $tax;
236 $default_term = get_option( 'default_' . $taxonomy );
238 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
239 return '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" />';
244 function column_name( $tag ) {
245 global $taxonomy, $tax, $post_type;
247 $default_term = get_option( 'default_' . $taxonomy );
249 $pad = str_repeat( '— ', max( 0, $this->level ) );
250 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
251 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
252 $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $post_type ) );
254 $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit “%s”' ), $name ) ) . '">' . $name . '</a></strong><br />';
257 if ( current_user_can( $tax->cap->edit_terms ) ) {
258 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
259 $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick Edit' ) . '</a>';
261 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
262 $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 global $taxonomy, $post_type;
288 $count = number_format_i18n( $tag->count );
290 $tax = get_taxonomy( $taxonomy );
292 $ptype_object = get_post_type_object( $post_type );
293 if ( ! $ptype_object->show_ui )
296 if ( $tax->query_var ) {
297 $args = array( $tax->query_var => $tag->slug );
299 $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
302 if ( 'post' != $post_type )
303 $args['post_type'] = $post_type;
305 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
308 function column_links( $tag ) {
309 $count = number_format_i18n( $tag->count );
311 $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
315 function column_default( $tag, $column_name ) {
316 $screen = get_current_screen();
318 return apply_filters( "manage_{$screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
322 * Outputs the hidden row displayed when inline editing
326 function inline_edit() {
327 global $post_type, $tax;
329 if ( ! current_user_can( $tax->cap->edit_terms ) )
333 <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
334 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
336 <fieldset><div class="inline-edit-col">
337 <h4><?php _e( 'Quick Edit' ); ?></h4>
340 <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
341 <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
343 <?php if ( !global_terms_enabled() ) { ?>
345 <span class="title"><?php _e( 'Slug' ); ?></span>
346 <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
352 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
354 list( $columns ) = $this->get_column_info();
356 foreach ( $columns as $column_name => $column_display_name ) {
357 if ( isset( $core_columns[$column_name] ) )
360 do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $tax->name );
365 <p class="inline-edit-save submit">
366 <a accesskey="c" href="#inline-edit" title="<?php esc_attr_e( 'Cancel' ); ?>" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
367 <?php $update_text = $tax->labels->update_item; ?>
368 <a accesskey="s" href="#inline-edit" title="<?php echo esc_attr( $update_text ); ?>" class="save button-primary alignright"><?php echo $update_text; ?></a>
369 <img class="waiting" style="display:none;" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
370 <span class="error" style="display:none;"></span>
371 <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
372 <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $tax->name ); ?>" />
373 <input type="hidden" name="post_type" value="<?php echo esc_attr( $post_type ); ?>" />
377 </tbody></table></form>