diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php
index 465bcb7156..b3db41e514 100644
|
|
class WP_Term_Query { |
88 | 88 | * @since 4.6.0 Introduced 'term_taxonomy_id' parameter. |
89 | 89 | * @since 4.7.0 Introduced 'object_ids' parameter. |
90 | 90 | * @since 4.9.0 Added 'slug__in' support for 'orderby'. |
| 91 | * @since 5.x.0 Introduced 'update_get_terms_cache' parameter. |
91 | 92 | * |
92 | 93 | * @param string|array $query { |
93 | 94 | * Optional. Array or query string of term query parameters. Default empty. |
… |
… |
class WP_Term_Query { |
174 | 175 | * Default false. |
175 | 176 | * @type string $cache_domain Unique cache key to be produced when this query is stored in |
176 | 177 | * an object cache. Default is 'core'. |
| 178 | * @type bool $update_get_terms_cache Whether to cache the query results. Default true. |
177 | 179 | * @type bool $update_term_meta_cache Whether to prime meta caches for matched terms. Default true. |
178 | 180 | * @type array $meta_query Optional. Meta query clauses to limit retrieved terms by. |
179 | 181 | * See `WP_Meta_Query`. Default empty. |
… |
… |
class WP_Term_Query { |
213 | 215 | 'parent' => '', |
214 | 216 | 'childless' => false, |
215 | 217 | 'cache_domain' => 'core', |
| 218 | 'update_get_terms_cache' => true, |
216 | 219 | 'update_term_meta_cache' => true, |
217 | 220 | 'meta_query' => '', |
218 | 221 | 'meta_key' => '', |
… |
… |
class WP_Term_Query { |
736 | 739 | return $this->terms; |
737 | 740 | } |
738 | 741 | |
739 | | // $args can be anything. Only use the args defined in defaults to compute the key. |
740 | | $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request ); |
741 | | $last_changed = wp_cache_get_last_changed( 'terms' ); |
742 | | $cache_key = "get_terms:$key:$last_changed"; |
743 | | $cache = wp_cache_get( $cache_key, 'terms' ); |
| 742 | $cache_key = false; |
| 743 | |
| 744 | if ( ! empty( $args['update_get_terms_cache'] ) ) { |
| 745 | // $args can be anything. Only use the args defined in defaults to compute the key. |
| 746 | $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request ); |
| 747 | $last_changed = wp_cache_get_last_changed( 'terms' ); |
| 748 | $cache_key = "get_terms:$key:$last_changed"; |
| 749 | } |
| 750 | |
| 751 | $cache = $cache_key ? wp_cache_get( $cache_key, 'terms' ) : false; |
744 | 752 | if ( false !== $cache ) { |
745 | 753 | if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) { |
746 | 754 | $cache = $this->populate_terms( $cache ); |
… |
… |
class WP_Term_Query { |
752 | 760 | |
753 | 761 | if ( 'count' === $_fields ) { |
754 | 762 | $count = $wpdb->get_var( $this->request ); |
755 | | wp_cache_set( $cache_key, $count, 'terms' ); |
| 763 | if ( $cache_key ) { |
| 764 | wp_cache_set( $cache_key, $count, 'terms' ); |
| 765 | } |
756 | 766 | return $count; |
757 | 767 | } |
758 | 768 | |
… |
… |
class WP_Term_Query { |
769 | 779 | } |
770 | 780 | |
771 | 781 | if ( empty( $terms ) ) { |
772 | | wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); |
| 782 | if ( $cache_key ) { |
| 783 | wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); |
| 784 | } |
773 | 785 | return array(); |
774 | 786 | } |
775 | 787 | |
… |
… |
class WP_Term_Query { |
874 | 886 | } |
875 | 887 | } |
876 | 888 | |
877 | | wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); |
| 889 | if ( $cache_key ) { |
| 890 | wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); |
| 891 | } |
878 | 892 | |
879 | 893 | if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) { |
880 | 894 | $terms = $this->populate_terms( $terms ); |
diff --git tests/phpunit/tests/term/getTerms.php tests/phpunit/tests/term/getTerms.php
index cb17e84238..380628693f 100644
|
|
class Tests_Term_getTerms extends WP_UnitTestCase { |
194 | 194 | // @todo Repeat with term insert and update. |
195 | 195 | } |
196 | 196 | |
| 197 | /** |
| 198 | * @ticket 52710 |
| 199 | */ |
| 200 | public function test_get_terms_without_update_get_terms_cache() { |
| 201 | global $wpdb; |
| 202 | |
| 203 | $this->set_up_three_posts_and_tags(); |
| 204 | |
| 205 | $num_queries = $wpdb->num_queries; |
| 206 | |
| 207 | // last_changed and num_queries should bump. |
| 208 | $terms = get_terms( |
| 209 | 'post_tag', |
| 210 | array( |
| 211 | 'update_get_terms_cache' => false, |
| 212 | 'update_term_meta_cache' => false, |
| 213 | ) |
| 214 | ); |
| 215 | $this->assertSame( 3, count( $terms ) ); |
| 216 | $this->assertEmpty( wp_cache_get( 'last_changed', 'terms' ) ); |
| 217 | $this->assertSame( $num_queries + 1, $wpdb->num_queries ); |
| 218 | |
| 219 | $num_queries = $wpdb->num_queries; |
| 220 | |
| 221 | // last_changed and num_queries should bump again. |
| 222 | $terms = get_terms( |
| 223 | 'post_tag', |
| 224 | array( |
| 225 | 'update_get_terms_cache' => false, |
| 226 | 'update_term_meta_cache' => false, |
| 227 | ) |
| 228 | ); |
| 229 | $this->assertSame( 3, count( $terms ) ); |
| 230 | $this->assertEmpty( wp_cache_get( 'last_changed', 'terms' ) ); |
| 231 | $this->assertSame( $num_queries + 1, $wpdb->num_queries ); |
| 232 | } |
| 233 | |
197 | 234 | /** |
198 | 235 | * @ticket 23506 |
199 | 236 | */ |