Taxonomy: Remove placeholder from WP_Term_Query cache key.

Remove escape placeholder from query cache key, as placeholders are on a based on a unique id on every request. This meant that it is impossible for a cache to be reused, making queries that use escape placeholders such as searches, meta queries or using the `description__like` / `name__like` parameters were unable to be cached.  
 
Follow on from [54634]. 

Props spacedmonkey, peterwilsoncc.
Fixes #57298.

git-svn-id: https://develop.svn.wordpress.org/trunk@55083 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-01-18 09:56:55 +00:00
parent 7f788f38c8
commit ec6b2f2045
2 changed files with 154 additions and 13 deletions

View File

@@ -867,4 +867,124 @@ class Tests_Term_Query extends WP_UnitTestCase {
$this->assertContains( $t1, $q->terms );
}
/**
* Ensure cache keys are generated without WPDB placeholders.
*
* @ticket 57298
*
* @covers WP_Term_Query::generate_cache_key
* @dataProvider data_query_cache
*/
public function test_generate_cache_key_placeholder( $args ) {
global $wpdb;
$query1 = new WP_Term_Query();
$query1->query( $args );
$query_vars = $query1->query_vars;
$request = $query1->request;
$reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
$reflection->setAccessible( true );
$cache_key_1 = $reflection->invoke( $query1, $query_vars, $request );
$request_without_placeholder = $wpdb->remove_placeholder_escape( $request );
$cache_key_2 = $reflection->invoke( $query1, $query_vars, $request_without_placeholder );
$this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
}
/**
* Data provider.
*
* @return array[] Test parameters.
*/
public function data_query_cache() {
return array(
'empty query' => array(
'args' => array(),
),
'search query' => array(
'args' => array(
'search' => 'title',
),
),
'search name query' => array(
'args' => array(
'name__like' => 'title',
),
),
'search description query' => array(
'args' => array(
'description__like' => 'title',
),
),
'meta query' => array(
'args' => array(
'meta_query' => array(
array(
'key' => 'color',
),
),
),
),
'meta query search' => array(
'args' => array(
'meta_query' => array(
array(
'key' => 'color',
'value' => '00',
'compare' => 'LIKE',
),
),
),
),
'nested meta query search' => array(
'args' => array(
'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',
),
),
),
),
),
),
'meta query not like search' => array(
'args' => array(
'meta_query' => array(
array(
'key' => 'color',
'value' => 'ff',
'compare' => 'NOT LIKE',
),
),
),
),
);
}
}