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
This commit is contained in:
Boone Gorges
2015-09-26 16:01:05 +00:00
parent 5da80b3d64
commit eb8b241559
4 changed files with 90 additions and 15 deletions

View File

@@ -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 );
}
}