Make WordPress Core

Changeset 36358


Ignore:
Timestamp:
01/20/2016 05:34:30 AM (9 years ago)
Author:
dd32
Message:

Taxonomy: Populate term cache with proper clone of term objects.

[34999] modified the cache strategy for terms in the context of
wp_get_object_terms(). As part of these changes, the object_id property of
term objects had to be unset before being cached. To avoid modifying passed-by-
reference terms, update_term_cache() attempted to make a copy of the terms
passed to the function; however, it failed to use the clone keyword, and thus
only created a reference instead of a copy.

Merges [36323] to the 4.4 branch.
Props berengerzyla.
Fixes #35462.

Location:
branches/4.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/taxonomy.php

    r36109 r36358  
    36473647    foreach ( (array) $terms as $term ) {
    36483648        // Create a copy in case the array was passed by reference.
    3649         $_term = $term;
     3649        $_term = clone $term;
    36503650
    36513651        // Object ID should not be cached.
  • branches/4.4/tests/phpunit/tests/term/cache.php

    r35242 r36358  
    201201        _unregister_taxonomy( 'wptests_tax' );
    202202    }
     203
     204    /**
     205     * @ticket 35462
     206     */
     207    public function test_term_objects_should_not_be_modified_by_update_term_cache() {
     208        register_taxonomy( 'wptests_tax', 'post' );
     209        $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     210        $p = self::factory()->post->create();
     211
     212        wp_set_object_terms( $p, $t, 'wptests_tax' );
     213
     214        $terms = wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'all_with_object_id' ) );
     215
     216        update_term_cache( $terms );
     217
     218        foreach ( $terms as $term ) {
     219            $this->assertSame( $p, $term->object_id );
     220        }
     221    }
    203222}
Note: See TracChangeset for help on using the changeset viewer.