Make WordPress Core

Ticket #24461: 24461.patch

File 24461.patch, 3.9 KB (added by boonebgorges, 12 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 63d53a7..acf3c97 100644
    function _get_term_hierarchy($taxonomy) {  
    38183818 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
    38193819 * @return array The subset of $terms that are descendants of $term_id.
    38203820 */
    3821 function _get_term_children($term_id, $terms, $taxonomy) {
     3821function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
    38223822        $empty_array = array();
    38233823        if ( empty($terms) )
    38243824                return $empty_array;
    function _get_term_children($term_id, $terms, $taxonomy) {  
    38293829        if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
    38303830                return $empty_array;
    38313831
     3832        // We check $ancestors to avoid recursion, so we need to be sure the term itself is listed.
     3833        if ( empty( $ancestors ) ) {
     3834                $ancestors[] = $term_id;
     3835        }
     3836
    38323837        foreach ( (array) $terms as $term ) {
    38333838                $use_id = false;
    38343839                if ( !is_object($term) ) {
    function _get_term_children($term_id, $terms, $taxonomy) {  
    38383843                        $use_id = true;
    38393844                }
    38403845
    3841                 if ( $term->term_id == $term_id ) {
     3846                // Don't recurse if we've already identified the term as a child - this indicates a loop.
     3847                if ( in_array( $term->term_id, $ancestors ) ) {
    38423848                        continue;
    38433849                }
    38443850
    function _get_term_children($term_id, $terms, $taxonomy) {  
    38513857                        if ( !isset($has_children[$term->term_id]) )
    38523858                                continue;
    38533859
    3854                         if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )
     3860                        if ( $use_id ) {
     3861                                $ancestors = array_merge( $ancestors, $term_list );
     3862                        } else {
     3863                                $ancestors = array_merge( $ancestors, wp_list_pluck( $term_list, 'term_id' ) );
     3864                        }
     3865
     3866                        if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
    38553867                                $term_list = array_merge($term_list, $children);
    38563868                }
    38573869        }
  • tests/phpunit/tests/term/getTerms.php

    diff --git tests/phpunit/tests/term/getTerms.php tests/phpunit/tests/term/getTerms.php
    index 865357a..8a11365 100644
    class Tests_Term_getTerms extends WP_UnitTestCase {  
    394394                add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
    395395        }
    396396
     397        /**
     398         * @covers ::_get_term_children
     399         * @ticket 24461
     400         */
     401        public function test__get_term_children_handles_cycles() {
     402                remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
     403
     404                $c1 = $this->factory->category->create();
     405                $c2 = $this->factory->category->create( array( 'parent' => $c1 ) );
     406                $c3 = $this->factory->category->create( array( 'parent' => $c2 ) );
     407                wp_update_term( $c1, 'category', array( 'parent' => $c3 ) );
     408
     409                add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
     410
     411                $result = _get_term_children( $c1, array( $c1, $c2, $c3 ), 'category' );
     412
     413                $this->assertEqualSets( array( $c2, $c3 ), $result );
     414        }
     415
     416        /**
     417         * @covers ::_get_term_children
     418         * @ticket 24461
     419         */
     420        public function test__get_term_children_handles_cycles_when_terms_argument_contains_objects() {
     421                remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
     422
     423                $c1 = $this->factory->category->create_and_get();
     424                $c2 = $this->factory->category->create_and_get( array( 'parent' => $c1->term_id ) );
     425                $c3 = $this->factory->category->create_and_get( array( 'parent' => $c2->term_id ) );
     426                wp_update_term( $c1->term_id, 'category', array( 'parent' => $c3->term_id ) );
     427
     428                add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
     429
     430                $result = _get_term_children( $c1->term_id, array( $c1, $c2, $c3 ), 'category' );
     431
     432                $this->assertEqualSets( array( $c2, $c3 ), $result );
     433        }
     434
    397435        public function test_get_terms_by_slug() {
    398436                $t1 = $this->factory->tag->create( array( 'slug' => 'foo' ) );
    399437                $t2 = $this->factory->tag->create( array( 'slug' => 'bar' ) );