Make WordPress Core


Ignore:
Timestamp:
09/25/2015 03:12:09 PM (11 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment.php

    r34533 r34546  
    289289                $this->assertEquals( 'fire', get_comment_meta( $c, 'sauce', true ) );
    290290        }
     291
     292        /**
     293         * @ticket 8071
     294         */
     295        public function test_wp_comment_get_children_should_fill_children() {
     296
     297                $p = $this->factory->post->create();
     298
     299                $c1 = $this->factory->comment->create( array(
     300                        'comment_post_ID' => $p,
     301                        'comment_approved' => '1',
     302                ) );
     303
     304                $c2 = $this->factory->comment->create( array(
     305                        'comment_post_ID' => $p,
     306                        'comment_approved' => '1',
     307                        'comment_parent' => $c1,
     308                ) );
     309
     310                $c3 = $this->factory->comment->create( array(
     311                        'comment_post_ID' => $p,
     312                        'comment_approved' => '1',
     313                        'comment_parent' => $c2,
     314                ) );
     315
     316                $c4 = $this->factory->comment->create( array(
     317                        'comment_post_ID' => $p,
     318                        'comment_approved' => '1',
     319                        'comment_parent' => $c1,
     320                ) );
     321
     322                $c5 = $this->factory->comment->create( array(
     323                        'comment_post_ID' => $p,
     324                        'comment_approved' => '1',
     325                ) );
     326
     327                $c6 = $this->factory->comment->create( array(
     328                        'comment_post_ID' => $p,
     329                        'comment_approved' => '1',
     330                        'comment_parent' => $c5,
     331                ) );
     332
     333                $comment = get_comment( $c1 );
     334                $children = $comment->get_children();
     335
     336                // Direct descendants of $c1.
     337                $this->assertEquals( array( $c2, $c4 ), array_values( wp_list_pluck( $children, 'comment_ID' ) ) );
     338
     339                // Direct descendants of $c2.
     340                $this->assertEquals( array( $c3 ), array_values( wp_list_pluck( $children[ $c2 ]->get_children(), 'comment_ID' ) ) );
     341        }
    291342}
Note: See TracChangeset for help on using the changeset viewer.