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.
Built from https://develop.svn.wordpress.org/trunk@51896


git-svn-id: http://core.svn.wordpress.org/trunk@51489 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-10-08 00:38:00 +00:00
parent f57070bfd1
commit c477dcda88
2 changed files with 30 additions and 47 deletions

View File

@ -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 '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
$this->no_items();
echo '</td></tr>';
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 );
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-51895';
$wp_version = '5.9-alpha-51896';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.