PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/comment.php

http://github.com/wordpress/wordpress
PHP | 3726 lines | 1689 code | 442 blank | 1595 comment | 349 complexity | 89a7e224fb8b33b30509a244f32f3d64 MD5 | raw file
Possible License(s): 0BSD

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

Large files files are truncated, but you can click here to view the full file