Make WordPress Core

Changeset 27125


Ignore:
Timestamp:
02/07/2014 07:53:01 PM (11 years ago)
Author:
wonderboymusic
Message:

When a term_id matches in _get_term_children(), recurse through its children until there is no more depth in the hierarchy. Since get_terms() return terms with a count of 0 when their children are not empty, we must return all children so that get_terms() can check their count.

In [27108], #26903 was fixed, but only because we were using the example in the ticket, leaving out infinite depth for hierarchical taxonomies.

Adds unit tests, including Tests_Term_getTerms::test_get_terms_seven_levels_deep().

Fixes #26903. Again.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r27115 r27125  
    30053005        if ( $term->term_id == $term_id ) {
    30063006            if ( isset( $has_children[$term_id] ) ) {
    3007                 foreach ( $has_children[$term_id] as $t_id ) {
    3008                     if ( $use_id ) {
    3009                         $term_list[] = $t_id;
    3010                     } else {
    3011                         $term_list[] = get_term( $t_id, $taxonomy );
     3007                $current_id = $term_id;
     3008                while ( $current_id > 0 ) {
     3009                    foreach ( $has_children[$current_id] as $t_id ) {
     3010                        if ( $use_id ) {
     3011                            $term_list[] = $t_id;
     3012                        } else {
     3013                            $term_list[] = get_term( $t_id, $taxonomy );
     3014                        }
    30123015                    }
     3016
     3017                    $current_id = isset( $has_children[$t_id] ) ? $t_id : 0;
    30133018                }
    30143019            }
  • trunk/tests/phpunit/tests/term/getTerms.php

    r27108 r27125  
    274274        $this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
    275275    }
     276
     277    function test_get_terms_grandparent_zero() {
     278        $tax = 'food';
     279        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     280
     281        $cheese = $this->factory->term->create( array( 'name' => 'Cheese', 'taxonomy' => $tax ) );
     282        $cheddar = $this->factory->term->create( array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ) );
     283        $spread = $this->factory->term->create( array( 'name' => 'Spread', 'parent' => $cheddar, 'taxonomy' => $tax ) );
     284        $post_id = $this->factory->post->create();
     285        wp_set_post_terms( $post_id, $spread, $tax );
     286        $term = get_term( $spread, $tax );
     287        $this->assertEquals( 1, $term->count );
     288
     289        $terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
     290        $this->assertNotEmpty( $terms );
     291        $this->assertEquals( array( 'Cheese' ), wp_list_pluck( $terms, 'name' ) );
     292
     293        _unregister_taxonomy( $tax );
     294    }
     295
     296    function test_get_terms_seven_levels_deep() {
     297        $tax = 'deep';
     298        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     299        $parent = 0;
     300        $t = array();
     301        foreach ( range( 1, 7 ) as $depth ) {
     302            $t[$depth] = $this->factory->term->create( array( 'name' => 'term' . $depth, 'taxonomy' => $tax, 'parent' => $parent ) );
     303            $parent = $t[$depth];
     304        }
     305        $post_id = $this->factory->post->create();
     306        wp_set_post_terms( $post_id, $t[7], $tax );
     307        $term = get_term( $t[7], $tax );
     308        $this->assertEquals( 1, $term->count );
     309
     310        $terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
     311        $this->assertNotEmpty( $terms );
     312        $this->assertEquals( array( 'term1' ), wp_list_pluck( $terms, 'name' ) );
     313
     314        _unregister_taxonomy( $tax );
     315    }
    276316}
Note: See TracChangeset for help on using the changeset viewer.