Skip to content

Commit

Permalink
Taxonomy: Remove placeholder from WP_Term_Query cache key.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
VenusPR committed Jan 18, 2023
1 parent cf7819c commit cd67ffd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
47 changes: 34 additions & 13 deletions wp-includes/class-wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,19 +774,8 @@ public function get_terms() {
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 ) {
Expand Down Expand Up @@ -1142,4 +1131,36 @@ protected function populate_terms( $terms ) {

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";
}
}
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cd67ffd

Please sign in to comment.