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