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