diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index c015fea4f2..9054e1acee 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -423,7 +423,11 @@ function get_comment_count( $post_id = 0 ) { * @return int|bool Meta ID on success, false on failure. */ function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) { - return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique); + $added = add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); + if ( $added ) { + wp_cache_set( 'last_changed', microtime(), 'comment' ); + } + return $added; } /** @@ -442,7 +446,11 @@ function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) * @return bool True on success, false on failure. */ function delete_comment_meta($comment_id, $meta_key, $meta_value = '') { - return delete_metadata('comment', $comment_id, $meta_key, $meta_value); + $deleted = delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); + if ( $deleted ) { + wp_cache_set( 'last_changed', microtime(), 'comment' ); + } + return $deleted; } /** @@ -479,7 +487,11 @@ function get_comment_meta($comment_id, $key = '', $single = false) { * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. */ function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') { - return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value); + $updated = update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); + if ( $updated ) { + wp_cache_set( 'last_changed', microtime(), 'comment' ); + } + return $updated; } /** diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index dbfe8dfdee..4d16b29675 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -3025,4 +3025,139 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertEquals( $number_of_queries, $wpdb->num_queries ); } + + /** + * @ticket 40669 + */ + public function test_add_comment_meta_should_invalidate_query_cache() { + global $wpdb; + + $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + $c1 = self::factory()->comment->create_post_comments( $p, 1 ); + $c2 = self::factory()->comment->create_post_comments( $p, 1 ); + + foreach ( $c1 as $cid ) { + add_comment_meta( $cid, 'sauce', 'fire' ); + } + + $cached = get_comments( array( + 'post_id' => $p, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'sauce', + 'value' => 'fire', + ), + ) + ) ); + + $this->assertEqualSets( $c1, $cached ); + + foreach ( $c2 as $cid ) { + add_comment_meta( $cid, 'sauce', 'fire' ); + } + + $found = get_comments( array( + 'post_id' => $p, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'sauce', + 'value' => 'fire', + ), + ) + ) ); + + $this->assertEqualSets( array_merge( $c1, $c2 ), $found ); + } + + /** + * @ticket 40669 + */ + public function test_update_comment_meta_should_invalidate_query_cache() { + global $wpdb; + + $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + $c1 = self::factory()->comment->create_post_comments( $p, 1 ); + $c2 = self::factory()->comment->create_post_comments( $p, 1 ); + + foreach ( array_merge( $c1, $c2 ) as $cid ) { + add_comment_meta( $cid, 'sauce', 'fire' ); + } + + $cached = get_comments( array( + 'post_id' => $p, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'sauce', + 'value' => 'fire', + ), + ) + ) ); + + $this->assertEqualSets( array_merge( $c1, $c2 ), $cached ); + + foreach ( $c2 as $cid ) { + update_comment_meta( $cid, 'sauce', 'foo' ); + } + + $found = get_comments( array( + 'post_id' => $p, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'sauce', + 'value' => 'fire', + ), + ) + ) ); + + $this->assertEqualSets( $c1, $found ); + } + + /** + * @ticket 40669 + */ + public function test_delete_comment_meta_should_invalidate_query_cache() { + global $wpdb; + + $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + $c1 = self::factory()->comment->create_post_comments( $p, 1 ); + $c2 = self::factory()->comment->create_post_comments( $p, 1 ); + + foreach ( array_merge( $c1, $c2 ) as $cid ) { + add_comment_meta( $cid, 'sauce', 'fire' ); + } + + $cached = get_comments( array( + 'post_id' => $p, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'sauce', + 'value' => 'fire', + ), + ) + ) ); + + $this->assertEqualSets( array_merge( $c1, $c2 ), $cached ); + + foreach ( $c2 as $cid ) { + delete_comment_meta( $cid, 'sauce' ); + } + + $found = get_comments( array( + 'post_id' => $p, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'sauce', + 'value' => 'fire', + ), + ) + ) ); + + $this->assertEqualSets( $c1, $found ); + } }