Make WordPress Core

Changeset 34538


Ignore:
Timestamp:
09/25/2015 01:46:36 PM (11 years ago)
Author:
boonebgorges
Message:

Bust term query cache when modifying term meta.

The 'last_changed' incrementor is used to invalidate the get_terms() query
cache. Since get_terms() queries may reference 'meta_query', changing term
metadata could change the results of the queries. So we invalidate the cache
on add, delete, and update.

See #10142.

Location:
trunk
Files:
2 edited

Legend:

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

    r34534 r34538  
    14891489 */
    14901490function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
    1491         return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
     1491        $added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
     1492
     1493        // Bust term query cache.
     1494        if ( $added ) {
     1495                wp_cache_set( 'last_changed', microtime(), 'terms' );
     1496        }
     1497
     1498        return $added;
    14921499}
    14931500
     
    15031510 */
    15041511function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
    1505         return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
     1512        $deleted = delete_metadata( 'term', $term_id, $meta_key, $meta_value );
     1513
     1514        // Bust term query cache.
     1515        if ( $deleted ) {
     1516                wp_cache_set( 'last_changed', microtime(), 'terms' );
     1517        }
     1518
     1519        return $deleted;
    15061520}
    15071521
     
    15371551 */
    15381552function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
    1539         return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
     1553        $updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
     1554
     1555        // Bust term query cache.
     1556        if ( $updated ) {
     1557                wp_cache_set( 'last_changed', microtime(), 'terms' );
     1558        }
     1559
     1560        return $updated;
    15401561}
    15411562
  • trunk/tests/phpunit/tests/term/meta.php

    r34529 r34538  
    145145                }
    146146        }
     147
     148        public function test_adding_term_meta_should_bust_get_terms_cache() {
     149                $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     150
     151                add_term_meta( $terms[0], 'foo', 'bar' );
     152
     153                // Prime cache.
     154                $found = get_terms( 'wptests_tax', array(
     155                        'hide_empty' => false,
     156                        'fields' => 'ids',
     157                        'meta_query' => array(
     158                                array(
     159                                        'key' => 'foo',
     160                                        'value' => 'bar',
     161                                ),
     162                        ),
     163                ) );
     164
     165                $this->assertEqualSets( array( $terms[0] ), $found );
     166
     167                add_term_meta( $terms[1], 'foo', 'bar' );
     168
     169                $found = get_terms( 'wptests_tax', array(
     170                        'hide_empty' => false,
     171                        'fields' => 'ids',
     172                        'meta_query' => array(
     173                                array(
     174                                        'key' => 'foo',
     175                                        'value' => 'bar',
     176                                ),
     177                        ),
     178                ) );
     179
     180                $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     181        }
     182
     183        public function test_updating_term_meta_should_bust_get_terms_cache() {
     184                $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     185
     186                add_term_meta( $terms[0], 'foo', 'bar' );
     187                add_term_meta( $terms[1], 'foo', 'baz' );
     188
     189                // Prime cache.
     190                $found = get_terms( 'wptests_tax', array(
     191                        'hide_empty' => false,
     192                        'fields' => 'ids',
     193                        'meta_query' => array(
     194                                array(
     195                                        'key' => 'foo',
     196                                        'value' => 'bar',
     197                                ),
     198                        ),
     199                ) );
     200
     201                $this->assertEqualSets( array( $terms[0] ), $found );
     202
     203                update_term_meta( $terms[1], 'foo', 'bar' );
     204
     205                $found = get_terms( 'wptests_tax', array(
     206                        'hide_empty' => false,
     207                        'fields' => 'ids',
     208                        'meta_query' => array(
     209                                array(
     210                                        'key' => 'foo',
     211                                        'value' => 'bar',
     212                                ),
     213                        ),
     214                ) );
     215
     216                $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     217        }
     218
     219        public function test_deleting_term_meta_should_bust_get_terms_cache() {
     220                $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     221
     222                add_term_meta( $terms[0], 'foo', 'bar' );
     223                add_term_meta( $terms[1], 'foo', 'bar' );
     224
     225                // Prime cache.
     226                $found = get_terms( 'wptests_tax', array(
     227                        'hide_empty' => false,
     228                        'fields' => 'ids',
     229                        'meta_query' => array(
     230                                array(
     231                                        'key' => 'foo',
     232                                        'value' => 'bar',
     233                                ),
     234                        ),
     235                ) );
     236
     237                $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     238
     239                delete_term_meta( $terms[1], 'foo', 'bar' );
     240
     241                $found = get_terms( 'wptests_tax', array(
     242                        'hide_empty' => false,
     243                        'fields' => 'ids',
     244                        'meta_query' => array(
     245                                array(
     246                                        'key' => 'foo',
     247                                        'value' => 'bar',
     248                                ),
     249                        ),
     250                ) );
     251
     252                $this->assertEqualSets( array( $terms[0] ), $found );
     253        }
    147254}
Note: See TracChangeset for help on using the changeset viewer.