Make WordPress Core

Changeset 56585

Timestamp:
09/14/2023 12:38:04 PM (11 months 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.

Location:
trunk
Files:
3 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
    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
    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
    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['update_term_meta_cache'] );
    11571169
    11581170        if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) {
  • trunk/src/wp-includes/taxonomy.php

    r56549 r56585  
    15421542    // Ensure that while importing, queries are not cached.
    15431543    if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
    1544         // @todo Disable caching once #52710 is merged.
    1545         $defaults['cache_domain'] = microtime();
     1544        $defaults['cache_results'] = false;
    15461545    }
    15471546
  • trunk/tests/phpunit/tests/term/getTerms.php

    r56548 r56585  
    30273027
    30283028    /**
     3029
     3030
     3031
     3032
     3033
     3034
     3035
     3036
     3037
     3038
     3039
     3040
     3041
     3042
     3043
     3044
     3045
     3046
     3047
     3048
     3049
     3050
     3051
     3052
     3053
     3054
     3055
     3056
     3057
     3058
     3059
     3060
     3061
    30293062     * @ticket 35935
    30303063     */
Note: See TracChangeset for help on using the changeset viewer.