web/wp-admin/includes/class-wp-terms-list-table.php
changeset 194 32102edaa81b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 /**
       
     3  * Terms List Table class.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage List_Table
       
     7  * @since 3.1.0
       
     8  * @access private
       
     9  */
       
    10 class WP_Terms_List_Table extends WP_List_Table {
       
    11 
       
    12 	var $callback_args;
       
    13 
       
    14 	function __construct() {
       
    15 		global $post_type, $taxonomy, $tax;
       
    16 
       
    17 		wp_reset_vars( array( 'action', 'taxonomy', 'post_type' ) );
       
    18 
       
    19 		if ( empty( $taxonomy ) )
       
    20 			$taxonomy = 'post_tag';
       
    21 
       
    22 		if ( !taxonomy_exists( $taxonomy ) )
       
    23 			wp_die( __( 'Invalid taxonomy' ) );
       
    24 
       
    25 		$tax = get_taxonomy( $taxonomy );
       
    26 
       
    27 		if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
       
    28 			$post_type = 'post';
       
    29 
       
    30 		parent::__construct( array(
       
    31 			'plural' => 'tags',
       
    32 			'singular' => 'tag',
       
    33 		) );
       
    34 	}
       
    35 
       
    36 	function ajax_user_can() {
       
    37 		global $tax;
       
    38 
       
    39 		return current_user_can( $tax->cap->manage_terms );
       
    40 	}
       
    41 
       
    42 	function prepare_items() {
       
    43 		global $taxonomy;
       
    44 
       
    45 		$tags_per_page = $this->get_items_per_page( 'edit_' . $taxonomy . '_per_page' );
       
    46 
       
    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
       
    52 		}
       
    53 
       
    54 		$search = !empty( $_REQUEST['s'] ) ? trim( stripslashes( $_REQUEST['s'] ) ) : '';
       
    55 
       
    56 		$args = array(
       
    57 			'search' => $search,
       
    58 			'page' => $this->get_pagenum(),
       
    59 			'number' => $tags_per_page,
       
    60 		);
       
    61 
       
    62 		if ( !empty( $_REQUEST['orderby'] ) )
       
    63 			$args['orderby'] = trim( stripslashes( $_REQUEST['orderby'] ) );
       
    64 
       
    65 		if ( !empty( $_REQUEST['order'] ) )
       
    66 			$args['order'] = trim( stripslashes( $_REQUEST['order'] ) );
       
    67 
       
    68 		$this->callback_args = $args;
       
    69 
       
    70 		$this->set_pagination_args( array(
       
    71 			'total_items' => wp_count_terms( $taxonomy, compact( 'search' ) ),
       
    72 			'per_page' => $tags_per_page,
       
    73 		) );
       
    74 	}
       
    75 
       
    76 	function has_items() {
       
    77 		// todo: populate $this->items in prepare_items()
       
    78 		return true;
       
    79 	}
       
    80 
       
    81 	function get_bulk_actions() {
       
    82 		$actions = array();
       
    83 		$actions['delete'] = __( 'Delete' );
       
    84 
       
    85 		return $actions;
       
    86 	}
       
    87 
       
    88 	function current_action() {
       
    89 		if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
       
    90 			return 'bulk-delete';
       
    91 
       
    92 		return parent::current_action();
       
    93 	}
       
    94 
       
    95 	function get_columns() {
       
    96 		global $taxonomy, $post_type;
       
    97 
       
    98 		$columns = array(
       
    99 			'cb'          => '<input type="checkbox" />',
       
   100 			'name'        => _x( 'Name', 'term name' ),
       
   101 			'description' => __( 'Description' ),
       
   102 			'slug'        => __( 'Slug' ),
       
   103 		);
       
   104 
       
   105 		if ( 'link_category' == $taxonomy ) {
       
   106 			$columns['links'] = __( 'Links' );
       
   107 		} else {
       
   108 			$post_type_object = get_post_type_object( $post_type );
       
   109 			$columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' );
       
   110 		}
       
   111 
       
   112 		return $columns;
       
   113 	}
       
   114 
       
   115 	function get_sortable_columns() {
       
   116 		return array(
       
   117 			'name'        => 'name',
       
   118 			'description' => 'description',
       
   119 			'slug'        => 'slug',
       
   120 			'posts'       => 'count',
       
   121 			'links'       => 'count'
       
   122 		);
       
   123 	}
       
   124 
       
   125 	function display_rows_or_placeholder() {
       
   126 		global $taxonomy;
       
   127 
       
   128 		$args = wp_parse_args( $this->callback_args, array(
       
   129 			'page' => 1,
       
   130 			'number' => 20,
       
   131 			'search' => '',
       
   132 			'hide_empty' => 0
       
   133 		) );
       
   134 
       
   135 		extract( $args, EXTR_SKIP );
       
   136 
       
   137 		$args['offset'] = $offset = ( $page - 1 ) * $number;
       
   138 
       
   139 		// convert it to table rows
       
   140 		$out = '';
       
   141 		$count = 0;
       
   142 
       
   143 		$terms = array();
       
   144 
       
   145 		if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
       
   146 			// We'll need the full set of terms then.
       
   147 			$args['number'] = $args['offset'] = 0;
       
   148 
       
   149 			$terms = get_terms( $taxonomy, $args );
       
   150 			if ( !empty( $search ) ) // Ignore children on searches.
       
   151 				$children = array();
       
   152 			else
       
   153 				$children = _get_term_hierarchy( $taxonomy );
       
   154 
       
   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 );
       
   157 		} else {
       
   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.
       
   162 		}
       
   163 
       
   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() . '">';
       
   167 			$this->no_items();
       
   168 			echo '</td></tr>';
       
   169 		} else {
       
   170 			echo $out;
       
   171 		}
       
   172 	}
       
   173 
       
   174 	function _rows( $taxonomy, $terms, &$children, $start = 0, $per_page = 20, &$count, $parent = 0, $level = 0 ) {
       
   175 
       
   176 		$end = $start + $per_page;
       
   177 
       
   178 		$output = '';
       
   179 		foreach ( $terms as $key => $term ) {
       
   180 
       
   181 			if ( $count >= $end )
       
   182 				break;
       
   183 
       
   184 			if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
       
   185 				continue;
       
   186 
       
   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();
       
   190 				$p = $term->parent;
       
   191 				while ( $p ) {
       
   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.
       
   196 						break;
       
   197 					$parent_ids[] = $p;
       
   198 				}
       
   199 				unset( $parent_ids );
       
   200 
       
   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 );
       
   204 					$num_parents--;
       
   205 				}
       
   206 			}
       
   207 
       
   208 			if ( $count >= $start )
       
   209 				$output .= "\t" . $this->single_row( $term, $level, $taxonomy );
       
   210 
       
   211 			++$count;
       
   212 
       
   213 			unset( $terms[$key] );
       
   214 
       
   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 );
       
   217 		}
       
   218 
       
   219 		return $output;
       
   220 	}
       
   221 
       
   222 	function single_row( $tag, $level = 0 ) {
       
   223 		static $row_class = '';
       
   224 		$row_class = ( $row_class == '' ? ' class="alternate"' : '' );
       
   225 
       
   226 		$this->level = $level;
       
   227 
       
   228 		echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
       
   229 		echo $this->single_row_columns( $tag );
       
   230 		echo '</tr>';
       
   231 	}
       
   232 
       
   233 	function column_cb( $tag ) {
       
   234 		global $taxonomy, $tax;
       
   235 
       
   236 		$default_term = get_option( 'default_' . $taxonomy );
       
   237 
       
   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 . '" />';
       
   240 		else
       
   241 			return '&nbsp;';
       
   242 	}
       
   243 
       
   244 	function column_name( $tag ) {
       
   245 		global $taxonomy, $tax, $post_type;
       
   246 
       
   247 		$default_term = get_option( 'default_' . $taxonomy );
       
   248 
       
   249 		$pad = str_repeat( '&#8212; ', 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 ) );
       
   253 
       
   254 		$out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $name ) ) . '">' . $name . '</a></strong><br />';
       
   255 
       
   256 		$actions = array();
       
   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&nbsp;Edit' ) . '</a>';
       
   260 		}
       
   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&amp;taxonomy=$taxonomy&amp;tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ) . "'>" . __( 'Delete' ) . "</a>";
       
   263 		$actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
       
   264 
       
   265 		$actions = apply_filters( 'tag_row_actions', $actions, $tag );
       
   266 		$actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
       
   267 
       
   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>';
       
   273 
       
   274 		return $out;
       
   275 	}
       
   276 
       
   277 	function column_description( $tag ) {
       
   278 		return $tag->description;
       
   279 	}
       
   280 
       
   281 	function column_slug( $tag ) {
       
   282 		return apply_filters( 'editable_slug', $tag->slug );
       
   283 	}
       
   284 
       
   285 	function column_posts( $tag ) {
       
   286 		global $taxonomy, $post_type;
       
   287 
       
   288 		$count = number_format_i18n( $tag->count );
       
   289 
       
   290 		$tax = get_taxonomy( $taxonomy );
       
   291 
       
   292 		$ptype_object = get_post_type_object( $post_type );
       
   293 		if ( ! $ptype_object->show_ui )
       
   294 			return $count;
       
   295 
       
   296 		if ( $tax->query_var ) {
       
   297 			$args = array( $tax->query_var => $tag->slug );
       
   298 		} else {
       
   299 			$args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
       
   300 		}
       
   301 
       
   302 		if ( 'post' != $post_type )
       
   303 			$args['post_type'] = $post_type;
       
   304 
       
   305 		return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
       
   306 	}
       
   307 
       
   308 	function column_links( $tag ) {
       
   309 		$count = number_format_i18n( $tag->count );
       
   310 		if ( $count )
       
   311 			$count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
       
   312 		return $count;
       
   313 	}
       
   314 
       
   315 	function column_default( $tag, $column_name ) {
       
   316 		$screen = get_current_screen();
       
   317 
       
   318 		return apply_filters( "manage_{$screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
       
   319 	}
       
   320 
       
   321 	/**
       
   322 	 * Outputs the hidden row displayed when inline editing
       
   323 	 *
       
   324 	 * @since 3.1.0
       
   325 	 */
       
   326 	function inline_edit() {
       
   327 		global $post_type, $tax;
       
   328 
       
   329 		if ( ! current_user_can( $tax->cap->edit_terms ) )
       
   330 			return;
       
   331 ?>
       
   332 
       
   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">
       
   335 
       
   336 			<fieldset><div class="inline-edit-col">
       
   337 				<h4><?php _e( 'Quick Edit' ); ?></h4>
       
   338 
       
   339 				<label>
       
   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>
       
   342 				</label>
       
   343 	<?php if ( !global_terms_enabled() ) { ?>
       
   344 				<label>
       
   345 					<span class="title"><?php _e( 'Slug' ); ?></span>
       
   346 					<span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
       
   347 				</label>
       
   348 	<?php } ?>
       
   349 			</div></fieldset>
       
   350 	<?php
       
   351 
       
   352 		$core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
       
   353 
       
   354 		list( $columns ) = $this->get_column_info();
       
   355 
       
   356 		foreach ( $columns as $column_name => $column_display_name ) {
       
   357 			if ( isset( $core_columns[$column_name] ) )
       
   358 				continue;
       
   359 
       
   360 			do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $tax->name );
       
   361 		}
       
   362 
       
   363 	?>
       
   364 
       
   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 ); ?>" />
       
   374 			<br class="clear" />
       
   375 		</p>
       
   376 		</td></tr>
       
   377 		</tbody></table></form>
       
   378 	<?php
       
   379 	}
       
   380 }