PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/comment.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 1570 lines | 650 code | 158 blank | 762 comment | 161 complexity | a6f920e74aad4624b6c805c797234625 MD5 | raw file
  1. <?php
  2. /**
  3. * Core Comment API
  4. *
  5. * @package WordPress
  6. * @subpackage Comment
  7. */
  8. /**
  9. * Check whether a comment passes internal checks to be allowed to add.
  10. *
  11. * If manual comment moderation is set in the administration, then all checks,
  12. * regardless of their type and whitelist, will fail and the function will
  13. * return false.
  14. *
  15. * If the number of links exceeds the amount in the administration, then the
  16. * check fails. If any of the parameter contents match the blacklist of words,
  17. * then the check fails.
  18. *
  19. * If the comment author was approved before, then the comment is automatically
  20. * whitelisted.
  21. *
  22. * If all checks pass, the function will return true.
  23. *
  24. * @since 1.2.0
  25. *
  26. * @global wpdb $wpdb WordPress database abstraction object.
  27. *
  28. * @param string $author Comment author name.
  29. * @param string $email Comment author email.
  30. * @param string $url Comment author URL.
  31. * @param string $comment Content of the comment.
  32. * @param string $user_ip Comment author IP address.
  33. * @param string $user_agent Comment author User-Agent.
  34. * @param string $comment_type Comment type, either user-submitted comment,
  35. * trackback, or pingback.
  36. * @return bool If all checks pass, true, otherwise false.
  37. */
  38. function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  39. global $wpdb;
  40. // If manual moderation is enabled, skip all checks and return false.
  41. if ( 1 == get_option('comment_moderation') )
  42. return false;
  43. /** This filter is documented in wp-includes/comment-template.php */
  44. $comment = apply_filters( 'comment_text', $comment );
  45. // Check for the number of external links if a max allowed number is set.
  46. if ( $max_links = get_option( 'comment_max_links' ) ) {
  47. $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
  48. /**
  49. * Filters the number of links found in a comment.
  50. *
  51. * @since 3.0.0
  52. *
  53. * @param int $num_links The number of links found.
  54. * @param string $url Comment author's URL. Included in allowed links total.
  55. */
  56. $num_links = apply_filters( 'comment_max_links_url', $num_links, $url );
  57. /*
  58. * If the number of links in the comment exceeds the allowed amount,
  59. * fail the check by returning false.
  60. */
  61. if ( $num_links >= $max_links )
  62. return false;
  63. }
  64. $mod_keys = trim(get_option('moderation_keys'));
  65. // If moderation 'keys' (keywords) are set, process them.
  66. if ( !empty($mod_keys) ) {
  67. $words = explode("\n", $mod_keys );
  68. foreach ( (array) $words as $word) {
  69. $word = trim($word);
  70. // Skip empty lines.
  71. if ( empty($word) )
  72. continue;
  73. /*
  74. * Do some escaping magic so that '#' (number of) characters in the spam
  75. * words don't break things:
  76. */
  77. $word = preg_quote($word, '#');
  78. /*
  79. * Check the comment fields for moderation keywords. If any are found,
  80. * fail the check for the given field by returning false.
  81. */
  82. $pattern = "#$word#i";
  83. if ( preg_match($pattern, $author) ) return false;
  84. if ( preg_match($pattern, $email) ) return false;
  85. if ( preg_match($pattern, $url) ) return false;
  86. if ( preg_match($pattern, $comment) ) return false;
  87. if ( preg_match($pattern, $user_ip) ) return false;
  88. if ( preg_match($pattern, $user_agent) ) return false;
  89. }
  90. }
  91. /*
  92. * Check if the option to approve comments by previously-approved authors is enabled.
  93. *
  94. * If it is enabled, check whether the comment author has a previously-approved comment,
  95. * as well as whether there are any moderation keywords (if set) present in the author
  96. * email address. If both checks pass, return true. Otherwise, return false.
  97. */
  98. if ( 1 == get_option('comment_whitelist')) {
  99. if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
  100. // expected_slashed ($author, $email)
  101. $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1");
  102. if ( ( 1 == $ok_to_comment ) &&
  103. ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
  104. return true;
  105. else
  106. return false;
  107. } else {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. /**
  114. * Retrieve the approved comments for post $post_id.
  115. *
  116. * @since 2.0.0
  117. * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
  118. *
  119. * @param int $post_id The ID of the post.
  120. * @param array $args Optional. See WP_Comment_Query::query() for information on accepted arguments.
  121. * @return int|array $comments The approved comments, or number of comments if `$count`
  122. * argument is true.
  123. */
  124. function get_approved_comments( $post_id, $args = array() ) {
  125. if ( ! $post_id ) {
  126. return array();
  127. }
  128. $defaults = array(
  129. 'status' => 1,
  130. 'post_id' => $post_id,
  131. 'order' => 'ASC',
  132. );
  133. $r = wp_parse_args( $args, $defaults );
  134. $query = new WP_Comment_Query;
  135. return $query->query( $r );
  136. }
  137. /**
  138. * Retrieves comment data given a comment ID or comment object.
  139. *
  140. * If an object is passed then the comment data will be cached and then returned
  141. * after being passed through a filter. If the comment is empty, then the global
  142. * comment variable will be used, if it is set.
  143. *
  144. * @since 2.0.0
  145. *
  146. * @global WP_Comment $comment
  147. *
  148. * @param WP_Comment|string|int $comment Comment to retrieve.
  149. * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
  150. * @return WP_Comment|array|null Depends on $output value.
  151. */
  152. function get_comment( &$comment = null, $output = OBJECT ) {
  153. if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
  154. $comment = $GLOBALS['comment'];
  155. }
  156. if ( $comment instanceof WP_Comment ) {
  157. $_comment = $comment;
  158. } elseif ( is_object( $comment ) ) {
  159. $_comment = new WP_Comment( $comment );
  160. } else {
  161. $_comment = WP_Comment::get_instance( $comment );
  162. }
  163. if ( ! $_comment ) {
  164. return null;
  165. }
  166. /**
  167. * Fires after a comment is retrieved.
  168. *
  169. * @since 2.3.0
  170. *
  171. * @param mixed $_comment Comment data.
  172. */
  173. $_comment = apply_filters( 'get_comment', $_comment );
  174. if ( $output == OBJECT ) {
  175. return $_comment;
  176. } elseif ( $output == ARRAY_A ) {
  177. return $_comment->to_array();
  178. } elseif ( $output == ARRAY_N ) {
  179. return array_values( $_comment->to_array() );
  180. }
  181. return $_comment;
  182. }
  183. /**
  184. * Retrieve a list of comments.
  185. *
  186. * The comment list can be for the blog as a whole or for an individual post.
  187. *
  188. * @since 2.7.0
  189. *
  190. * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::parse_query()
  191. * for information on accepted arguments. Default empty.
  192. * @return int|array List of comments or number of found comments if `$count` argument is true.
  193. */
  194. function get_comments( $args = '' ) {
  195. $query = new WP_Comment_Query;
  196. return $query->query( $args );
  197. }
  198. /**
  199. * Retrieve all of the WordPress supported comment statuses.
  200. *
  201. * Comments have a limited set of valid status values, this provides the comment
  202. * status values and descriptions.
  203. *
  204. * @since 2.7.0
  205. *
  206. * @return array List of comment statuses.
  207. */
  208. function get_comment_statuses() {
  209. $status = array(
  210. 'hold' => __( 'Unapproved' ),
  211. 'approve' => _x( 'Approved', 'comment status' ),
  212. 'spam' => _x( 'Spam', 'comment status' ),
  213. 'trash' => _x( 'Trash', 'comment status' ),
  214. );
  215. return $status;
  216. }
  217. /**
  218. * Gets the default comment status for a post type.
  219. *
  220. * @since 4.3.0
  221. *
  222. * @param string $post_type Optional. Post type. Default 'post'.
  223. * @param string $comment_type Optional. Comment type. Default 'comment'.
  224. * @return string Expected return value is 'open' or 'closed'.
  225. */
  226. function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
  227. switch ( $comment_type ) {
  228. case 'pingback' :
  229. case 'trackback' :
  230. $supports = 'trackbacks';
  231. $option = 'ping';
  232. break;
  233. default :
  234. $supports = 'comments';
  235. $option = 'comment';
  236. }
  237. // Set the status.
  238. if ( 'page' === $post_type ) {
  239. $status = 'closed';
  240. } elseif ( post_type_supports( $post_type, $supports ) ) {
  241. $status = get_option( "default_{$option}_status" );
  242. } else {
  243. $status = 'closed';
  244. }
  245. /**
  246. * Filters the default comment status for the given post type.
  247. *
  248. * @since 4.3.0
  249. *
  250. * @param string $status Default status for the given post type,
  251. * either 'open' or 'closed'.
  252. * @param string $post_type Post type. Default is `post`.
  253. * @param string $comment_type Type of comment. Default is `comment`.
  254. */
  255. return apply_filters( 'get_default_comment_status' , $status, $post_type, $comment_type );
  256. }
  257. /**
  258. * The date the last comment was modified.
  259. *
  260. * @since 1.5.0
  261. *
  262. * @global wpdb $wpdb WordPress database abstraction object.
  263. * @staticvar array $cache_lastcommentmodified
  264. *
  265. * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
  266. * or 'server' locations.
  267. * @return string Last comment modified date.
  268. */
  269. function get_lastcommentmodified($timezone = 'server') {
  270. global $wpdb;
  271. static $cache_lastcommentmodified = array();
  272. if ( isset($cache_lastcommentmodified[$timezone]) )
  273. return $cache_lastcommentmodified[$timezone];
  274. $add_seconds_server = date('Z');
  275. switch ( strtolower($timezone)) {
  276. case 'gmt':
  277. $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
  278. break;
  279. case 'blog':
  280. $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
  281. break;
  282. case 'server':
  283. $lastcommentmodified = $wpdb->get_var($wpdb->prepare("SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server));
  284. break;
  285. }
  286. $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
  287. return $lastcommentmodified;
  288. }
  289. /**
  290. * The amount of comments in a post or total comments.
  291. *
  292. * A lot like wp_count_comments(), in that they both return comment stats (albeit with different types).
  293. * The wp_count_comments() actually caches, but this function does not.
  294. *
  295. * @since 2.0.0
  296. *
  297. * @global wpdb $wpdb WordPress database abstraction object.
  298. *
  299. * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
  300. * @return array The amount of spam, approved, awaiting moderation, and total comments.
  301. */
  302. function get_comment_count( $post_id = 0 ) {
  303. global $wpdb;
  304. $post_id = (int) $post_id;
  305. $where = '';
  306. if ( $post_id > 0 ) {
  307. $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
  308. }
  309. $totals = (array) $wpdb->get_results("
  310. SELECT comment_approved, COUNT( * ) AS total
  311. FROM {$wpdb->comments}
  312. {$where}
  313. GROUP BY comment_approved
  314. ", ARRAY_A);
  315. $comment_count = array(
  316. 'approved' => 0,
  317. 'awaiting_moderation' => 0,
  318. 'spam' => 0,
  319. 'trash' => 0,
  320. 'post-trashed' => 0,
  321. 'total_comments' => 0,
  322. 'all' => 0,
  323. );
  324. foreach ( $totals as $row ) {
  325. switch ( $row['comment_approved'] ) {
  326. case 'trash':
  327. $comment_count['trash'] = $row['total'];
  328. break;
  329. case 'post-trashed':
  330. $comment_count['post-trashed'] = $row['total'];
  331. break;
  332. case 'spam':
  333. $comment_count['spam'] = $row['total'];
  334. $comment_count['total_comments'] += $row['total'];
  335. break;
  336. case '1':
  337. $comment_count['approved'] = $row['total'];
  338. $comment_count['total_comments'] += $row['total'];
  339. $comment_count['all'] += $row['total'];
  340. break;
  341. case '0':
  342. $comment_count['awaiting_moderation'] = $row['total'];
  343. $comment_count['total_comments'] += $row['total'];
  344. $comment_count['all'] += $row['total'];
  345. break;
  346. default:
  347. break;
  348. }
  349. }
  350. return $comment_count;
  351. }
  352. //
  353. // Comment meta functions
  354. //
  355. /**
  356. * Add meta data field to a comment.
  357. *
  358. * @since 2.9.0
  359. * @link https://codex.wordpress.org/Function_Reference/add_comment_meta
  360. *
  361. * @param int $comment_id Comment ID.
  362. * @param string $meta_key Metadata name.
  363. * @param mixed $meta_value Metadata value.
  364. * @param bool $unique Optional, default is false. Whether the same key should not be added.
  365. * @return int|bool Meta ID on success, false on failure.
  366. */
  367. function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
  368. return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
  369. }
  370. /**
  371. * Remove metadata matching criteria from a comment.
  372. *
  373. * You can match based on the key, or key and value. Removing based on key and
  374. * value, will keep from removing duplicate metadata with the same key. It also
  375. * allows removing all metadata matching key, if needed.
  376. *
  377. * @since 2.9.0
  378. * @link https://codex.wordpress.org/Function_Reference/delete_comment_meta
  379. *
  380. * @param int $comment_id comment ID
  381. * @param string $meta_key Metadata name.
  382. * @param mixed $meta_value Optional. Metadata value.
  383. * @return bool True on success, false on failure.
  384. */
  385. function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
  386. return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
  387. }
  388. /**
  389. * Retrieve comment meta field for a comment.
  390. *
  391. * @since 2.9.0
  392. * @link https://codex.wordpress.org/Function_Reference/get_comment_meta
  393. *
  394. * @param int $comment_id Comment ID.
  395. * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
  396. * @param bool $single Whether to return a single value.
  397. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
  398. * is true.
  399. */
  400. function get_comment_meta($comment_id, $key = '', $single = false) {
  401. return get_metadata('comment', $comment_id, $key, $single);
  402. }
  403. /**
  404. * Update comment meta field based on comment ID.
  405. *
  406. * Use the $prev_value parameter to differentiate between meta fields with the
  407. * same key and comment ID.
  408. *
  409. * If the meta field for the comment does not exist, it will be added.
  410. *
  411. * @since 2.9.0
  412. * @link https://codex.wordpress.org/Function_Reference/update_comment_meta
  413. *
  414. * @param int $comment_id Comment ID.
  415. * @param string $meta_key Metadata key.
  416. * @param mixed $meta_value Metadata value.
  417. * @param mixed $prev_value Optional. Previous value to check before removing.
  418. * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
  419. */
  420. function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
  421. return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
  422. }
  423. /**
  424. * Queues comments for metadata lazy-loading.
  425. *
  426. * @since 4.5.0
  427. *
  428. * @param array $comments Array of comment objects.
  429. */
  430. function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
  431. // Don't use `wp_list_pluck()` to avoid by-reference manipulation.
  432. $comment_ids = array();
  433. if ( is_array( $comments ) ) {
  434. foreach ( $comments as $comment ) {
  435. if ( $comment instanceof WP_Comment ) {
  436. $comment_ids[] = $comment->comment_ID;
  437. }
  438. }
  439. }
  440. if ( $comment_ids ) {
  441. $lazyloader = wp_metadata_lazyloader();
  442. $lazyloader->queue_objects( 'comment', $comment_ids );
  443. }
  444. }
  445. /**
  446. * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
  447. * to recall previous comments by this commentator that are still held in moderation.
  448. *
  449. * @param WP_Comment $comment Comment object.
  450. * @param object $user Comment author's object.
  451. *
  452. * @since 3.4.0
  453. */
  454. function wp_set_comment_cookies($comment, $user) {
  455. if ( $user->exists() )
  456. return;
  457. /**
  458. * Filters the lifetime of the comment cookie in seconds.
  459. *
  460. * @since 2.8.0
  461. *
  462. * @param int $seconds Comment cookie lifetime. Default 30000000.
  463. */
  464. $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
  465. $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
  466. setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
  467. setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
  468. setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
  469. }
  470. /**
  471. * Sanitizes the cookies sent to the user already.
  472. *
  473. * Will only do anything if the cookies have already been created for the user.
  474. * Mostly used after cookies had been sent to use elsewhere.
  475. *
  476. * @since 2.0.4
  477. */
  478. function sanitize_comment_cookies() {
  479. if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
  480. /**
  481. * Filters the comment author's name cookie before it is set.
  482. *
  483. * When this filter hook is evaluated in wp_filter_comment(),
  484. * the comment author's name string is passed.
  485. *
  486. * @since 1.5.0
  487. *
  488. * @param string $author_cookie The comment author name cookie.
  489. */
  490. $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
  491. $comment_author = wp_unslash($comment_author);
  492. $comment_author = esc_attr($comment_author);
  493. $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
  494. }
  495. if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
  496. /**
  497. * Filters the comment author's email cookie before it is set.
  498. *
  499. * When this filter hook is evaluated in wp_filter_comment(),
  500. * the comment author's email string is passed.
  501. *
  502. * @since 1.5.0
  503. *
  504. * @param string $author_email_cookie The comment author email cookie.
  505. */
  506. $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
  507. $comment_author_email = wp_unslash($comment_author_email);
  508. $comment_author_email = esc_attr($comment_author_email);
  509. $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
  510. }
  511. if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
  512. /**
  513. * Filters the comment author's URL cookie before it is set.
  514. *
  515. * When this filter hook is evaluated in wp_filter_comment(),
  516. * the comment author's URL string is passed.
  517. *
  518. * @since 1.5.0
  519. *
  520. * @param string $author_url_cookie The comment author URL cookie.
  521. */
  522. $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
  523. $comment_author_url = wp_unslash($comment_author_url);
  524. $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
  525. }
  526. }
  527. /**
  528. * Validates whether this comment is allowed to be made.
  529. *
  530. * @since 2.0.0
  531. *
  532. * @global wpdb $wpdb WordPress database abstraction object.
  533. *
  534. * @param array $commentdata Contains information on the comment
  535. * @return int|string Signifies the approval status (0|1|'spam')
  536. */
  537. function wp_allow_comment( $commentdata ) {
  538. global $wpdb;
  539. // Simple duplicate check
  540. // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
  541. $dupe = $wpdb->prepare(
  542. "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
  543. wp_unslash( $commentdata['comment_post_ID'] ),
  544. wp_unslash( $commentdata['comment_parent'] ),
  545. wp_unslash( $commentdata['comment_author'] )
  546. );
  547. if ( $commentdata['comment_author_email'] ) {
  548. $dupe .= $wpdb->prepare(
  549. "AND comment_author_email = %s ",
  550. wp_unslash( $commentdata['comment_author_email'] )
  551. );
  552. }
  553. $dupe .= $wpdb->prepare(
  554. ") AND comment_content = %s LIMIT 1",
  555. wp_unslash( $commentdata['comment_content'] )
  556. );
  557. $dupe_id = $wpdb->get_var( $dupe );
  558. /**
  559. * Filters the ID, if any, of the duplicate comment found when creating a new comment.
  560. *
  561. * Return an empty value from this filter to allow what WP considers a duplicate comment.
  562. *
  563. * @since 4.4.0
  564. *
  565. * @param int $dupe_id ID of the comment identified as a duplicate.
  566. * @param array $commentdata Data for the comment being created.
  567. */
  568. $dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata );
  569. if ( $dupe_id ) {
  570. /**
  571. * Fires immediately after a duplicate comment is detected.
  572. *
  573. * @since 3.0.0
  574. *
  575. * @param array $commentdata Comment data.
  576. */
  577. do_action( 'comment_duplicate_trigger', $commentdata );
  578. if ( defined( 'DOING_AJAX' ) ) {
  579. die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
  580. }
  581. wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
  582. }
  583. /**
  584. * Fires immediately before a comment is marked approved.
  585. *
  586. * Allows checking for comment flooding.
  587. *
  588. * @since 2.3.0
  589. *
  590. * @param string $comment_author_IP Comment author's IP address.
  591. * @param string $comment_author_email Comment author's email.
  592. * @param string $comment_date_gmt GMT date the comment was posted.
  593. */
  594. do_action(
  595. 'check_comment_flood',
  596. $commentdata['comment_author_IP'],
  597. $commentdata['comment_author_email'],
  598. $commentdata['comment_date_gmt']
  599. );
  600. if ( ! empty( $commentdata['user_id'] ) ) {
  601. $user = get_userdata( $commentdata['user_id'] );
  602. $post_author = $wpdb->get_var( $wpdb->prepare(
  603. "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
  604. $commentdata['comment_post_ID']
  605. ) );
  606. }
  607. if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
  608. // The author and the admins get respect.
  609. $approved = 1;
  610. } else {
  611. // Everyone else's comments will be checked.
  612. if ( check_comment(
  613. $commentdata['comment_author'],
  614. $commentdata['comment_author_email'],
  615. $commentdata['comment_author_url'],
  616. $commentdata['comment_content'],
  617. $commentdata['comment_author_IP'],
  618. $commentdata['comment_agent'],
  619. $commentdata['comment_type']
  620. ) ) {
  621. $approved = 1;
  622. } else {
  623. $approved = 0;
  624. }
  625. if ( wp_blacklist_check(
  626. $commentdata['comment_author'],
  627. $commentdata['comment_author_email'],
  628. $commentdata['comment_author_url'],
  629. $commentdata['comment_content'],
  630. $commentdata['comment_author_IP'],
  631. $commentdata['comment_agent']
  632. ) ) {
  633. $approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
  634. }
  635. }
  636. /**
  637. * Filters a comment's approval status before it is set.
  638. *
  639. * @since 2.1.0
  640. *
  641. * @param bool|string $approved The approval status. Accepts 1, 0, or 'spam'.
  642. * @param array $commentdata Comment data.
  643. */
  644. $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
  645. return $approved;
  646. }
  647. /**
  648. * Check whether comment flooding is occurring.
  649. *
  650. * Won't run, if current user can manage options, so to not block
  651. * administrators.
  652. *
  653. * @since 2.3.0
  654. *
  655. * @global wpdb $wpdb WordPress database abstraction object.
  656. *
  657. * @param string $ip Comment IP.
  658. * @param string $email Comment author email address.
  659. * @param string $date MySQL time string.
  660. */
  661. function check_comment_flood_db( $ip, $email, $date ) {
  662. global $wpdb;
  663. // don't throttle admins or moderators
  664. if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
  665. return;
  666. }
  667. $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
  668. if ( is_user_logged_in() ) {
  669. $user = get_current_user_id();
  670. $check_column = '`user_id`';
  671. } else {
  672. $user = $ip;
  673. $check_column = '`comment_author_IP`';
  674. }
  675. $sql = $wpdb->prepare(
  676. "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1",
  677. $hour_ago,
  678. $user,
  679. $email
  680. );
  681. $lasttime = $wpdb->get_var( $sql );
  682. if ( $lasttime ) {
  683. $time_lastcomment = mysql2date('U', $lasttime, false);
  684. $time_newcomment = mysql2date('U', $date, false);
  685. /**
  686. * Filters the comment flood status.
  687. *
  688. * @since 2.1.0
  689. *
  690. * @param bool $bool Whether a comment flood is occurring. Default false.
  691. * @param int $time_lastcomment Timestamp of when the last comment was posted.
  692. * @param int $time_newcomment Timestamp of when the new comment was posted.
  693. */
  694. $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
  695. if ( $flood_die ) {
  696. /**
  697. * Fires before the comment flood message is triggered.
  698. *
  699. * @since 1.5.0
  700. *
  701. * @param int $time_lastcomment Timestamp of when the last comment was posted.
  702. * @param int $time_newcomment Timestamp of when the new comment was posted.
  703. */
  704. do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
  705. if ( defined('DOING_AJAX') )
  706. die( __('You are posting comments too quickly. Slow down.') );
  707. wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
  708. }
  709. }
  710. }
  711. /**
  712. * Separates an array of comments into an array keyed by comment_type.
  713. *
  714. * @since 2.7.0
  715. *
  716. * @param array $comments Array of comments
  717. * @return array Array of comments keyed by comment_type.
  718. */
  719. function separate_comments(&$comments) {
  720. $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
  721. $count = count($comments);
  722. for ( $i = 0; $i < $count; $i++ ) {
  723. $type = $comments[$i]->comment_type;
  724. if ( empty($type) )
  725. $type = 'comment';
  726. $comments_by_type[$type][] = &$comments[$i];
  727. if ( 'trackback' == $type || 'pingback' == $type )
  728. $comments_by_type['pings'][] = &$comments[$i];
  729. }
  730. return $comments_by_type;
  731. }
  732. /**
  733. * Calculate the total number of comment pages.
  734. *
  735. * @since 2.7.0
  736. *
  737. * @uses Walker_Comment
  738. *
  739. * @global WP_Query $wp_query
  740. *
  741. * @param array $comments Optional array of WP_Comment objects. Defaults to $wp_query->comments
  742. * @param int $per_page Optional comments per page.
  743. * @param bool $threaded Optional control over flat or threaded comments.
  744. * @return int Number of comment pages.
  745. */
  746. function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
  747. global $wp_query;
  748. if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
  749. return $wp_query->max_num_comment_pages;
  750. if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) )
  751. $comments = $wp_query->comments;
  752. if ( empty($comments) )
  753. return 0;
  754. if ( ! get_option( 'page_comments' ) ) {
  755. return 1;
  756. }
  757. if ( !isset($per_page) )
  758. $per_page = (int) get_query_var('comments_per_page');
  759. if ( 0 === $per_page )
  760. $per_page = (int) get_option('comments_per_page');
  761. if ( 0 === $per_page )
  762. return 1;
  763. if ( !isset($threaded) )
  764. $threaded = get_option('thread_comments');
  765. if ( $threaded ) {
  766. $walker = new Walker_Comment;
  767. $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
  768. } else {
  769. $count = ceil( count( $comments ) / $per_page );
  770. }
  771. return $count;
  772. }
  773. /**
  774. * Calculate what page number a comment will appear on for comment paging.
  775. *
  776. * @since 2.7.0
  777. *
  778. * @global wpdb $wpdb WordPress database abstraction object.
  779. *
  780. * @param int $comment_ID Comment ID.
  781. * @param array $args {
  782. * Array of optional arguments.
  783. * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment',
  784. * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
  785. * Default is 'all'.
  786. * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the
  787. * 'comments_per_page' option.
  788. * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
  789. * `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
  790. * } *
  791. * @return int|null Comment page number or null on error.
  792. */
  793. function get_page_of_comment( $comment_ID, $args = array() ) {
  794. global $wpdb;
  795. $page = null;
  796. if ( !$comment = get_comment( $comment_ID ) )
  797. return;
  798. $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
  799. $args = wp_parse_args( $args, $defaults );
  800. $original_args = $args;
  801. // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.
  802. if ( get_option( 'page_comments' ) ) {
  803. if ( '' === $args['per_page'] ) {
  804. $args['per_page'] = get_query_var( 'comments_per_page' );
  805. }
  806. if ( '' === $args['per_page'] ) {
  807. $args['per_page'] = get_option( 'comments_per_page' );
  808. }
  809. }
  810. if ( empty($args['per_page']) ) {
  811. $args['per_page'] = 0;
  812. $args['page'] = 0;
  813. }
  814. if ( $args['per_page'] < 1 ) {
  815. $page = 1;
  816. }
  817. if ( null === $page ) {
  818. if ( '' === $args['max_depth'] ) {
  819. if ( get_option('thread_comments') )
  820. $args['max_depth'] = get_option('thread_comments_depth');
  821. else
  822. $args['max_depth'] = -1;
  823. }
  824. // Find this comment's top level parent if threading is enabled
  825. if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
  826. return get_page_of_comment( $comment->comment_parent, $args );
  827. $comment_args = array(
  828. 'type' => $args['type'],
  829. 'post_id' => $comment->comment_post_ID,
  830. 'fields' => 'ids',
  831. 'count' => true,
  832. 'status' => 'approve',
  833. 'parent' => 0,
  834. 'date_query' => array(
  835. array(
  836. 'column' => "$wpdb->comments.comment_date_gmt",
  837. 'before' => $comment->comment_date_gmt,
  838. )
  839. ),
  840. );
  841. $comment_query = new WP_Comment_Query();
  842. $older_comment_count = $comment_query->query( $comment_args );
  843. // No older comments? Then it's page #1.
  844. if ( 0 == $older_comment_count ) {
  845. $page = 1;
  846. // Divide comments older than this one by comments per page to get this comment's page number
  847. } else {
  848. $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
  849. }
  850. }
  851. /**
  852. * Filters the calculated page on which a comment appears.
  853. *
  854. * @since 4.4.0
  855. *
  856. * @param int $page Comment page.
  857. * @param array $args {
  858. * Arguments used to calculate pagination. These include arguments auto-detected by the function,
  859. * based on query vars, system settings, etc. For pristine arguments passed to the function,
  860. * see `$original_args`.
  861. *
  862. * @type string $type Type of comments to count.
  863. * @type int $page Calculated current page.
  864. * @type int $per_page Calculated number of comments per page.
  865. * @type int $max_depth Maximum comment threading depth allowed.
  866. * }
  867. * @param array $original_args {
  868. * Array of arguments passed to the function. Some or all of these may not be set.
  869. *
  870. * @type string $type Type of comments to count.
  871. * @type int $page Current comment page.
  872. * @type int $per_page Number of comments per page.
  873. * @type int $max_depth Maximum comment threading depth allowed.
  874. * }
  875. */
  876. return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args );
  877. }
  878. /**
  879. * Retrieves the maximum character lengths for the comment form fields.
  880. *
  881. * @since 4.5.0
  882. *
  883. * @global wpdb $wpdb WordPress database abstraction object.
  884. *
  885. * @return array Maximum character length for the comment form fields.
  886. */
  887. function wp_get_comment_fields_max_lengths() {
  888. global $wpdb;
  889. $lengths = array(
  890. 'comment_author' => 245,
  891. 'comment_author_email' => 100,
  892. 'comment_author_url' => 200,
  893. 'comment_content' => 65525,
  894. );
  895. if ( $wpdb->is_mysql ) {
  896. foreach ( $lengths as $column => $length ) {
  897. $col_length = $wpdb->get_col_length( $wpdb->comments, $column );
  898. $max_length = 0;
  899. // No point if we can't get the DB column lengths
  900. if ( is_wp_error( $col_length ) ) {
  901. break;
  902. }
  903. if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
  904. $max_length = (int) $col_length;
  905. } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && intval( $col_length['length'] ) > 0 ) {
  906. $max_length = (int) $col_length['length'];
  907. if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
  908. $max_length = $max_length - 10;
  909. }
  910. }
  911. if ( $max_length > 0 ) {
  912. $lengths[ $column ] = $max_length;
  913. }
  914. }
  915. }
  916. /**
  917. * Filters the lengths for the comment form fields.
  918. *
  919. * @since 4.5.0
  920. *
  921. * @param array $lengths Associative array `'field_name' => 'maximum length'`.
  922. */
  923. return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths );
  924. }
  925. /**
  926. * Does comment contain blacklisted characters or words.
  927. *
  928. * @since 1.5.0
  929. *
  930. * @param string $author The author of the comment
  931. * @param string $email The email of the comment
  932. * @param string $url The url used in the comment
  933. * @param string $comment The comment content
  934. * @param string $user_ip The comment author IP address
  935. * @param string $user_agent The author's browser user agent
  936. * @return bool True if comment contains blacklisted content, false if comment does not
  937. */
  938. function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
  939. /**
  940. * Fires before the comment is tested for blacklisted characters or words.
  941. *
  942. * @since 1.5.0
  943. *
  944. * @param string $author Comment author.
  945. * @param string $email Comment author's email.
  946. * @param string $url Comment author's URL.
  947. * @param string $comment Comment content.
  948. * @param string $user_ip Comment author's IP address.
  949. * @param string $user_agent Comment author's browser user agent.
  950. */
  951. do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
  952. $mod_keys = trim( get_option('blacklist_keys') );
  953. if ( '' == $mod_keys )
  954. return false; // If moderation keys are empty
  955. // Ensure HTML tags are not being used to bypass the blacklist.
  956. $comment_without_html = wp_strip_all_tags( $comment );
  957. $words = explode("\n", $mod_keys );
  958. foreach ( (array) $words as $word ) {
  959. $word = trim($word);
  960. // Skip empty lines
  961. if ( empty($word) ) { continue; }
  962. // Do some escaping magic so that '#' chars in the
  963. // spam words don't break things:
  964. $word = preg_quote($word, '#');
  965. $pattern = "#$word#i";
  966. if (
  967. preg_match($pattern, $author)
  968. || preg_match($pattern, $email)
  969. || preg_match($pattern, $url)
  970. || preg_match($pattern, $comment)
  971. || preg_match($pattern, $comment_without_html)
  972. || preg_match($pattern, $user_ip)
  973. || preg_match($pattern, $user_agent)
  974. )
  975. return true;
  976. }
  977. return false;
  978. }
  979. /**
  980. * Retrieve total comments for blog or single post.
  981. *
  982. * The properties of the returned object contain the 'moderated', 'approved',
  983. * and spam comments for either the entire blog or single post. Those properties
  984. * contain the amount of comments that match the status. The 'total_comments'
  985. * property contains the integer of total comments.
  986. *
  987. * The comment stats are cached and then retrieved, if they already exist in the
  988. * cache.
  989. *
  990. * @since 2.5.0
  991. *
  992. * @param int $post_id Optional. Post ID.
  993. * @return object|array Comment stats.
  994. */
  995. function wp_count_comments( $post_id = 0 ) {
  996. $post_id = (int) $post_id;
  997. /**
  998. * Filters the comments count for a given post.
  999. *
  1000. * @since 2.7.0
  1001. *
  1002. * @param array $count An empty array.
  1003. * @param int $post_id The post ID.
  1004. */
  1005. $filtered = apply_filters( 'wp_count_comments', array(), $post_id );
  1006. if ( ! empty( $filtered ) ) {
  1007. return $filtered;
  1008. }
  1009. $count = wp_cache_get( "comments-{$post_id}", 'counts' );
  1010. if ( false !== $count ) {
  1011. return $count;
  1012. }
  1013. $stats = get_comment_count( $post_id );
  1014. $stats['moderated'] = $stats['awaiting_moderation'];
  1015. unset( $stats['awaiting_moderation'] );
  1016. $stats_object = (object) $stats;
  1017. wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
  1018. return $stats_object;
  1019. }
  1020. /**
  1021. * Trashes or deletes a comment.
  1022. *
  1023. * The comment is moved to trash instead of permanently deleted unless trash is
  1024. * disabled, item is already in the trash, or $force_delete is true.
  1025. *
  1026. * The post comment count will be updated if the comment was approved and has a
  1027. * post ID available.
  1028. *
  1029. * @since 2.0.0
  1030. *
  1031. * @global wpdb $wpdb WordPress database abstraction object.
  1032. *
  1033. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1034. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  1035. * @return bool True on success, false on failure.
  1036. */
  1037. function wp_delete_comment($comment_id, $force_delete = false) {
  1038. global $wpdb;
  1039. if (!$comment = get_comment($comment_id))
  1040. return false;
  1041. if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) )
  1042. return wp_trash_comment($comment_id);
  1043. /**
  1044. * Fires immediately before a comment is deleted from the database.
  1045. *
  1046. * @since 1.2.0
  1047. *
  1048. * @param int $comment_id The comment ID.
  1049. */
  1050. do_action( 'delete_comment', $comment->comment_ID );
  1051. // Move children up a level.
  1052. $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID) );
  1053. if ( !empty($children) ) {
  1054. $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID));
  1055. clean_comment_cache($children);
  1056. }
  1057. // Delete metadata
  1058. $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
  1059. foreach ( $meta_ids as $mid )
  1060. delete_metadata_by_mid( 'comment', $mid );
  1061. if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) )
  1062. return false;
  1063. /**
  1064. * Fires immediately after a comment is deleted from the database.
  1065. *
  1066. * @since 2.9.0
  1067. *
  1068. * @param int $comment_id The comment ID.
  1069. */
  1070. do_action( 'deleted_comment', $comment->comment_ID );
  1071. $post_id = $comment->comment_post_ID;
  1072. if ( $post_id && $comment->comment_approved == 1 )
  1073. wp_update_comment_count($post_id);
  1074. clean_comment_cache( $comment->comment_ID );
  1075. /** This action is documented in wp-includes/comment.php */
  1076. do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' );
  1077. wp_transition_comment_status('delete', $comment->comment_approved, $comment);
  1078. return true;
  1079. }
  1080. /**
  1081. * Moves a comment to the Trash
  1082. *
  1083. * If trash is disabled, comment is permanently deleted.
  1084. *
  1085. * @since 2.9.0
  1086. *
  1087. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1088. * @return bool True on success, false on failure.
  1089. */
  1090. function wp_trash_comment($comment_id) {
  1091. if ( !EMPTY_TRASH_DAYS )
  1092. return wp_delete_comment($comment_id, true);
  1093. if ( !$comment = get_comment($comment_id) )
  1094. return false;
  1095. /**
  1096. * Fires immediately before a comment is sent to the Trash.
  1097. *
  1098. * @since 2.9.0
  1099. *
  1100. * @param int $comment_id The comment ID.
  1101. */
  1102. do_action( 'trash_comment', $comment->comment_ID );
  1103. if ( wp_set_comment_status( $comment, 'trash' ) ) {
  1104. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1105. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1106. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
  1107. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
  1108. /**
  1109. * Fires immediately after a comment is sent to Trash.
  1110. *
  1111. * @since 2.9.0
  1112. *
  1113. * @param int $comment_id The comment ID.
  1114. */
  1115. do_action( 'trashed_comment', $comment->comment_ID );
  1116. return true;
  1117. }
  1118. return false;
  1119. }
  1120. /**
  1121. * Removes a comment from the Trash
  1122. *
  1123. * @since 2.9.0
  1124. *
  1125. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1126. * @return bool True on success, false on failure.
  1127. */
  1128. function wp_untrash_comment($comment_id) {
  1129. $comment = get_comment( $comment_id );
  1130. if ( ! $comment ) {
  1131. return false;
  1132. }
  1133. /**
  1134. * Fires immediately before a comment is restored from the Trash.
  1135. *
  1136. * @since 2.9.0
  1137. *
  1138. * @param int $comment_id The comment ID.
  1139. */
  1140. do_action( 'untrash_comment', $comment->comment_ID );
  1141. $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1142. if ( empty($status) )
  1143. $status = '0';
  1144. if ( wp_set_comment_status( $comment, $status ) ) {
  1145. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1146. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1147. /**
  1148. * Fires immediately after a comment is restored from the Trash.
  1149. *
  1150. * @since 2.9.0
  1151. *
  1152. * @param int $comment_id The comment ID.
  1153. */
  1154. do_action( 'untrashed_comment', $comment->comment_ID );
  1155. return true;
  1156. }
  1157. return false;
  1158. }
  1159. /**
  1160. * Marks a comment as Spam
  1161. *
  1162. * @since 2.9.0
  1163. *
  1164. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1165. * @return bool True on success, false on failure.
  1166. */
  1167. function wp_spam_comment( $comment_id ) {
  1168. $comment = get_comment( $comment_id );
  1169. if ( ! $comment ) {
  1170. return false;
  1171. }
  1172. /**
  1173. * Fires immediately before a comment is marked as Spam.
  1174. *
  1175. * @since 2.9.0
  1176. *
  1177. * @param int $comment_id The comment ID.
  1178. */
  1179. do_action( 'spam_comment', $comment->comment_ID );
  1180. if ( wp_set_comment_status( $comment, 'spam' ) ) {
  1181. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1182. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1183. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
  1184. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
  1185. /**
  1186. * Fires immediately after a comment is marked as Spam.
  1187. *
  1188. * @since 2.9.0
  1189. *
  1190. * @param int $comment_id The comment ID.
  1191. */
  1192. do_action( 'spammed_comment', $comment->comment_ID );
  1193. return true;
  1194. }
  1195. return false;
  1196. }
  1197. /**
  1198. * Removes a comment from the Spam
  1199. *
  1200. * @since 2.9.0
  1201. *
  1202. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1203. * @return bool True on success, false on failure.
  1204. */
  1205. function wp_unspam_comment( $comment_id ) {
  1206. $comment = get_comment( $comment_id );
  1207. if ( ! $comment ) {
  1208. return false;
  1209. }
  1210. /**
  1211. * Fires immediately before a comment is unmarked as Spam.
  1212. *
  1213. * @since 2.9.0
  1214. *
  1215. * @param int $comment_id The comment ID.
  1216. */
  1217. do_action( 'unspam_comment', $comment->comment_ID );
  1218. $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1219. if ( empty($status) )
  1220. $status = '0';
  1221. if ( wp_set_comment_status( $comment, $status ) ) {
  1222. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1223. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1224. /**
  1225. * Fires immediately after a comment is unmarked as Spam.
  1226. *
  1227. * @since 2.9.0
  1228. *
  1229. * @param int $comment_id The comment ID.
  1230. */
  1231. do_action( 'unspammed_comment', $comment->comment_ID );
  1232. return true;
  1233. }
  1234. return false;
  1235. }
  1236. /**
  1237. * The status of a comment by ID.
  1238. *
  1239. * @since 1.0.0
  1240. *
  1241. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object
  1242. * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
  1243. */
  1244. function wp_get_comment_status($comment_id) {
  1245. $comment = get_comment($comment_id);
  1246. if ( !$comment )
  1247. return false;
  1248. $approved = $comment->comment_approved;
  1249. if ( $approved == null )
  1250. return false;
  1251. elseif ( $approved == '1' )
  1252. return 'approved';
  1253. elseif ( $approved == '0' )
  1254. return 'unapproved';
  1255. elseif ( $approved == 'spam' )
  1256. return 'spam';
  1257. elseif ( $approved == 'trash' )
  1258. return 'trash';
  1259. else
  1260. return false;
  1261. }
  1262. /**
  1263. * Call hooks for when a comment status transition occurs.
  1264. *
  1265. * Calls hooks for comment status transitions. If the new comment status is not the same
  1266. * as the previous comment status, then two hooks will be ran, the first is
  1267. * {@see 'transition_comment_status'} with new status, old status, and comment data. The
  1268. * next action called is {@see comment_$old_status_to_$new_status'}. It has the
  1269. * comment data.
  1270. *
  1271. * The final action will run whether or not the comment statuses are the same. The
  1272. * action is named {@see 'comment_$new_status_$comment->comment_type'}.
  1273. *
  1274. * @since 2.7.0
  1275. *
  1276. * @param string $new_status New comment status.
  1277. * @param string $old_status Previous comment status.
  1278. * @param object $comment Comment data.
  1279. */
  1280. function wp_transition_comment_status($new_status, $old_status, $comment) {
  1281. /*
  1282. * Translate raw statuses to human readable formats for the hooks.
  1283. * This is not a complete list of comment status, it's only the ones
  1284. * that need to be renamed
  1285. */
  1286. $comment_statuses = array(
  1287. 0 => 'unapproved',
  1288. 'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
  1289. 1 => 'approved',
  1290. 'approve' => 'approved', // wp_set_comment_status() uses "approve"
  1291. );
  1292. if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
  1293. if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
  1294. // Call the hooks
  1295. if ( $new_status != $old_status ) {
  1296. /**
  1297. * Fires when the comment status is in transition.
  1298. *
  1299. * @since 2.7.0
  1300. *
  1301. * @param int|string $new_status The new comment status.
  1302. * @param int|string $old_status The old comment status.
  1303. * @param object $comment The comment data.
  1304. */
  1305. do_action( 'transition_comment_status', $new_status, $old_status, $comment );
  1306. /**
  1307. * Fires when the comment status is in transition from one specific status to another.
  1308. *
  1309. * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
  1310. * refer to the old and new comment statuses, respectively.
  1311. *
  1312. * @since 2.7.0
  1313. *
  1314. * @param WP_Comment $comment Comment object.
  1315. */
  1316. do_action( "comment_{$old_status}_to_{$new_status}", $comment );
  1317. }
  1318. /**
  1319. * Fires when the status of a specific comment type is in transition.
  1320. *
  1321. * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
  1322. * refer to the new comment status, and the type of comment, respectively.
  1323. *
  1324. * Typical comment types include an empty string (standard comment), 'pingback',
  1325. * or 'trackback'.
  1326. *
  1327. * @since 2.7.0
  1328. *
  1329. * @param int $comment_ID The comment ID.
  1330. * @param WP_Comment $comment Comment object.
  1331. */
  1332. do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
  1333. }
  1334. /**
  1335. * Get current commenter's name, email, and URL.
  1336. *
  1337. * Expects cookies content to already be sanitized. User of this function might
  1338. * wish to recheck the returned array for validity.
  1339. *
  1340. * @see sanitize_comment_cookies() Use to sanitize cookies
  1341. *
  1342. * @since 2.0.4
  1343. *
  1344. * @return array Comment author, email, url respectively.
  1345. */
  1346. function wp_get_current_commenter() {
  1347. // Cookies should already be sanitized.
  1348. $comment_author = '';
  1349. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
  1350. $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
  1351. $comment_author_email = '';
  1352. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
  1353. $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
  1354. $comment_author_url = '';
  1355. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
  1356. $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
  1357. /**
  1358. * Filters the current commenter's name, email, and URL.
  1359. *
  1360. * @since 3.1.0
  1361. *
  1362. * @param array $comment_author_data {
  1363. * An array of current commenter variables.
  1364. *
  1365. * @type string $comment_author The name of the author of the comment. Default empty.
  1366. * @type string $comment_author_email The email address of the `$comment_author`. Default empty.
  1367. * @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
  1368. * }
  1369. */
  1370. return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
  1371. }
  1372. /**
  1373. * Inserts a comment into the database.
  1374. *
  1375. * @since 2.0.0
  1376. * @since 4.4.0 Introduced `$comment_meta` argument.
  1377. *
  1378. * @global wpdb $wpdb WordPress database abstraction object.
  1379. *
  1380. * @param array $commentdata {
  1381. * Array of arguments for inserting a new comment.
  1382. *
  1383. * @type string $comment_agent The HTTP user agent of the `$comment_author` when
  1384. * the comment was submitted. Default empty.
  1385. * @type int|string $comment_approved Whether the comment has been approved. Default 1.
  1386. * @type string $comment_author The name of the author of the comment. Default empty.
  1387. * @type string $comment_author_email The email address of the `$comment_author`. Default empty.
  1388. * @type string $comment_author_IP The IP address of the `$comment_author`. Default empty.
  1389. * @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
  1390. * @type string $comment_content The content of the comment. Default empty.
  1391. * @type string $comment_date The date the comment was submitted. To set the date
  1392. * manually, `$comment_date_gmt` must also be specified.
  1393. * Default is the current time.
  1394. * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
  1395. * Default is `$comment_date` in the site's GMT timezone.
  1396. * @type int $comment_karma The karma of the comment. Default 0.
  1397. * @type int $comment_parent ID of this comment's parent, if any. Default 0.
  1398. * @type int $comment_post_ID ID of the post that relates to the comment, if any.
  1399. * Default 0.
  1400. * @type string $comment_type Comment type. Default empty.
  1401. * @type array $comment_meta Optional. Array of key/value pairs to be stored in commentmeta for the
  1402. * new comment.
  1403. * @type int $user_id ID of the user who submitted the comment. Default 0.
  1404. * }
  1405. * @return int|false The new comment's ID on success, false on failure.
  1406. */
  1407. function wp_insert_comment( $commentdata ) {
  1408. global $wpdb;
  1409. $data = wp_unslash( $commentdata );
  1410. $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];
  1411. $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
  1412. $comment_author_url = ! isset( $data['comment_