From ac1c0a71718b82e9484dbf0132b81f2097943f87 Mon Sep 17 00:00:00 2001
From: spacedmonkey <spacedmonkey@git.wordpress.org>
Date: Tue, 18 Oct 2022 15:08:12 +0000
Subject: [PATCH] Query: Remove placeholder from query cache key.

Remove escape placeholder from query cache key, as placeholders are a based on a unique id on every request. This means that it is impossible for a cache to be reused, making queries that use escape placeholders such as `LIKE` searches, unable to be cached.

Props dhl, spacedmonkey, peterwilsoncc, desrosj, chaion07, davidbaumwald, mukesh27.
Fixes #56802.
Built from https://develop.svn.wordpress.org/trunk@54634


git-svn-id: http://core.svn.wordpress.org/trunk@54186 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
 wp-includes/class-wp-query.php | 19 +++++++++++++++++--
 wp-includes/version.php        |  2 +-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php
index ae7f48711b..601ff077a2 100644
--- a/wp-includes/class-wp-query.php
+++ b/wp-includes/class-wp-query.php
@@ -3115,10 +3115,12 @@ class WP_Query {
 				$cache_args['update_post_meta_cache'],
 				$cache_args['update_post_term_cache'],
 				$cache_args['lazy_load_term_meta'],
-				$cache_args['update_menu_item_cache']
+				$cache_args['update_menu_item_cache'],
+				$cache_args['search_orderby_title']
 			);
 
 			$new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
+			$new_request = $wpdb->remove_placeholder_escape( $new_request );
 			$key         = md5( serialize( $cache_args ) . $new_request );
 
 			$last_changed = wp_cache_get_last_changed( 'posts' );
@@ -3126,7 +3128,20 @@ class WP_Query {
 				$last_changed .= wp_cache_get_last_changed( 'terms' );
 			}
 
-			$cache_key   = "wp_query:$key:$last_changed";
+			$cache_key = "wp_query:$key:$last_changed";
+
+			/**
+			 * Filters query cache key.
+			 *
+			 * @since 6.1.0
+			 *
+			 * @param string   $cache_key   Cache key.
+			 * @param array    $cache_args  Query args used to generate the cache key.
+			 * @param string   $new_request SQL Query.
+			 * @param WP_Query $query       The WP_Query instance.
+			 */
+			$cache_key = apply_filters( 'wp_query_cache_key', $cache_key, $cache_args, $new_request, $this );
+
 			$cache_found = false;
 			if ( null === $this->posts ) {
 				$cached_results = wp_cache_get( $cache_key, 'posts', false, $cache_found );
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 004426c7f8..014b4b2237 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '6.1-RC1-54633';
+$wp_version = '6.1-RC1-54634';
 
 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.