WP_Comment

Core class used to organize comments as instantiated objects with defined members.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-comment.php  
  1. final class WP_Comment { 
  2.  
  3. /** 
  4. * Comment ID. 
  5. * @since 4.4.0 
  6. * @access public 
  7. * @var int 
  8. */ 
  9. public $comment_ID; 
  10.  
  11. /** 
  12. * ID of the post the comment is associated with. 
  13. * @since 4.4.0 
  14. * @access public 
  15. * @var int 
  16. */ 
  17. public $comment_post_ID = 0; 
  18.  
  19. /** 
  20. * Comment author name. 
  21. * @since 4.4.0 
  22. * @access public 
  23. * @var string 
  24. */ 
  25. public $comment_author = ''; 
  26.  
  27. /** 
  28. * Comment author email address. 
  29. * @since 4.4.0 
  30. * @access public 
  31. * @var string 
  32. */ 
  33. public $comment_author_email = ''; 
  34.  
  35. /** 
  36. * Comment author URL. 
  37. * @since 4.4.0 
  38. * @access public 
  39. * @var string 
  40. */ 
  41. public $comment_author_url = ''; 
  42.  
  43. /** 
  44. * Comment author IP address (IPv4 format). 
  45. * @since 4.4.0 
  46. * @access public 
  47. * @var string 
  48. */ 
  49. public $comment_author_IP = ''; 
  50.  
  51. /** 
  52. * Comment date in YYYY-MM-DD HH:MM:SS format. 
  53. * @since 4.4.0 
  54. * @access public 
  55. * @var string 
  56. */ 
  57. public $comment_date = '0000-00-00 00:00:00'; 
  58.  
  59. /** 
  60. * Comment GMT date in YYYY-MM-DD HH::MM:SS format. 
  61. * @since 4.4.0 
  62. * @access public 
  63. * @var string 
  64. */ 
  65. public $comment_date_gmt = '0000-00-00 00:00:00'; 
  66.  
  67. /** 
  68. * Comment content. 
  69. * @since 4.4.0 
  70. * @access public 
  71. * @var string 
  72. */ 
  73. public $comment_content; 
  74.  
  75. /** 
  76. * Comment karma count. 
  77. * @since 4.4.0 
  78. * @access public 
  79. * @var int 
  80. */ 
  81. public $comment_karma = 0; 
  82.  
  83. /** 
  84. * Comment approval status. 
  85. * @since 4.4.0 
  86. * @access public 
  87. * @var string 
  88. */ 
  89. public $comment_approved = '1'; 
  90.  
  91. /** 
  92. * Comment author HTTP user agent. 
  93. * @since 4.4.0 
  94. * @access public 
  95. * @var string 
  96. */ 
  97. public $comment_agent = ''; 
  98.  
  99. /** 
  100. * Comment type. 
  101. * @since 4.4.0 
  102. * @access public 
  103. * @var string 
  104. */ 
  105. public $comment_type = ''; 
  106.  
  107. /** 
  108. * Parent comment ID. 
  109. * @since 4.4.0 
  110. * @access public 
  111. * @var int 
  112. */ 
  113. public $comment_parent = 0; 
  114.  
  115. /** 
  116. * Comment author ID. 
  117. * @since 4.4.0 
  118. * @access public 
  119. * @var int 
  120. */ 
  121. public $user_id = 0; 
  122.  
  123. /** 
  124. * Comment children. 
  125. * @since 4.4.0 
  126. * @access protected 
  127. * @var array 
  128. */ 
  129. protected $children; 
  130.  
  131. /** 
  132. * Whether children have been populated for this comment object. 
  133. * @since 4.4.0 
  134. * @access protected 
  135. * @var bool 
  136. */ 
  137. protected $populated_children = false; 
  138.  
  139. /** 
  140. * Post fields. 
  141. * @since 4.4.0 
  142. * @access protected 
  143. * @var array 
  144. */ 
  145. 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' ); 
  146.  
  147. /** 
  148. * Retrieves a WP_Comment instance. 
  149. * @since 4.4.0 
  150. * @access public 
  151. * @static 
  152. * @global wpdb $wpdb WordPress database abstraction object. 
  153. * @param int $id Comment ID. 
  154. * @return WP_Comment|false Comment object, otherwise false. 
  155. */ 
  156. public static function get_instance( $id ) { 
  157. global $wpdb; 
  158.  
  159. $comment_id = (int) $id; 
  160. if ( ! $comment_id ) { 
  161. return false; 
  162.  
  163. $_comment = wp_cache_get( $comment_id, 'comment' ); 
  164.  
  165. if ( ! $_comment ) { 
  166. $_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) ); 
  167.  
  168. if ( ! $_comment ) { 
  169. return false; 
  170.  
  171. wp_cache_add( $_comment->comment_ID, $_comment, 'comment' ); 
  172.  
  173. return new WP_Comment( $_comment ); 
  174.  
  175. /** 
  176. * Constructor. 
  177. * Populates properties with object vars. 
  178. * @since 4.4.0 
  179. * @access public 
  180. * @param WP_Comment $comment Comment object. 
  181. */ 
  182. public function __construct( $comment ) { 
  183. foreach ( get_object_vars( $comment ) as $key => $value ) { 
  184. $this->$key = $value; 
  185.  
  186. /** 
  187. * Convert object to array. 
  188. * @since 4.4.0 
  189. * @access public 
  190. * @return array Object as array. 
  191. */ 
  192. public function to_array() { 
  193. return get_object_vars( $this ); 
  194.  
  195. /** 
  196. * Get the children of a comment. 
  197. * @since 4.4.0 
  198. * @access public 
  199. * @param array $args { 
  200. * Array of arguments used to pass to get_comments() and determine format. 
  201. * @type string $format Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array. 
  202. * Default 'tree'. 
  203. * @type string $status Comment status to limit results by. Accepts 'hold' (`comment_status=0`),  
  204. * 'approve' (`comment_status=1`), 'all', or a custom comment status. 
  205. * Default 'all'. 
  206. * @type string $hierarchical Whether to include comment descendants in the results. 
  207. * 'threaded' returns a tree, with each comment's children 
  208. * stored in a `children` property on the `WP_Comment` object. 
  209. * 'flat' returns a flat array of found comments plus their children. 
  210. * Pass `false` to leave out descendants. 
  211. * The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'. 
  212. * Accepts 'threaded', 'flat', or false. Default: 'threaded'. 
  213. * @type string|array $orderby Comment status or array of statuses. To use 'meta_value' 
  214. * or 'meta_value_num', `$meta_key` must also be defined. 
  215. * To sort by a specific `$meta_query` clause, use that 
  216. * clause's array key. Accepts 'comment_agent',  
  217. * 'comment_approved', 'comment_author',  
  218. * 'comment_author_email', 'comment_author_IP',  
  219. * 'comment_author_url', 'comment_content', 'comment_date',  
  220. * 'comment_date_gmt', 'comment_ID', 'comment_karma',  
  221. * 'comment_parent', 'comment_post_ID', 'comment_type',  
  222. * 'user_id', 'comment__in', 'meta_value', 'meta_value_num',  
  223. * the value of $meta_key, and the array keys of 
  224. * `$meta_query`. Also accepts false, an empty array, or 
  225. * 'none' to disable `ORDER BY` clause. 
  226. * } 
  227. * @return array Array of `WP_Comment` objects. 
  228. */ 
  229. public function get_children( $args = array() ) { 
  230. $defaults = array( 
  231. 'format' => 'tree',  
  232. 'status' => 'all',  
  233. 'hierarchical' => 'threaded',  
  234. 'orderby' => '',  
  235. ); 
  236.  
  237. $_args = wp_parse_args( $args, $defaults ); 
  238. $_args['parent'] = $this->comment_ID; 
  239.  
  240. if ( is_null( $this->children ) ) { 
  241. if ( $this->populated_children ) { 
  242. $this->children = array(); 
  243. } else { 
  244. $this->children = get_comments( $_args ); 
  245.  
  246. if ( 'flat' === $_args['format'] ) { 
  247. $children = array(); 
  248. foreach ( $this->children as $child ) { 
  249. $child_args = $_args; 
  250. $child_args['format'] = 'flat'; 
  251. // get_children() resets this value automatically. 
  252. unset( $child_args['parent'] ); 
  253.  
  254. $children = array_merge( $children, array( $child ), $child->get_children( $child_args ) ); 
  255. } else { 
  256. $children = $this->children; 
  257.  
  258. return $children; 
  259.  
  260. /** 
  261. * Add a child to the comment. 
  262. * Used by `WP_Comment_Query` when bulk-filling descendants. 
  263. * @since 4.4.0 
  264. * @access public 
  265. * @param WP_Comment $child Child comment. 
  266. */ 
  267. public function add_child( WP_Comment $child ) { 
  268. $this->children[ $child->comment_ID ] = $child; 
  269.  
  270. /** 
  271. * Get a child comment by ID. 
  272. * @since 4.4.0 
  273. * @access public 
  274. * @param int $child_id ID of the child. 
  275. * @return WP_Comment|bool Returns the comment object if found, otherwise false. 
  276. */ 
  277. public function get_child( $child_id ) { 
  278. if ( isset( $this->children[ $child_id ] ) ) { 
  279. return $this->children[ $child_id ]; 
  280.  
  281. return false; 
  282.  
  283. /** 
  284. * Set the 'populated_children' flag. 
  285. * This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger 
  286. * unneeded database queries. 
  287. * @since 4.4.0 
  288. * @param bool $set Whether the comment's children have already been populated. 
  289. */ 
  290. public function populated_children( $set ) { 
  291. $this->populated_children = (bool) $set; 
  292.  
  293. /** 
  294. * Check whether a non-public property is set. 
  295. * If `$name` matches a post field, the comment post will be loaded and the post's value checked. 
  296. * @since 4.4.0 
  297. * @access public 
  298. * @param string $name Property name. 
  299. * @return bool 
  300. */ 
  301. public function __isset( $name ) { 
  302. if ( in_array( $name, $this->post_fields ) && 0 !== (int) $this->comment_post_ID ) { 
  303. $post = get_post( $this->comment_post_ID ); 
  304. return property_exists( $post, $name ); 
  305.  
  306. /** 
  307. * Magic getter. 
  308. * If `$name` matches a post field, the comment post will be loaded and the post's value returned. 
  309. * @since 4.4.0 
  310. * @access public 
  311. * @param string $name 
  312. * @return mixed 
  313. */ 
  314. public function __get( $name ) { 
  315. if ( in_array( $name, $this->post_fields ) ) { 
  316. $post = get_post( $this->comment_post_ID ); 
  317. return $post->$name;