WordPress.org

Make WordPress Core

Changeset 34569


Ignore:
Timestamp:
09/26/2015 02:24:17 AM (4 years ago)
Author:
wonderboymusic
Message:

Comments: in WP_Comment::get_children(), accept an array so that the values for format, status, hierarchical, and orderby can be passed, instead of just format. The defaults for get_comments() include status = 'all' and orderby = '' - which is no bueno.

For threaded comments, we need comments to be retrieved within bounds, so logged-out users don't see unmoderated comments on the front end, etc.

Updates unit tests.

See #8071.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment.php

    r34561 r34569  
    228228     * @access public
    229229     *
    230      * @param string $format Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
    231      *                       Default 'tree'.
     230     * @param array $args {
     231     *     Array of arguments used to pass to get_comments() and determine format.
     232     *
     233     *     @type string $format        Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
     234     *                                 Default 'tree'.
     235     *     @type string $status        Comment status to limit results by. Accepts 'hold' (`comment_status=0`),
     236     *                                 'approve' (`comment_status=1`), 'all', or a custom comment status.
     237     *                                 Default 'all'.
     238     *     @type string $hierarchical  Whether to include comment descendants in the results.
     239     *                                 'threaded' returns a tree, with each comment's children
     240     *                                 stored in a `children` property on the `WP_Comment` object.
     241     *                                 'flat' returns a flat array of found comments plus their children.
     242     *                                 Pass `false` to leave out descendants.
     243     *                                 The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'.
     244     *                                 Accepts 'threaded', 'flat', or false. Default: 'threaded'.
     245     *     @type string|array $orderby Comment status or array of statuses. To use 'meta_value'
     246     *                                 or 'meta_value_num', `$meta_key` must also be defined.
     247     *                                 To sort by a specific `$meta_query` clause, use that
     248     *                                 clause's array key. Accepts 'comment_agent',
     249     *                                 'comment_approved', 'comment_author',
     250     *                                 'comment_author_email', 'comment_author_IP',
     251     *                                 'comment_author_url', 'comment_content', 'comment_date',
     252     *                                 'comment_date_gmt', 'comment_ID', 'comment_karma',
     253     *                                 'comment_parent', 'comment_post_ID', 'comment_type',
     254     *                                 'user_id', 'comment__in', 'meta_value', 'meta_value_num',
     255     *                                 the value of $meta_key, and the array keys of
     256     *                                 `$meta_query`. Also accepts false, an empty array, or
     257     *                                 'none' to disable `ORDER BY` clause.
     258     * }
    232259     * @return array Array of `WP_Comment` objects.
    233260     */
    234     public function get_children( $format = 'tree' ) {
     261    public function get_children( $args = array() ) {
     262        $defaults = array(
     263            'format' => 'tree',
     264            'status' => 'all',
     265            'hierarchical' => 'threaded',
     266            'orderby' => '',
     267        );
     268
     269        $_args = wp_parse_args( $args, $defaults );
     270        $_args['parent'] = $this->comment_ID;
     271
    235272        if ( is_null( $this->children ) ) {
    236             $this->children = get_comments( array(
    237                 'parent' => $this->comment_ID,
    238                 'hierarchical' => 'threaded',
    239             ) );
    240         }
    241 
    242         if ( 'flat' === $format ) {
     273            $this->children = get_comments( $_args );
     274        }
     275
     276        if ( 'flat' === $_args['format'] ) {
    243277            $children = array();
    244278            foreach ( $this->children as $child ) {
    245                 $children = array_merge( $children, array( $child ), $child->get_children( 'flat' ) );
     279                $child_args = $_args;
     280                $child_args['format'] = 'flat';
     281                // get_children() resets this value automatically.
     282                unset( $child_args['parent'] );
     283
     284                $children = array_merge( $children, array( $child ), $child->get_children( $child_args ) );
    246285            }
    247286        } else {
  • trunk/src/wp-includes/comment-template.php

    r34561 r34569  
    13081308    $comments_flat = array();
    13091309    foreach ( $_comments as $_comment ) {
    1310         $comments_flat = array_merge( $comments_flat, array( $_comment ), $_comment->get_children( 'flat' ) );
     1310        $comments_flat = array_merge( $comments_flat, array( $_comment ), $_comment->get_children( array(
     1311            'format' => 'flat',
     1312            'status' => $comment_args['status'],
     1313            'orderby' => $comment_args['orderby']
     1314        ) ) );
    13111315    }
    13121316
  • trunk/tests/phpunit/tests/comment/query.php

    r34550 r34569  
    20242024        ) );
    20252025
    2026         $q = new WP_Comment_Query( array(
    2027             'post_id' => $this->post_id,
     2026        $args = array(
    20282027            'hierarchical' => 'threaded',
    20292028            'orderby' => 'comment_ID',
    20302029            'order' => 'ASC',
    2031         ) );
     2030        );
     2031
     2032        $query_args = array_merge( $args, array(
     2033            'post_id' => $this->post_id,
     2034        ) );
     2035
     2036        $q = new WP_Comment_Query( $query_args );
    20322037
    20332038        // Top-level comments.
     
    20352040
    20362041        // Direct descendants of $c1.
    2037         $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children(), 'comment_ID' ) ) );
     2042        $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children( $args ), 'comment_ID' ) ) );
    20382043
    20392044        // Direct descendants of $c2.
    2040         $this->assertEqualSets( array( $c3 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children(), 'comment_ID' ) ) );
     2045        $this->assertEqualSets( array( $c3 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children( $args ), 'comment_ID' ) ) );
    20412046
    20422047        // Direct descendants of $c5.
    2043         $this->assertEqualSets( array( $c6 ), array_values( wp_list_pluck( $q->comments[ $c5 ]->get_children(), 'comment_ID' ) ) );
     2048        $this->assertEqualSets( array( $c6 ), array_values( wp_list_pluck( $q->comments[ $c5 ]->get_children( $args ), 'comment_ID' ) ) );
     2049    }
     2050
     2051    /**
     2052     * @ticket 8071
     2053     */
     2054    public function test_hierarchical_threaded_approved() {
     2055        $c1 = $this->factory->comment->create( array(
     2056            'comment_post_ID' => $this->post_id,
     2057            'comment_approved' => '1',
     2058        ) );
     2059
     2060        $c2 = $this->factory->comment->create( array(
     2061            'comment_post_ID' => $this->post_id,
     2062            'comment_approved' => '1',
     2063            'comment_parent' => $c1,
     2064        ) );
     2065
     2066        $c3 = $this->factory->comment->create( array(
     2067            'comment_post_ID' => $this->post_id,
     2068            'comment_approved' => '0',
     2069            'comment_parent' => $c2,
     2070        ) );
     2071
     2072        $c4 = $this->factory->comment->create( array(
     2073            'comment_post_ID' => $this->post_id,
     2074            'comment_approved' => '1',
     2075            'comment_parent' => $c1,
     2076        ) );
     2077
     2078        $c5 = $this->factory->comment->create( array(
     2079            'comment_post_ID' => $this->post_id,
     2080            'comment_approved' => '1',
     2081        ) );
     2082
     2083        $this->factory->comment->create( array(
     2084            'comment_post_ID' => $this->post_id,
     2085            'comment_approved' => '1',
     2086            'comment_parent' => $c5,
     2087        ) );
     2088
     2089        $args = array(
     2090            'hierarchical' => 'threaded',
     2091            'status' => 'approve',
     2092            'orderby' => 'comment_ID',
     2093            'order' => 'ASC',
     2094        );
     2095
     2096        $query_args = array_merge( $args, array(
     2097            'post_id' => $this->post_id,
     2098        ) );
     2099
     2100        $q = new WP_Comment_Query( $query_args );
     2101
     2102        // Top-level comments.
     2103        $this->assertEqualSets( array( $c1, $c5 ), array_values( wp_list_pluck( $q->comments, 'comment_ID' ) ) );
     2104
     2105        // Direct descendants of $c1.
     2106        $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children( $args ), 'comment_ID' ) ) );
     2107
     2108        // Direct descendants of $c2.
     2109        $this->assertEqualSets( array(), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children( $args ), 'comment_ID' ) ) );
    20442110    }
    20452111}
Note: See TracChangeset for help on using the changeset viewer.