From da05dbb69c9099feb707b2b40746291d57e7a560 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 18 Jan 2023 09:58:16 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-term-query.php | 47 +++++++++++++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/wp-includes/class-wp-term-query.php b/wp-includes/class-wp-term-query.php index ae391bfcf5..2c72663525 100644 --- a/wp-includes/class-wp-term-query.php +++ b/wp-includes/class-wp-term-query.php @@ -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"; + } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 46dca879a7..95ca57f5ca 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.