Bump 'posts' query cache incrementor when modifying postmeta.

This ensures that the `get_pages()` query cache doesn't go stale when
postmeta is modified.

Props spacedmonkey.
Fixes #40669.

git-svn-id: https://develop.svn.wordpress.org/trunk@41849 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2017-10-12 15:19:30 +00:00
parent 6ce4e413ce
commit 5c8c480d22
2 changed files with 139 additions and 4 deletions

View File

@ -1728,7 +1728,11 @@ function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
if ( $the_post = wp_is_post_revision($post_id) )
$post_id = $the_post;
return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
$added = add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
if ( $added ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $added;
}
/**
@ -1751,7 +1755,11 @@ function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
if ( $the_post = wp_is_post_revision($post_id) )
$post_id = $the_post;
return delete_metadata('post', $post_id, $meta_key, $meta_value);
$deleted = delete_metadata( 'post', $post_id, $meta_key, $meta_value );
if ( $deleted ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $deleted;
}
/**
@ -1793,7 +1801,11 @@ function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' )
if ( $the_post = wp_is_post_revision($post_id) )
$post_id = $the_post;
return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
$updated = update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
if ( $updated ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $updated;
}
/**
@ -1805,7 +1817,11 @@ function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' )
* @return bool Whether the post meta key was deleted from the database.
*/
function delete_post_meta_by_key( $post_meta_key ) {
return delete_metadata( 'post', null, $post_meta_key, '', true );
$deleted = delete_metadata( 'post', null, $post_meta_key, '', true );
if ( $deleted ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $deleted;
}
/**

View File

@ -95,6 +95,125 @@ class Tests_Post_getPages extends WP_UnitTestCase {
$this->assertInstanceOf( 'WP_Post', $page );
}
/**
* @ticket 40669
*/
public function test_cache_should_be_invalidated_by_add_post_meta() {
$posts = self::factory()->post->create_many( 2, array(
'post_type' => 'page',
) );
add_post_meta( $posts[0], 'foo', 'bar' );
$cached = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( array( $posts[0] ), $cached_ids );
add_post_meta( $posts[1], 'foo', 'bar' );
$found = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( $posts, $found_ids );
}
/**
* @ticket 40669
*/
public function test_cache_should_be_invalidated_by_update_post_meta() {
$posts = self::factory()->post->create_many( 2, array(
'post_type' => 'page',
) );
add_post_meta( $posts[0], 'foo', 'bar' );
add_post_meta( $posts[1], 'foo', 'bar' );
$cached = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( $posts, $cached_ids );
update_post_meta( $posts[1], 'foo', 'baz' );
$found = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( array( $posts[0] ), $found_ids );
}
/**
* @ticket 40669
*/
public function test_cache_should_be_invalidated_by_delete_post_meta() {
$posts = self::factory()->post->create_many( 2, array(
'post_type' => 'page',
) );
add_post_meta( $posts[0], 'foo', 'bar' );
add_post_meta( $posts[1], 'foo', 'bar' );
$cached = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( $posts, $cached_ids );
delete_post_meta( $posts[1], 'foo' );
$found = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( array( $posts[0] ), $found_ids );
}
/**
* @ticket 40669
*/
public function test_cache_should_be_invalidated_by_delete_post_meta_by_key() {
$posts = self::factory()->post->create_many( 2, array(
'post_type' => 'page',
) );
add_post_meta( $posts[0], 'foo', 'bar' );
add_post_meta( $posts[1], 'foo', 'bar' );
$cached = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( $posts, $cached_ids );
delete_post_meta_by_key( 'foo' );
$found = get_pages( array(
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( array(), $found_ids );
}
/**
* @ticket 20376
*/