Make WordPress Core

Changeset 28583


Ignore:
Timestamp:
05/27/2014 03:28:05 AM (11 years ago)
Author:
wonderboymusic
Message:

In wp_get_object_terms(), before returning terms (and before running them through the 'wp_get_object_terms' filter) - run them through $terms = array_values( array_unique( $terms, SORT_REGULAR ) ).

There will be "dupes" when the function is called with 'fields' => 'all_with_object_id', but the objects will actually be unique due to the object_id addition, so they shouldn't be filtered out.

Adds unit tests. All other unit tests pass.

Fixes #11003.

Location:
trunk
Files:
2 edited

Legend:

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

    r28562 r28583  
    23382338    }
    23392339
    2340     if ( ! $terms )
     2340    if ( ! $terms ) {
    23412341        $terms = array();
    2342 
     2342    } else {
     2343        $terms = array_values( array_unique( $terms, SORT_REGULAR ) );
     2344    }
    23432345    /**
    23442346     * Filter the terms for a given object or objects.
  • trunk/tests/phpunit/tests/term.php

    r28566 r28583  
    571571        $this->assertEquals( 0, $count );
    572572    }
     573
     574    function test_wp_get_object_terms_no_dupes() {
     575        $post_id1 = $this->factory->post->create();
     576        $post_id2 = $this->factory->post->create();
     577        $cat_id = $this->factory->category->create();
     578        $cat_id2 = $this->factory->category->create();
     579        wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) );
     580        wp_set_post_categories( $post_id2, $cat_id );
     581
     582        $terms = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category' );
     583        $this->assertCount( 2, $terms );
     584        $this->assertEquals( array( $cat_id, $cat_id2 ), wp_list_pluck( $terms, 'term_id' ) );
     585
     586        $terms2 = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category', array(
     587            'fields' => 'all_with_object_id'
     588        ) );
     589
     590        $this->assertCount( 3, $terms2 );
     591        $this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) );
     592    }
    573593}
Note: See TracChangeset for help on using the changeset viewer.