Make WordPress Core


Ignore:
Timestamp:
01/24/2015 06:47:30 PM (10 years ago)
Author:
boonebgorges
Message:

Introduce 'childless' parameter to get_terms().

This new parameter allows developers to limit queried terms to terminal nodes -
ie, those without any descendants.

As part of the improvement, some internal logic in get_terms() has been
consolidated. Parameters that resolve to a NOT IN clause containing term IDs
('exclude', 'exclude_tree', and 'childless') are now parsed into a single
"exclusions" array before the SQL clause is generated.

Props theMikeD, horike.
Fixes #29839.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTerms.php

    r31248 r31275  
    494494
    495495        $this->assertEqualSets( array( $t3, $t1 ), $found );
     496    }
     497
     498    /**
     499     * @ticket 29839
     500     */
     501    public function test_childless_should_return_all_terms_for_flat_hierarchy() {
     502        // If run on a flat hierarchy it should return everything.
     503        $flat_tax = 'countries';
     504        register_taxonomy( $flat_tax, 'post', array( 'hierarchical' => false ) );
     505        $australia = $this->factory->term->create( array( 'name' => 'Australia', 'taxonomy' => $flat_tax ) );
     506        $china     = $this->factory->term->create( array( 'name' => 'China',     'taxonomy' => $flat_tax ) );
     507        $tanzania  =  $this->factory->term->create( array( 'name' => 'Tanzania',  'taxonomy' => $flat_tax ) );
     508
     509        $terms = get_terms( $flat_tax, array(
     510            'childless' => true,
     511            'hide_empty' => false,
     512            'fields' => 'ids',
     513        ) );
     514
     515        $expected = array( $australia, $china, $tanzania );
     516        $this->assertEqualSets( $expected, $terms );
     517    }
     518
     519
     520    /**
     521     * @ticket 29839
     522     */
     523    public function test_childless_hierarchical_taxonomy() {
     524        $tax = 'location';
     525        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     526        /*
     527        Canada
     528            Ontario
     529                Ottawa
     530                    Nepean
     531                Toronto
     532            Quebec
     533                Montreal
     534            PEI
     535        */
     536        // Level 1
     537        $canada = $this->factory->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) );
     538
     539        // Level 2
     540        $ontario = $this->factory->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) );
     541        $quebec  = $this->factory->term->create( array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ) );
     542        $pei     = $this->factory->term->create( array( 'name' => 'PEI', 'parent' => $canada, 'taxonomy' => $tax ) );
     543
     544        // Level 3
     545        $toronto  = $this->factory->term->create( array( 'name' => 'Toronto', 'parent' => $ontario, 'taxonomy' => $tax ) );
     546        $ottawa   = $this->factory->term->create( array( 'name' => 'Ottawa', 'parent' => $ontario, 'taxonomy' => $tax ) );
     547        $montreal = $this->factory->term->create( array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ) );
     548
     549        // Level 4
     550        $nepean = $this->factory->term->create( array( 'name' => 'Nepean', 'parent' => $ottawa, 'taxonomy' => $tax ) );
     551
     552        $terms = get_terms( $tax, array(
     553            'childless' => true,
     554            'hide_empty' => false,
     555            'fields' => 'ids',
     556        ) );
     557
     558        $this->assertEqualSets( array( $montreal, $nepean, $toronto, $pei ), $terms );
     559    }
     560
     561    /**
     562     * @ticket 29839
     563     */
     564    public function test_childless_hierarchical_taxonomy_used_with_child_of() {
     565        $tax = 'location';
     566        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     567
     568        // Level 1
     569        $canada = $this->factory->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) );
     570
     571        // Level 2
     572        $ontario = $this->factory->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) );
     573        $quebec  = $this->factory->term->create( array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ) );
     574
     575        // Level 3
     576        $laval   = $this->factory->term->create( array( 'name' => 'Laval', 'parent' => $quebec, 'taxonomy' => $tax ) );
     577        $montreal = $this->factory->term->create( array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ) );
     578
     579        // Level 4
     580        $dorval = $this->factory->term->create( array( 'name' => 'Dorval', 'parent' => $montreal, 'taxonomy' => $tax ) );
     581
     582        $terms = get_terms( $tax, array(
     583            'childless' => true,
     584            'child_of' => $quebec,
     585            'hide_empty' => false,
     586            'fields' => 'ids',
     587        ) );
     588
     589        $this->assertEqualSets( array( $laval ), $terms );
     590    }
     591
     592    /**
     593     * @ticket 29839
     594     */
     595    public function test_childless_should_enforce_childless_status_for_all_queried_taxonomies() {
     596        register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) );
     597        register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
     598
     599        $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     600        $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ) );
     601        $t3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     602        $t4 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t3 ) );
     603
     604        $found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
     605            'fields' => 'ids',
     606            'hide_empty' => false,
     607            'childless' => true,
     608        ) );
     609
     610        $this->assertEqualSets( array( $t2, $t4 ), $found );
    496611    }
    497612
Note: See TracChangeset for help on using the changeset viewer.