diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index ae391bfcf5..2c72663525 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/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/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index ca0e21b09c..b15b4787fc 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -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', + ), + ), + ), + ), + ); + } }