PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/comment.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1484 lines | 750 code | 191 blank | 543 comment | 210 complexity | 8a511761c0ee26d14ea78e43e537f7f6 MD5 | raw file
  1. <?php
  2. /**
  3. * Manages WordPress comments
  4. *
  5. * @package WordPress
  6. * @subpackage Comment
  7. */
  8. /**
  9. * Checks whether a comment passes internal checks to be allowed to add.
  10. *
  11. * If comment moderation is set in the administration, then all comments,
  12. * regardless of their type and whitelist will be set to false. If the number of
  13. * links exceeds the amount in the administration, then the check fails. If any
  14. * of the parameter contents match the blacklist of words, then the check fails.
  15. *
  16. * If the number of links exceeds the amount in the administration, then the
  17. * check fails. If any of the parameter contents match the blacklist of words,
  18. * then the check fails.
  19. *
  20. * If the comment is a trackback and part of the blogroll, then the trackback is
  21. * automatically whitelisted. If the comment author was approved before, then
  22. * the comment is automatically whitelisted.
  23. *
  24. * If none of the checks fail, then the failback is to set the check to pass
  25. * (return true).
  26. *
  27. * @since 1.2.0
  28. * @uses $wpdb
  29. *
  30. * @param string $author Comment Author's name
  31. * @param string $email Comment Author's email
  32. * @param string $url Comment Author's URL
  33. * @param string $comment Comment contents
  34. * @param string $user_ip Comment Author's IP address
  35. * @param string $user_agent Comment Author's User Agent
  36. * @param string $comment_type Comment type, either user submitted comment,
  37. * trackback, or pingback
  38. * @return bool Whether the checks passed (true) and the comments should be
  39. * displayed or set to moderated
  40. */
  41. function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  42. global $wpdb;
  43. if ( 1 == get_option('comment_moderation') )
  44. return false; // If moderation is set to manual
  45. // Check # of external links
  46. if ( $max_links = get_option( 'comment_max_links' ) ) {
  47. $num_links = preg_match_all( '/<a [^>]*href/i', apply_filters( 'comment_text', $comment ), $out );
  48. $num_links = apply_filters( 'comment_max_links_url', $num_links, $url ); // provide for counting of $url as a link
  49. if ( $num_links >= $max_links )
  50. return false;
  51. }
  52. $mod_keys = trim(get_option('moderation_keys'));
  53. if ( !empty($mod_keys) ) {
  54. $words = explode("\n", $mod_keys );
  55. foreach ( (array) $words as $word) {
  56. $word = trim($word);
  57. // Skip empty lines
  58. if ( empty($word) )
  59. continue;
  60. // Do some escaping magic so that '#' chars in the
  61. // spam words don't break things:
  62. $word = preg_quote($word, '#');
  63. $pattern = "#$word#i";
  64. if ( preg_match($pattern, $author) ) return false;
  65. if ( preg_match($pattern, $email) ) return false;
  66. if ( preg_match($pattern, $url) ) return false;
  67. if ( preg_match($pattern, $comment) ) return false;
  68. if ( preg_match($pattern, $user_ip) ) return false;
  69. if ( preg_match($pattern, $user_agent) ) return false;
  70. }
  71. }
  72. // Comment whitelisting:
  73. if ( 1 == get_option('comment_whitelist')) {
  74. if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
  75. $uri = parse_url($url);
  76. $domain = $uri['host'];
  77. $uri = parse_url( home_url() );
  78. $home_domain = $uri['host'];
  79. if ( $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE (%s) LIMIT 1", '%'.$domain.'%')) || $domain == $home_domain )
  80. return true;
  81. else
  82. return false;
  83. } elseif ( $author != '' && $email != '' ) {
  84. // expected_slashed ($author, $email)
  85. $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");
  86. if ( ( 1 == $ok_to_comment ) &&
  87. ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
  88. return true;
  89. else
  90. return false;
  91. } else {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. /**
  98. * Retrieve the approved comments for post $post_id.
  99. *
  100. * @since 2.0.0
  101. * @uses $wpdb
  102. *
  103. * @param int $post_id The ID of the post
  104. * @return array $comments The approved comments
  105. */
  106. function get_approved_comments($post_id) {
  107. global $wpdb;
  108. return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
  109. }
  110. /**
  111. * Retrieves comment data given a comment ID or comment object.
  112. *
  113. * If an object is passed then the comment data will be cached and then returned
  114. * after being passed through a filter. If the comment is empty, then the global
  115. * comment variable will be used, if it is set.
  116. *
  117. * If the comment is empty, then the global comment variable will be used, if it
  118. * is set.
  119. *
  120. * @since 2.0.0
  121. * @uses $wpdb
  122. *
  123. * @param object|string|int $comment Comment to retrieve.
  124. * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
  125. * @return object|array|null Depends on $output value.
  126. */
  127. function &get_comment(&$comment, $output = OBJECT) {
  128. global $wpdb;
  129. $null = null;
  130. if ( empty($comment) ) {
  131. if ( isset($GLOBALS['comment']) )
  132. $_comment = & $GLOBALS['comment'];
  133. else
  134. $_comment = null;
  135. } elseif ( is_object($comment) ) {
  136. wp_cache_add($comment->comment_ID, $comment, 'comment');
  137. $_comment = $comment;
  138. } else {
  139. if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
  140. $_comment = & $GLOBALS['comment'];
  141. } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
  142. $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
  143. if ( ! $_comment )
  144. return $null;
  145. wp_cache_add($_comment->comment_ID, $_comment, 'comment');
  146. }
  147. }
  148. $_comment = apply_filters('get_comment', $_comment);
  149. if ( $output == OBJECT ) {
  150. return $_comment;
  151. } elseif ( $output == ARRAY_A ) {
  152. $__comment = get_object_vars($_comment);
  153. return $__comment;
  154. } elseif ( $output == ARRAY_N ) {
  155. $__comment = array_values(get_object_vars($_comment));
  156. return $__comment;
  157. } else {
  158. return $_comment;
  159. }
  160. }
  161. /**
  162. * Retrieve a list of comments.
  163. *
  164. * The comment list can be for the blog as a whole or for an individual post.
  165. *
  166. * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt',
  167. * 'order', 'number', 'offset', and 'post_id'.
  168. *
  169. * @since 2.7.0
  170. * @uses $wpdb
  171. *
  172. * @param mixed $args Optional. Array or string of options to override defaults.
  173. * @return array List of comments.
  174. */
  175. function get_comments( $args = '' ) {
  176. global $wpdb;
  177. $defaults = array(
  178. 'author_email' => '',
  179. 'ID' => '',
  180. 'karma' => '',
  181. 'number' => '',
  182. 'offset' => '',
  183. 'orderby' => '',
  184. 'order' => 'DESC',
  185. 'parent' => '',
  186. 'post_ID' => '',
  187. 'post_id' => 0,
  188. 'status' => '',
  189. 'type' => '',
  190. 'user_id' => '',
  191. );
  192. $args = wp_parse_args( $args, $defaults );
  193. extract( $args, EXTR_SKIP );
  194. // $args can be whatever, only use the args defined in defaults to compute the key
  195. $key = md5( serialize( compact(array_keys($defaults)) ) );
  196. $last_changed = wp_cache_get('last_changed', 'comment');
  197. if ( !$last_changed ) {
  198. $last_changed = time();
  199. wp_cache_set('last_changed', $last_changed, 'comment');
  200. }
  201. $cache_key = "get_comments:$key:$last_changed";
  202. if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
  203. return $cache;
  204. }
  205. $post_id = absint($post_id);
  206. if ( 'hold' == $status )
  207. $approved = "comment_approved = '0'";
  208. elseif ( 'approve' == $status )
  209. $approved = "comment_approved = '1'";
  210. elseif ( 'spam' == $status )
  211. $approved = "comment_approved = 'spam'";
  212. elseif ( 'trash' == $status )
  213. $approved = "comment_approved = 'trash'";
  214. else
  215. $approved = "( comment_approved = '0' OR comment_approved = '1' )";
  216. $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
  217. if ( ! empty( $orderby ) ) {
  218. $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
  219. $ordersby = array_intersect(
  220. $ordersby,
  221. array(
  222. 'comment_agent',
  223. 'comment_approved',
  224. 'comment_author',
  225. 'comment_author_email',
  226. 'comment_author_IP',
  227. 'comment_author_url',
  228. 'comment_content',
  229. 'comment_date',
  230. 'comment_date_gmt',
  231. 'comment_ID',
  232. 'comment_karma',
  233. 'comment_parent',
  234. 'comment_post_ID',
  235. 'comment_type',
  236. 'user_id',
  237. )
  238. );
  239. $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
  240. } else {
  241. $orderby = 'comment_date_gmt';
  242. }
  243. $number = absint($number);
  244. $offset = absint($offset);
  245. if ( !empty($number) ) {
  246. if ( $offset )
  247. $number = 'LIMIT ' . $offset . ',' . $number;
  248. else
  249. $number = 'LIMIT ' . $number;
  250. } else {
  251. $number = '';
  252. }
  253. $post_where = '';
  254. if ( ! empty($post_id) )
  255. $post_where .= $wpdb->prepare( 'comment_post_ID = %d AND ', $post_id );
  256. if ( '' !== $author_email )
  257. $post_where .= $wpdb->prepare( 'comment_author_email = %s AND ', $author_email );
  258. if ( '' !== $karma )
  259. $post_where .= $wpdb->prepare( 'comment_karma = %d AND ', $karma );
  260. if ( 'comment' == $type )
  261. $post_where .= "comment_type = '' AND ";
  262. elseif ( ! empty( $type ) )
  263. $post_where .= $wpdb->prepare( 'comment_type = %s AND ', $type );
  264. if ( '' !== $parent )
  265. $post_where .= $wpdb->prepare( 'comment_parent = %d AND ', $parent );
  266. if ( '' !== $user_id )
  267. $post_where .= $wpdb->prepare( 'user_id = %d AND ', $user_id );
  268. $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
  269. wp_cache_add( $cache_key, $comments, 'comment' );
  270. return $comments;
  271. }
  272. /**
  273. * Retrieve all of the WordPress supported comment statuses.
  274. *
  275. * Comments have a limited set of valid status values, this provides the comment
  276. * status values and descriptions.
  277. *
  278. * @package WordPress
  279. * @subpackage Post
  280. * @since 2.7.0
  281. *
  282. * @return array List of comment statuses.
  283. */
  284. function get_comment_statuses( ) {
  285. $status = array(
  286. 'hold' => __('Unapproved'),
  287. /* translators: comment status */
  288. 'approve' => _x('Approved', 'adjective'),
  289. /* translators: comment status */
  290. 'spam' => _x('Spam', 'adjective'),
  291. );
  292. return $status;
  293. }
  294. /**
  295. * The date the last comment was modified.
  296. *
  297. * @since 1.5.0
  298. * @uses $wpdb
  299. * @global array $cache_lastcommentmodified
  300. *
  301. * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
  302. * or 'server' locations.
  303. * @return string Last comment modified date.
  304. */
  305. function get_lastcommentmodified($timezone = 'server') {
  306. global $cache_lastcommentmodified, $wpdb;
  307. if ( isset($cache_lastcommentmodified[$timezone]) )
  308. return $cache_lastcommentmodified[$timezone];
  309. $add_seconds_server = date('Z');
  310. switch ( strtolower($timezone)) {
  311. case 'gmt':
  312. $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
  313. break;
  314. case 'blog':
  315. $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
  316. break;
  317. case 'server':
  318. $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));
  319. break;
  320. }
  321. $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
  322. return $lastcommentmodified;
  323. }
  324. /**
  325. * The amount of comments in a post or total comments.
  326. *
  327. * A lot like {@link wp_count_comments()}, in that they both return comment
  328. * stats (albeit with different types). The {@link wp_count_comments()} actual
  329. * caches, but this function does not.
  330. *
  331. * @since 2.0.0
  332. * @uses $wpdb
  333. *
  334. * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
  335. * @return array The amount of spam, approved, awaiting moderation, and total comments.
  336. */
  337. function get_comment_count( $post_id = 0 ) {
  338. global $wpdb;
  339. $post_id = (int) $post_id;
  340. $where = '';
  341. if ( $post_id > 0 ) {
  342. $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
  343. }
  344. $totals = (array) $wpdb->get_results("
  345. SELECT comment_approved, COUNT( * ) AS total
  346. FROM {$wpdb->comments}
  347. {$where}
  348. GROUP BY comment_approved
  349. ", ARRAY_A);
  350. $comment_count = array(
  351. "approved" => 0,
  352. "awaiting_moderation" => 0,
  353. "spam" => 0,
  354. "total_comments" => 0
  355. );
  356. foreach ( $totals as $row ) {
  357. switch ( $row['comment_approved'] ) {
  358. case 'spam':
  359. $comment_count['spam'] = $row['total'];
  360. $comment_count["total_comments"] += $row['total'];
  361. break;
  362. case 1:
  363. $comment_count['approved'] = $row['total'];
  364. $comment_count['total_comments'] += $row['total'];
  365. break;
  366. case 0:
  367. $comment_count['awaiting_moderation'] = $row['total'];
  368. $comment_count['total_comments'] += $row['total'];
  369. break;
  370. default:
  371. break;
  372. }
  373. }
  374. return $comment_count;
  375. }
  376. //
  377. // Comment meta functions
  378. //
  379. /**
  380. * Add meta data field to a comment.
  381. *
  382. * @since 2.9.0
  383. * @uses add_metadata
  384. * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
  385. *
  386. * @param int $comment_id Comment ID.
  387. * @param string $key Metadata name.
  388. * @param mixed $value Metadata value.
  389. * @param bool $unique Optional, default is false. Whether the same key should not be added.
  390. * @return bool False for failure. True for success.
  391. */
  392. function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
  393. return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
  394. }
  395. /**
  396. * Remove metadata matching criteria from a comment.
  397. *
  398. * You can match based on the key, or key and value. Removing based on key and
  399. * value, will keep from removing duplicate metadata with the same key. It also
  400. * allows removing all metadata matching key, if needed.
  401. *
  402. * @since 2.9.0
  403. * @uses delete_metadata
  404. * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
  405. *
  406. * @param int $comment_id comment ID
  407. * @param string $meta_key Metadata name.
  408. * @param mixed $meta_value Optional. Metadata value.
  409. * @return bool False for failure. True for success.
  410. */
  411. function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
  412. return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
  413. }
  414. /**
  415. * Retrieve comment meta field for a comment.
  416. *
  417. * @since 2.9.0
  418. * @uses get_metadata
  419. * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
  420. *
  421. * @param int $comment_id Comment ID.
  422. * @param string $key The meta key to retrieve.
  423. * @param bool $single Whether to return a single value.
  424. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
  425. * is true.
  426. */
  427. function get_comment_meta($comment_id, $key, $single = false) {
  428. return get_metadata('comment', $comment_id, $key, $single);
  429. }
  430. /**
  431. * Update comment meta field based on comment ID.
  432. *
  433. * Use the $prev_value parameter to differentiate between meta fields with the
  434. * same key and comment ID.
  435. *
  436. * If the meta field for the comment does not exist, it will be added.
  437. *
  438. * @since 2.9.0
  439. * @uses update_metadata
  440. * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
  441. *
  442. * @param int $comment_id Comment ID.
  443. * @param string $key Metadata key.
  444. * @param mixed $value Metadata value.
  445. * @param mixed $prev_value Optional. Previous value to check before removing.
  446. * @return bool False on failure, true if success.
  447. */
  448. function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
  449. return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
  450. }
  451. /**
  452. * Sanitizes the cookies sent to the user already.
  453. *
  454. * Will only do anything if the cookies have already been created for the user.
  455. * Mostly used after cookies had been sent to use elsewhere.
  456. *
  457. * @since 2.0.4
  458. */
  459. function sanitize_comment_cookies() {
  460. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
  461. $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
  462. $comment_author = stripslashes($comment_author);
  463. $comment_author = esc_attr($comment_author);
  464. $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
  465. }
  466. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
  467. $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
  468. $comment_author_email = stripslashes($comment_author_email);
  469. $comment_author_email = esc_attr($comment_author_email);
  470. $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
  471. }
  472. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
  473. $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
  474. $comment_author_url = stripslashes($comment_author_url);
  475. $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
  476. }
  477. }
  478. /**
  479. * Validates whether this comment is allowed to be made.
  480. *
  481. * @since 2.0.0
  482. * @uses $wpdb
  483. * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment
  484. * @uses apply_filters() Calls 'comment_duplicate_trigger' hook on commentdata.
  485. * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt
  486. *
  487. * @param array $commentdata Contains information on the comment
  488. * @return mixed Signifies the approval status (0|1|'spam')
  489. */
  490. function wp_allow_comment($commentdata) {
  491. global $wpdb;
  492. extract($commentdata, EXTR_SKIP);
  493. // Simple duplicate check
  494. // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
  495. $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' ";
  496. if ( $comment_author_email )
  497. $dupe .= "OR comment_author_email = '$comment_author_email' ";
  498. $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
  499. if ( $wpdb->get_var($dupe) ) {
  500. do_action( 'comment_duplicate_trigger', $commentdata );
  501. if ( defined('DOING_AJAX') )
  502. die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
  503. wp_die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
  504. }
  505. do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
  506. if ( isset($user_id) && $user_id) {
  507. $userdata = get_userdata($user_id);
  508. $user = new WP_User($user_id);
  509. $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
  510. }
  511. if ( isset($userdata) && ( $user_id == $post_author || $user->has_cap('moderate_comments') ) ) {
  512. // The author and the admins get respect.
  513. $approved = 1;
  514. } else {
  515. // Everyone else's comments will be checked.
  516. if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
  517. $approved = 1;
  518. else
  519. $approved = 0;
  520. if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
  521. $approved = 'spam';
  522. }
  523. $approved = apply_filters('pre_comment_approved', $approved);
  524. return $approved;
  525. }
  526. /**
  527. * Check whether comment flooding is occurring.
  528. *
  529. * Won't run, if current user can manage options, so to not block
  530. * administrators.
  531. *
  532. * @since 2.3.0
  533. * @uses $wpdb
  534. * @uses apply_filters() Calls 'comment_flood_filter' filter with first
  535. * parameter false, last comment timestamp, new comment timestamp.
  536. * @uses do_action() Calls 'comment_flood_trigger' action with parameters with
  537. * last comment timestamp and new comment timestamp.
  538. *
  539. * @param string $ip Comment IP.
  540. * @param string $email Comment author email address.
  541. * @param string $date MySQL time string.
  542. */
  543. function check_comment_flood_db( $ip, $email, $date ) {
  544. global $wpdb;
  545. if ( current_user_can( 'manage_options' ) )
  546. return; // don't throttle admins
  547. $hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 );
  548. if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) {
  549. $time_lastcomment = mysql2date('U', $lasttime, false);
  550. $time_newcomment = mysql2date('U', $date, false);
  551. $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
  552. if ( $flood_die ) {
  553. do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
  554. if ( defined('DOING_AJAX') )
  555. die( __('You are posting comments too quickly. Slow down.') );
  556. wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) );
  557. }
  558. }
  559. }
  560. /**
  561. * Separates an array of comments into an array keyed by comment_type.
  562. *
  563. * @since 2.7.0
  564. *
  565. * @param array $comments Array of comments
  566. * @return array Array of comments keyed by comment_type.
  567. */
  568. function &separate_comments(&$comments) {
  569. $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
  570. $count = count($comments);
  571. for ( $i = 0; $i < $count; $i++ ) {
  572. $type = $comments[$i]->comment_type;
  573. if ( empty($type) )
  574. $type = 'comment';
  575. $comments_by_type[$type][] = &$comments[$i];
  576. if ( 'trackback' == $type || 'pingback' == $type )
  577. $comments_by_type['pings'][] = &$comments[$i];
  578. }
  579. return $comments_by_type;
  580. }
  581. /**
  582. * Calculate the total number of comment pages.
  583. *
  584. * @since 2.7.0
  585. * @uses get_query_var() Used to fill in the default for $per_page parameter.
  586. * @uses get_option() Used to fill in defaults for parameters.
  587. * @uses Walker_Comment
  588. *
  589. * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
  590. * @param int $per_page Optional comments per page.
  591. * @param boolean $threaded Optional control over flat or threaded comments.
  592. * @return int Number of comment pages.
  593. */
  594. function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
  595. global $wp_query;
  596. if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
  597. return $wp_query->max_num_comment_pages;
  598. if ( !$comments || !is_array($comments) )
  599. $comments = $wp_query->comments;
  600. if ( empty($comments) )
  601. return 0;
  602. if ( !isset($per_page) )
  603. $per_page = (int) get_query_var('comments_per_page');
  604. if ( 0 === $per_page )
  605. $per_page = (int) get_option('comments_per_page');
  606. if ( 0 === $per_page )
  607. return 1;
  608. if ( !isset($threaded) )
  609. $threaded = get_option('thread_comments');
  610. if ( $threaded ) {
  611. $walker = new Walker_Comment;
  612. $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
  613. } else {
  614. $count = ceil( count( $comments ) / $per_page );
  615. }
  616. return $count;
  617. }
  618. /**
  619. * Calculate what page number a comment will appear on for comment paging.
  620. *
  621. * @since 2.7.0
  622. * @uses get_comment() Gets the full comment of the $comment_ID parameter.
  623. * @uses get_option() Get various settings to control function and defaults.
  624. * @uses get_page_of_comment() Used to loop up to top level comment.
  625. *
  626. * @param int $comment_ID Comment ID.
  627. * @param array $args Optional args.
  628. * @return int|null Comment page number or null on error.
  629. */
  630. function get_page_of_comment( $comment_ID, $args = array() ) {
  631. global $wpdb;
  632. if ( !$comment = get_comment( $comment_ID ) )
  633. return;
  634. $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
  635. $args = wp_parse_args( $args, $defaults );
  636. if ( '' === $args['per_page'] && get_option('page_comments') )
  637. $args['per_page'] = get_query_var('comments_per_page');
  638. if ( empty($args['per_page']) ) {
  639. $args['per_page'] = 0;
  640. $args['page'] = 0;
  641. }
  642. if ( $args['per_page'] < 1 )
  643. return 1;
  644. if ( '' === $args['max_depth'] ) {
  645. if ( get_option('thread_comments') )
  646. $args['max_depth'] = get_option('thread_comments_depth');
  647. else
  648. $args['max_depth'] = -1;
  649. }
  650. // Find this comment's top level parent if threading is enabled
  651. if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
  652. return get_page_of_comment( $comment->comment_parent, $args );
  653. $allowedtypes = array(
  654. 'comment' => '',
  655. 'pingback' => 'pingback',
  656. 'trackback' => 'trackback',
  657. );
  658. $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
  659. // Count comments older than this one
  660. $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
  661. // No older comments? Then it's page #1.
  662. if ( 0 == $oldercoms )
  663. return 1;
  664. // Divide comments older than this one by comments per page to get this comment's page number
  665. return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
  666. }
  667. /**
  668. * Does comment contain blacklisted characters or words.
  669. *
  670. * @since 1.5.0
  671. * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters.
  672. *
  673. * @param string $author The author of the comment
  674. * @param string $email The email of the comment
  675. * @param string $url The url used in the comment
  676. * @param string $comment The comment content
  677. * @param string $user_ip The comment author IP address
  678. * @param string $user_agent The author's browser user agent
  679. * @return bool True if comment contains blacklisted content, false if comment does not
  680. */
  681. function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
  682. do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
  683. $mod_keys = trim( get_option('blacklist_keys') );
  684. if ( '' == $mod_keys )
  685. return false; // If moderation keys are empty
  686. $words = explode("\n", $mod_keys );
  687. foreach ( (array) $words as $word ) {
  688. $word = trim($word);
  689. // Skip empty lines
  690. if ( empty($word) ) { continue; }
  691. // Do some escaping magic so that '#' chars in the
  692. // spam words don't break things:
  693. $word = preg_quote($word, '#');
  694. $pattern = "#$word#i";
  695. if (
  696. preg_match($pattern, $author)
  697. || preg_match($pattern, $email)
  698. || preg_match($pattern, $url)
  699. || preg_match($pattern, $comment)
  700. || preg_match($pattern, $user_ip)
  701. || preg_match($pattern, $user_agent)
  702. )
  703. return true;
  704. }
  705. return false;
  706. }
  707. /**
  708. * Retrieve total comments for blog or single post.
  709. *
  710. * The properties of the returned object contain the 'moderated', 'approved',
  711. * and spam comments for either the entire blog or single post. Those properties
  712. * contain the amount of comments that match the status. The 'total_comments'
  713. * property contains the integer of total comments.
  714. *
  715. * The comment stats are cached and then retrieved, if they already exist in the
  716. * cache.
  717. *
  718. * @since 2.5.0
  719. *
  720. * @param int $post_id Optional. Post ID.
  721. * @return object Comment stats.
  722. */
  723. function wp_count_comments( $post_id = 0 ) {
  724. global $wpdb;
  725. $post_id = (int) $post_id;
  726. $stats = apply_filters('wp_count_comments', array(), $post_id);
  727. if ( !empty($stats) )
  728. return $stats;
  729. $count = wp_cache_get("comments-{$post_id}", 'counts');
  730. if ( false !== $count )
  731. return $count;
  732. $where = '';
  733. if ( $post_id > 0 )
  734. $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
  735. $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
  736. $total = 0;
  737. $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
  738. $known_types = array_keys( $approved );
  739. foreach ( (array) $count as $row ) {
  740. // Don't count post-trashed toward totals
  741. if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
  742. $total += $row['num_comments'];
  743. if ( in_array( $row['comment_approved'], $known_types ) )
  744. $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
  745. }
  746. $stats['total_comments'] = $total;
  747. foreach ( $approved as $key ) {
  748. if ( empty($stats[$key]) )
  749. $stats[$key] = 0;
  750. }
  751. $stats = (object) $stats;
  752. wp_cache_set("comments-{$post_id}", $stats, 'counts');
  753. return $stats;
  754. }
  755. /**
  756. * Trashes or deletes a comment.
  757. *
  758. * The comment is moved to trash instead of permanently deleted unless trash is
  759. * disabled, item is already in the trash, or $force_delete is true.
  760. *
  761. * The post comment count will be updated if the comment was approved and has a
  762. * post ID available.
  763. *
  764. * @since 2.0.0
  765. * @uses $wpdb
  766. * @uses do_action() Calls 'delete_comment' hook on comment ID
  767. * @uses do_action() Calls 'deleted_comment' hook on comment ID after deletion, on success
  768. * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
  769. * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
  770. *
  771. * @param int $comment_id Comment ID
  772. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  773. * @return bool False if delete comment query failure, true on success.
  774. */
  775. function wp_delete_comment($comment_id, $force_delete = false) {
  776. global $wpdb;
  777. if (!$comment = get_comment($comment_id))
  778. return false;
  779. if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
  780. return wp_trash_comment($comment_id);
  781. do_action('delete_comment', $comment_id);
  782. // Move children up a level.
  783. $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
  784. if ( !empty($children) ) {
  785. $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
  786. clean_comment_cache($children);
  787. }
  788. // Delete metadata
  789. $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d ", $comment_id ) );
  790. if ( !empty($meta_ids) ) {
  791. do_action( 'delete_commentmeta', $meta_ids );
  792. $in_meta_ids = "'" . implode("', '", $meta_ids) . "'";
  793. $wpdb->query( "DELETE FROM $wpdb->commentmeta WHERE meta_id IN ($in_meta_ids)" );
  794. do_action( 'deleted_commentmeta', $meta_ids );
  795. }
  796. if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
  797. return false;
  798. do_action('deleted_comment', $comment_id);
  799. $post_id = $comment->comment_post_ID;
  800. if ( $post_id && $comment->comment_approved == 1 )
  801. wp_update_comment_count($post_id);
  802. clean_comment_cache($comment_id);
  803. do_action('wp_set_comment_status', $comment_id, 'delete');
  804. wp_transition_comment_status('delete', $comment->comment_approved, $comment);
  805. return true;
  806. }
  807. /**
  808. * Moves a comment to the Trash
  809. *
  810. * If trash is disabled, comment is permanently deleted.
  811. *
  812. * @since 2.9.0
  813. * @uses do_action() on 'trash_comment' before trashing
  814. * @uses do_action() on 'trashed_comment' after trashing
  815. * @uses wp_delete_comment() if trash is disabled
  816. *
  817. * @param int $comment_id Comment ID.
  818. * @return mixed False on failure
  819. */
  820. function wp_trash_comment($comment_id) {
  821. if ( !EMPTY_TRASH_DAYS )
  822. return wp_delete_comment($comment_id, true);
  823. if ( !$comment = get_comment($comment_id) )
  824. return false;
  825. do_action('trash_comment', $comment_id);
  826. if ( wp_set_comment_status($comment_id, 'trash') ) {
  827. add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
  828. add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
  829. do_action('trashed_comment', $comment_id);
  830. return true;
  831. }
  832. return false;
  833. }
  834. /**
  835. * Removes a comment from the Trash
  836. *
  837. * @since 2.9.0
  838. * @uses do_action() on 'untrash_comment' before untrashing
  839. * @uses do_action() on 'untrashed_comment' after untrashing
  840. *
  841. * @param int $comment_id Comment ID.
  842. * @return mixed False on failure
  843. */
  844. function wp_untrash_comment($comment_id) {
  845. if ( ! (int)$comment_id )
  846. return false;
  847. do_action('untrash_comment', $comment_id);
  848. $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
  849. if ( empty($status) )
  850. $status = '0';
  851. if ( wp_set_comment_status($comment_id, $status) ) {
  852. delete_comment_meta($comment_id, '_wp_trash_meta_time');
  853. delete_comment_meta($comment_id, '_wp_trash_meta_status');
  854. do_action('untrashed_comment', $comment_id);
  855. return true;
  856. }
  857. return false;
  858. }
  859. /**
  860. * Marks a comment as Spam
  861. *
  862. * @since 2.9.0
  863. * @uses do_action() on 'spam_comment' before spamming
  864. * @uses do_action() on 'spammed_comment' after spamming
  865. *
  866. * @param int $comment_id Comment ID.
  867. * @return mixed False on failure
  868. */
  869. function wp_spam_comment($comment_id) {
  870. if ( !$comment = get_comment($comment_id) )
  871. return false;
  872. do_action('spam_comment', $comment_id);
  873. if ( wp_set_comment_status($comment_id, 'spam') ) {
  874. add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
  875. do_action('spammed_comment', $comment_id);
  876. return true;
  877. }
  878. return false;
  879. }
  880. /**
  881. * Removes a comment from the Spam
  882. *
  883. * @since 2.9.0
  884. * @uses do_action() on 'unspam_comment' before unspamming
  885. * @uses do_action() on 'unspammed_comment' after unspamming
  886. *
  887. * @param int $comment_id Comment ID.
  888. * @return mixed False on failure
  889. */
  890. function wp_unspam_comment($comment_id) {
  891. if ( ! (int)$comment_id )
  892. return false;
  893. do_action('unspam_comment', $comment_id);
  894. $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
  895. if ( empty($status) )
  896. $status = '0';
  897. if ( wp_set_comment_status($comment_id, $status) ) {
  898. delete_comment_meta($comment_id, '_wp_trash_meta_status');
  899. do_action('unspammed_comment', $comment_id);
  900. return true;
  901. }
  902. return false;
  903. }
  904. /**
  905. * The status of a comment by ID.
  906. *
  907. * @since 1.0.0
  908. *
  909. * @param int $comment_id Comment ID
  910. * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
  911. */
  912. function wp_get_comment_status($comment_id) {
  913. $comment = get_comment($comment_id);
  914. if ( !$comment )
  915. return false;
  916. $approved = $comment->comment_approved;
  917. if ( $approved == NULL )
  918. return false;
  919. elseif ( $approved == '1' )
  920. return 'approved';
  921. elseif ( $approved == '0' )
  922. return 'unapproved';
  923. elseif ( $approved == 'spam' )
  924. return 'spam';
  925. elseif ( $approved == 'trash' )
  926. return 'trash';
  927. else
  928. return false;
  929. }
  930. /**
  931. * Call hooks for when a comment status transition occurs.
  932. *
  933. * Calls hooks for comment status transitions. If the new comment status is not the same
  934. * as the previous comment status, then two hooks will be ran, the first is
  935. * 'transition_comment_status' with new status, old status, and comment data. The
  936. * next action called is 'comment_OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
  937. * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
  938. * comment data.
  939. *
  940. * The final action will run whether or not the comment statuses are the same. The
  941. * action is named 'comment_NEWSTATUS_COMMENTTYPE', NEWSTATUS is from the $new_status
  942. * parameter and COMMENTTYPE is comment_type comment data.
  943. *
  944. * @since 2.7.0
  945. *
  946. * @param string $new_status New comment status.
  947. * @param string $old_status Previous comment status.
  948. * @param object $comment Comment data.
  949. */
  950. function wp_transition_comment_status($new_status, $old_status, $comment) {
  951. // Translate raw statuses to human readable formats for the hooks
  952. // This is not a complete list of comment status, it's only the ones that need to be renamed
  953. $comment_statuses = array(
  954. 0 => 'unapproved',
  955. 'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
  956. 1 => 'approved',
  957. 'approve' => 'approved', // wp_set_comment_status() uses "approve"
  958. );
  959. if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
  960. if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
  961. // Call the hooks
  962. if ( $new_status != $old_status ) {
  963. do_action('transition_comment_status', $new_status, $old_status, $comment);
  964. do_action("comment_${old_status}_to_$new_status", $comment);
  965. }
  966. do_action("comment_${new_status}_$comment->comment_type", $comment->comment_ID, $comment);
  967. }
  968. /**
  969. * Get current commenter's name, email, and URL.
  970. *
  971. * Expects cookies content to already be sanitized. User of this function might
  972. * wish to recheck the returned array for validity.
  973. *
  974. * @see sanitize_comment_cookies() Use to sanitize cookies
  975. *
  976. * @since 2.0.4
  977. *
  978. * @return array Comment author, email, url respectively.
  979. */
  980. function wp_get_current_commenter() {
  981. // Cookies should already be sanitized.
  982. $comment_author = '';
  983. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
  984. $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
  985. $comment_author_email = '';
  986. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
  987. $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
  988. $comment_author_url = '';
  989. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
  990. $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
  991. return compact('comment_author', 'comment_author_email', 'comment_author_url');
  992. }
  993. /**
  994. * Inserts a comment to the database.
  995. *
  996. * The available comment data key names are 'comment_author_IP', 'comment_date',
  997. * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
  998. *
  999. * @since 2.0.0
  1000. * @uses $wpdb
  1001. *
  1002. * @param array $commentdata Contains information on the comment.
  1003. * @return int The new comment's ID.
  1004. */
  1005. function wp_insert_comment($commentdata) {
  1006. global $wpdb;
  1007. extract(stripslashes_deep($commentdata), EXTR_SKIP);
  1008. if ( ! isset($comment_author_IP) )
  1009. $comment_author_IP = '';
  1010. if ( ! isset($comment_date) )
  1011. $comment_date = current_time('mysql');
  1012. if ( ! isset($comment_date_gmt) )
  1013. $comment_date_gmt = get_gmt_from_date($comment_date);
  1014. if ( ! isset($comment_parent) )
  1015. $comment_parent = 0;
  1016. if ( ! isset($comment_approved) )
  1017. $comment_approved = 1;
  1018. if ( ! isset($comment_karma) )
  1019. $comment_karma = 0;
  1020. if ( ! isset($user_id) )
  1021. $user_id = 0;
  1022. if ( ! isset($comment_type) )
  1023. $comment_type = '';
  1024. $data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id');
  1025. $wpdb->insert($wpdb->comments, $data);
  1026. $id = (int) $wpdb->insert_id;
  1027. if ( $comment_approved == 1 )
  1028. wp_update_comment_count($comment_post_ID);
  1029. $comment = get_comment($id);
  1030. do_action('wp_insert_comment', $id, $comment);
  1031. return $id;
  1032. }
  1033. /**
  1034. * Filters and sanitizes comment data.
  1035. *
  1036. * Sets the comment data 'filtered' field to true when finished. This can be
  1037. * checked as to whether the comment should be filtered and to keep from
  1038. * filtering the same comment more than once.
  1039. *
  1040. * @since 2.0.0
  1041. * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID
  1042. * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent
  1043. * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name
  1044. * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content
  1045. * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP
  1046. * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL
  1047. * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address
  1048. *
  1049. * @param array $commentdata Contains information on the comment.
  1050. * @return array Parsed comment information.
  1051. */
  1052. function wp_filter_comment($commentdata) {
  1053. if ( isset($commentdata['user_ID']) )
  1054. $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
  1055. elseif ( isset($commentdata['user_id']) )
  1056. $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_id']);
  1057. $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
  1058. $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
  1059. $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']);
  1060. $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
  1061. $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
  1062. $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
  1063. $commentdata['filtered'] = true;
  1064. return $commentdata;
  1065. }
  1066. /**
  1067. * Whether comment should be blocked because of comment flood.
  1068. *
  1069. * @since 2.1.0
  1070. *
  1071. * @param bool $block Whether plugin has already blocked comment.
  1072. * @param int $time_lastcomment Timestamp for last comment.
  1073. * @param int $time_newcomment Timestamp for new comment.
  1074. * @return bool Whether comment should be blocked.
  1075. */
  1076. function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
  1077. if ( $block ) // a plugin has already blocked... we'll let that decision stand
  1078. return $block;
  1079. if ( ($time_newcomment - $time_lastcomment) < 15 )
  1080. return true;
  1081. return false;
  1082. }
  1083. /**
  1084. * Adds a new comment to the database.
  1085. *
  1086. * Filters new comment to ensure that the fields are sanitized and valid before
  1087. * inserting comment into database. Calls 'comment_post' action with comment ID
  1088. * and whether comment is approved by WordPress. Also has 'preprocess_comment'
  1089. * filter for processing the comment data before the function handles it.
  1090. *
  1091. * @since 1.5.0
  1092. * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
  1093. * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
  1094. * @uses wp_filter_comment() Used to filter comment before adding comment.
  1095. * @uses wp_allow_comment() checks to see if comment is approved.
  1096. * @uses wp_insert_comment() Does the actual comment insertion to the database.
  1097. *
  1098. * @param array $commentdata Contains information on the comment.
  1099. * @return int The ID of the comment after adding.
  1100. */
  1101. function wp_new_comment( $commentdata ) {
  1102. $commentdata = apply_filters('preprocess_comment', $commentdata);
  1103. $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
  1104. if ( isset($commentdata['user_ID']) )
  1105. $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
  1106. elseif ( isset($commentdata['user_id']) )
  1107. $commentdata['user_id'] = (int) $commentdata['user_id'];
  1108. $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
  1109. $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
  1110. $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
  1111. $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
  1112. $commentdata['comment_agent'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
  1113. $commentdata['comment_date'] = current_time('mysql');
  1114. $commentdata['comment_date_gmt'] = current_time('mysql', 1);
  1115. $commentdata = wp_filter_comment($commentdata);
  1116. $commentdata['comment_approved'] = wp_allow_comment($commentdata);
  1117. $comment_ID = wp_insert_comment($commentdata);
  1118. do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
  1119. if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
  1120. if ( '0' == $commentdata['comment_approved'] )
  1121. wp_notify_moderator($comment_ID);
  1122. $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
  1123. if ( get_option('comments_notify') && $commentdata['comment_approved'] && ( ! isset( $commentdata['user_id'] ) || $post->post_author != $commentdata['user_id'] ) )
  1124. wp_notify_postauthor($comment_ID, empty( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '' );
  1125. }
  1126. return $comment_ID;
  1127. }
  1128. /**
  1129. * Sets the status of a comment.
  1130. *
  1131. * The 'wp_set_comment_status' action is called after the comment is handled and
  1132. * will only be called, if the comment status is either 'hold', 'approve', or
  1133. * 'spam'. If the comment status is not in the list, then false is returned and
  1134. * if the status is 'delete', then the comment is deleted without calling the
  1135. * action.
  1136. *
  1137. * @since 1.0.0
  1138. * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
  1139. *
  1140. * @param int $comment_id Comment ID.
  1141. * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'.
  1142. * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
  1143. * @return bool False on failure or deletion and true on success.
  1144. */
  1145. function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
  1146. global $wpdb;
  1147. $status = '0';
  1148. switch ( $comment_status ) {
  1149. case 'hold':
  1150. case '0':
  1151. $status = '0';
  1152. break;
  1153. case 'approve':
  1154. case '1':
  1155. $status = '1';
  1156. if ( get_option('comments_notify') ) {
  1157. $comment = get_comment($comment_id);
  1158. wp_notify_postauthor($comment_id, $comment->comment_type);
  1159. }
  1160. break;
  1161. case 'spam':
  1162. $status = 'spam';
  1163. break;
  1164. case 'trash':
  1165. $status = 'trash';
  1166. break;
  1167. default:
  1168. return false;
  1169. }
  1170. $comment_old = wp_clone(get_comment($comment_id));
  1171. if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
  1172. if ( $wp_error )
  1173. return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
  1174. else
  1175. return false;
  1176. }
  1177. clean_comment_cache($comment_id);
  1178. $comment = get_comment($comment_id);
  1179. do_action('wp_set_comment_status', $comment_id, $comment_status);
  1180. wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
  1181. wp_update_comment_count($comment->comment_post_ID);
  1182. return true;
  1183. }
  1184. /**
  1185. * Updates an existing comment in the database.
  1186. *
  1187. * Filters the comment and makes sure certain fields are valid before updating.
  1188. *
  1189. * @since 2.0.0
  1190. * @uses $wpdb
  1191. * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
  1192. *
  1193. * @param array $commentarr Contains information on the comment.
  1194. * @return int Comment was updated if value is 1, or was not updated if value is 0.
  1195. */
  1196. function wp_update_comment($commentarr) {
  1197. global $wpdb;
  1198. // First, get all of the original fields
  1199. $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
  1200. // Escape data pulled from DB.
  1201. $comment = esc_sql($comment);
  1202. $old_status = $comment['comment_approved'];
  1203. // Merge old and new fields with new fields overwriting old ones.
  1204. $commentarr = array_merge($comment, $commentarr);
  1205. $commentarr = wp_filter_comment( $commentarr );
  1206. // Now extract the merged array.
  1207. extract(stripslashes_deep($commentarr), EXTR_SKIP);
  1208. $comment_content = apply_filters('comment_save_pre', $comment_content);
  1209. $comment_date_gmt = get_gmt_from_date($comment_date);
  1210. if ( !isset($comment_approved) )
  1211. $comment_approved = 1;
  1212. else if ( 'hold' == $comment_approved )
  1213. $comment_approved = 0;
  1214. else if ( 'approve' == $comment_approved )
  1215. $comment_approved = 1;
  1216. $data = compact('comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt');
  1217. $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
  1218. clean_comment_cache($comment_ID);
  1219. wp_update_comment_count($comment_post_ID);
  1220. do_action('edit_comment', $comment_ID);
  1221. $comment = get_comment($comment_ID);
  1222. wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
  1223. return $rval;
  1224. }
  1225. /**
  1226. * Whether to defer comment counting.
  1227. *
  1228. * When setting $defer to true, all post comment counts will not be updated
  1229. * until $defer is set to false. When $defer is set to false, then all
  1230. * previously deferred updated post comment counts will then be automatically
  1231. * updated without having to call wp_update_comment_count() after.
  1232. *
  1233. * @since 2.5.0
  1234. * @staticvar bool $_defer
  1235. *
  1236. * @param bool $defer
  1237. * @return unknown
  1238. */
  1239. function wp_defer_comment_counting($defer=null) {
  1240. static $_defer = false;
  1241. if ( is_bool($defer) ) {
  1242. $_defer = $defer;
  1243. // flush any deferred counts
  1244. if ( !$defer )
  1245. wp_update_comment_count( null, true );
  1246. }
  1247. return $_defer;
  1248. }
  1249. /**
  1250. * Updates the comment count for post(s).
  1251. *
  1252. * When $do_deferred is false (is by default) and the comments have been set to
  1253. * be deferred, the post_id will be added to a queue, which will be updated at a
  1254. * later date and only updated once per post ID.
  1255. *
  1256. * If the comments have not be set up to be deferred, then the post will be
  1257. * updated. When $do_deferred is set to true, then all previous deferred post
  1258. * IDs will be updated along with the current $post_id.
  1259. *
  1260. * @since 2.1.0
  1261. * @see wp_update_comment_count_now() For what could cause a false return value
  1262. *
  1263. * @param int $post_id Post ID
  1264. * @param bool $do_deferred Whether to process previously deferred post comment counts
  1265. * @return bool True on success, false on failure
  1266. */
  1267. function wp_update_comment_count($post_id, $do_deferred=false) {
  1268. static $_deferred = array();
  1269. if ( $do_deferred ) {
  1270. $_deferred = array_unique($_deferred);
  1271. foreach ( $_deferred as $i => $_post_id ) {
  1272. wp_update_comment_count_now($_post_id);
  1273. unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
  1274. }
  1275. }
  1276. if ( wp_defer_comment_counting() ) {
  1277. $_deferred[] = $post_id;
  1278. return true;
  1279. }
  1280. elseif ( $post_id ) {
  1281. return wp_update_comment_count_now($post_id);
  1282. }
  1283. }
  1284. /**
  1285. * Updates the comment count for the post.
  1286. *
  1287. * @since 2.5.0
  1288. * @uses $wpdb
  1289. * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
  1290. * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
  1291. *
  1292. * @param int $post_id Post ID
  1293. * @return bool Fa