mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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:
parent
7f788f38c8
commit
ec6b2f2045
@ -774,19 +774,8 @@ class WP_Term_Query {
|
||||
return $this->terms;
|
||||
}
|
||||
|
||||
// $args can be anything. Only use the args defined in defaults to compute the key.
|
||||
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
|
||||
|
||||
unset( $cache_args['update_term_meta_cache'] );
|
||||
|
||||
if ( 'count' !== $_fields && 'all_with_object_id' !== $_fields ) {
|
||||
$cache_args['fields'] = 'all';
|
||||
}
|
||||
|
||||
$key = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $this->request );
|
||||
$last_changed = wp_cache_get_last_changed( 'terms' );
|
||||
$cache_key = "get_terms:$key:$last_changed";
|
||||
$cache = wp_cache_get( $cache_key, 'terms' );
|
||||
$cache_key = $this->generate_cache_key( $args, $this->request );
|
||||
$cache = wp_cache_get( $cache_key, 'terms' );
|
||||
|
||||
if ( false !== $cache ) {
|
||||
if ( 'ids' === $_fields ) {
|
||||
@ -1142,4 +1131,36 @@ class WP_Term_Query {
|
||||
|
||||
return $term_objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate cache key.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param array $args WP_Term_Query arguments.
|
||||
* @param string $sql SQL statement.
|
||||
*
|
||||
* @return string Cache key.
|
||||
*/
|
||||
protected function generate_cache_key( array $args, $sql ) {
|
||||
global $wpdb;
|
||||
// $args can be anything. Only use the args defined in defaults to compute the key.
|
||||
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
|
||||
|
||||
unset( $cache_args['update_term_meta_cache'] );
|
||||
|
||||
if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) {
|
||||
$cache_args['fields'] = 'all';
|
||||
}
|
||||
$taxonomies = (array) $args['taxonomy'];
|
||||
|
||||
// Replace wpdb placeholder in the SQL statement used by the cache key.
|
||||
$sql = $wpdb->remove_placeholder_escape( $sql );
|
||||
|
||||
$key = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $sql );
|
||||
$last_changed = wp_cache_get_last_changed( 'terms' );
|
||||
return "get_terms:$key:$last_changed";
|
||||
}
|
||||
}
|
||||
|
||||
@ -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',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user