mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
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:
@@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user