<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[34546] trunk: Introduce hierarchical query support to `WP_Comment_Query`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/34546">34546</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/34546","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-09-25 15:12:09 +0000 (Fri, 25 Sep 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Introduce hierarchical query support to `WP_Comment_Query`.

Comments can be threaded. Now your query can be threaded too! Bonus: it's
not totally insane.

* The new `$hierarchical` parameter for `WP_Comment_Query` accepts three values:
  * `false` - Default value, and equivalent to current behavior. No descendants are fetched for matched comments.
  * `'flat'` - `WP_Comment_Query` will fetch the descendant tree for each comment matched by the query paramaters, and append them to the flat array of comments returned. Use this when you have a separate routine for constructing the tree - for example, when passing a list of comments to a `Walker` object.
  * `'threaded'` - `WP_Comment_Query` will fetch the descendant tree for each comment, and return it in a tree structure located in the `children` property of the `WP_Comment` objects.
* `WP_Comment` now has a few utility methods for fetching the descendant tree (`get_children()`), fetching a single direct descendant comment (`get_child()`), and adding anothing `WP_Comment` object as a direct descendant (`add_child()`). Note that `add_child()` only modifies the comment object - it does not touch the database.

Props boonebgorges, wonderboymusic.
See <a href="https://core.trac.wordpress.org/ticket/8071">#8071</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcommentqueryphp">trunk/src/wp-includes/class-wp-comment-query.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcommentphp">trunk/src/wp-includes/class-wp-comment.php</a></li>
<li><a href="#trunktestsphpunittestscommentqueryphp">trunk/tests/phpunit/tests/comment/query.php</a></li>
<li><a href="#trunktestsphpunittestscommentphp">trunk/tests/phpunit/tests/comment.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpcommentqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-comment-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-comment-query.php  2015-09-25 14:42:48 UTC (rev 34545)
+++ trunk/src/wp-includes/class-wp-comment-query.php    2015-09-25 15:12:09 UTC (rev 34546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -137,7 +137,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.2.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache` and `$no_found_rows` were added.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
+        *              and `$hierarchical` were added.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string|array $query {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -206,6 +207,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type array        $type__in            Include comments from a given array of comment types. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type array        $type__not_in        Exclude comments from a given array of comment types. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int          $user_id             Include comments for a specific user ID. Default empty.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *     @type bool|string  $hierarchical        Whether to include comment descendants in the results.
+        *                                             'threaded' returns a tree, with each comment's children stored
+        *                                             in a `children` property on the `WP_Comment` object. 'flat'
+        *                                             returns a flat array of found comments plus their children.
+        *                                             Pass `false` to leave out descendants. The parameter is ignored
+        *                                             (forced to `false`) when `$fields` is 'ids' or 'counts'.
+        *                                             Accepts 'threaded', 'flat', or false. Default: false.
</ins><span class="cx" style="display: block; padding: 0 10px">          *     @type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
</span><span class="cx" style="display: block; padding: 0 10px">         *                                                   Default true.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -249,6 +257,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'meta_value' => '',
</span><span class="cx" style="display: block; padding: 0 10px">                        'meta_query' => '',
</span><span class="cx" style="display: block; padding: 0 10px">                        'date_query' => null, // See WP_Date_Query
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'hierarchical' => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'update_comment_meta_cache' => true,
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -396,6 +405,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Convert to WP_Comment instances
</span><span class="cx" style="display: block; padding: 0 10px">                $comments = array_map( 'get_comment', $_comments );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $this->query_vars['hierarchical'] ) {
+                       $comments = $this->fill_descendants( $comments );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->comments = $comments;
</span><span class="cx" style="display: block; padding: 0 10px">                return $this->comments;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -665,6 +678,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $this->query_vars['hierarchical'] && ! $this->query_vars['parent'] ) {
+                       $this->query_vars['parent'] = 0;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( '' !== $this->query_vars['parent'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -798,6 +815,84 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Fetch descendants for located comments.
+        *
+        * Instead of calling `get_children()` separately on each child comment, we do a single set of queries to fetch
+        * the descendant trees for all matched top-level comments.
+        *
+        * @since 4.4.0
+        *
+        * @param array $comments Array of top-level comments whose descendants should be filled in.
+        * @return array
+        */
+       protected function fill_descendants( $comments ) {
+               global $wpdb;
+
+               $levels = array(
+                       0 => wp_list_pluck( $comments, 'comment_ID' ),
+               );
+
+               $where_clauses = $this->sql_clauses['where'];
+               unset(
+                       $where_clauses['parent'],
+                       $where_clauses['parent__in'],
+                       $where_clauses['parent__not_in']
+               );
+
+               // Fetch an entire level of the descendant tree at a time.
+               $level = 0;
+               do {
+                       $parent_ids = $levels[ $level ];
+                       $where = 'WHERE ' . implode( ' AND ', $where_clauses ) . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
+                       $comment_ids = $wpdb->get_col( "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']}" );
+
+                       $level++;
+                       $levels[ $level ] = $comment_ids;
+               } while ( $comment_ids );
+
+               // Prime comment caches for non-top-level comments.
+               $descendant_ids = array();
+               for ( $i = 1; $i < count( $levels ); $i++ ) {
+                       $descendant_ids = array_merge( $descendant_ids, $levels[ $i ] );
+               }
+
+               _prime_comment_caches( $descendant_ids, $this->query_vars['update_comment_meta_cache'] );
+
+               // Assemble a flat array of all comments + descendants.
+               $all_comments = $comments;
+               foreach ( $descendant_ids as $descendant_id ) {
+                       $all_comments[] = get_comment( $descendant_id );
+               }
+
+               // If a threaded representation was requested, build the tree.
+               if ( 'threaded' === $this->query_vars['hierarchical'] ) {
+                       $threaded_comments = $ref = array();
+                       foreach ( $all_comments as $k => $c ) {
+                               $_c = get_comment( $c->comment_ID );
+
+                               // If the comment isn't in the reference array, it goes in the top level of the thread.
+                               if ( ! isset( $ref[ $c->comment_parent ] ) ) {
+                                       $threaded_comments[ $_c->comment_ID ] = $_c;
+                                       $ref[ $_c->comment_ID ] = $threaded_comments[ $_c->comment_ID ];
+
+                               // Otherwise, set it as a child of its parent.
+                               } else {
+
+                                       $ref[ $_c->comment_parent ]->add_child( $_c );
+//                                     $ref[ $c->comment_parent ]->children[ $c->comment_ID ] = $c;
+                                       $ref[ $_c->comment_ID ] = $ref[ $_c->comment_parent ]->get_child( $_c->comment_ID );
+                               }
+                       }
+
+                       $comments = $threaded_comments;
+               } else {
+                       $comments = $all_comments;
+               }
+
+               return $comments;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Used internally to generate an SQL string for searching across multiple columns
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.1.0
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-comment.php        2015-09-25 14:42:48 UTC (rev 34545)
+++ trunk/src/wp-includes/class-wp-comment.php  2015-09-25 15:12:09 UTC (rev 34546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -150,6 +150,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $user_id = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Comment children.
+        *
+        * @since 4.4.0
+        * @access protected
+        * @var array
+        */
+       protected $children;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Retrieves a WP_Comment instance.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -211,4 +220,54 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function to_array() {
</span><span class="cx" style="display: block; padding: 0 10px">                return get_object_vars( $this );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Get the children of a comment.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @return array Array of `WP_Comment` objects.
+        */
+       public function get_children() {
+               if ( is_null( $this->children ) ) {
+                       $this->children = get_comments( array(
+                               'parent' => $this->comment_ID,
+                               'hierarchical' => 'threaded',
+                       ) );
+               }
+
+               return $this->children;
+       }
+
+       /**
+        * Add a child to the comment.
+        *
+        * Used by `WP_Comment_Query` when bulk-filling descendants.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @param WP_Comment $child Child comment.
+        */
+       public function add_child( WP_Comment $child ) {
+               $this->comments[ $child->comment_ID ] = $child;
+       }
+
+       /**
+        * Get a child comment by ID.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @param int $child_id ID of the child.
+        * @return WP_Comment|bool Returns the comment object if found, otherwise false.
+        */
+       public function get_child( $child_id ) {
+               if ( isset( $this->comments[ $child_id ] ) ) {
+                       return $this->comments[ $child_id ];
+               }
+
+               return false;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestscommentqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment/query.php       2015-09-25 14:42:48 UTC (rev 34545)
+++ trunk/tests/phpunit/tests/comment/query.php 2015-09-25 15:12:09 UTC (rev 34546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1920,4 +1920,126 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 3, $q->found_comments );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 2, $q->max_num_pages );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 8071
+        */
+       public function test_hierarchical_should_skip_child_comments_in_offset() {
+               $top_level_0 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+               ) );
+
+               $child_of_0 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+                       'comment_parent' => $top_level_0,
+               ) );
+
+               $top_level_comments = $this->factory->comment->create_many( 3, array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+               ) );
+
+               $q = new WP_Comment_Query( array(
+                       'post_id' => $this->post_id,
+                       'hierarchical' => 'flat',
+                       'number' => 2,
+                       'offset' => 1,
+                       'orderby' => 'comment_ID',
+                       'order' => 'ASC',
+                       'fields' => 'ids',
+               ) );
+
+               $this->assertEquals( array( $top_level_comments[0], $top_level_comments[1] ), $q->comments );
+       }
+
+       /**
+        * @ticket 8071
+        */
+       public function test_hierarchical_should_not_include_child_comments_in_number() {
+               $top_level_0 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+               ) );
+
+               $child_of_0 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+                       'comment_parent' => $top_level_0,
+               ) );
+
+               $top_level_comments = $this->factory->comment->create_many( 3, array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+               ) );
+
+               $q = new WP_Comment_Query( array(
+                       'post_id' => $this->post_id,
+                       'hierarchical' => 'flat',
+                       'number' => 2,
+                       'orderby' => 'comment_ID',
+                       'order' => 'ASC',
+               ) );
+
+               $this->assertEqualSets( array( $top_level_0, $child_of_0, $top_level_comments[0] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
+       }
+
+       /**
+        * @ticket 8071
+        */
+       public function test_hierarchical_threaded() {
+               $c1 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+               ) );
+
+               $c2 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c1,
+               ) );
+
+               $c3 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c2,
+               ) );
+
+               $c4 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c1,
+               ) );
+
+               $c5 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+               ) );
+
+               $c6 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->post_id,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c5,
+               ) );
+
+               $q = new WP_Comment_Query( array(
+                       'post_id' => $this->post_id,
+                       'hierarchical' => 'threaded',
+                       'orderby' => 'comment_ID',
+                       'order' => 'ASC',
+               ) );
+
+               // Top-level comments.
+               $this->assertEquals( array( $c1, $c5 ), array_values( wp_list_pluck( $q->comments, 'comment_ID' ) ) );
+
+               // Direct descendants of $c1.
+               $this->assertEquals( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children(), 'comment_ID' ) ) );
+
+               // Direct descendants of $c2.
+               $this->assertEquals( array( $c3 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children(), 'comment_ID' ) ) );
+
+               // Direct descendants of $c5.
+               $this->assertEquals( array( $c6 ), array_values( wp_list_pluck( $q->comments[ $c5 ]->get_children(), 'comment_ID' ) ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestscommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment.php     2015-09-25 14:42:48 UTC (rev 34545)
+++ trunk/tests/phpunit/tests/comment.php       2015-09-25 15:12:09 UTC (rev 34546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -288,4 +288,55 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'fire', get_comment_meta( $c, 'sauce', true ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 8071
+        */
+       public function test_wp_comment_get_children_should_fill_children() {
+
+               $p = $this->factory->post->create();
+
+               $c1 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_approved' => '1',
+               ) );
+
+               $c2 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c1,
+               ) );
+
+               $c3 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c2,
+               ) );
+
+               $c4 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c1,
+               ) );
+
+               $c5 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_approved' => '1',
+               ) );
+
+               $c6 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_approved' => '1',
+                       'comment_parent' => $c5,
+               ) );
+
+               $comment = get_comment( $c1 );
+               $children = $comment->get_children();
+
+               // Direct descendants of $c1.
+               $this->assertEquals( array( $c2, $c4 ), array_values( wp_list_pluck( $children, 'comment_ID' ) ) );
+
+               // Direct descendants of $c2.
+               $this->assertEquals( array( $c3 ), array_values( wp_list_pluck( $children[ $c2 ]->get_children(), 'comment_ID' ) ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>