mirror of
git://develop.git.wordpress.org/
synced 2025-03-14 17:09:47 +01:00
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:
parent
6ce4e413ce
commit
5c8c480d22
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user