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.

git-svn-id: https://develop.svn.wordpress.org/trunk@54634 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2022-10-18 15:06:48 +00:00
parent 6846467abd
commit 7b176e6dea
2 changed files with 84 additions and 2 deletions
@@ -33,6 +33,16 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
*/
public static $author_id;
/**
* @var array
*/
protected $cache_args;
/**
* @var string
*/
protected $new_request;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Make some post objects.
self::$posts = $factory->post->create_many( 5 );
@@ -57,14 +67,28 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
);
}
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 );
}
/**
* @dataProvider data_query_cache
* @ticket 22176
*/
public function test_query_cache( $args ) {
global $wpdb;
$query1 = new WP_Query();
$posts1 = $query1->query( $args );
$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' );
$queries_before = get_num_queries();
$query2 = new WP_Query();
$posts2 = $query2->query( $args );
@@ -191,6 +215,30 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
),
),
),
'cache meta query search' => array(
'args' => array(
'cache_results' => true,
'meta_query' => array(
array(
'key' => 'color',
'value' => '00',
'compare' => 'LIKE',
),
),
),
),
'cache meta query not search' => array(
'args' => array(
'cache_results' => true,
'meta_query' => array(
array(
'key' => 'color',
'value' => 'ff',
'compare' => 'NOT LIKE',
),
),
),
),
'cache comment_count' => array(
'args' => array(
'cache_results' => true,
@@ -209,6 +257,18 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
),
),
),
'cache search query' => array(
'args' => array(
'cache_results' => true,
's' => 'title',
),
),
'cache search query multiple terms' => array(
'args' => array(
'cache_results' => true,
's' => 'Post title',
),
),
);
}
@@ -827,6 +887,13 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
$this->assertNotSame( $query1->found_posts, $query2->found_posts );
}
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;
}
/**
* @ticket 22176
*/