PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/comment.php

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