]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-terms-list-table.php
WordPress 4.0.1
[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         private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
206
207                 $end = $start + $per_page;
208
209                 foreach ( $terms as $key => $term ) {
210
211                         if ( $count >= $end )
212                                 break;
213
214                         if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
215                                 continue;
216
217                         // If the page starts in a subtree, print the parents.
218                         if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
219                                 $my_parents = $parent_ids = array();
220                                 $p = $term->parent;
221                                 while ( $p ) {
222                                         $my_parent = get_term( $p, $taxonomy );
223                                         $my_parents[] = $my_parent;
224                                         $p = $my_parent->parent;
225                                         if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
226                                                 break;
227                                         $parent_ids[] = $p;
228                                 }
229                                 unset( $parent_ids );
230
231                                 $num_parents = count( $my_parents );
232                                 while ( $my_parent = array_pop( $my_parents ) ) {
233                                         echo "\t";
234                                         $this->single_row( $my_parent, $level - $num_parents );
235                                         $num_parents--;
236                                 }
237                         }
238
239                         if ( $count >= $start ) {
240                                 echo "\t";
241                                 $this->single_row( $term, $level );
242                         }
243
244                         ++$count;
245
246                         unset( $terms[$key] );
247
248                         if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
249                                 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
250                 }
251         }
252
253         public function single_row( $tag, $level = 0 ) {
254                 global $taxonomy;
255                 $tag = sanitize_term( $tag, $taxonomy );
256
257                 static $row_class = '';
258                 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
259
260                 $this->level = $level;
261
262                 echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
263                 $this->single_row_columns( $tag );
264                 echo '</tr>';
265         }
266
267         public function column_cb( $tag ) {
268                 $default_term = get_option( 'default_' . $this->screen->taxonomy );
269
270                 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
271                         return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
272                                 . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
273
274                 return '&nbsp;';
275         }
276
277         public function column_name( $tag ) {
278                 $taxonomy = $this->screen->taxonomy;
279                 $tax = get_taxonomy( $taxonomy );
280
281                 $default_term = get_option( 'default_' . $taxonomy );
282
283                 $pad = str_repeat( '&#8212; ', max( 0, $this->level ) );
284
285                 /**
286                  * Filter display of the term name in the terms list table.
287                  *
288                  * The default output may include padding due to the term's
289                  * current level in the term hierarchy.
290                  *
291                  * @since 2.5.0
292                  *
293                  * @see WP_Terms_List_Table::column_name()
294                  *
295                  * @param string $pad_tag_name The term name, padded if not top-level.
296                  * @param object $tag          Term object.
297                  */
298                 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
299
300                 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
301                 $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
302
303                 $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $name ) ) . '">' . $name . '</a></strong><br />';
304
305                 $actions = array();
306                 if ( current_user_can( $tax->cap->edit_terms ) ) {
307                         $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
308                         $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick&nbsp;Edit' ) . '</a>';
309                 }
310                 if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
311                         $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>";
312                 if ( $tax->public )
313                         $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
314
315                 /**
316                  * Filter the action links displayed for each term in the Tags list table.
317                  *
318                  * @since 2.8.0
319                  * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
320                  *
321                  * @param array  $actions An array of action links to be displayed. Default
322                  *                        'Edit', 'Quick Edit', 'Delete', and 'View'.
323                  * @param object $tag     Term object.
324                  */
325                 $actions = apply_filters( 'tag_row_actions', $actions, $tag );
326
327                 /**
328                  * Filter the action links displayed for each term in the terms list table.
329                  *
330                  * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
331                  *
332                  * @since 3.0.0
333                  *
334                  * @param array  $actions An array of action links to be displayed. Default
335                  *                        'Edit', 'Quick Edit', 'Delete', and 'View'.
336                  * @param object $tag     Term object.
337                  */
338                 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
339
340                 $out .= $this->row_actions( $actions );
341                 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
342                 $out .= '<div class="name">' . $qe_data->name . '</div>';
343
344                 /** This filter is documented in wp-admin/edit-tag-form.php */
345                 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
346                 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
347
348                 return $out;
349         }
350
351         public function column_description( $tag ) {
352                 return $tag->description;
353         }
354
355         public function column_slug( $tag ) {
356                 /** This filter is documented in wp-admin/edit-tag-form.php */
357                 return apply_filters( 'editable_slug', $tag->slug );
358         }
359
360         public function column_posts( $tag ) {
361                 $count = number_format_i18n( $tag->count );
362
363                 $tax = get_taxonomy( $this->screen->taxonomy );
364
365                 $ptype_object = get_post_type_object( $this->screen->post_type );
366                 if ( ! $ptype_object->show_ui )
367                         return $count;
368
369                 if ( $tax->query_var ) {
370                         $args = array( $tax->query_var => $tag->slug );
371                 } else {
372                         $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
373                 }
374
375                 if ( 'post' != $this->screen->post_type )
376                         $args['post_type'] = $this->screen->post_type;
377
378                 if ( 'attachment' == $this->screen->post_type )
379                         return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
380
381                 return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
382         }
383
384         public function column_links( $tag ) {
385                 $count = number_format_i18n( $tag->count );
386                 if ( $count )
387                         $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
388                 return $count;
389         }
390
391         public function column_default( $tag, $column_name ) {
392                 /**
393                  * Filter the displayed columns in the terms list table.
394                  *
395                  * The dynamic portion of the hook name, $this->screen->taxonomy,
396                  * refers to the slug of the current taxonomy.
397                  *
398                  * @since 2.8.0
399                  *
400                  * @param string $string      Blank string.
401                  * @param string $column_name Name of the column.
402                  * @param int    $term_id     Term ID.
403                  */
404                 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
405         }
406
407         /**
408          * Outputs the hidden row displayed when inline editing
409          *
410          * @since 3.1.0
411          */
412         public function inline_edit() {
413                 $tax = get_taxonomy( $this->screen->taxonomy );
414
415                 if ( ! current_user_can( $tax->cap->edit_terms ) )
416                         return;
417 ?>
418
419         <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
420                 <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
421
422                         <fieldset><div class="inline-edit-col">
423                                 <h4><?php _e( 'Quick Edit' ); ?></h4>
424
425                                 <label>
426                                         <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
427                                         <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
428                                 </label>
429         <?php if ( !global_terms_enabled() ) { ?>
430                                 <label>
431                                         <span class="title"><?php _e( 'Slug' ); ?></span>
432                                         <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
433                                 </label>
434         <?php } ?>
435                         </div></fieldset>
436         <?php
437
438                 $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
439
440                 list( $columns ) = $this->get_column_info();
441
442                 foreach ( $columns as $column_name => $column_display_name ) {
443                         if ( isset( $core_columns[$column_name] ) )
444                                 continue;
445
446                         /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
447                         do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
448                 }
449
450         ?>
451
452                 <p class="inline-edit-save submit">
453                         <a accesskey="c" href="#inline-edit" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
454                         <a accesskey="s" href="#inline-edit" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></a>
455                         <span class="spinner"></span>
456                         <span class="error" style="display:none;"></span>
457                         <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
458                         <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
459                         <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
460                         <br class="clear" />
461                 </p>
462                 </td></tr>
463                 </tbody></table></form>
464         <?php
465         }
466 }