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. Props berengerzyla. Fixes #35462. git-svn-id: https://develop.svn.wordpress.org/trunk@36323 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
03efa5ca83
commit
4d17d22a47
@ -3721,7 +3721,7 @@ function update_object_term_cache($object_ids, $object_type) {
|
||||
function update_term_cache( $terms, $taxonomy = '' ) {
|
||||
foreach ( (array) $terms as $term ) {
|
||||
// Create a copy in case the array was passed by reference.
|
||||
$_term = $term;
|
||||
$_term = clone $term;
|
||||
|
||||
// Object ID should not be cached.
|
||||
unset( $_term->object_id );
|
||||
|
@ -200,4 +200,23 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
|
||||
_unregister_taxonomy( 'wptests_tax' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35462
|
||||
*/
|
||||
public function test_term_objects_should_not_be_modified_by_update_term_cache() {
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
||||
$p = self::factory()->post->create();
|
||||
|
||||
wp_set_object_terms( $p, $t, 'wptests_tax' );
|
||||
|
||||
$terms = wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'all_with_object_id' ) );
|
||||
|
||||
update_term_cache( $terms );
|
||||
|
||||
foreach ( $terms as $term ) {
|
||||
$this->assertSame( $p, $term->object_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user