Make WordPress Core

Ticket #33521: class-wp-terms-list-table.php.diff

File class-wp-terms-list-table.php.diff, 16.5 KB (added by Cybr, 9 years ago)

class-wp-terms-list-table.php manage_{$post_type}_custom_column fix

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