Changeset 56585
- Timestamp:
- 09/14/2023 12:38:04 PM (17 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-term-query.php
r56555 r56585 92 92 * @since 5.1.0 Introduced the 'meta_compare_key' parameter. 93 93 * @since 5.3.0 Introduced the 'meta_type_key' parameter. 94 * @since 6.4.0 Introduced the 'cache_results' parameter. 94 95 * 95 96 * @param string|array $query { … … 179 180 * @type string $cache_domain Unique cache key to be produced when this query is stored in 180 181 * an object cache. Default 'core'. 182 * @type bool $cache_results Whether to cache term information. Default true. 181 183 * @type bool $update_term_meta_cache Whether to prime meta caches for matched terms. Default true. 182 184 * @type string|string[] $meta_key Meta key or keys to filter by. … … 221 223 'childless' => false, 222 224 'cache_domain' => 'core', 225 'cache_results' => true, 223 226 'update_term_meta_cache' => true, 224 227 'meta_query' => '', … … 777 780 } 778 781 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'] ) ) 787 791 || ( '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 ); 792 802 } 793 803 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 } 802 807 } 803 808 804 809 if ( 'count' === $_fields ) { 805 810 $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 } 807 814 return $count; 808 815 } … … 811 818 812 819 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 } 814 823 return array(); 815 824 } … … 893 902 } 894 903 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 896 908 $this->terms = $this->format_terms( $term_objects, $_fields ); 897 909 … … 1154 1166 $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ); 1155 1167 1156 unset( $cache_args[' update_term_meta_cache'] );1168 unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'] ); 1157 1169 1158 1170 if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) { -
trunk/src/wp-includes/taxonomy.php
r56549 r56585 1542 1542 // Ensure that while importing, queries are not cached. 1543 1543 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { 1544 // @todo Disable caching once #52710 is merged. 1545 $defaults['cache_domain'] = microtime(); 1544 $defaults['cache_results'] = false; 1546 1545 } 1547 1546 -
trunk/tests/phpunit/tests/term/getTerms.php
r56548 r56585 3027 3027 3028 3028 /** 3029 * @ticket 52710 3030 */ 3031 public function test_get_terms_without_update_get_terms_cache() { 3032 $this->set_up_three_posts_and_tags(); 3033 3034 $num_queries = get_num_queries(); 3035 3036 // last_changed and num_queries should bump. 3037 $terms = get_terms( 3038 'post_tag', 3039 array( 3040 'cache_results' => false, 3041 'update_term_meta_cache' => false, 3042 ) 3043 ); 3044 $this->assertCount( 3, $terms, 'After running get_terms, 3 terms should be returned' ); 3045 $this->assertSame( $num_queries + 2, get_num_queries(), 'There should be only 2 queries run, only term query and priming terms' ); 3046 3047 $num_queries = get_num_queries(); 3048 3049 // last_changed and num_queries should bump again. 3050 $terms = get_terms( 3051 'post_tag', 3052 array( 3053 'cache_results' => false, 3054 'update_term_meta_cache' => false, 3055 ) 3056 ); 3057 $this->assertCount( 3, $terms, 'After running get_terms for a second time, 3 terms should be returned' ); 3058 $this->assertSame( $num_queries + 1, get_num_queries(), 'On the second run, only run the term query, priming terms happens on the first run' ); 3059 } 3060 3061 /** 3029 3062 * @ticket 35935 3030 3063 */
Note: See TracChangeset
for help on using the changeset viewer.