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/src/wp-includes/class-wp-comment.php

    r34409 r34546  
    151151
    152152    /**
     153     * Comment children.
     154     *
     155     * @since 4.4.0
     156     * @access protected
     157     * @var array
     158     */
     159    protected $children;
     160
     161    /**
    153162     * Retrieves a WP_Comment instance.
    154163     *
     
    212221        return get_object_vars( $this );
    213222    }
     223
     224    /**
     225     * Get the children of a comment.
     226     *
     227     * @since 4.4.0
     228     * @access public
     229     *
     230     * @return array Array of `WP_Comment` objects.
     231     */
     232    public function get_children() {
     233        if ( is_null( $this->children ) ) {
     234            $this->children = get_comments( array(
     235                'parent' => $this->comment_ID,
     236                'hierarchical' => 'threaded',
     237            ) );
     238        }
     239
     240        return $this->children;
     241    }
     242
     243    /**
     244     * Add a child to the comment.
     245     *
     246     * Used by `WP_Comment_Query` when bulk-filling descendants.
     247     *
     248     * @since 4.4.0
     249     * @access public
     250     *
     251     * @param WP_Comment $child Child comment.
     252     */
     253    public function add_child( WP_Comment $child ) {
     254        $this->comments[ $child->comment_ID ] = $child;
     255    }
     256
     257    /**
     258     * Get a child comment by ID.
     259     *
     260     * @since 4.4.0
     261     * @access public
     262     *
     263     * @param int $child_id ID of the child.
     264     * @return WP_Comment|bool Returns the comment object if found, otherwise false.
     265     */
     266    public function get_child( $child_id ) {
     267        if ( isset( $this->comments[ $child_id ] ) ) {
     268            return $this->comments[ $child_id ];
     269        }
     270
     271        return false;
     272    }
    214273}
Note: See TracChangeset for help on using the changeset viewer.