3 * Comment API: WP_Comment class
11 * Core class used to organize comments as instantiated objects with defined members.
15 final class WP_Comment {
27 * ID of the post the comment is associated with.
33 public $comment_post_ID = 0;
36 * Comment author name.
42 public $comment_author = '';
45 * Comment author email address.
51 public $comment_author_email = '';
60 public $comment_author_url = '';
63 * Comment author IP address (IPv4 format).
69 public $comment_author_IP = '';
72 * Comment date in YYYY-MM-DD HH:MM:SS format.
78 public $comment_date = '0000-00-00 00:00:00';
81 * Comment GMT date in YYYY-MM-DD HH::MM:SS format.
87 public $comment_date_gmt = '0000-00-00 00:00:00';
96 public $comment_content;
99 * Comment karma count.
105 public $comment_karma = 0;
108 * Comment approval status.
114 public $comment_approved = '1';
117 * Comment author HTTP user agent.
123 public $comment_agent = '';
132 public $comment_type = '';
141 public $comment_parent = 0;
162 * Whether children have been populated for this comment object.
168 protected $populated_children = false;
177 protected $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
180 * Retrieves a WP_Comment instance.
186 * @global wpdb $wpdb WordPress database abstraction object.
188 * @param int $id Comment ID.
189 * @return WP_Comment|false Comment object, otherwise false.
191 public static function get_instance( $id ) {
194 $comment_id = (int) $id;
195 if ( ! $comment_id ) {
199 $_comment = wp_cache_get( $comment_id, 'comment' );
202 $_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
208 wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
211 return new WP_Comment( $_comment );
217 * Populates properties with object vars.
222 * @param WP_Comment $comment Comment object.
224 public function __construct( $comment ) {
225 foreach ( get_object_vars( $comment ) as $key => $value ) {
226 $this->$key = $value;
231 * Convert object to array.
236 * @return array Object as array.
238 public function to_array() {
239 return get_object_vars( $this );
243 * Get the children of a comment.
248 * @param array $args {
249 * Array of arguments used to pass to get_comments() and determine format.
251 * @type string $format Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
253 * @type string $status Comment status to limit results by. Accepts 'hold' (`comment_status=0`),
254 * 'approve' (`comment_status=1`), 'all', or a custom comment status.
256 * @type string $hierarchical Whether to include comment descendants in the results.
257 * 'threaded' returns a tree, with each comment's children
258 * stored in a `children` property on the `WP_Comment` object.
259 * 'flat' returns a flat array of found comments plus their children.
260 * Pass `false` to leave out descendants.
261 * The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'.
262 * Accepts 'threaded', 'flat', or false. Default: 'threaded'.
263 * @type string|array $orderby Comment status or array of statuses. To use 'meta_value'
264 * or 'meta_value_num', `$meta_key` must also be defined.
265 * To sort by a specific `$meta_query` clause, use that
266 * clause's array key. Accepts 'comment_agent',
267 * 'comment_approved', 'comment_author',
268 * 'comment_author_email', 'comment_author_IP',
269 * 'comment_author_url', 'comment_content', 'comment_date',
270 * 'comment_date_gmt', 'comment_ID', 'comment_karma',
271 * 'comment_parent', 'comment_post_ID', 'comment_type',
272 * 'user_id', 'comment__in', 'meta_value', 'meta_value_num',
273 * the value of $meta_key, and the array keys of
274 * `$meta_query`. Also accepts false, an empty array, or
275 * 'none' to disable `ORDER BY` clause.
277 * @return array Array of `WP_Comment` objects.
279 public function get_children( $args = array() ) {
283 'hierarchical' => 'threaded',
287 $_args = wp_parse_args( $args, $defaults );
288 $_args['parent'] = $this->comment_ID;
290 if ( is_null( $this->children ) ) {
291 if ( $this->populated_children ) {
292 $this->children = array();
294 $this->children = get_comments( $_args );
298 if ( 'flat' === $_args['format'] ) {
300 foreach ( $this->children as $child ) {
301 $child_args = $_args;
302 $child_args['format'] = 'flat';
303 // get_children() resets this value automatically.
304 unset( $child_args['parent'] );
306 $children = array_merge( $children, array( $child ), $child->get_children( $child_args ) );
309 $children = $this->children;
316 * Add a child to the comment.
318 * Used by `WP_Comment_Query` when bulk-filling descendants.
323 * @param WP_Comment $child Child comment.
325 public function add_child( WP_Comment $child ) {
326 $this->children[ $child->comment_ID ] = $child;
330 * Get a child comment by ID.
335 * @param int $child_id ID of the child.
336 * @return WP_Comment|bool Returns the comment object if found, otherwise false.
338 public function get_child( $child_id ) {
339 if ( isset( $this->children[ $child_id ] ) ) {
340 return $this->children[ $child_id ];
347 * Set the 'populated_children' flag.
349 * This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger
350 * unneeded database queries.
354 * @param bool $set Whether the comment's children have already been populated.
356 public function populated_children( $set ) {
357 $this->populated_children = (bool) $set;
361 * Check whether a non-public property is set.
363 * If `$name` matches a post field, the comment post will be loaded and the post's value checked.
368 * @param string $name Property name.
371 public function __isset( $name ) {
372 if ( in_array( $name, $this->post_fields ) && 0 !== (int) $this->comment_post_ID ) {
373 $post = get_post( $this->comment_post_ID );
374 return property_exists( $post, $name );
381 * If `$name` matches a post field, the comment post will be loaded and the post's value returned.
386 * @param string $name
389 public function __get( $name ) {
390 if ( in_array( $name, $this->post_fields ) ) {
391 $post = get_post( $this->comment_post_ID );