]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-terms-list-table.php
WordPress 3.9.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-terms-list-table.php
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                         /**
50                          * Filter the number of terms displayed per page for the Tags list table.
51                          *
52                          * @since 2.8.0
53                          *
54                          * @param int $tags_per_page Number of tags to be displayed. Default 20.
55                          */
56                         $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
57
58                         /**
59                          * Filter the number of terms displayed per page for the Tags list table.
60                          *
61                          * @since 2.7.0
62                          * @deprecated 2.8.0 Use edit_tags_per_page instead.
63                          *
64                          * @param int $tags_per_page Number of tags to be displayed. Default 20.
65                          */
66                         $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page );
67                 } elseif ( 'category' == $this->screen->taxonomy ) {
68                         /**
69                          * Filter the number of terms displayed per page for the Categories list table.
70                          *
71                          * @since 2.8.0
72                          *
73                          * @param int $tags_per_page Number of categories to be displayed. Default 20.
74                          */
75                         $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
76                 }
77
78                 $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
79
80                 $args = array(
81                         'search' => $search,
82                         'page' => $this->get_pagenum(),
83                         'number' => $tags_per_page,
84                 );
85
86                 if ( !empty( $_REQUEST['orderby'] ) )
87                         $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
88
89                 if ( !empty( $_REQUEST['order'] ) )
90                         $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
91
92                 $this->callback_args = $args;
93
94                 $this->set_pagination_args( array(
95                         'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
96                         'per_page' => $tags_per_page,
97                 ) );
98         }
99
100         function has_items() {
101                 // todo: populate $this->items in prepare_items()
102                 return true;
103         }
104
105         function get_bulk_actions() {
106                 $actions = array();
107                 $actions['delete'] = __( 'Delete' );
108
109                 return $actions;
110         }
111
112         function current_action() {
113                 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
114                         return 'bulk-delete';
115
116                 return parent::current_action();
117         }
118
119         function get_columns() {
120                 $columns = array(
121                         'cb'          => '<input type="checkbox" />',
122                         'name'        => _x( 'Name', 'term name' ),
123                         'description' => __( 'Description' ),
124                         'slug'        => __( 'Slug' ),
125                 );
126
127                 if ( 'link_category' == $this->screen->taxonomy ) {
128                         $columns['links'] = __( 'Links' );
129                 } else {
130                         $post_type_object = get_post_type_object( $this->screen->post_type );
131                         $columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' );
132                 }
133
134                 return $columns;
135         }
136
137         function get_sortable_columns() {
138                 return array(
139                         'name'        => 'name',
140                         'description' => 'description',
141                         'slug'        => 'slug',
142                         'posts'       => 'count',
143                         'links'       => 'count'
144                 );
145         }
146
147         function display_rows_or_placeholder() {
148                 $taxonomy = $this->screen->taxonomy;
149
150                 $args = wp_parse_args( $this->callback_args, array(
151                         'page' => 1,
152                         'number' => 20,
153                         'search' => '',
154                         'hide_empty' => 0
155                 ) );
156
157                 extract( $args, EXTR_SKIP );
158
159                 $args['offset'] = $offset = ( $page - 1 ) * $number;
160
161                 // convert it to table rows
162                 $count = 0;
163
164                 $terms = array();
165
166                 if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
167                         // We'll need the full set of terms then.
168                         $args['number'] = $args['offset'] = 0;
169                 }
170                 $terms = get_terms( $taxonomy, $args );
171
172                 if ( empty( $terms ) ) {
173                         list( $columns, $hidden ) = $this->get_column_info();
174                         echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
175                         $this->no_items();
176                         echo '</td></tr>';
177                         return;
178                 }
179
180                 if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
181                         if ( !empty( $search ) ) // Ignore children on searches.
182                                 $children = array();
183                         else
184                                 $children = _get_term_hierarchy( $taxonomy );
185
186                         // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
187                         $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
188                 } else {
189                         $terms = get_terms( $taxonomy, $args );
190                         foreach ( $terms as $term )
191                                 $this->single_row( $term );
192                         $count = $number; // Only displaying a single page.
193                 }
194         }
195
196         function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
197
198                 $end = $start + $per_page;
199
200                 foreach ( $terms as $key => $term ) {
201
202                         if ( $count >= $end )
203                                 break;
204
205                         if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
206                                 continue;
207
208                         // If the page starts in a subtree, print the parents.
209                         if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
210                                 $my_parents = $parent_ids = array();
211                                 $p = $term->parent;
212                                 while ( $p ) {
213                                         $my_parent = get_term( $p, $taxonomy );
214                                         $my_parents[] = $my_parent;
215                                         $p = $my_parent->parent;
216                                         if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
217                                                 break;
218                                         $parent_ids[] = $p;
219                                 }
220                                 unset( $parent_ids );
221
222                                 $num_parents = count( $my_parents );
223                                 while ( $my_parent = array_pop( $my_parents ) ) {
224                                         echo "\t";
225                                         $this->single_row( $my_parent, $level - $num_parents );
226                                         $num_parents--;
227                                 }
228                         }
229
230                         if ( $count >= $start ) {
231                                 echo "\t";
232                                 $this->single_row( $term, $level );
233                         }
234
235                         ++$count;
236
237                         unset( $terms[$key] );
238
239                         if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
240                                 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
241                 }
242         }
243
244         function single_row( $tag, $level = 0 ) {
245                 static $row_class = '';
246                 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
247
248                 $this->level = $level;
249
250                 echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
251                 $this->single_row_columns( $tag );
252                 echo '</tr>';
253         }
254
255         function column_cb( $tag ) {
256                 $default_term = get_option( 'default_' . $this->screen->taxonomy );
257
258                 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
259                         return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
260                                 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
261
262                 return '&nbsp;';
263         }
264
265         function column_name( $tag ) {
266                 $taxonomy = $this->screen->taxonomy;
267                 $tax = get_taxonomy( $taxonomy );
268
269                 $default_term = get_option( 'default_' . $taxonomy );
270
271                 $pad = str_repeat( '&#8212; ', max( 0, $this->level ) );
272
273                 /**
274                  * Filter display of the term name in the terms list table.
275                  *
276                  * The default output may include padding due to the term's
277                  * current level in the term hierarchy.
278                  *
279                  * @since 2.5.0
280                  *
281                  * @see WP_Terms_List_Table::column_name()
282                  *
283                  * @param string $pad_tag_name The term name, padded if not top-level.
284                  * @param object $tag          Term object.
285                  */
286                 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
287
288                 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
289                 $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
290
291                 $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $name ) ) . '">' . $name . '</a></strong><br />';
292
293                 $actions = array();
294                 if ( current_user_can( $tax->cap->edit_terms ) ) {
295                         $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
296                         $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick&nbsp;Edit' ) . '</a>';
297                 }
298                 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
299                         $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>";
300                 if ( $tax->public )
301                         $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
302
303                 /**
304                  * Filter the action links displayed for each term in the Tags list table.
305                  *
306                  * @since 2.8.0
307                  * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
308                  *
309                  * @param array  $actions An array of action links to be displayed. Default
310                  *                        'Edit', 'Quick Edit', 'Delete', and 'View'.
311                  * @param object $tag     Term object.
312                  */
313                 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
314
315                 /**
316                  * Filter the action links displayed for each term in the terms list table.
317                  *
318                  * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
319                  *
320                  * @since 3.0.0
321                  *
322                  * @param array  $actions An array of action links to be displayed. Default
323                  *                        'Edit', 'Quick Edit', 'Delete', and 'View'.
324                  * @param object $tag     Term object.
325                  */
326                 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
327
328                 $out .= $this->row_actions( $actions );
329                 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
330                 $out .= '<div class="name">' . $qe_data->name . '</div>';
331
332                 /** This filter is documented in wp-admin/edit-tag-form.php */
333                 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
334                 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
335
336                 return $out;
337         }
338
339         function column_description( $tag ) {
340                 return $tag->description;
341         }
342
343         function column_slug( $tag ) {
344                 /** This filter is documented in wp-admin/edit-tag-form.php */
345                 return apply_filters( 'editable_slug', $tag->slug );
346         }
347
348         function column_posts( $tag ) {
349                 $count = number_format_i18n( $tag->count );
350
351                 $tax = get_taxonomy( $this->screen->taxonomy );
352
353                 $ptype_object = get_post_type_object( $this->screen->post_type );
354                 if ( ! $ptype_object->show_ui )
355                         return $count;
356
357                 if ( $tax->query_var ) {
358                         $args = array( $tax->query_var => $tag->slug );
359                 } else {
360                         $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
361                 }
362
363                 if ( 'post' != $this->screen->post_type )
364                         $args['post_type'] = $this->screen->post_type;
365
366                 if ( 'attachment' == $this->screen->post_type )
367                         return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
368
369                 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
370         }
371
372         function column_links( $tag ) {
373                 $count = number_format_i18n( $tag->count );
374                 if ( $count )
375                         $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
376                 return $count;
377         }
378
379         function column_default( $tag, $column_name ) {
380                 /**
381                  * Filter the displayed columns in the terms list table.
382                  *
383                  * The dynamic portion of the hook name, $this->screen->taxonomy,
384                  * refers to the slug of the current taxonomy.
385                  *
386                  * @since 2.8.0
387                  *
388                  * @param string $string      Blank string.
389                  * @param string $column_name Name of the column.
390                  * @param int    $term_id     Term ID.
391                  */
392                 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
393         }
394
395         /**
396          * Outputs the hidden row displayed when inline editing
397          *
398          * @since 3.1.0
399          */
400         function inline_edit() {
401                 $tax = get_taxonomy( $this->screen->taxonomy );
402
403                 if ( ! current_user_can( $tax->cap->edit_terms ) )
404                         return;
405 ?>
406
407         <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
408                 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
409
410                         <fieldset><div class="inline-edit-col">
411                                 <h4><?php _e( 'Quick Edit' ); ?></h4>
412
413                                 <label>
414                                         <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
415                                         <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
416                                 </label>
417         <?php if ( !global_terms_enabled() ) { ?>
418                                 <label>
419                                         <span class="title"><?php _e( 'Slug' ); ?></span>
420                                         <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
421                                 </label>
422         <?php } ?>
423                         </div></fieldset>
424         <?php
425
426                 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
427
428                 list( $columns ) = $this->get_column_info();
429
430                 foreach ( $columns as $column_name => $column_display_name ) {
431                         if ( isset( $core_columns[$column_name] ) )
432                                 continue;
433
434                         /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
435                         do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
436                 }
437
438         ?>
439
440                 <p class="inline-edit-save submit">
441                         <a accesskey="c" href="#inline-edit" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
442                         <a accesskey="s" href="#inline-edit" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></a>
443                         <span class="spinner"></span>
444                         <span class="error" style="display:none;"></span>
445                         <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
446                         <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
447                         <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
448                         <br class="clear" />
449                 </p>
450                 </td></tr>
451                 </tbody></table></form>
452         <?php
453         }
454 }