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 #8071.

git-svn-id: https://develop.svn.wordpress.org/trunk@34546 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-09-25 15:12:09 +00:00
parent 27558ed678
commit e1b44f5203
4 changed files with 328 additions and 1 deletions

View File

@@ -288,4 +288,55 @@ class Tests_Comment extends WP_UnitTestCase {
$this->assertEquals( 'fire', get_comment_meta( $c, 'sauce', true ) );
}
/**
* @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' ) ) );
}
}