From dc07d3ca674652602b3325b3f540d7228147b74b Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 1 Apr 2022 03:18:05 +0000 Subject: [PATCH] Posts, Post Types: Set post filter in `update_post_cache()`. Ensure the post cache is primed with raw sanitized data. This resolves an inconsistency between how posts retrieved via `get_post()` vs `WP_Query` are cached. This prevents `sanitize_post( $post, 'raw' )` being run multiple times on a cached post. This can happen over 20 times per post on some page loads so avoiding this will provide a noticeable performance boost. Props Cybr, SergeyBiryukov, peterwilsoncc, hellofromTonya, costdev. Fixes #50567. Built from https://develop.svn.wordpress.org/trunk@53042 git-svn-id: http://core.svn.wordpress.org/trunk@52631 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-post.php | 2 +- wp-includes/post.php | 3 +++ wp-includes/version.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/wp-includes/class-wp-post.php b/wp-includes/class-wp-post.php index 4f85f1280c..654dd05bfa 100644 --- a/wp-includes/class-wp-post.php +++ b/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/wp-includes/post.php b/wp-includes/post.php index 3906e21f7c..182031cf99 100644 --- a/wp-includes/post.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 55d397a0d8..66720e0022 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-53041'; +$wp_version = '6.0-alpha-53042'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.