From 3010c5aa204a76294822ebd5854ae0d9d4d9f74f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 7 Mar 2014 19:29:01 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 26 ++++++++------------------ tests/phpunit/tests/term/getTerms.php | 17 ++++++++++++++--- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index de6adbb0b9..a7baa167ef 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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; } diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index c0da2474bb..82af1a7983 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -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 ) ); + } }