mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@56585 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -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 ) ) {
|
||||
|
||||
@@ -3025,6 +3025,39 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
|
||||
$this->assertSameSets( array( $terms[1] ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52710
|
||||
*/
|
||||
public function test_get_terms_without_update_get_terms_cache() {
|
||||
$this->set_up_three_posts_and_tags();
|
||||
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// last_changed and num_queries should bump.
|
||||
$terms = get_terms(
|
||||
'post_tag',
|
||||
array(
|
||||
'cache_results' => false,
|
||||
'update_term_meta_cache' => false,
|
||||
)
|
||||
);
|
||||
$this->assertCount( 3, $terms, 'After running get_terms, 3 terms should be returned' );
|
||||
$this->assertSame( $num_queries + 2, get_num_queries(), 'There should be only 2 queries run, only term query and priming terms' );
|
||||
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// last_changed and num_queries should bump again.
|
||||
$terms = get_terms(
|
||||
'post_tag',
|
||||
array(
|
||||
'cache_results' => false,
|
||||
'update_term_meta_cache' => false,
|
||||
)
|
||||
);
|
||||
$this->assertCount( 3, $terms, 'After running get_terms for a second time, 3 terms should be returned' );
|
||||
$this->assertSame( $num_queries + 1, get_num_queries(), 'On the second run, only run the term query, priming terms happens on the first run' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35935
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user