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. git-svn-id: https://develop.svn.wordpress.org/trunk@41848 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ee44859ee4
commit
6ce4e413ce
|
@ -423,7 +423,11 @@ function get_comment_count( $post_id = 0 ) {
|
||||||
* @return int|bool Meta ID on success, false on failure.
|
* @return int|bool Meta ID on success, false on failure.
|
||||||
*/
|
*/
|
||||||
function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
|
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.
|
* @return bool True on success, false on failure.
|
||||||
*/
|
*/
|
||||||
function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
|
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.
|
* @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 = '') {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3025,4 +3025,139 @@ class Tests_Comment_Query extends WP_UnitTestCase {
|
||||||
|
|
||||||
$this->assertEquals( $number_of_queries, $wpdb->num_queries );
|
$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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue