class-wp-comment.php 8.8 KB

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