Make WordPress Core

Changeset 27458


Ignore:
Timestamp:
03/07/2014 07:29:01 PM (10 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r27457 r27458  
    14781478        foreach ( $terms as $k => $term ) {
    14791479            if ( ! $term->count ) {
    1480                 $children = _get_term_children( $term->term_id, $terms, reset( $taxonomies ) );
    1481                 if ( is_array( $children ) )
    1482                     foreach ( $children as $child )
    1483                         if ( $child->count )
     1480                $children = get_term_children( $term->term_id, reset( $taxonomies ) );
     1481                if ( is_array( $children ) ) {
     1482                    foreach ( $children as $child_id ) {
     1483                        $child = get_term( $child_id, reset( $taxonomies ) );
     1484                        if ( $child->count ) {
    14841485                            continue 2;
     1486                        }
     1487                    }
     1488                }
    14851489
    14861490                // It really is empty
     
    29242928
    29252929        if ( $term->term_id == $term_id ) {
    2926             if ( isset( $has_children[$term_id] ) ) {
    2927                 $current_id = $term_id;
    2928                 while ( $current_id > 0 ) {
    2929                     foreach ( $has_children[$current_id] as $t_id ) {
    2930                         if ( $use_id ) {
    2931                             $term_list[] = $t_id;
    2932                         } else {
    2933                             $term_list[] = get_term( $t_id, $taxonomy );
    2934                         }
    2935                     }
    2936 
    2937                     $current_id = isset( $has_children[$t_id] ) ? $t_id : 0;
    2938                 }
    2939             }
    29402930            continue;
    29412931        }
  • trunk/tests/phpunit/tests/term/getTerms.php

    r27197 r27458  
    277277
    278278        $terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
    279         $this->assertNotEmpty( $terms );
     279        $this->assertEquals( 2, count( $terms ) );
    280280        $this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
    281281    }
     
    297297
    298298        $terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
    299         $this->assertNotEmpty( $terms );
     299        $this->assertEquals( 1, count( $terms ) );
    300300        $this->assertEquals( array( 'Cheese' ), wp_list_pluck( $terms, 'name' ) );
    301301
     
    321321
    322322        $terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
    323         $this->assertNotEmpty( $terms );
     323        $this->assertEquals( 1, count( $terms ) );
    324324        $this->assertEquals( array( 'term1' ), wp_list_pluck( $terms, 'name' ) );
    325325
    326326        _unregister_taxonomy( $tax );
    327327    }
     328
     329    /**
     330     * @ticket 27123
     331     */
     332    function test_get_terms_child_of() {
     333        $parent = $this->factory->category->create();
     334        $child = $this->factory->category->create( array( 'parent' => $parent ) );
     335
     336        $terms = get_terms( 'category', array( 'child_of' => $parent, 'hide_empty' => false ) );
     337        $this->assertEquals( 1, count( $terms ) );
     338    }
    328339}
Note: See TracChangeset for help on using the changeset viewer.