Taxonomy: Introduce 'cache_results' parameter to WP_Term_Query for bypassing query caching.

Incorporating a new 'cache_results' parameter into the WP_Term_Query class, this commit empowers developers with the ability to bypass query caches, explicitly triggering database queries when needed.  This brings the `WP_Term_Query` class inline with `WP_Query` and `WP_User_Query` that already have a 'cache_results' parameter.

Update the `term_exists` function to use this new parameter, so the term query caches are not used while importing. 

Props dlh, spacedmonkey, peterwilsoncc.
Fixes #52710.
Built from https://develop.svn.wordpress.org/trunk@56585


git-svn-id: http://core.svn.wordpress.org/trunk@56097 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-09-14 12:40:18 +00:00
parent bb3fac7b77
commit 6388ac820b
3 changed files with 36 additions and 25 deletions

View File

@ -91,6 +91,7 @@ class WP_Term_Query {
* @since 4.9.0 Added 'slug__in' support for 'orderby'.
* @since 5.1.0 Introduced the 'meta_compare_key' parameter.
* @since 5.3.0 Introduced the 'meta_type_key' parameter.
* @since 6.4.0 Introduced the 'cache_results' parameter.
*
* @param string|array $query {
* Optional. Array or query string of term query parameters. Default empty.
@ -178,6 +179,7 @@ class WP_Term_Query {
* Default false.
* @type string $cache_domain Unique cache key to be produced when this query is stored in
* an object cache. Default 'core'.
* @type bool $cache_results Whether to cache term information. Default true.
* @type bool $update_term_meta_cache Whether to prime meta caches for matched terms. Default true.
* @type string|string[] $meta_key Meta key or keys to filter by.
* @type string|string[] $meta_value Meta value or values to filter by.
@ -220,6 +222,7 @@ class WP_Term_Query {
'parent' => '',
'childless' => false,
'cache_domain' => 'core',
'cache_results' => true,
'update_term_meta_cache' => true,
'meta_query' => '',
'meta_key' => '',
@ -776,41 +779,47 @@ class WP_Term_Query {
return $this->terms;
}
$cache_key = $this->generate_cache_key( $args, $this->request );
$cache = wp_cache_get( $cache_key, 'term-queries' );
if ( $args['cache_results'] ) {
$cache_key = $this->generate_cache_key( $args, $this->request );
$cache = wp_cache_get( $cache_key, 'term-queries' );
if ( false !== $cache ) {
if ( 'ids' === $_fields ) {
$cache = array_map( 'intval', $cache );
} elseif ( 'count' !== $_fields ) {
if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
if ( false !== $cache ) {
if ( 'ids' === $_fields ) {
$cache = array_map( 'intval', $cache );
} elseif ( 'count' !== $_fields ) {
if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
|| ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
) {
$term_ids = wp_list_pluck( $cache, 'term_id' );
} else {
$term_ids = array_map( 'intval', $cache );
) {
$term_ids = wp_list_pluck( $cache, 'term_id' );
} else {
$term_ids = array_map( 'intval', $cache );
}
_prime_term_caches( $term_ids, $args['update_term_meta_cache'] );
$term_objects = $this->populate_terms( $cache );
$cache = $this->format_terms( $term_objects, $_fields );
}
_prime_term_caches( $term_ids, $args['update_term_meta_cache'] );
$term_objects = $this->populate_terms( $cache );
$cache = $this->format_terms( $term_objects, $_fields );
$this->terms = $cache;
return $this->terms;
}
$this->terms = $cache;
return $this->terms;
}
if ( 'count' === $_fields ) {
$count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
wp_cache_set( $cache_key, $count, 'term-queries' );
if ( $args['cache_results'] ) {
wp_cache_set( $cache_key, $count, 'term-queries' );
}
return $count;
}
$terms = $wpdb->get_results( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( empty( $terms ) ) {
wp_cache_add( $cache_key, array(), 'term-queries' );
if ( $args['cache_results'] ) {
wp_cache_add( $cache_key, array(), 'term-queries' );
}
return array();
}
@ -892,7 +901,10 @@ class WP_Term_Query {
$term_cache = wp_list_pluck( $term_objects, 'term_id' );
}
wp_cache_add( $cache_key, $term_cache, 'term-queries' );
if ( $args['cache_results'] ) {
wp_cache_add( $cache_key, $term_cache, 'term-queries' );
}
$this->terms = $this->format_terms( $term_objects, $_fields );
return $this->terms;
@ -1153,7 +1165,7 @@ class WP_Term_Query {
// $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'] );
unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'] );
if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) {
$cache_args['fields'] = 'all';

View File

@ -1541,8 +1541,7 @@ function term_exists( $term, $taxonomy = '', $parent_term = null ) {
// Ensure that while importing, queries are not cached.
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
// @todo Disable caching once #52710 is merged.
$defaults['cache_domain'] = microtime();
$defaults['cache_results'] = false;
}
if ( ! empty( $taxonomy ) ) {

View File

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