Make WordPress Core

Changeset 36511


Ignore:
Timestamp:
02/12/2016 02:46:28 AM (8 years ago)
Author:
boonebgorges
Message:

In delete_metadata(), only invalidate cache for affected objects.

The $delete_all flag in delete_metadata() triggers cache invalidation for
multiple objects. Previously, invalidation took place for all objects matching
the $meta_key parameter, regardless of whether $meta_value was also set.
This resulted in overly aggressive invalidation.

Props rahal.aboulfeth.
Fixes #35797.

Location:
trunk
Files:
2 edited

Legend:

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

    r36509 r36511  
    364364        return false;
    365365
    366     if ( $delete_all )
    367         $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
     366    if ( $delete_all ) {
     367        $value_clause = '';
     368        if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
     369            $value_clause = $wpdb->prepare( " AND meta_value = %s", $meta_value );
     370        }
     371
     372        $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s $value_clause", $meta_key ) );
     373    }
    368374
    369375    /**
  • trunk/tests/phpunit/tests/meta/deleteMetadata.php

    r32331 r36511  
    9898        $this->assertEqualSets( array(), $m );
    9999    }
     100
     101    /**
     102     * @ticket 35797
     103     */
     104    public function test_delete_all_should_only_invalidate_cache_for_objects_matching_meta_value() {
     105        $p1 = 1234;
     106        $p2 = 5678;
     107
     108        add_metadata( 'post', $p1, 'foo', 'value1' );
     109        add_metadata( 'post', $p2, 'foo', 'value2' );
     110
     111        // Prime caches.
     112        update_meta_cache( 'post', array( $p1, $p2 ) );
     113
     114        $deleted = delete_metadata( 'post', 5, 'foo', 'value1', true );
     115
     116        $p1_cache = wp_cache_get( $p1, 'post_meta' );
     117        $this->assertFalse( $p1_cache );
     118
     119        // Should not have been touched.
     120        $p2_cache = wp_cache_get( $p2, 'post_meta' );
     121        $this->assertNotEmpty( $p2_cache );
     122    }
     123
     124    /**
     125     * @ticket 35797
     126     */
     127    public function test_delete_all_should_invalidate_cache_for_all_objects_with_meta_key_when_meta_value_is_not_provided() {
     128        $p1 = 1234;
     129        $p2 = 5678;
     130
     131        add_metadata( 'post', $p1, 'foo', 'value1' );
     132        add_metadata( 'post', $p2, 'foo', 'value2' );
     133
     134        // Prime caches.
     135        update_meta_cache( 'post', array( $p1, $p2 ) );
     136
     137        $deleted = delete_metadata( 'post', 5, 'foo', false, true );
     138
     139        $p1_cache = wp_cache_get( $p1, 'post_meta' );
     140        $this->assertFalse( $p1_cache );
     141
     142        $p2_cache = wp_cache_get( $p2, 'post_meta' );
     143        $this->assertFalse( $p2_cache );
     144    }
    100145}
Note: See TracChangeset for help on using the changeset viewer.