From b2f253fc8ed70a675fc37b695518b6541b82d95b Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 1 Oct 2015 03:58:23 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-comment-query.php | 5 +++++ wp-includes/class-wp-comment.php | 27 +++++++++++++++++++++++++- wp-includes/version.php | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index ab49452ed8..6d215615d8 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -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; diff --git a/wp-includes/class-wp-comment.php b/wp-includes/class-wp-comment.php index 8dc9e9413b..6319ad4764 100644 --- a/wp-includes/class-wp-comment.php +++ b/wp-includes/class-wp-comment.php @@ -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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 6676fe9e40..6ba61c2dad 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.