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. git-svn-id: https://develop.svn.wordpress.org/trunk@34730 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f14dc03ad6
commit
a7dbb948c5
@ -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;
|
||||
|
@ -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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user