Make WordPress Core

Changeset 31287


Ignore:
Timestamp:
01/28/2015 08:42:25 PM (9 years ago)
Author:
boonebgorges
Message:

Don't use term IDs for array indexes when caching object terms.

Uncached results pulled from wp_get_object_terms() are zero-indexed (ie 0,
1, 2...). As a result, get_the_terms() was returning a strictly different
array when pulling from the cache and when the cache was empty.

Props joshlevinson.
Fixes #31086.

Location:
trunk
Files:
2 edited

Legend:

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

    r31285 r31287  
    37843784    $object_terms = array();
    37853785    foreach ( (array) $terms as $term )
    3786         $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;
     3786        $object_terms[$term->object_id][$term->taxonomy][] = $term;
    37873787
    37883788    foreach ( $ids as $id ) {
  • trunk/tests/phpunit/tests/term.php

    r31230 r31287  
    15861586
    15871587    /**
     1588     * @ticket 31086
     1589     */
     1590    public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() {
     1591        register_taxonomy( 'wptests_tax', 'post' );
     1592        $p = $this->factory->post->create();
     1593        wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
     1594
     1595        $found = get_the_terms( $p, 'wptests_tax' );
     1596
     1597        $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
     1598    }
     1599
     1600    /**
     1601     * @ticket 31086
     1602     */
     1603    public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() {
     1604        register_taxonomy( 'wptests_tax', 'post' );
     1605        $p = $this->factory->post->create();
     1606        wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
     1607
     1608        // Prime cache.
     1609        update_object_term_cache( array( $p ), array( 'post' ) );
     1610
     1611        $found = get_the_terms( $p, 'wptests_tax' );
     1612
     1613        $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
     1614    }
     1615
     1616    /**
    15881617     * @ticket 19205
    15891618     */
Note: See TracChangeset for help on using the changeset viewer.