<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54692] branches/6.1: Query: Move cache key generation to its own method.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54692">54692</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54692","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>davidbaumwald</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-10-25 18:31:35 +0000 (Tue, 25 Oct 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Query: Move cache key generation to its own method.

Introduce `WP_Query::generate_cache_key()` for generating the cache key used by the main database query.

This removes the need for a filter to test that cache keys do not include the WPDB placeholder causing unreachable cache keys. The tests now call `WP_Query::generate_cache_key()` directly.

The filter `wp_query_cache_key` is removed as a hard deprecation. The filter was not included in a stable release.

Follow up to <a href="https://core.trac.wordpress.org/changeset/54685">[54685]</a>.

Props spacedmonkey, jorbin, azaozz, hellofromtonya, mukesh27, peterwilsoncc, desrosj, audrasjb, adamsilverstein, flixos90, davidbaumwald, joedolson, sergeybiryukov.
Reviewed by mikeschroder.
Merges <a href="https://core.trac.wordpress.org/changeset/54685">[54685]</a> to the 6.1 branch.
Fixes <a href="https://core.trac.wordpress.org/ticket/56802">#56802</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches61srcwpincludesclasswpqueryphp">branches/6.1/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#branches61testsphpunittestsquerycacheResultsphp">branches/6.1/tests/phpunit/tests/query/cacheResults.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches61srcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.1/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/src/wp-includes/class-wp-query.php   2022-10-25 15:00:25 UTC (rev 54691)
+++ branches/6.1/src/wp-includes/class-wp-query.php     2022-10-25 18:31:35 UTC (rev 54692)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3106,42 +3106,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                $id_query_is_cacheable = ! str_contains( strtoupper( $orderby ), ' RAND(' );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $q['cache_results'] && $id_query_is_cacheable ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $cache_args = $q;
-
-                       unset(
-                               $cache_args['suppress_filters'],
-                               $cache_args['cache_results'],
-                               $cache_args['fields'],
-                               $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['search_orderby_title']
-                       );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $new_request = $wpdb->remove_placeholder_escape( $new_request );
-                       $key         = md5( serialize( $cache_args ) . $new_request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $cache_key   = $this->generate_cache_key( $q, $new_request );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $last_changed = wp_cache_get_last_changed( 'posts' );
-                       if ( ! empty( $this->tax_query->queries ) ) {
-                               $last_changed .= wp_cache_get_last_changed( 'terms' );
-                       }
-
-                       $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 );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         $cache_found = false;
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( null === $this->posts ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $cached_results = wp_cache_get( $cache_key, 'posts', false, $cache_found );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4755,7 +4722,63 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $elements;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Generate cache key.
+        *
+        * @since 6.1.0
+        *
+        * @global wpdb $wpdb WordPress database abstraction object.
+        *
+        * @param array  $args Query arguments.
+        * @param string $sql  SQL statement.
+        *
+        * @return string Cache key.
+        */
+       protected function generate_cache_key( array $args, $sql ) {
+               global $wpdb;
+
+               unset(
+                       $args['cache_results'],
+                       $args['fields'],
+                       $args['lazy_load_term_meta'],
+                       $args['update_post_meta_cache'],
+                       $args['update_post_term_cache'],
+                       $args['update_menu_item_cache'],
+                       $args['suppress_filters']
+               );
+
+               $placeholder = $wpdb->placeholder_escape();
+               array_walk_recursive(
+                       $args,
+                       /*
+                        * Replace wpdb placeholders with the string used in the database
+                        * query to avoid unreachable cache keys. This is necessary because
+                        * the placeholder is randomly generated in each request.
+                        *
+                        * $value is passed by reference to allow it to be modified.
+                        * array_walk_recursive() does not return an array.
+                        */
+                       function ( &$value ) use ( $wpdb, $placeholder ) {
+                               if ( is_string( $value ) && str_contains( $value, $placeholder ) ) {
+                                       $value = $wpdb->remove_placeholder_escape( $value );
+                               }
+                       }
+               );
+
+               // Replace wpdb placeholder in the SQL statement used by the cache key.
+               $sql = $wpdb->remove_placeholder_escape( $sql );
+               $key = md5( serialize( $args ) . $sql );
+
+               $last_changed = wp_cache_get_last_changed( 'posts' );
+               if ( ! empty( $this->tax_query->queries ) ) {
+                       $last_changed .= wp_cache_get_last_changed( 'terms' );
+               }
+
+               return "wp_query:$key:$last_changed";
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * After looping through a nested query, this function
</span><span class="cx" style="display: block; padding: 0 10px">         * restores the $post global to the current post in this query.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="branches61testsphpunittestsquerycacheResultsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.1/tests/phpunit/tests/query/cacheResults.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/tests/phpunit/tests/query/cacheResults.php   2022-10-25 15:00:25 UTC (rev 54691)
+++ branches/6.1/tests/phpunit/tests/query/cacheResults.php     2022-10-25 18:31:35 UTC (rev 54692)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,14 +34,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public static $author_id;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @var array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * For testing test_generate_cache_key() includes a test containing the
+        * placeholder within the generated SQL query.
+        *
+        * @var bool
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected $cache_args;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static $sql_placeholder_cache_key_tested = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @var string
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * For testing test_generate_cache_key() includes a test containing the
+        * placeholder within the generated WP_Query variables.
+        *
+        * @var bool
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected $new_request;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static $wp_query_placeholder_cache_key_tested = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Make some post objects.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -67,28 +73,112 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function set_up() {
-               parent::set_up();
-               $this->cache_args  = null;
-               $this->new_request = null;
-               add_filter( 'wp_query_cache_key', array( $this, 'filter_wp_query_cache_key' ), 15, 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Ensure cache keys are generated without WPDB placeholders.
+        *
+        * @ticket 56802
+        *
+        * @covers WP_Query::generate_cache_key
+        *
+        * @dataProvider data_query_cache
+        */
+       public function test_generate_cache_key( $args ) {
+               global $wpdb;
+               $query1 = new WP_Query();
+               $query1->query( $args );
+
+               $query_vars             = $query1->query_vars;
+               $request                = $query1->request;
+               $request_no_placeholder = $wpdb->remove_placeholder_escape( $request );
+
+               $this->assertStringNotContainsString( $wpdb->placeholder_escape(), $request_no_placeholder, 'Placeholder escape should be removed from the modified request.' );
+
+               if ( str_contains( $request, $wpdb->placeholder_escape() ) ) {
+                       self::$sql_placeholder_cache_key_tested = true;
+               }
+
+               if ( str_contains( serialize( $query_vars ), $wpdb->placeholder_escape() ) ) {
+                       self::$wp_query_placeholder_cache_key_tested = true;
+               }
+
+               $reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
+               $reflection->setAccessible( true );
+
+               $cache_key_1 = $reflection->invoke( $query1, $query_vars, $request );
+               $cache_key_2 = $reflection->invoke( $query1, $query_vars, $request_no_placeholder );
+
+               $this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Ensure cache keys tests include WPDB placeholder in SQL Query.
+        *
+        * @ticket 56802
+        *
+        * @covers WP_Query::generate_cache_key
+        *
+        * @depends test_generate_cache_key
+        */
+       public function test_sql_placeholder_cache_key_tested() {
+               $this->assertTrue( self::$sql_placeholder_cache_key_tested, 'Cache key containing WPDB placeholder in SQL query was not tested.' );
+       }
+
+       /**
+        * Ensure cache keys tests include WPDB placeholder in WP_Query arguments.
+        *
+        * This test mainly covers the search query which generates the `search_orderby_title`
+        * query_var in WP_Query.
+        *
+        * @ticket 56802
+        *
+        * @covers WP_Query::generate_cache_key
+        *
+        * @depends test_generate_cache_key
+        */
+       public function test_wp_query_placeholder_cache_key_tested() {
+               $this->assertTrue( self::$wp_query_placeholder_cache_key_tested, 'Cache key containing WPDB placeholder in WP_Query arguments was not tested.' );
+       }
+
+       /**
+        * Ensure cache keys are generated without WPDB placeholders.
+        *
+        * @ticket 56802
+        *
+        * @covers WP_Query::generate_cache_key
+        */
+       public function test_generate_cache_key_placeholder() {
+               global $wpdb;
+               $query1 = new WP_Query();
+               $query1->query( array() );
+
+               $query_vars                                  = $query1->query_vars;
+               $request                                     = $query1->request;
+               $query_vars['test']['nest']                  = '%';
+               $query_vars['test2']['nest']['nest']['nest'] = '%';
+               $this->assertStringNotContainsString( $wpdb->placeholder_escape(), serialize( $query_vars ), 'Query vars should not contain the wpdb placeholder.' );
+
+               $reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
+               $reflection->setAccessible( true );
+
+               $cache_key_1 = $reflection->invoke( $query1, $query_vars, $request );
+
+               $query_vars['test']['nest']                  = $wpdb->placeholder_escape();
+               $query_vars['test2']['nest']['nest']['nest'] = $wpdb->placeholder_escape();
+               $this->assertStringContainsString( $wpdb->placeholder_escape(), serialize( $query_vars ), 'Query vars should not contain the wpdb placeholder.' );
+
+               $cache_key_2 = $reflection->invoke( $query1, $query_vars, $request );
+
+               $this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @dataProvider data_query_cache
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 22176
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_query_cache( $args ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                global $wpdb;
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $query1 = new WP_Query();
</span><span class="cx" style="display: block; padding: 0 10px">                $posts1 = $query1->query( $args );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $placeholder = $wpdb->placeholder_escape();
-               $this->assertNotEmpty( $this->new_request, 'Check new request is not empty' );
-               $this->assertStringNotContainsString( $placeholder, $this->new_request, 'Check if request does not contain placeholder' );
-               $this->assertStringNotContainsString( $placeholder, wp_json_encode( $this->cache_args ), 'Check if cache arrays does not contain placeholder' );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $queries_before = get_num_queries();
</span><span class="cx" style="display: block; padding: 0 10px">                $query2         = new WP_Query();
</span><span class="cx" style="display: block; padding: 0 10px">                $posts2         = $query2->query( $args );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -227,6 +317,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'cache nested meta query search'              => array(
+                               'args' => array(
+                                       'cache_results' => true,
+                                       'meta_query'    => array(
+                                               'relation' => 'AND',
+                                               array(
+                                                       'key'     => 'color',
+                                                       'value'   => '00',
+                                                       'compare' => 'LIKE',
+                                               ),
+                                               array(
+                                                       'relation' => 'OR',
+                                                       array(
+                                                               'key'     => 'color',
+                                                               'value'   => '00',
+                                                               'compare' => 'LIKE',
+                                                       ),
+                                                       array(
+                                                               'relation' => 'AND',
+                                                               array(
+                                                                       'key'     => 'wp_test_suite',
+                                                                       'value'   => '56802',
+                                                                       'compare' => 'LIKE',
+                                                               ),
+                                                               array(
+                                                                       'key'     => 'wp_test_suite_too',
+                                                                       'value'   => '56802',
+                                                                       'compare' => 'LIKE',
+                                                               ),
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         'cache meta query not search'                 => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'args' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'cache_results' => true,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -887,13 +1011,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotSame( $query1->found_posts, $query2->found_posts );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function filter_wp_query_cache_key( $cache_key, $cache_args, $new_request ) {
-               $this->cache_args  = $cache_args;
-               $this->new_request = $new_request;
-
-               return $cache_key;
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 22176
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre>
</div>
</div>

</body>
</html>