Make WordPress Core

Changeset 34812


Ignore:
Timestamp:
10/03/2015 09:18:55 PM (9 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r34811 r34812  
    43914391
    43924392    $object_terms = get_object_term_cache( $object_id, $taxonomy );
    4393     if ( false === $object_terms )
    4394          $object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
     4393    if ( false === $object_terms ) {
     4394        $object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
     4395        wp_cache_set( $object_id, $object_terms, "{$taxonomy}_relationships" );
     4396    }
    43954397
    43964398    if ( is_wp_error( $object_terms ) )
  • trunk/tests/phpunit/tests/term/isObjectInTerm.php

    r30205 r34812  
    101101        $this->assertTrue( is_object_in_term( $post_ID, 'wptests_tax', $int_tax_name ) );
    102102    }
     103
     104    /**
     105     * @ticket 32044
     106     */
     107    public function test_should_populate_and_hit_relationships_cache() {
     108        global $wpdb;
     109
     110        register_taxonomy( 'wptests_tax', 'post' );
     111        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     112
     113        $o = 12345;
     114        wp_set_object_terms( $o, $terms[0], 'wptests_tax' );
     115
     116        $num_queries = $wpdb->num_queries;
     117        $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) );
     118        $num_queries++;
     119        $this->assertSame( $num_queries, $wpdb->num_queries );
     120
     121        $this->assertFalse( is_object_in_term( $o, 'wptests_tax', $terms[1] ) );
     122        $this->assertSame( $num_queries, $wpdb->num_queries );
     123    }
     124
     125    /**
     126     * @ticket 32044
     127     */
     128    public function test_should_not_be_fooled_by_a_stale_relationship_cache() {
     129        global $wpdb;
     130
     131        register_taxonomy( 'wptests_tax', 'post' );
     132        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     133
     134        $o = 12345;
     135        wp_set_object_terms( $o, $terms[0], 'wptests_tax' );
     136
     137        $num_queries = $wpdb->num_queries;
     138        $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) );
     139        $num_queries++;
     140        $this->assertSame( $num_queries, $wpdb->num_queries );
     141
     142        wp_set_object_terms( $o, $terms[1], 'wptests_tax' );
     143
     144        $num_queries = $wpdb->num_queries;
     145        $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[1] ) );
     146        $num_queries++;
     147        $this->assertSame( $num_queries, $wpdb->num_queries );
     148    }
    103149}
Note: See TracChangeset for help on using the changeset viewer.