Skip to content

is_object_in_term: Honor object addition suspend and prevent memory leak #9054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4892,7 +4892,7 @@ function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
return $object_terms;
}

wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
wp_cache_add( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
}

if ( is_wp_error( $object_terms ) ) {
Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/tests/term/isObjectInTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,32 @@ public function test_should_not_be_fooled_by_a_stale_relationship_cache() {
public function test_invalid_taxonomy_should_return_wp_error_object() {
$this->assertWPError( is_object_in_term( 12345, 'foo', 'bar' ) );
}

/**
* @ticket 63618
*/
public function test_term_objects_should_not_be_added_to_cache_when_suspend_cache_addition() {
register_taxonomy( 'wptests_tax', 'post' );

$t = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'slug' => 'foo',
)
);

$object_id = 12345;
wp_cache_delete( $object_id, 'wptests_tax_relationships' );

$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );

is_object_in_term( $object_id, 'wptests_tax', $t );

wp_suspend_cache_addition( $suspend );

$this->assertFalse( wp_cache_get( $object_id, 'wptests_tax_relationships' ) );

_unregister_taxonomy( 'wptests_tax', 'post' );
}
}
Loading