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