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.
Built from https://develop.svn.wordpress.org/trunk@55083


git-svn-id: http://core.svn.wordpress.org/trunk@54616 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-01-18 09:58:16 +00:00
parent 5bda5f22dc
commit da05dbb69c
2 changed files with 35 additions and 14 deletions

View File

@ -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";
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55082';
$wp_version = '6.2-alpha-55083';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.