WordPress.org

Make WordPress Core

Changeset 51896


Ignore:
Timestamp:
10/08/2021 12:36:18 AM (7 weeks ago)
Author:
SergeyBiryukov
Message:

Taxonomy: Populate the WP_Terms_List_Table::$items property in ::prepare_items().

This allows the parent WP_List_Table::has_items() method to work as expected, and the override in the child class can now be removed. It also makes the class more consistent with other list table classes.

As a result of this change, the "Bulk actions" dropdown is no longer unnecessarily displayed if there are no terms.

Follow-up to [15491], [17025], [17026].

Props mattoakley, swissspidy, audrasjb, SergeyBiryukov.
Fixes #54181.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-terms-list-table.php

    r51737 r51896  
    7878     */
    7979    public function prepare_items() {
    80         $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
    81 
    82         if ( 'post_tag' === $this->screen->taxonomy ) {
     80        $taxonomy = $this->screen->taxonomy;
     81
     82        $tags_per_page = $this->get_items_per_page( "edit_{$taxonomy}_per_page" );
     83
     84        if ( 'post_tag' === $taxonomy ) {
    8385            /**
    8486             * Filters the number of terms displayed per page for the Tags list table.
     
    99101             */
    100102            $tags_per_page = apply_filters_deprecated( 'tagsperpage', array( $tags_per_page ), '2.8.0', 'edit_tags_per_page' );
    101         } elseif ( 'category' === $this->screen->taxonomy ) {
     103        } elseif ( 'category' === $taxonomy ) {
    102104            /**
    103105             * Filters the number of terms displayed per page for the Categories list table.
     
    113115
    114116        $args = array(
    115             'search' => $search,
    116             'page'   => $this->get_pagenum(),
    117             'number' => $tags_per_page,
     117            'taxonomy'   => $taxonomy,
     118            'search'     => $search,
     119            'page'       => $this->get_pagenum(),
     120            'number'     => $tags_per_page,
     121            'hide_empty' => 0,
    118122        );
    119123
     
    126130        }
    127131
     132        $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
     133
     134        // Save the values because 'number' and 'offset' can be subsequently overridden.
    128135        $this->callback_args = $args;
     136
     137        if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
     138            // We'll need the full set of terms then.
     139            $args['number'] = 0;
     140            $args['offset'] = $args['number'];
     141        }
     142
     143        $this->items = get_terms( $args );
    129144
    130145        $this->set_pagination_args(
     
    132147                'total_items' => wp_count_terms(
    133148                    array(
    134                         'taxonomy' => $this->screen->taxonomy,
     149                        'taxonomy' => $taxonomy,
    135150                        'search'   => $search,
    136151                    )
     
    139154            )
    140155        );
    141     }
    142 
    143     /**
    144      * @return bool
    145      */
    146     public function has_items() {
    147         // @todo Populate $this->items in prepare_items().
    148         return true;
    149156    }
    150157
     
    217224        $taxonomy = $this->screen->taxonomy;
    218225
    219         $args = wp_parse_args(
    220             $this->callback_args,
    221             array(
    222                 'taxonomy'   => $taxonomy,
    223                 'page'       => 1,
    224                 'number'     => 20,
    225                 'search'     => '',
    226                 'hide_empty' => 0,
    227             )
    228         );
    229 
    230         $page = $args['page'];
    231 
    232         // Set variable because $args['number'] can be subsequently overridden.
    233         $number = $args['number'];
    234 
    235         $offset         = ( $page - 1 ) * $number;
    236         $args['offset'] = $offset;
     226        $number = $this->callback_args['number'];
     227        $offset = $this->callback_args['offset'];
    237228
    238229        // Convert it to table rows.
    239230        $count = 0;
    240231
    241         if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
    242             // We'll need the full set of terms then.
    243             $args['number'] = 0;
    244             $args['offset'] = $args['number'];
    245         }
    246 
    247         $terms = get_terms( $args );
    248 
    249         if ( empty( $terms ) || ! is_array( $terms ) ) {
     232        if ( empty( $this->items ) || ! is_array( $this->items ) ) {
    250233            echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
    251234            $this->no_items();
     
    254237        }
    255238
    256         if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
    257             if ( ! empty( $args['search'] ) ) {// Ignore children on searches.
     239        if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $this->callback_args['orderby'] ) ) {
     240            if ( ! empty( $this->callback_args['search'] ) ) {// Ignore children on searches.
    258241                $children = array();
    259242            } else {
     
    265248             * Skip it for non-hierarchical taxonomies for performance sake.
    266249             */
    267             $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
     250            $this->_rows( $taxonomy, $this->items, $children, $offset, $number, $count );
    268251        } else {
    269             foreach ( $terms as $term ) {
     252            foreach ( $this->items as $term ) {
    270253                $this->single_row( $term );
    271254            }
Note: See TracChangeset for help on using the changeset viewer.