class-wp-comment.php 9.1 KB

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