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