Make WordPress Core


Ignore:
Timestamp:
07/13/2017 01:42:44 PM (9 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.

Ports [41037] to the 4.8 branch.

Props bor0, atanasangelovdev.
Fixes #41010.

Location:
branches/4.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • branches/4.8/tests/phpunit/tests/term/wpGetObjectTerms.php

    r40290 r41038  
    733733                $this->assertEquals( array( $t1, $t3, $t2 ), $found );
    734734        }
     735
     736        /**
     737         * @ticket 41010
     738         */
     739        public function test_duplicate_terms_should_not_be_returned_when_passed_multiple_taxonomies_registered_with_args_array() {
     740                $taxonomy1 = 'wptests_tax';
     741                $taxonomy2 = 'wptests_tax_2';
     742
     743                // Any non-empty 'args' array triggers the bug.
     744                $taxonomy_arguments = array(
     745                        'args' => array( 0 ),
     746                );
     747
     748                register_taxonomy( $taxonomy1, 'post', $taxonomy_arguments );
     749                register_taxonomy( $taxonomy2, 'post', $taxonomy_arguments );
     750
     751                $post_id = self::factory()->post->create();
     752                $term_1_id = self::factory()->term->create( array(
     753                        'taxonomy' => $taxonomy1,
     754                ) );
     755                $term_2_id = self::factory()->term->create( array(
     756                        'taxonomy' => $taxonomy2,
     757                ) );
     758
     759                wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 );
     760                wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 );
     761
     762                $expected = array( $term_1_id, $term_2_id );
     763
     764                $actual = wp_get_object_terms( $post_id, array( $taxonomy1, $taxonomy2 ), array(
     765                        'orderby' => 'term_id',
     766                        'fields' => 'ids',
     767                ) );
     768
     769                $this->assertEqualSets( $expected, $actual );
     770        }
    735771}
Note: See TracChangeset for help on using the changeset viewer.