Нет описания

class-wp-comment.php 9.1KB

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