PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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