Make WordPress Core

Changeset 41037


Ignore:
Timestamp:
07/13/2017 01:40:39 PM (7 years ago)
Author:
boonebgorges
Message:

Taxonomy: Avoid duplicates when querying for terms in taxonomies registered with $args parameter.

[40514] introduced a bug that caused term queries to return some duplicates
when the $taxonomies array contained only taxonomies that were originally
registered with an $args array. We fix this bug by ensuring that
recursive get_terms() queries stop when all queried $taxonomies have
already been referenced.

Props bor0, atanasangelovdev.
Fixes #41010.

Location:
trunk
Files:
2 edited

Legend:

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

    r40994 r41037  
    19181918    $args['object_ids'] = $object_ids;
    19191919
    1920     $terms = array_merge( $terms, get_terms( $args ) );
     1920    // Taxonomies registered without an 'args' param are handled here.
     1921    if ( ! empty( $taxonomies ) ) {
     1922        $terms = array_merge( $terms, get_terms( $args ) );
     1923    }
    19211924
    19221925    /**
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r40994 r41037  
    761761        return $args;
    762762    }
     763
     764    /**
     765     * @ticket 41010
     766     */
     767    public function test_duplicate_terms_should_not_be_returned_when_passed_multiple_taxonomies_registered_with_args_array() {
     768        $taxonomy1 = 'wptests_tax';
     769        $taxonomy2 = 'wptests_tax_2';
     770
     771        // Any non-empty 'args' array triggers the bug.
     772        $taxonomy_arguments = array(
     773            'args' => array( 0 ),
     774        );
     775
     776        register_taxonomy( $taxonomy1, 'post', $taxonomy_arguments );
     777        register_taxonomy( $taxonomy2, 'post', $taxonomy_arguments );
     778
     779        $post_id = self::factory()->post->create();
     780        $term_1_id = self::factory()->term->create( array(
     781            'taxonomy' => $taxonomy1,
     782        ) );
     783        $term_2_id = self::factory()->term->create( array(
     784            'taxonomy' => $taxonomy2,
     785        ) );
     786
     787        wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 );
     788        wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 );
     789
     790        $expected = array( $term_1_id, $term_2_id );
     791
     792        $actual = wp_get_object_terms( $post_id, array( $taxonomy1, $taxonomy2 ), array(
     793            'orderby' => 'term_id',
     794            'fields' => 'ids',
     795        ) );
     796
     797        $this->assertEqualSets( $expected, $actual );
     798    }
    763799}
Note: See TracChangeset for help on using the changeset viewer.