mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-25 13:44:30 +00:00
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:
@@ -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 );
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user