Make WordPress Core


Ignore:
Timestamp:
09/14/2023 12:38:04 PM (3 years ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r56555 r56585  
    9292         * @since 5.1.0 Introduced the 'meta_compare_key' parameter.
    9393         * @since 5.3.0 Introduced the 'meta_type_key' parameter.
     94         * @since 6.4.0 Introduced the 'cache_results' parameter.
    9495         *
    9596         * @param string|array $query {
    … …  
    179180         *     @type string          $cache_domain           Unique cache key to be produced when this query is stored in
    180181         *                                                   an object cache. Default 'core'.
     182        *      @type bool            $cache_results          Whether to cache term information. Default true.
    181183         *     @type bool            $update_term_meta_cache Whether to prime meta caches for matched terms. Default true.
    182184         *     @type string|string[] $meta_key               Meta key or keys to filter by.
    … …  
    221223                        'childless'              => false,
    222224                        'cache_domain'           => 'core',
     225                        'cache_results'          => true,
    223226                        'update_term_meta_cache' => true,
    224227                        'meta_query'             => '',
    … …  
    777780                }
    778781
    779                 $cache_key = $this->generate_cache_key( $args, $this->request );
    780                 $cache     = wp_cache_get( $cache_key, 'term-queries' );
    781 
    782                 if ( false !== $cache ) {
    783                         if ( 'ids' === $_fields ) {
    784                                 $cache = array_map( 'intval', $cache );
    785                         } elseif ( 'count' !== $_fields ) {
    786                                 if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
     782                if ( $args['cache_results'] ) {
     783                        $cache_key = $this->generate_cache_key( $args, $this->request );
     784                        $cache     = wp_cache_get( $cache_key, 'term-queries' );
     785
     786                        if ( false !== $cache ) {
     787                                if ( 'ids' === $_fields ) {
     788                                        $cache = array_map( 'intval', $cache );
     789                                } elseif ( 'count' !== $_fields ) {
     790                                        if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
    787791                                        || ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
    788                                 ) {
    789                                         $term_ids = wp_list_pluck( $cache, 'term_id' );
    790                                 } else {
    791                                         $term_ids = array_map( 'intval', $cache );
     792                                        ) {
     793                                                $term_ids = wp_list_pluck( $cache, 'term_id' );
     794                                        } else {
     795                                                $term_ids = array_map( 'intval', $cache );
     796                                        }
     797
     798                                        _prime_term_caches( $term_ids, $args['update_term_meta_cache'] );
     799
     800                                        $term_objects = $this->populate_terms( $cache );
     801                                        $cache        = $this->format_terms( $term_objects, $_fields );
    792802                                }
    793803
    794                                 _prime_term_caches( $term_ids, $args['update_term_meta_cache'] );
    795 
    796                                 $term_objects = $this->populate_terms( $cache );
    797                                 $cache        = $this->format_terms( $term_objects, $_fields );
    798                         }
    799 
    800                         $this->terms = $cache;
    801                         return $this->terms;
     804                                $this->terms = $cache;
     805                                return $this->terms;
     806                        }
    802807                }
    803808
    804809                if ( 'count' === $_fields ) {
    805810                        $count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    806                         wp_cache_set( $cache_key, $count, 'term-queries' );
     811                        if ( $args['cache_results'] ) {
     812                                wp_cache_set( $cache_key, $count, 'term-queries' );
     813                        }
    807814                        return $count;
    808815                }
    … …  
    811818
    812819                if ( empty( $terms ) ) {
    813                         wp_cache_add( $cache_key, array(), 'term-queries' );
     820                        if ( $args['cache_results'] ) {
     821                                wp_cache_add( $cache_key, array(), 'term-queries' );
     822                        }
    814823                        return array();
    815824                }
    … …  
    893902                }
    894903
    895                 wp_cache_add( $cache_key, $term_cache, 'term-queries' );
     904                if ( $args['cache_results'] ) {
     905                        wp_cache_add( $cache_key, $term_cache, 'term-queries' );
     906                }
     907
    896908                $this->terms = $this->format_terms( $term_objects, $_fields );
    897909
    … …  
    11541166                $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
    11551167
    1156                 unset( $cache_args['update_term_meta_cache'] );
     1168                unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'] );
    11571169
    11581170                if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) {
Note: See TracChangeset for help on using the changeset viewer.