From 440336b73ed718363c7cc1b2b91d76c32e353edb Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 8 Oct 2021 00:36:18 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@51896 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-terms-list-table.php | 75 +++++++------------ 1 file changed, 29 insertions(+), 46 deletions(-) diff --git a/src/wp-admin/includes/class-wp-terms-list-table.php b/src/wp-admin/includes/class-wp-terms-list-table.php index e905749088..4823d29c9d 100644 --- a/src/wp-admin/includes/class-wp-terms-list-table.php +++ b/src/wp-admin/includes/class-wp-terms-list-table.php @@ -77,9 +77,11 @@ class WP_Terms_List_Table extends WP_List_Table { /** */ public function prepare_items() { - $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' ); + $taxonomy = $this->screen->taxonomy; - if ( 'post_tag' === $this->screen->taxonomy ) { + $tags_per_page = $this->get_items_per_page( "edit_{$taxonomy}_per_page" ); + + if ( 'post_tag' === $taxonomy ) { /** * Filters the number of terms displayed per page for the Tags list table. * @@ -98,7 +100,7 @@ class WP_Terms_List_Table extends WP_List_Table { * @param int $tags_per_page Number of tags to be displayed. Default 20. */ $tags_per_page = apply_filters_deprecated( 'tagsperpage', array( $tags_per_page ), '2.8.0', 'edit_tags_per_page' ); - } elseif ( 'category' === $this->screen->taxonomy ) { + } elseif ( 'category' === $taxonomy ) { /** * Filters the number of terms displayed per page for the Categories list table. * @@ -112,9 +114,11 @@ class WP_Terms_List_Table extends WP_List_Table { $search = ! empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : ''; $args = array( - 'search' => $search, - 'page' => $this->get_pagenum(), - 'number' => $tags_per_page, + 'taxonomy' => $taxonomy, + 'search' => $search, + 'page' => $this->get_pagenum(), + 'number' => $tags_per_page, + 'hide_empty' => 0, ); if ( ! empty( $_REQUEST['orderby'] ) ) { @@ -125,13 +129,24 @@ class WP_Terms_List_Table extends WP_List_Table { $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) ); } + $args['offset'] = ( $args['page'] - 1 ) * $args['number']; + + // Save the values because 'number' and 'offset' can be subsequently overridden. $this->callback_args = $args; + if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { + // We'll need the full set of terms then. + $args['number'] = 0; + $args['offset'] = $args['number']; + } + + $this->items = get_terms( $args ); + $this->set_pagination_args( array( 'total_items' => wp_count_terms( array( - 'taxonomy' => $this->screen->taxonomy, + 'taxonomy' => $taxonomy, 'search' => $search, ) ), @@ -140,14 +155,6 @@ class WP_Terms_List_Table extends WP_List_Table { ); } - /** - * @return bool - */ - public function has_items() { - // @todo Populate $this->items in prepare_items(). - return true; - } - /** */ public function no_items() { @@ -216,45 +223,21 @@ class WP_Terms_List_Table extends WP_List_Table { public function display_rows_or_placeholder() { $taxonomy = $this->screen->taxonomy; - $args = wp_parse_args( - $this->callback_args, - array( - 'taxonomy' => $taxonomy, - 'page' => 1, - 'number' => 20, - 'search' => '', - 'hide_empty' => 0, - ) - ); - - $page = $args['page']; - - // Set variable because $args['number'] can be subsequently overridden. - $number = $args['number']; - - $offset = ( $page - 1 ) * $number; - $args['offset'] = $offset; + $number = $this->callback_args['number']; + $offset = $this->callback_args['offset']; // Convert it to table rows. $count = 0; - if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { - // We'll need the full set of terms then. - $args['number'] = 0; - $args['offset'] = $args['number']; - } - - $terms = get_terms( $args ); - - if ( empty( $terms ) || ! is_array( $terms ) ) { + if ( empty( $this->items ) || ! is_array( $this->items ) ) { echo ''; $this->no_items(); echo ''; return; } - if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { - if ( ! empty( $args['search'] ) ) {// Ignore children on searches. + if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $this->callback_args['orderby'] ) ) { + if ( ! empty( $this->callback_args['search'] ) ) {// Ignore children on searches. $children = array(); } else { $children = _get_term_hierarchy( $taxonomy ); @@ -264,9 +247,9 @@ class WP_Terms_List_Table extends WP_List_Table { * Some funky recursion to get the job done (paging & parents mainly) is contained within. * Skip it for non-hierarchical taxonomies for performance sake. */ - $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count ); + $this->_rows( $taxonomy, $this->items, $children, $offset, $number, $count ); } else { - foreach ( $terms as $term ) { + foreach ( $this->items as $term ) { $this->single_row( $term ); } }