Prevent extra db queries in `WP_Comment::get_children()`.

`WP_Comment_Query::fill_descendants()` queries for a comment tree in a way that
minimizes database overhead, and places the located descendants with their
proper parents. However, it doesn't touch leaf nodes - comments with no
children - so future calls to `get_children()` on those comment objects
result in unnecessary database queries. To prevent this, `fill_descendants()`
now sets a `populated_children` flag on all located `WP_Comment` objects.

See #8071.
Built from https://develop.svn.wordpress.org/trunk@34730


git-svn-id: http://core.svn.wordpress.org/trunk@34694 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-10-01 03:58:23 +00:00
parent dd9709d179
commit b2f253fc8e
3 changed files with 32 additions and 2 deletions

View File

@ -922,6 +922,11 @@ class WP_Comment_Query {
}
}
// Set the 'populated_children' flag, to ensure additional database queries aren't run.
foreach ( $ref as $_ref ) {
$_ref->populated_children( true );
}
$comments = $threaded_comments;
} else {
$comments = $all_comments;

View File

@ -158,6 +158,15 @@ final class WP_Comment {
*/
protected $children;
/**
* Whether children have been populated for this comment object.
*
* @since 4.4.0
* @access protected
* @var bool
*/
protected $populated_children = false;
/**
* Post fields.
*
@ -279,7 +288,11 @@ final class WP_Comment {
$_args['parent'] = $this->comment_ID;
if ( is_null( $this->children ) ) {
$this->children = get_comments( $_args );
if ( $this->populated_children ) {
$this->children = array();
} else {
$this->children = get_comments( $_args );
}
}
if ( 'flat' === $_args['format'] ) {
@ -330,6 +343,18 @@ final class WP_Comment {
return false;
}
/**
* Set the 'populated_children' flag.
*
* This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger
* unneeded database queries.
*
* @since 4.4.0
*/
public function populated_children( $set ) {
$this->populated_children = (bool) $set;
}
/**
* Check whether a non-public property is set.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.4-alpha-34729';
$wp_version = '4.4-alpha-34730';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.