Make WordPress Core


Ignore:
Timestamp:
09/25/2015 03:12:09 PM (9 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/query.php

    r34544 r34546  
    19211921        $this->assertEquals( 2, $q->max_num_pages );
    19221922    }
     1923
     1924    /**
     1925     * @ticket 8071
     1926     */
     1927    public function test_hierarchical_should_skip_child_comments_in_offset() {
     1928        $top_level_0 = $this->factory->comment->create( array(
     1929            'comment_post_ID' => $this->post_id,
     1930            'comment_approved' => '1',
     1931        ) );
     1932
     1933        $child_of_0 = $this->factory->comment->create( array(
     1934            'comment_post_ID' => $this->post_id,
     1935            'comment_approved' => '1',
     1936            'comment_parent' => $top_level_0,
     1937        ) );
     1938
     1939        $top_level_comments = $this->factory->comment->create_many( 3, array(
     1940            'comment_post_ID' => $this->post_id,
     1941            'comment_approved' => '1',
     1942        ) );
     1943
     1944        $q = new WP_Comment_Query( array(
     1945            'post_id' => $this->post_id,
     1946            'hierarchical' => 'flat',
     1947            'number' => 2,
     1948            'offset' => 1,
     1949            'orderby' => 'comment_ID',
     1950            'order' => 'ASC',
     1951            'fields' => 'ids',
     1952        ) );
     1953
     1954        $this->assertEquals( array( $top_level_comments[0], $top_level_comments[1] ), $q->comments );
     1955    }
     1956
     1957    /**
     1958     * @ticket 8071
     1959     */
     1960    public function test_hierarchical_should_not_include_child_comments_in_number() {
     1961        $top_level_0 = $this->factory->comment->create( array(
     1962            'comment_post_ID' => $this->post_id,
     1963            'comment_approved' => '1',
     1964        ) );
     1965
     1966        $child_of_0 = $this->factory->comment->create( array(
     1967            'comment_post_ID' => $this->post_id,
     1968            'comment_approved' => '1',
     1969            'comment_parent' => $top_level_0,
     1970        ) );
     1971
     1972        $top_level_comments = $this->factory->comment->create_many( 3, array(
     1973            'comment_post_ID' => $this->post_id,
     1974            'comment_approved' => '1',
     1975        ) );
     1976
     1977        $q = new WP_Comment_Query( array(
     1978            'post_id' => $this->post_id,
     1979            'hierarchical' => 'flat',
     1980            'number' => 2,
     1981            'orderby' => 'comment_ID',
     1982            'order' => 'ASC',
     1983        ) );
     1984
     1985        $this->assertEqualSets( array( $top_level_0, $child_of_0, $top_level_comments[0] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
     1986    }
     1987
     1988    /**
     1989     * @ticket 8071
     1990     */
     1991    public function test_hierarchical_threaded() {
     1992        $c1 = $this->factory->comment->create( array(
     1993            'comment_post_ID' => $this->post_id,
     1994            'comment_approved' => '1',
     1995        ) );
     1996
     1997        $c2 = $this->factory->comment->create( array(
     1998            'comment_post_ID' => $this->post_id,
     1999            'comment_approved' => '1',
     2000            'comment_parent' => $c1,
     2001        ) );
     2002
     2003        $c3 = $this->factory->comment->create( array(
     2004            'comment_post_ID' => $this->post_id,
     2005            'comment_approved' => '1',
     2006            'comment_parent' => $c2,
     2007        ) );
     2008
     2009        $c4 = $this->factory->comment->create( array(
     2010            'comment_post_ID' => $this->post_id,
     2011            'comment_approved' => '1',
     2012            'comment_parent' => $c1,
     2013        ) );
     2014
     2015        $c5 = $this->factory->comment->create( array(
     2016            'comment_post_ID' => $this->post_id,
     2017            'comment_approved' => '1',
     2018        ) );
     2019
     2020        $c6 = $this->factory->comment->create( array(
     2021            'comment_post_ID' => $this->post_id,
     2022            'comment_approved' => '1',
     2023            'comment_parent' => $c5,
     2024        ) );
     2025
     2026        $q = new WP_Comment_Query( array(
     2027            'post_id' => $this->post_id,
     2028            'hierarchical' => 'threaded',
     2029            'orderby' => 'comment_ID',
     2030            'order' => 'ASC',
     2031        ) );
     2032
     2033        // Top-level comments.
     2034        $this->assertEquals( array( $c1, $c5 ), array_values( wp_list_pluck( $q->comments, 'comment_ID' ) ) );
     2035
     2036        // Direct descendants of $c1.
     2037        $this->assertEquals( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children(), 'comment_ID' ) ) );
     2038
     2039        // Direct descendants of $c2.
     2040        $this->assertEquals( array( $c3 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children(), 'comment_ID' ) ) );
     2041
     2042        // Direct descendants of $c5.
     2043        $this->assertEquals( array( $c6 ), array_values( wp_list_pluck( $q->comments[ $c5 ]->get_children(), 'comment_ID' ) ) );
     2044    }
    19232045}
Note: See TracChangeset for help on using the changeset viewer.