WordPress/wp-includes/class-wp-comment.php
Boone Gorges fc854a837f 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.
Built from https://develop.svn.wordpress.org/trunk@34546


git-svn-id: http://core.svn.wordpress.org/trunk@34510 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-09-25 15:13:24 +00:00

274 lines
4.5 KiB
PHP

<?php
/**
* Comment API: WP_Comment object class
*
* @package WordPress
* @subpackage Comments
* @since 4.4.0
*/
/**
* Core class used to organize comments as instantiated objects with defined members.
*
* @since 4.4.0
*/
final class WP_Comment {
/**
* Comment ID.
*
* @since 4.4.0
* @access public
* @var int
*/
public $comment_ID;
/**
* ID of the post the comment is associated with.
*
* @since 4.4.0
* @access public
* @var int
*/
public $comment_post_ID = 0;
/**
* Comment author ID.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_author = '';
/**
* Comment author email address.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_author_email = '';
/**
* Comment author URL.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_author_url = '';
/**
* Comment author IP address (IPv4 format).
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_author_IP = '';
/**
* Comment date in YYYY-MM-DD HH:MM:SS format.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_date = '0000-00-00 00:00:00';
/**
* Comment GMT date in YYYY-MM-DD HH::MM:SS format.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_date_gmt = '0000-00-00 00:00:00';
/**
* Comment content.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_content;
/**
* Comment karma count.
*
* @since 4.4.0
* @access public
* @var int
*/
public $comment_karma = 0;
/**
* Comment approval status.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_approved = '1';
/**
* Comment author HTTP user agent.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_agent = '';
/**
* Comment type.
*
* @since 4.4.0
* @access public
* @var string
*/
public $comment_type = '';
/**
* Parent comment ID.
*
* @since 4.4.0
* @access public
* @var int
*/
public $comment_parent = 0;
/**
* Comment author ID.
*
* @since 4.4.0
* @access public
* @var int
*/
public $user_id = 0;
/**
* Comment children.
*
* @since 4.4.0
* @access protected
* @var array
*/
protected $children;
/**
* Retrieves a WP_Comment instance.
*
* @since 4.4.0
* @access public
* @static
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $id Comment ID.
* @return WP_Comment|false Comment object, otherwise false.
*/
public static function get_instance( $id ) {
global $wpdb;
$comment_id = (int) $id;
if ( ! $comment_id ) {
return false;
}
$_comment = wp_cache_get( $comment_id, 'comment' );
if ( ! $_comment ) {
$_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
if ( ! $_comment ) {
return false;
}
wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
}
return new WP_Comment( $_comment );
}
/**
* Constructor.
*
* Populates properties with object vars.
*
* @since 4.4.0
* @access public
*
* @param WP_Comment $comment Comment object.
*/
public function __construct( $comment ) {
foreach ( get_object_vars( $comment ) as $key => $value ) {
$this->$key = $value;
}
}
/**
* Convert object to array.
*
* @since 4.4.0
* @access public
*
* @return array Object as array.
*/
public function to_array() {
return get_object_vars( $this );
}
/**
* Get the children of a comment.
*
* @since 4.4.0
* @access public
*
* @return array Array of `WP_Comment` objects.
*/
public function get_children() {
if ( is_null( $this->children ) ) {
$this->children = get_comments( array(
'parent' => $this->comment_ID,
'hierarchical' => 'threaded',
) );
}
return $this->children;
}
/**
* Add a child to the comment.
*
* Used by `WP_Comment_Query` when bulk-filling descendants.
*
* @since 4.4.0
* @access public
*
* @param WP_Comment $child Child comment.
*/
public function add_child( WP_Comment $child ) {
$this->comments[ $child->comment_ID ] = $child;
}
/**
* Get a child comment by ID.
*
* @since 4.4.0
* @access public
*
* @param int $child_id ID of the child.
* @return WP_Comment|bool Returns the comment object if found, otherwise false.
*/
public function get_child( $child_id ) {
if ( isset( $this->comments[ $child_id ] ) ) {
return $this->comments[ $child_id ];
}
return false;
}
}