Make WordPress Core

Changeset 41848


Ignore:
Timestamp:
10/12/2017 02:58:58 PM (8 years ago)
Author:
boonebgorges
Message:

Invalidate comment query cache when modifying comment meta.

Comment queries are sensitive to comment meta due to the meta_query
parameter, so the cache must be invalidated when comment meta is changed,
added, or deleted.

Props spacedmonkey.
See #40669.

Location:
trunk
Files:
2 edited

Legend:

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

    r41704 r41848  
    424424 */
    425425function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
    426     return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
     426    $added = add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
     427    if ( $added ) {
     428        wp_cache_set( 'last_changed', microtime(), 'comment' );
     429    }
     430    return $added;
    427431}
    428432
     
    443447 */
    444448function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
    445     return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
     449    $deleted = delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
     450    if ( $deleted ) {
     451        wp_cache_set( 'last_changed', microtime(), 'comment' );
     452    }
     453    return $deleted;
    446454}
    447455
     
    480488 */
    481489function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
    482     return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
     490    $updated = update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
     491    if ( $updated ) {
     492        wp_cache_set( 'last_changed', microtime(), 'comment' );
     493    }
     494    return $updated;
    483495}
    484496
  • trunk/tests/phpunit/tests/comment/query.php

    r41287 r41848  
    30263026        $this->assertEquals( $number_of_queries, $wpdb->num_queries );
    30273027    }
     3028
     3029    /**
     3030     * @ticket 40669
     3031     */
     3032    public function test_add_comment_meta_should_invalidate_query_cache() {
     3033        global $wpdb;
     3034
     3035        $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     3036        $c1 = self::factory()->comment->create_post_comments( $p, 1 );
     3037        $c2 = self::factory()->comment->create_post_comments( $p, 1 );
     3038
     3039        foreach ( $c1 as $cid ) {
     3040            add_comment_meta( $cid, 'sauce', 'fire' );
     3041        }
     3042
     3043        $cached = get_comments( array(
     3044            'post_id' => $p,
     3045            'fields' => 'ids',
     3046            'meta_query' => array(
     3047                array(
     3048                    'key' => 'sauce',
     3049                    'value' => 'fire',
     3050                ),
     3051            )
     3052        ) );
     3053
     3054        $this->assertEqualSets( $c1, $cached );
     3055
     3056        foreach ( $c2 as $cid ) {
     3057            add_comment_meta( $cid, 'sauce', 'fire' );
     3058        }
     3059
     3060        $found = get_comments( array(
     3061            'post_id' => $p,
     3062            'fields' => 'ids',
     3063            'meta_query' => array(
     3064                array(
     3065                    'key' => 'sauce',
     3066                    'value' => 'fire',
     3067                ),
     3068            )
     3069        ) );
     3070
     3071        $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     3072    }
     3073
     3074    /**
     3075     * @ticket 40669
     3076     */
     3077    public function test_update_comment_meta_should_invalidate_query_cache() {
     3078        global $wpdb;
     3079
     3080        $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     3081        $c1 = self::factory()->comment->create_post_comments( $p, 1 );
     3082        $c2 = self::factory()->comment->create_post_comments( $p, 1 );
     3083
     3084        foreach ( array_merge( $c1, $c2 ) as $cid ) {
     3085            add_comment_meta( $cid, 'sauce', 'fire' );
     3086        }
     3087
     3088        $cached = get_comments( array(
     3089            'post_id' => $p,
     3090            'fields' => 'ids',
     3091            'meta_query' => array(
     3092                array(
     3093                    'key' => 'sauce',
     3094                    'value' => 'fire',
     3095                ),
     3096            )
     3097        ) );
     3098
     3099        $this->assertEqualSets( array_merge( $c1, $c2 ), $cached );
     3100
     3101        foreach ( $c2 as $cid ) {
     3102            update_comment_meta( $cid, 'sauce', 'foo' );
     3103        }
     3104
     3105        $found = get_comments( array(
     3106            'post_id' => $p,
     3107            'fields' => 'ids',
     3108            'meta_query' => array(
     3109                array(
     3110                    'key' => 'sauce',
     3111                    'value' => 'fire',
     3112                ),
     3113            )
     3114        ) );
     3115
     3116        $this->assertEqualSets( $c1, $found );
     3117    }
     3118
     3119    /**
     3120     * @ticket 40669
     3121     */
     3122    public function test_delete_comment_meta_should_invalidate_query_cache() {
     3123        global $wpdb;
     3124
     3125        $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     3126        $c1 = self::factory()->comment->create_post_comments( $p, 1 );
     3127        $c2 = self::factory()->comment->create_post_comments( $p, 1 );
     3128
     3129        foreach ( array_merge( $c1, $c2 ) as $cid ) {
     3130            add_comment_meta( $cid, 'sauce', 'fire' );
     3131        }
     3132
     3133        $cached = get_comments( array(
     3134            'post_id' => $p,
     3135            'fields' => 'ids',
     3136            'meta_query' => array(
     3137                array(
     3138                    'key' => 'sauce',
     3139                    'value' => 'fire',
     3140                ),
     3141            )
     3142        ) );
     3143
     3144        $this->assertEqualSets( array_merge( $c1, $c2 ), $cached );
     3145
     3146        foreach ( $c2 as $cid ) {
     3147            delete_comment_meta( $cid, 'sauce' );
     3148        }
     3149
     3150        $found = get_comments( array(
     3151            'post_id' => $p,
     3152            'fields' => 'ids',
     3153            'meta_query' => array(
     3154                array(
     3155                    'key' => 'sauce',
     3156                    'value' => 'fire',
     3157                ),
     3158            )
     3159        ) );
     3160
     3161        $this->assertEqualSets( $c1, $found );
     3162    }
    30283163}
Note: See TracChangeset for help on using the changeset viewer.