WordPress.org

Make WordPress Core

Changeset 27108


Ignore:
Timestamp:
02/06/2014 05:44:50 PM (4 years ago)
Author:
wonderboymusic
Message:

In get_terms(), don't set automatically hierarchical to false when parent => 0 is passed. The default value for parent is ''.

In _get_term_children(), don't skip a top-level term without first including its children in the returned term list. Ironically, the call to _get_term_children() in get_terms() has a comment stating "Make sure we show empty categories that have children.", but it didn't work if you were retrieving top-level categories only.

All unit tests pass. Added a unit test based on the use case described in this ticket.

Fixes #26903.

Location:
trunk
Files:
2 edited

Legend:

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

    r27102 r27108  
    12801280    $args['offset'] = absint( $args['offset'] );
    12811281    if ( !$single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ||
    1282         '' !== $args['parent'] ) {
     1282        ( '' !== $args['parent'] && 0 !== $args['parent'] ) ) {
    12831283        $args['child_of'] = 0;
    12841284        $args['hierarchical'] = false;
     
    30033003        }
    30043004
    3005         if ( $term->term_id == $term_id )
     3005        if ( $term->term_id == $term_id ) {
     3006            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 );
     3012                    }
     3013                }
     3014            }
    30063015            continue;
     3016        }
    30073017
    30083018        if ( $term->parent == $term_id ) {
     
    35253535 * Determine if a post's cache for the passed taxonomy
    35263536 *  is in sync.
    3527  * 
     3537 *
    35283538 * @since 3.9.0
    35293539 *
  • trunk/tests/phpunit/tests/term/getTerms.php

    r26187 r27108  
    77    function setUp() {
    88        parent::setUp();
    9        
     9
    1010        _clean_term_filters();
    1111        wp_cache_delete( 'last_changed', 'terms' );
     
    225225        $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms8 );
    226226    }
     227
     228    function test_get_terms_parent_zero() {
     229        $tax = 'food';
     230        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     231
     232        $cheese = $this->factory->term->create( array( 'name' => 'Cheese', 'taxonomy' => $tax ) );
     233
     234        $cheddar = $this->factory->term->create( array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ) );
     235
     236        $post_ids = $this->factory->post->create_many( 2 );
     237        foreach ( $post_ids as $id ) {
     238            wp_set_post_terms( $id, $cheddar, $tax );
     239        }
     240        $term = get_term( $cheddar, $tax );
     241        $this->assertEquals( 2, $term->count );
     242
     243        $brie = $this->factory->term->create( array( 'name' => 'Brie', 'parent' => $cheese, 'taxonomy' => $tax ) );
     244        $post_ids = $this->factory->post->create_many( 7 );
     245        foreach ( $post_ids as $id ) {
     246            wp_set_post_terms( $id, $brie, $tax );
     247        }
     248        $term = get_term( $brie, $tax );
     249        $this->assertEquals( 7, $term->count );
     250
     251        $crackers = $this->factory->term->create( array( 'name' => 'Crackers', 'taxonomy' => $tax ) );
     252
     253        $butter = $this->factory->term->create( array( 'name' => 'Butter', 'parent' => $crackers, 'taxonomy' => $tax ) );
     254        $post_ids = $this->factory->post->create_many( 1 );
     255        foreach ( $post_ids as $id ) {
     256            wp_set_post_terms( $id, $butter, $tax );
     257        }
     258        $term = get_term( $butter, $tax );
     259        $this->assertEquals( 1, $term->count );
     260
     261        $multigrain = $this->factory->term->create( array( 'name' => 'Multigrain', 'parent' => $crackers, 'taxonomy' => $tax ) );
     262        $post_ids = $this->factory->post->create_many( 3 );
     263        foreach ( $post_ids as $id ) {
     264            wp_set_post_terms( $id, $multigrain, $tax );
     265        }
     266        $term = get_term( $multigrain, $tax );
     267        $this->assertEquals( 3, $term->count );
     268
     269        $fruit = $this->factory->term->create( array( 'name' => 'Fruit', 'taxonomy' => $tax ) );
     270        $cranberries = $this->factory->term->create( array( 'name' => 'Cranberries', 'parent' => $fruit, 'taxonomy' => $tax ) );
     271
     272        $terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
     273        $this->assertNotEmpty( $terms );
     274        $this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
     275    }
    227276}
Note: See TracChangeset for help on using the changeset viewer.