From eb8b24155978b2bd4b53696d9ddd01129ec33441 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 26 Sep 2015 16:01:05 +0000 Subject: [PATCH] Improve post field lazyloading for comments. [34583] modified comment queries so that all post fields are no longer loaded by default. Instead, they are loaded only when requested on individual comment objects. This changeset improves that flow: * `WP_Comment` magic methods `__isset()` and `__get()` should only load the post when a post field is being requested. * The new `update_comment_post_cache` argument for `WP_Comment_Query` allows developers to specify that, when comments are queried, all of the posts matching those comments should be loaded into cache with a single DB hit. This parameter defaults to false, since typical comment queries are linked to a single post. Fixes #27571. git-svn-id: https://develop.svn.wordpress.org/trunk@34599 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-comment-query.php | 15 ++++++++- src/wp-includes/class-wp-comment.php | 34 ++++++++++++-------- tests/phpunit/tests/comment.php | 19 +++++++++++ tests/phpunit/tests/comment/query.php | 37 ++++++++++++++++++++++ 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 4050e48182..ab49452ed8 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -138,7 +138,7 @@ class WP_Comment_Query { * @since 4.2.0 * @since 4.4.0 `$parent__in` and `$parent__not_in` were added. * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`, - * and `$hierarchical` were added. + * `$hierarchical`, and `$update_comment_post_cache` were added. * @access public * * @param string|array $query { @@ -238,6 +238,8 @@ class WP_Comment_Query { * 'flat', or false. Default: false. * @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments. * Default true. + * @type bool $update_comment_post_cache Whether to prime the cache for comment posts. + * Default false. * } */ public function __construct( $query = '' ) { @@ -281,6 +283,7 @@ class WP_Comment_Query { 'date_query' => null, // See WP_Date_Query 'hierarchical' => false, 'update_comment_meta_cache' => true, + 'update_comment_post_cache' => false, ); if ( ! empty( $query ) ) { @@ -414,6 +417,16 @@ class WP_Comment_Query { } } + // Prime comment post caches. + if ( $this->query_vars['update_comment_post_cache'] ) { + $comment_post_ids = array(); + foreach ( $_comments as $_comment ) { + $comment_post_ids[] = $_comment->comment_post_ID; + } + + _prime_post_caches( $comment_post_ids, false, false ); + } + /** * Filter the comment query results. * diff --git a/src/wp-includes/class-wp-comment.php b/src/wp-includes/class-wp-comment.php index c141c6ecd9..8dc9e9413b 100644 --- a/src/wp-includes/class-wp-comment.php +++ b/src/wp-includes/class-wp-comment.php @@ -158,6 +158,15 @@ final class WP_Comment { */ protected $children; + /** + * Post fields. + * + * @since 4.4.0 + * @access protected + * @var array + */ + protected $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' ); + /** * Retrieves a WP_Comment instance. * @@ -322,30 +331,27 @@ final class WP_Comment { } /** - * Whether a comment has post from which to retrieve magic properties + * Check whether a non-public property is set. + * + * If `$name` matches a post field, the comment post will be loaded and the post's value checked. * * @since 4.4.0 * @access public * - * @param string $name + * @param string $name Property name. * @return bool */ public function __isset( $name ) { - if ( - 0 === (int) $this->comment_post_ID - || property_exists( $this, $name ) - ) { - return; - } - - $post = get_post( $this->comment_post_ID ); - if ( $post ) { + if ( in_array( $name, $this->post_fields ) && 0 !== (int) $this->comment_post_ID ) { + $post = get_post( $this->comment_post_ID ); return property_exists( $post, $name ); } } /** - * Magic getter for $post properties + * Magic getter. + * + * If `$name` matches a post field, the comment post will be loaded and the post's value returned. * * @since 4.4.0 * @access public @@ -354,8 +360,8 @@ final class WP_Comment { * @return mixed */ public function __get( $name ) { - $post = get_post( $this->comment_post_ID ); - if ( $post ) { + if ( in_array( $name, $this->post_fields ) ) { + $post = get_post( $this->comment_post_ID ); return $post->$name; } } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 07c02c3bda..cdbfd3a35f 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -339,4 +339,23 @@ class Tests_Comment extends WP_UnitTestCase { // Direct descendants of $c2. $this->assertEquals( array( $c3 ), array_values( wp_list_pluck( $children[ $c2 ]->get_children(), 'comment_ID' ) ) ); } + + /** + * @group 27571 + */ + public function test_post_properties_should_be_lazyloaded() { + $p = $this->factory->post->create(); + + $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) ); + + $post = get_post( $p ); + $comment = get_comment( $c ); + + $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' ); + + foreach ( $post_fields as $pf ) { + $this->assertTrue( isset( $comment->$pf ), $pf ); + $this->assertSame( $post->$pf, $comment->$pf, $pf ); + } + } } diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 420036ca2e..3a5e351b14 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -2108,4 +2108,41 @@ class Tests_Comment_Query extends WP_UnitTestCase { // Direct descendants of $c2. $this->assertEqualSets( array(), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children( $args ), 'comment_ID' ) ) ); } + + /** + * @ticket 27571 + */ + public function test_update_comment_post_cache_should_be_disabled_by_default() { + global $wpdb; + + $p = $this->factory->post->create(); + $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) ); + + $q = new WP_Comment_Query( array( + 'post_ID' => $p, + ) ); + + $num_queries = $wpdb->num_queries; + $this->assertTrue( isset( $q->comments[0]->post_name ) ); + $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + } + + /** + * @ticket 27571 + */ + public function test_should_respect_update_comment_post_cache_true() { + global $wpdb; + + $p = $this->factory->post->create(); + $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) ); + + $q = new WP_Comment_Query( array( + 'post_ID' => $p, + 'update_comment_post_cache' => true, + ) ); + + $num_queries = $wpdb->num_queries; + $this->assertTrue( isset( $q->comments[0]->post_name ) ); + $this->assertSame( $num_queries, $wpdb->num_queries ); + } }