PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-comment.php

http://github.com/wordpress/wordpress
PHP | 369 lines | 98 code | 40 blank | 231 comment | 12 complexity | 31ea2168fad3696f5a76298eb5670313 MD5 | raw file
Possible License(s): 0BSD
  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. * @since 5.5.0 Default value changed to `comment`.
  104. * @var string
  105. */
  106. public $comment_type = 'comment';
  107. /**
  108. * Parent comment ID.
  109. *
  110. * @since 4.4.0
  111. * @var int
  112. */
  113. public $comment_parent = 0;
  114. /**
  115. * Comment author ID.
  116. *
  117. * @since 4.4.0
  118. * @var int
  119. */
  120. public $user_id = 0;
  121. /**
  122. * Comment children.
  123. *
  124. * @since 4.4.0
  125. * @var array
  126. */
  127. protected $children;
  128. /**
  129. * Whether children have been populated for this comment object.
  130. *
  131. * @since 4.4.0
  132. * @var bool
  133. */
  134. protected $populated_children = false;
  135. /**
  136. * Post fields.
  137. *
  138. * @since 4.4.0
  139. * @var array
  140. */
  141. 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' );
  142. /**
  143. * Retrieves a WP_Comment instance.
  144. *
  145. * @since 4.4.0
  146. *
  147. * @global wpdb $wpdb WordPress database abstraction object.
  148. *
  149. * @param int $id Comment ID.
  150. * @return WP_Comment|false Comment object, otherwise false.
  151. */
  152. public static function get_instance( $id ) {
  153. global $wpdb;
  154. $comment_id = (int) $id;
  155. if ( ! $comment_id ) {
  156. return false;
  157. }
  158. $_comment = wp_cache_get( $comment_id, 'comment' );
  159. if ( ! $_comment ) {
  160. $_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
  161. if ( ! $_comment ) {
  162. return false;
  163. }
  164. wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
  165. }
  166. return new WP_Comment( $_comment );
  167. }
  168. /**
  169. * Constructor.
  170. *
  171. * Populates properties with object vars.
  172. *
  173. * @since 4.4.0
  174. *
  175. * @param WP_Comment $comment Comment object.
  176. */
  177. public function __construct( $comment ) {
  178. foreach ( get_object_vars( $comment ) as $key => $value ) {
  179. $this->$key = $value;
  180. }
  181. }
  182. /**
  183. * Convert object to array.
  184. *
  185. * @since 4.4.0
  186. *
  187. * @return array Object as array.
  188. */
  189. public function to_array() {
  190. return get_object_vars( $this );
  191. }
  192. /**
  193. * Get the children of a comment.
  194. *
  195. * @since 4.4.0
  196. *
  197. * @param array $args {
  198. * Array of arguments used to pass to get_comments() and determine format.
  199. *
  200. * @type string $format Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
  201. * Default 'tree'.
  202. * @type string $status Comment status to limit results by. Accepts 'hold' (`comment_status=0`),
  203. * 'approve' (`comment_status=1`), 'all', or a custom comment status.
  204. * Default 'all'.
  205. * @type string $hierarchical Whether to include comment descendants in the results.
  206. * 'threaded' returns a tree, with each comment's children
  207. * stored in a `children` property on the `WP_Comment` object.
  208. * 'flat' returns a flat array of found comments plus their children.
  209. * Pass `false` to leave out descendants.
  210. * The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'.
  211. * Accepts 'threaded', 'flat', or false. Default: 'threaded'.
  212. * @type string|array $orderby Comment status or array of statuses. To use 'meta_value'
  213. * or 'meta_value_num', `$meta_key` must also be defined.
  214. * To sort by a specific `$meta_query` clause, use that
  215. * clause's array key. Accepts 'comment_agent',
  216. * 'comment_approved', 'comment_author',
  217. * 'comment_author_email', 'comment_author_IP',
  218. * 'comment_author_url', 'comment_content', 'comment_date',
  219. * 'comment_date_gmt', 'comment_ID', 'comment_karma',
  220. * 'comment_parent', 'comment_post_ID', 'comment_type',
  221. * 'user_id', 'comment__in', 'meta_value', 'meta_value_num',
  222. * the value of $meta_key, and the array keys of
  223. * `$meta_query`. Also accepts false, an empty array, or
  224. * 'none' to disable `ORDER BY` clause.
  225. * }
  226. * @return WP_Comment[] Array of `WP_Comment` objects.
  227. */
  228. public function get_children( $args = array() ) {
  229. $defaults = array(
  230. 'format' => 'tree',
  231. 'status' => 'all',
  232. 'hierarchical' => 'threaded',
  233. 'orderby' => '',
  234. );
  235. $_args = wp_parse_args( $args, $defaults );
  236. $_args['parent'] = $this->comment_ID;
  237. if ( is_null( $this->children ) ) {
  238. if ( $this->populated_children ) {
  239. $this->children = array();
  240. } else {
  241. $this->children = get_comments( $_args );
  242. }
  243. }
  244. if ( 'flat' === $_args['format'] ) {
  245. $children = array();
  246. foreach ( $this->children as $child ) {
  247. $child_args = $_args;
  248. $child_args['format'] = 'flat';
  249. // get_children() resets this value automatically.
  250. unset( $child_args['parent'] );
  251. $children = array_merge( $children, array( $child ), $child->get_children( $child_args ) );
  252. }
  253. } else {
  254. $children = $this->children;
  255. }
  256. return $children;
  257. }
  258. /**
  259. * Add a child to the comment.
  260. *
  261. * Used by `WP_Comment_Query` when bulk-filling descendants.
  262. *
  263. * @since 4.4.0
  264. *
  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. *
  273. * @since 4.4.0
  274. *
  275. * @param int $child_id ID of the child.
  276. * @return WP_Comment|bool Returns the comment object if found, otherwise false.
  277. */
  278. public function get_child( $child_id ) {
  279. if ( isset( $this->children[ $child_id ] ) ) {
  280. return $this->children[ $child_id ];
  281. }
  282. return false;
  283. }
  284. /**
  285. * Set the 'populated_children' flag.
  286. *
  287. * This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger
  288. * unneeded database queries.
  289. *
  290. * @since 4.4.0
  291. *
  292. * @param bool $set Whether the comment's children have already been populated.
  293. */
  294. public function populated_children( $set ) {
  295. $this->populated_children = (bool) $set;
  296. }
  297. /**
  298. * Check whether a non-public property is set.
  299. *
  300. * If `$name` matches a post field, the comment post will be loaded and the post's value checked.
  301. *
  302. * @since 4.4.0
  303. *
  304. * @param string $name Property name.
  305. * @return bool
  306. */
  307. public function __isset( $name ) {
  308. if ( in_array( $name, $this->post_fields, true ) && 0 !== (int) $this->comment_post_ID ) {
  309. $post = get_post( $this->comment_post_ID );
  310. return property_exists( $post, $name );
  311. }
  312. }
  313. /**
  314. * Magic getter.
  315. *
  316. * If `$name` matches a post field, the comment post will be loaded and the post's value returned.
  317. *
  318. * @since 4.4.0
  319. *
  320. * @param string $name
  321. * @return mixed
  322. */
  323. public function __get( $name ) {
  324. if ( in_array( $name, $this->post_fields, true ) ) {
  325. $post = get_post( $this->comment_post_ID );
  326. return $post->$name;
  327. }
  328. }
  329. }