]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-terms-list-table.php
Wordpress 4.5.3
[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 = sprintf(
379                         '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong><br />',
380                         esc_url( $edit_link ),
381                         /* translators: %s: taxonomy term name */
382                         esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $tag->name ) ),
383                         $name
384                 );
385
386                 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
387                 $out .= '<div class="name">' . $qe_data->name . '</div>';
388
389                 /** This filter is documented in wp-admin/edit-tag-form.php */
390                 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '</div>';
391                 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
392
393                 return $out;
394         }
395
396         /**
397          * Gets the name of the default primary column.
398          *
399          * @since 4.3.0
400          * @access protected
401          *
402          * @return string Name of the default primary column, in this case, 'name'.
403          */
404         protected function get_default_primary_column_name() {
405                 return 'name';
406         }
407
408         /**
409          * Generates and displays row action links.
410          *
411          * @since 4.3.0
412          * @access protected
413          *
414          * @param object $tag         Tag being acted upon.
415          * @param string $column_name Current column name.
416          * @param string $primary     Primary column name.
417          * @return string Row actions output for terms.
418          */
419         protected function handle_row_actions( $tag, $column_name, $primary ) {
420                 if ( $primary !== $column_name ) {
421                         return '';
422                 }
423
424                 $taxonomy = $this->screen->taxonomy;
425                 $tax = get_taxonomy( $taxonomy );
426                 $default_term = get_option( 'default_' . $taxonomy );
427
428                 $uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI'];
429
430                 $edit_link = add_query_arg(
431                         'wp_http_referer',
432                         urlencode( wp_unslash( $uri ) ),
433                         get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
434                 );
435
436                 $actions = array();
437                 if ( current_user_can( $tax->cap->edit_terms ) ) {
438                         $actions['edit'] = sprintf(
439                                 '<a href="%s" aria-label="%s">%s</a>',
440                                 esc_url( $edit_link ),
441                                 /* translators: %s: taxonomy term name */
442                                 esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $tag->name ) ),
443                                 __( 'Edit' )
444                         );
445                         $actions['inline hide-if-no-js'] = sprintf(
446                                 '<a href="#" class="editinline aria-button-if-js" aria-label="%s">%s</a>',
447                                 /* translators: %s: taxonomy term name */
448                                 esc_attr( sprintf( __( 'Quick edit &#8220;%s&#8221; inline' ), $tag->name ) ),
449                                 __( 'Quick&nbsp;Edit' )
450                         );
451                 }
452                 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term ) {
453                         $actions['delete'] = sprintf(
454                                 '<a href="%s" class="delete-tag aria-button-if-js" aria-label="%s">%s</a>',
455                                 wp_nonce_url( "edit-tags.php?action=delete&amp;taxonomy=$taxonomy&amp;tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ),
456                                 /* translators: %s: taxonomy term name */
457                                 esc_attr( sprintf( __( 'Delete &#8220;%s&#8221;' ), $tag->name ) ),
458                                 __( 'Delete' )
459                         );
460                 }
461                 if ( $tax->public ) {
462                         $actions['view'] = sprintf(
463                                 '<a href="%s" aria-label="%s">%s</a>',
464                                 get_term_link( $tag ),
465                                 /* translators: %s: taxonomy term name */
466                                 esc_attr( sprintf( __( 'View &#8220;%s&#8221; archive' ), $tag->name ) ),
467                                 __( 'View' )
468                         );
469                 }
470
471                 /**
472                  * Filter the action links displayed for each term in the Tags list table.
473                  *
474                  * @since 2.8.0
475                  * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
476                  *
477                  * @param array  $actions An array of action links to be displayed. Default
478                  *                        'Edit', 'Quick Edit', 'Delete', and 'View'.
479                  * @param object $tag     Term object.
480                  */
481                 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
482
483                 /**
484                  * Filter the action links displayed for each term in the terms list table.
485                  *
486                  * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
487                  *
488                  * @since 3.0.0
489                  *
490                  * @param array  $actions An array of action links to be displayed. Default
491                  *                        'Edit', 'Quick Edit', 'Delete', and 'View'.
492                  * @param object $tag     Term object.
493                  */
494                 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
495
496                 return $this->row_actions( $actions );
497         }
498
499         /**
500          * @param object $tag
501          * @return string
502          */
503         public function column_description( $tag ) {
504                 return $tag->description;
505         }
506
507         /**
508          * @param object $tag
509          * @return string
510          */
511         public function column_slug( $tag ) {
512                 /** This filter is documented in wp-admin/edit-tag-form.php */
513                 return apply_filters( 'editable_slug', $tag->slug, $tag );
514         }
515
516         /**
517          * @param object $tag
518          * @return string
519          */
520         public function column_posts( $tag ) {
521                 $count = number_format_i18n( $tag->count );
522
523                 $tax = get_taxonomy( $this->screen->taxonomy );
524
525                 $ptype_object = get_post_type_object( $this->screen->post_type );
526                 if ( ! $ptype_object->show_ui )
527                         return $count;
528
529                 if ( $tax->query_var ) {
530                         $args = array( $tax->query_var => $tag->slug );
531                 } else {
532                         $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
533                 }
534
535                 if ( 'post' != $this->screen->post_type )
536                         $args['post_type'] = $this->screen->post_type;
537
538                 if ( 'attachment' === $this->screen->post_type )
539                         return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
540
541                 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
542         }
543
544         /**
545          * @param object $tag
546          * @return string
547          */
548         public function column_links( $tag ) {
549                 $count = number_format_i18n( $tag->count );
550                 if ( $count )
551                         $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
552                 return $count;
553         }
554
555         /**
556          * @param object $tag
557          * @param string $column_name
558          * @return string
559          */
560         public function column_default( $tag, $column_name ) {
561                 /**
562                  * Filter the displayed columns in the terms list table.
563                  *
564                  * The dynamic portion of the hook name, `$this->screen->taxonomy`,
565                  * refers to the slug of the current taxonomy.
566                  *
567                  * @since 2.8.0
568                  *
569                  * @param string $string      Blank string.
570                  * @param string $column_name Name of the column.
571                  * @param int    $term_id     Term ID.
572                  */
573                 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
574         }
575
576         /**
577          * Outputs the hidden row displayed when inline editing
578          *
579          * @since 3.1.0
580          */
581         public function inline_edit() {
582                 $tax = get_taxonomy( $this->screen->taxonomy );
583
584                 if ( ! current_user_can( $tax->cap->edit_terms ) )
585                         return;
586 ?>
587
588         <form method="get"><table style="display: none"><tbody id="inlineedit">
589                 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
590
591                         <fieldset>
592                                 <legend class="inline-edit-legend"><?php _e( 'Quick Edit' ); ?></legend>
593                                 <div class="inline-edit-col">
594                                 <label>
595                                         <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
596                                         <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
597                                 </label>
598         <?php if ( !global_terms_enabled() ) { ?>
599                                 <label>
600                                         <span class="title"><?php _e( 'Slug' ); ?></span>
601                                         <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
602                                 </label>
603         <?php } ?>
604                         </div></fieldset>
605         <?php
606
607                 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
608
609                 list( $columns ) = $this->get_column_info();
610
611                 foreach ( $columns as $column_name => $column_display_name ) {
612                         if ( isset( $core_columns[$column_name] ) )
613                                 continue;
614
615                         /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
616                         do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
617                 }
618
619         ?>
620
621                 <p class="inline-edit-save submit">
622                         <button type="button" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></button>
623                         <button type="button" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></button>
624                         <span class="spinner"></span>
625                         <span class="error" style="display:none;"></span>
626                         <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
627                         <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
628                         <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
629                         <br class="clear" />
630                 </p>
631                 </td></tr>
632                 </tbody></table></form>
633         <?php
634         }
635 }