In get_terms()
, leverage get_term_children()
over _get_term_children()
when making sure to show empty terms that have children in a hierarchical taxonomy while avoiding duplicates.
Adds unit test for `child_of` param. Adjusts unit tests for `get_terms()`. See [27108] and [27125]. Props SergeyBiryukov. Fixes #27123. git-svn-id: https://develop.svn.wordpress.org/trunk@27458 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
666cd37087
commit
3010c5aa20
@ -1477,11 +1477,15 @@ function get_terms($taxonomies, $args = '') {
|
||||
if ( $hierarchical && $hide_empty && is_array( $terms ) ) {
|
||||
foreach ( $terms as $k => $term ) {
|
||||
if ( ! $term->count ) {
|
||||
$children = _get_term_children( $term->term_id, $terms, reset( $taxonomies ) );
|
||||
if ( is_array( $children ) )
|
||||
foreach ( $children as $child )
|
||||
if ( $child->count )
|
||||
$children = get_term_children( $term->term_id, reset( $taxonomies ) );
|
||||
if ( is_array( $children ) ) {
|
||||
foreach ( $children as $child_id ) {
|
||||
$child = get_term( $child_id, reset( $taxonomies ) );
|
||||
if ( $child->count ) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// It really is empty
|
||||
unset($terms[$k]);
|
||||
@ -2923,20 +2927,6 @@ function _get_term_children($term_id, $terms, $taxonomy) {
|
||||
}
|
||||
|
||||
if ( $term->term_id == $term_id ) {
|
||||
if ( isset( $has_children[$term_id] ) ) {
|
||||
$current_id = $term_id;
|
||||
while ( $current_id > 0 ) {
|
||||
foreach ( $has_children[$current_id] as $t_id ) {
|
||||
if ( $use_id ) {
|
||||
$term_list[] = $t_id;
|
||||
} else {
|
||||
$term_list[] = get_term( $t_id, $taxonomy );
|
||||
}
|
||||
}
|
||||
|
||||
$current_id = isset( $has_children[$t_id] ) ? $t_id : 0;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -276,7 +276,7 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
|
||||
$cranberries = $this->factory->term->create( array( 'name' => 'Cranberries', 'parent' => $fruit, 'taxonomy' => $tax ) );
|
||||
|
||||
$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
|
||||
$this->assertNotEmpty( $terms );
|
||||
$this->assertEquals( 2, count( $terms ) );
|
||||
$this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
|
||||
}
|
||||
|
||||
@ -296,7 +296,7 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
|
||||
$this->assertEquals( 1, $term->count );
|
||||
|
||||
$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
|
||||
$this->assertNotEmpty( $terms );
|
||||
$this->assertEquals( 1, count( $terms ) );
|
||||
$this->assertEquals( array( 'Cheese' ), wp_list_pluck( $terms, 'name' ) );
|
||||
|
||||
_unregister_taxonomy( $tax );
|
||||
@ -320,9 +320,20 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
|
||||
$this->assertEquals( 1, $term->count );
|
||||
|
||||
$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
|
||||
$this->assertNotEmpty( $terms );
|
||||
$this->assertEquals( 1, count( $terms ) );
|
||||
$this->assertEquals( array( 'term1' ), wp_list_pluck( $terms, 'name' ) );
|
||||
|
||||
_unregister_taxonomy( $tax );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 27123
|
||||
*/
|
||||
function test_get_terms_child_of() {
|
||||
$parent = $this->factory->category->create();
|
||||
$child = $this->factory->category->create( array( 'parent' => $parent ) );
|
||||
|
||||
$terms = get_terms( 'category', array( 'child_of' => $parent, 'hide_empty' => false ) );
|
||||
$this->assertEquals( 1, count( $terms ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user