From c96858b0f26584007c8155a346a175281f62cca4 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 28 May 2016 03:09:09 +0000 Subject: [PATCH] Allow term meta lazy-loading to be selectively disabled in `WP_Query`. The process of lazy-loading can be resource intensive for object that have terms in large numbers of taxonomies and are running a persistent object cache. This new parameter allows the feature to be disabled in these cases. Props DBrumbaugh10Up. See #36953. git-svn-id: https://develop.svn.wordpress.org/trunk@37589 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 12 +++++-- tests/phpunit/tests/term/meta.php | 55 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index b0df07b0ee..d6915de507 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1468,7 +1468,7 @@ class WP_Query { * @since 4.5.0 Removed the `$comments_popup` parameter. * Introduced the `$comment_status` and `$ping_status` parameters. * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts. - * @since 4.6.0 Added 'post_name__in' support for `$orderby`. + * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced `$lazy_load_term_meta`. * @access public * * @param string|array $query { @@ -1567,6 +1567,10 @@ class WP_Query { * @type string $title Post title. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true. * @type bool $update_post_term_cache Whether to update the post term cache. Default true. + * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will + * disable cache priming for term meta, so that each + * get_term_meta() call will hit the database. + * Defaults to the value of `$update_post_term_cache`. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53. * @type int $year The four-digit year. Default empty. Accepts any four-digit year. * } @@ -2545,6 +2549,10 @@ class WP_Query { if ( !isset($q['update_post_term_cache']) ) $q['update_post_term_cache'] = true; + if ( ! isset( $q['lazy_load_term_meta'] ) ) { + $q['lazy_load_term_meta'] = $q['update_post_term_cache']; + } + if ( !isset($q['update_post_meta_cache']) ) $q['update_post_meta_cache'] = true; @@ -3782,7 +3790,7 @@ class WP_Query { $this->posts = array(); } - if ( $q['update_post_term_cache'] ) { + if ( $q['lazy_load_term_meta'] ) { wp_queue_posts_for_term_meta_lazyload( $this->posts ); } diff --git a/tests/phpunit/tests/term/meta.php b/tests/phpunit/tests/term/meta.php index 684dde41cb..ac309dc622 100644 --- a/tests/phpunit/tests/term/meta.php +++ b/tests/phpunit/tests/term/meta.php @@ -150,6 +150,61 @@ class Tests_Term_Meta extends WP_UnitTestCase { } } + /** + * @ticket 36593 + */ + public function test_lazy_load_term_meta_should_fall_back_on_update_post_term_cache() { + $q = new WP_Query( array( + 'update_post_term_cache' => true, + ) ); + + $this->assertTrue( $q->get( 'lazy_load_term_meta' ) ); + + $q = new WP_Query( array( + 'update_post_term_cache' => false, + ) ); + + $this->assertFalse( $q->get( 'lazy_load_term_meta' ) ); + } + + /** + * @ticket 36593 + */ + public function test_lazy_load_term_meta_false() { + global $wpdb; + + $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); + wp_set_object_terms( $p, $terms, 'wptests_tax' ); + foreach ( $terms as $t ) { + add_term_meta( $t, 'foo', 'bar' ); + } + + $q = new WP_Query( array( + 'cache_results' => true, + 'update_post_term_cache' => true, + 'lazy_load_term_meta' => false, + ) ); + + if ( $q->have_posts() ) { + while ( $q->have_posts() ) { + $q->the_post(); + + // Requests will hit the database. + $num_queries = $wpdb->num_queries; + $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) ); + $num_queries++; + $this->assertSame( $num_queries, $wpdb->num_queries ); + + $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) ); + $num_queries++; + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + } + } + public function test_adding_term_meta_should_bust_get_terms_cache() { $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );