Update the taxonomy relationship cache in is_object_in_term()
.
This function attempts to read from the relationship cache, and uses any data it finds. If it finds no data, it does a query for the data it needs. Since we are going to the trouble to query for the relationships, and since we are already using cached data when available, let's go ahead and cache it for later use. Props joehoyle, boonebgorges. Fixes #32044. git-svn-id: https://develop.svn.wordpress.org/trunk@34812 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
8974a2bb33
commit
ffa997a342
@ -4390,8 +4390,10 @@ function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
|
||||
return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
|
||||
|
||||
$object_terms = get_object_term_cache( $object_id, $taxonomy );
|
||||
if ( false === $object_terms )
|
||||
$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
|
||||
if ( false === $object_terms ) {
|
||||
$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
|
||||
wp_cache_set( $object_id, $object_terms, "{$taxonomy}_relationships" );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $object_terms ) )
|
||||
return $object_terms;
|
||||
|
@ -100,4 +100,50 @@ class Tests_IsObjectInTerm extends WP_UnitTestCase {
|
||||
wp_set_object_terms( $post_ID, array( $int_tax_name ), 'wptests_tax' );
|
||||
$this->assertTrue( is_object_in_term( $post_ID, 'wptests_tax', $int_tax_name ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 32044
|
||||
*/
|
||||
public function test_should_populate_and_hit_relationships_cache() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
$o = 12345;
|
||||
wp_set_object_terms( $o, $terms[0], 'wptests_tax' );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) );
|
||||
$num_queries++;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
$this->assertFalse( is_object_in_term( $o, 'wptests_tax', $terms[1] ) );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 32044
|
||||
*/
|
||||
public function test_should_not_be_fooled_by_a_stale_relationship_cache() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
$o = 12345;
|
||||
wp_set_object_terms( $o, $terms[0], 'wptests_tax' );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) );
|
||||
$num_queries++;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
wp_set_object_terms( $o, $terms[1], 'wptests_tax' );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[1] ) );
|
||||
$num_queries++;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user