From 5c8c480d22a0412bb6c10c5302696d9eb62a7447 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 12 Oct 2017 15:19:30 +0000 Subject: [PATCH] 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 --- src/wp-includes/post.php | 24 +++++- tests/phpunit/tests/post/getPages.php | 119 ++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index e52ae5776b..d82e8a4d40 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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; } /** diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index 5391815aa0..b4bdd1745c 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -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 */