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