diff --git a/src/wp-includes/class-wp-post.php b/src/wp-includes/class-wp-post.php index 4f85f1280c..654dd05bfa 100644 --- a/src/wp-includes/class-wp-post.php +++ b/src/wp-includes/class-wp-post.php @@ -247,7 +247,7 @@ final class WP_Post { $_post = sanitize_post( $_post, 'raw' ); wp_cache_add( $_post->ID, $_post, 'posts' ); - } elseif ( empty( $_post->filter ) ) { + } elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) { $_post = sanitize_post( $_post, 'raw' ); } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3906e21f7c..182031cf99 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -7374,6 +7374,9 @@ function update_post_cache( &$posts ) { $data = array(); foreach ( $posts as $post ) { + if ( empty( $post->filter ) || 'raw' !== $post->filter ) { + $post = sanitize_post( $post, 'raw' ); + } $data[ $post->ID ] = $post; } wp_cache_add_multiple( $data, 'posts' ); diff --git a/tests/phpunit/tests/post/updatePostCache.php b/tests/phpunit/tests/post/updatePostCache.php new file mode 100644 index 0000000000..976f7af118 --- /dev/null +++ b/tests/phpunit/tests/post/updatePostCache.php @@ -0,0 +1,141 @@ +post->create_many( 1 ); + } + + /** + * Ensure that `update_post_cache()` returns `null` when + * `$posts` is empty. + * + * @ticket 50567 + */ + public function test_should_return_null_with_an_empty_array() { + $posts = array(); + $this->assertNull( update_post_cache( $posts ) ); + } + + /** + * Ensure filter = raw is always set via Query. + * + * @ticket 50567 + */ + public function test_query_caches_post_filter() { + $post_id = self::$post_ids[0]; + $this->go_to( '/' ); + + $cached_post = wp_cache_get( $post_id, 'posts' ); + $this->assertIsObject( + $cached_post, + 'The cached post is not an object' + ); + + $this->assertObjectHasAttribute( + 'filter', + $cached_post, + 'The cached post does not have a "filter" property' + ); + + $this->assertSame( + 'raw', + $cached_post->filter, + 'The filter is not set to "raw"' + ); + } + + /** + * Ensure filter = raw is always set via get_post. + * + * @ticket 50567 + */ + public function test_get_post_caches_post_filter() { + $post_id = self::$post_ids[0]; + get_post( $post_id ); + + $cached_post = wp_cache_get( $post_id, 'posts' ); + $this->assertSame( 'raw', $cached_post->filter ); + } + + /** + * Ensure filter = raw is always set via get_post called with a different filter setting. + * + * @ticket 50567 + */ + public function test_get_post_caches_post_filter_is_always_raw() { + $post_id = self::$post_ids[0]; + get_post( $post_id, OBJECT, 'display' ); + + $cached_post = wp_cache_get( $post_id, 'posts' ); + $this->assertIsObject( + $cached_post, + 'The cached post is not an object' + ); + + $this->assertObjectHasAttribute( + 'filter', + $cached_post, + 'The cached post does not have a "filter" property' + ); + + $this->assertSame( + 'raw', + $cached_post->filter, + 'The filter is not set to "raw"' + ); + } + + /** + * Ensure filter = raw is always set via get_posts. + * + * @ticket 50567 + */ + public function test_get_posts_caches_post_filter_is_always_raw() { + $post_id = self::$post_ids[0]; + get_posts( array( 'includes' => $post_id ) ); + + $cached_post = wp_cache_get( $post_id, 'posts' ); + $this->assertIsObject( + $cached_post, + 'The cached post is not an object' + ); + + $this->assertObjectHasAttribute( + 'filter', + $cached_post, + 'The cached post does not have a "filter" property' + ); + + $this->assertSame( + 'raw', + $cached_post->filter, + 'The filter is not set to "raw"' + ); + } +}