comment.php (125758B)
1 <?php 2 /** 3 * Core Comment API 4 * 5 * @package WordPress 6 * @subpackage Comment 7 */ 8 9 /** 10 * Check whether a comment passes internal checks to be allowed to add. 11 * 12 * If manual comment moderation is set in the administration, then all checks, 13 * regardless of their type and substance, will fail and the function will 14 * return false. 15 * 16 * If the number of links exceeds the amount in the administration, then the 17 * check fails. If any of the parameter contents contain any disallowed words, 18 * then the check fails. 19 * 20 * If the comment author was approved before, then the comment is automatically 21 * approved. 22 * 23 * If all checks pass, the function will return true. 24 * 25 * @since 1.2.0 26 * 27 * @global wpdb $wpdb WordPress database abstraction object. 28 * 29 * @param string $author Comment author name. 30 * @param string $email Comment author email. 31 * @param string $url Comment author URL. 32 * @param string $comment Content of the comment. 33 * @param string $user_ip Comment author IP address. 34 * @param string $user_agent Comment author User-Agent. 35 * @param string $comment_type Comment type, either user-submitted comment, 36 * trackback, or pingback. 37 * @return bool If all checks pass, true, otherwise false. 38 */ 39 function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) { 40 global $wpdb; 41 42 // If manual moderation is enabled, skip all checks and return false. 43 if ( 1 == get_option( 'comment_moderation' ) ) { 44 return false; 45 } 46 47 /** This filter is documented in wp-includes/comment-template.php */ 48 $comment = apply_filters( 'comment_text', $comment, null, array() ); 49 50 // Check for the number of external links if a max allowed number is set. 51 $max_links = get_option( 'comment_max_links' ); 52 if ( $max_links ) { 53 $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out ); 54 55 /** 56 * Filters the number of links found in a comment. 57 * 58 * @since 3.0.0 59 * @since 4.7.0 Added the `$comment` parameter. 60 * 61 * @param int $num_links The number of links found. 62 * @param string $url Comment author's URL. Included in allowed links total. 63 * @param string $comment Content of the comment. 64 */ 65 $num_links = apply_filters( 'comment_max_links_url', $num_links, $url, $comment ); 66 67 /* 68 * If the number of links in the comment exceeds the allowed amount, 69 * fail the check by returning false. 70 */ 71 if ( $num_links >= $max_links ) { 72 return false; 73 } 74 } 75 76 $mod_keys = trim( get_option( 'moderation_keys' ) ); 77 78 // If moderation 'keys' (keywords) are set, process them. 79 if ( ! empty( $mod_keys ) ) { 80 $words = explode( "\n", $mod_keys ); 81 82 foreach ( (array) $words as $word ) { 83 $word = trim( $word ); 84 85 // Skip empty lines. 86 if ( empty( $word ) ) { 87 continue; 88 } 89 90 /* 91 * Do some escaping magic so that '#' (number of) characters in the spam 92 * words don't break things: 93 */ 94 $word = preg_quote( $word, '#' ); 95 96 /* 97 * Check the comment fields for moderation keywords. If any are found, 98 * fail the check for the given field by returning false. 99 */ 100 $pattern = "#$word#i"; 101 if ( preg_match( $pattern, $author ) ) { 102 return false; 103 } 104 if ( preg_match( $pattern, $email ) ) { 105 return false; 106 } 107 if ( preg_match( $pattern, $url ) ) { 108 return false; 109 } 110 if ( preg_match( $pattern, $comment ) ) { 111 return false; 112 } 113 if ( preg_match( $pattern, $user_ip ) ) { 114 return false; 115 } 116 if ( preg_match( $pattern, $user_agent ) ) { 117 return false; 118 } 119 } 120 } 121 122 /* 123 * Check if the option to approve comments by previously-approved authors is enabled. 124 * 125 * If it is enabled, check whether the comment author has a previously-approved comment, 126 * as well as whether there are any moderation keywords (if set) present in the author 127 * email address. If both checks pass, return true. Otherwise, return false. 128 */ 129 if ( 1 == get_option( 'comment_previously_approved' ) ) { 130 if ( 'trackback' !== $comment_type && 'pingback' !== $comment_type && '' !== $author && '' !== $email ) { 131 $comment_user = get_user_by( 'email', wp_unslash( $email ) ); 132 if ( ! empty( $comment_user->ID ) ) { 133 $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 ) ); 134 } else { 135 // expected_slashed ($author, $email) 136 $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 ) ); 137 } 138 if ( ( 1 == $ok_to_comment ) && 139 ( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) { 140 return true; 141 } else { 142 return false; 143 } 144 } else { 145 return false; 146 } 147 } 148 return true; 149 } 150 151 /** 152 * Retrieve the approved comments for post $post_id. 153 * 154 * @since 2.0.0 155 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query. 156 * 157 * @param int $post_id The ID of the post. 158 * @param array $args Optional. See WP_Comment_Query::__construct() for information on accepted arguments. 159 * @return int|array The approved comments, or number of comments if `$count` 160 * argument is true. 161 */ 162 function get_approved_comments( $post_id, $args = array() ) { 163 if ( ! $post_id ) { 164 return array(); 165 } 166 167 $defaults = array( 168 'status' => 1, 169 'post_id' => $post_id, 170 'order' => 'ASC', 171 ); 172 $parsed_args = wp_parse_args( $args, $defaults ); 173 174 $query = new WP_Comment_Query; 175 return $query->query( $parsed_args ); 176 } 177 178 /** 179 * Retrieves comment data given a comment ID or comment object. 180 * 181 * If an object is passed then the comment data will be cached and then returned 182 * after being passed through a filter. If the comment is empty, then the global 183 * comment variable will be used, if it is set. 184 * 185 * @since 2.0.0 186 * 187 * @global WP_Comment $comment Global comment object. 188 * 189 * @param WP_Comment|string|int $comment Comment to retrieve. 190 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which 191 * correspond to a WP_Comment object, an associative array, or a numeric array, 192 * respectively. Default OBJECT. 193 * @return WP_Comment|array|null Depends on $output value. 194 */ 195 function get_comment( $comment = null, $output = OBJECT ) { 196 if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) { 197 $comment = $GLOBALS['comment']; 198 } 199 200 if ( $comment instanceof WP_Comment ) { 201 $_comment = $comment; 202 } elseif ( is_object( $comment ) ) { 203 $_comment = new WP_Comment( $comment ); 204 } else { 205 $_comment = WP_Comment::get_instance( $comment ); 206 } 207 208 if ( ! $_comment ) { 209 return null; 210 } 211 212 /** 213 * Fires after a comment is retrieved. 214 * 215 * @since 2.3.0 216 * 217 * @param WP_Comment $_comment Comment data. 218 */ 219 $_comment = apply_filters( 'get_comment', $_comment ); 220 221 if ( OBJECT === $output ) { 222 return $_comment; 223 } elseif ( ARRAY_A === $output ) { 224 return $_comment->to_array(); 225 } elseif ( ARRAY_N === $output ) { 226 return array_values( $_comment->to_array() ); 227 } 228 return $_comment; 229 } 230 231 /** 232 * Retrieve a list of comments. 233 * 234 * The comment list can be for the blog as a whole or for an individual post. 235 * 236 * @since 2.7.0 237 * 238 * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct() 239 * for information on accepted arguments. Default empty. 240 * @return int|array List of comments or number of found comments if `$count` argument is true. 241 */ 242 function get_comments( $args = '' ) { 243 $query = new WP_Comment_Query; 244 return $query->query( $args ); 245 } 246 247 /** 248 * Retrieve all of the WordPress supported comment statuses. 249 * 250 * Comments have a limited set of valid status values, this provides the comment 251 * status values and descriptions. 252 * 253 * @since 2.7.0 254 * 255 * @return string[] List of comment status labels keyed by status. 256 */ 257 function get_comment_statuses() { 258 $status = array( 259 'hold' => __( 'Unapproved' ), 260 'approve' => _x( 'Approved', 'comment status' ), 261 'spam' => _x( 'Spam', 'comment status' ), 262 'trash' => _x( 'Trash', 'comment status' ), 263 ); 264 265 return $status; 266 } 267 268 /** 269 * Gets the default comment status for a post type. 270 * 271 * @since 4.3.0 272 * 273 * @param string $post_type Optional. Post type. Default 'post'. 274 * @param string $comment_type Optional. Comment type. Default 'comment'. 275 * @return string Expected return value is 'open' or 'closed'. 276 */ 277 function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) { 278 switch ( $comment_type ) { 279 case 'pingback': 280 case 'trackback': 281 $supports = 'trackbacks'; 282 $option = 'ping'; 283 break; 284 default: 285 $supports = 'comments'; 286 $option = 'comment'; 287 break; 288 } 289 290 // Set the status. 291 if ( 'page' === $post_type ) { 292 $status = 'closed'; 293 } elseif ( post_type_supports( $post_type, $supports ) ) { 294 $status = get_option( "default_{$option}_status" ); 295 } else { 296 $status = 'closed'; 297 } 298 299 /** 300 * Filters the default comment status for the given post type. 301 * 302 * @since 4.3.0 303 * 304 * @param string $status Default status for the given post type, 305 * either 'open' or 'closed'. 306 * @param string $post_type Post type. Default is `post`. 307 * @param string $comment_type Type of comment. Default is `comment`. 308 */ 309 return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type ); 310 } 311 312 /** 313 * The date the last comment was modified. 314 * 315 * @since 1.5.0 316 * @since 4.7.0 Replaced caching the modified date in a local static variable 317 * with the Object Cache API. 318 * 319 * @global wpdb $wpdb WordPress database abstraction object. 320 * 321 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', or 'server' locations. 322 * @return string|false Last comment modified date on success, false on failure. 323 */ 324 function get_lastcommentmodified( $timezone = 'server' ) { 325 global $wpdb; 326 327 $timezone = strtolower( $timezone ); 328 $key = "lastcommentmodified:$timezone"; 329 330 $comment_modified_date = wp_cache_get( $key, 'timeinfo' ); 331 if ( false !== $comment_modified_date ) { 332 return $comment_modified_date; 333 } 334 335 switch ( $timezone ) { 336 case 'gmt': 337 $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" ); 338 break; 339 case 'blog': 340 $comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" ); 341 break; 342 case 'server': 343 $add_seconds_server = gmdate( 'Z' ); 344 345 $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 ) ); 346 break; 347 } 348 349 if ( $comment_modified_date ) { 350 wp_cache_set( $key, $comment_modified_date, 'timeinfo' ); 351 352 return $comment_modified_date; 353 } 354 355 return false; 356 } 357 358 /** 359 * Retrieves the total comment counts for the whole site or a single post. 360 * 361 * Unlike wp_count_comments(), this function always returns the live comment counts without caching. 362 * 363 * @since 2.0.0 364 * 365 * @global wpdb $wpdb WordPress database abstraction object. 366 * 367 * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that 368 * comment counts for the whole site will be retrieved. 369 * @return array() { 370 * The number of comments keyed by their status. 371 * 372 * @type int $approved The number of approved comments. 373 * @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending). 374 * @type int $spam The number of spam comments. 375 * @type int $trash The number of trashed comments. 376 * @type int $post-trashed The number of comments for posts that are in the trash. 377 * @type int $total_comments The total number of non-trashed comments, including spam. 378 * @type int $all The total number of pending or approved comments. 379 * } 380 */ 381 function get_comment_count( $post_id = 0 ) { 382 global $wpdb; 383 384 $post_id = (int) $post_id; 385 386 $where = ''; 387 if ( $post_id > 0 ) { 388 $where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id ); 389 } 390 391 $totals = (array) $wpdb->get_results( 392 " 393 SELECT comment_approved, COUNT( * ) AS total 394 FROM {$wpdb->comments} 395 {$where} 396 GROUP BY comment_approved 397 ", 398 ARRAY_A 399 ); 400 401 $comment_count = array( 402 'approved' => 0, 403 'awaiting_moderation' => 0, 404 'spam' => 0, 405 'trash' => 0, 406 'post-trashed' => 0, 407 'total_comments' => 0, 408 'all' => 0, 409 ); 410 411 foreach ( $totals as $row ) { 412 switch ( $row['comment_approved'] ) { 413 case 'trash': 414 $comment_count['trash'] = $row['total']; 415 break; 416 case 'post-trashed': 417 $comment_count['post-trashed'] = $row['total']; 418 break; 419 case 'spam': 420 $comment_count['spam'] = $row['total']; 421 $comment_count['total_comments'] += $row['total']; 422 break; 423 case '1': 424 $comment_count['approved'] = $row['total']; 425 $comment_count['total_comments'] += $row['total']; 426 $comment_count['all'] += $row['total']; 427 break; 428 case '0': 429 $comment_count['awaiting_moderation'] = $row['total']; 430 $comment_count['total_comments'] += $row['total']; 431 $comment_count['all'] += $row['total']; 432 break; 433 default: 434 break; 435 } 436 } 437 438 return array_map( 'intval', $comment_count ); 439 } 440 441 // 442 // Comment meta functions. 443 // 444 445 /** 446 * Add meta data field to a comment. 447 * 448 * @since 2.9.0 449 * 450 * @link https://developer.wordpress.org/reference/functions/add_comment_meta/ 451 * 452 * @param int $comment_id Comment ID. 453 * @param string $meta_key Metadata name. 454 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 455 * @param bool $unique Optional. Whether the same key should not be added. 456 * Default false. 457 * @return int|false Meta ID on success, false on failure. 458 */ 459 function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) { 460 return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); 461 } 462 463 /** 464 * Remove metadata matching criteria from a comment. 465 * 466 * You can match based on the key, or key and value. Removing based on key and 467 * value, will keep from removing duplicate metadata with the same key. It also 468 * allows removing all metadata matching key, if needed. 469 * 470 * @since 2.9.0 471 * 472 * @link https://developer.wordpress.org/reference/functions/delete_comment_meta/ 473 * 474 * @param int $comment_id Comment ID. 475 * @param string $meta_key Metadata name. 476 * @param mixed $meta_value Optional. Metadata value. If provided, 477 * rows will only be removed that match the value. 478 * Must be serializable if non-scalar. Default empty. 479 * @return bool True on success, false on failure. 480 */ 481 function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) { 482 return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); 483 } 484 485 /** 486 * Retrieve comment meta field for a comment. 487 * 488 * @since 2.9.0 489 * 490 * @link https://developer.wordpress.org/reference/functions/get_comment_meta/ 491 * 492 * @param int $comment_id Comment ID. 493 * @param string $key Optional. The meta key to retrieve. By default, 494 * returns data for all keys. 495 * @param bool $single Optional. Whether to return a single value. 496 * This parameter has no effect if `$key` is not specified. 497 * Default false. 498 * @return mixed An array of values if `$single` is false. 499 * The value of meta data field if `$single` is true. 500 * False for an invalid `$comment_id` (non-numeric, zero, or negative value). 501 * An empty string if a valid but non-existing comment ID is passed. 502 */ 503 function get_comment_meta( $comment_id, $key = '', $single = false ) { 504 return get_metadata( 'comment', $comment_id, $key, $single ); 505 } 506 507 /** 508 * Update comment meta field based on comment ID. 509 * 510 * Use the $prev_value parameter to differentiate between meta fields with the 511 * same key and comment ID. 512 * 513 * If the meta field for the comment does not exist, it will be added. 514 * 515 * @since 2.9.0 516 * 517 * @link https://developer.wordpress.org/reference/functions/update_comment_meta/ 518 * 519 * @param int $comment_id Comment ID. 520 * @param string $meta_key Metadata key. 521 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 522 * @param mixed $prev_value Optional. Previous value to check before updating. 523 * If specified, only update existing metadata entries with 524 * this value. Otherwise, update all entries. Default empty. 525 * @return int|bool Meta ID if the key didn't exist, true on successful update, 526 * false on failure or if the value passed to the function 527 * is the same as the one that is already in the database. 528 */ 529 function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) { 530 return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); 531 } 532 533 /** 534 * Queues comments for metadata lazy-loading. 535 * 536 * @since 4.5.0 537 * 538 * @param WP_Comment[] $comments Array of comment objects. 539 */ 540 function wp_queue_comments_for_comment_meta_lazyload( $comments ) { 541 // Don't use `wp_list_pluck()` to avoid by-reference manipulation. 542 $comment_ids = array(); 543 if ( is_array( $comments ) ) { 544 foreach ( $comments as $comment ) { 545 if ( $comment instanceof WP_Comment ) { 546 $comment_ids[] = $comment->comment_ID; 547 } 548 } 549 } 550 551 if ( $comment_ids ) { 552 $lazyloader = wp_metadata_lazyloader(); 553 $lazyloader->queue_objects( 'comment', $comment_ids ); 554 } 555 } 556 557 /** 558 * Sets the cookies used to store an unauthenticated commentator's identity. Typically used 559 * to recall previous comments by this commentator that are still held in moderation. 560 * 561 * @since 3.4.0 562 * @since 4.9.6 The `$cookies_consent` parameter was added. 563 * 564 * @param WP_Comment $comment Comment object. 565 * @param WP_User $user Comment author's user object. The user may not exist. 566 * @param bool $cookies_consent Optional. Comment author's consent to store cookies. Default true. 567 */ 568 function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) { 569 // If the user already exists, or the user opted out of cookies, don't set cookies. 570 if ( $user->exists() ) { 571 return; 572 } 573 574 if ( false === $cookies_consent ) { 575 // Remove any existing cookies. 576 $past = time() - YEAR_IN_SECONDS; 577 setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN ); 578 setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN ); 579 setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN ); 580 581 return; 582 } 583 584 /** 585 * Filters the lifetime of the comment cookie in seconds. 586 * 587 * @since 2.8.0 588 * 589 * @param int $seconds Comment cookie lifetime. Default 30000000. 590 */ 591 $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 ); 592 593 $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); 594 595 setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); 596 setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); 597 setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); 598 } 599 600 /** 601 * Sanitizes the cookies sent to the user already. 602 * 603 * Will only do anything if the cookies have already been created for the user. 604 * Mostly used after cookies had been sent to use elsewhere. 605 * 606 * @since 2.0.4 607 */ 608 function sanitize_comment_cookies() { 609 if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) { 610 /** 611 * Filters the comment author's name cookie before it is set. 612 * 613 * When this filter hook is evaluated in wp_filter_comment(), 614 * the comment author's name string is passed. 615 * 616 * @since 1.5.0 617 * 618 * @param string $author_cookie The comment author name cookie. 619 */ 620 $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE[ 'comment_author_' . COOKIEHASH ] ); 621 $comment_author = wp_unslash( $comment_author ); 622 $comment_author = esc_attr( $comment_author ); 623 624 $_COOKIE[ 'comment_author_' . COOKIEHASH ] = $comment_author; 625 } 626 627 if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { 628 /** 629 * Filters the comment author's email cookie before it is set. 630 * 631 * When this filter hook is evaluated in wp_filter_comment(), 632 * the comment author's email string is passed. 633 * 634 * @since 1.5.0 635 * 636 * @param string $author_email_cookie The comment author email cookie. 637 */ 638 $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ); 639 $comment_author_email = wp_unslash( $comment_author_email ); 640 $comment_author_email = esc_attr( $comment_author_email ); 641 642 $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] = $comment_author_email; 643 } 644 645 if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) { 646 /** 647 * Filters the comment author's URL cookie before it is set. 648 * 649 * When this filter hook is evaluated in wp_filter_comment(), 650 * the comment author's URL string is passed. 651 * 652 * @since 1.5.0 653 * 654 * @param string $author_url_cookie The comment author URL cookie. 655 */ 656 $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ); 657 $comment_author_url = wp_unslash( $comment_author_url ); 658 659 $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] = $comment_author_url; 660 } 661 } 662 663 /** 664 * Validates whether this comment is allowed to be made. 665 * 666 * @since 2.0.0 667 * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function 668 * to return a WP_Error object instead of dying. 669 * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. 670 * 671 * @global wpdb $wpdb WordPress database abstraction object. 672 * 673 * @param array $commentdata Contains information on the comment. 674 * @param bool $wp_error When true, a disallowed comment will result in the function 675 * returning a WP_Error object, rather than executing wp_die(). 676 * Default false. 677 * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam'|'trash'). 678 * If `$wp_error` is true, disallowed comments return a WP_Error. 679 */ 680 function wp_allow_comment( $commentdata, $wp_error = false ) { 681 global $wpdb; 682 683 // Simple duplicate check. 684 // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) 685 $dupe = $wpdb->prepare( 686 "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", 687 wp_unslash( $commentdata['comment_post_ID'] ), 688 wp_unslash( $commentdata['comment_parent'] ), 689 wp_unslash( $commentdata['comment_author'] ) 690 ); 691 if ( $commentdata['comment_author_email'] ) { 692 $dupe .= $wpdb->prepare( 693 'AND comment_author_email = %s ', 694 wp_unslash( $commentdata['comment_author_email'] ) 695 ); 696 } 697 $dupe .= $wpdb->prepare( 698 ') AND comment_content = %s LIMIT 1', 699 wp_unslash( $commentdata['comment_content'] ) 700 ); 701 702 $dupe_id = $wpdb->get_var( $dupe ); 703 704 /** 705 * Filters the ID, if any, of the duplicate comment found when creating a new comment. 706 * 707 * Return an empty value from this filter to allow what WP considers a duplicate comment. 708 * 709 * @since 4.4.0 710 * 711 * @param int $dupe_id ID of the comment identified as a duplicate. 712 * @param array $commentdata Data for the comment being created. 713 */ 714 $dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata ); 715 716 if ( $dupe_id ) { 717 /** 718 * Fires immediately after a duplicate comment is detected. 719 * 720 * @since 3.0.0 721 * 722 * @param array $commentdata Comment data. 723 */ 724 do_action( 'comment_duplicate_trigger', $commentdata ); 725 726 /** 727 * Filters duplicate comment error message. 728 * 729 * @since 5.2.0 730 * 731 * @param string $comment_duplicate_message Duplicate comment error message. 732 */ 733 $comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ) ); 734 735 if ( $wp_error ) { 736 return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 ); 737 } else { 738 if ( wp_doing_ajax() ) { 739 die( $comment_duplicate_message ); 740 } 741 742 wp_die( $comment_duplicate_message, 409 ); 743 } 744 } 745 746 /** 747 * Fires immediately before a comment is marked approved. 748 * 749 * Allows checking for comment flooding. 750 * 751 * @since 2.3.0 752 * @since 4.7.0 The `$avoid_die` parameter was added. 753 * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. 754 * 755 * @param string $comment_author_IP Comment author's IP address. 756 * @param string $comment_author_email Comment author's email. 757 * @param string $comment_date_gmt GMT date the comment was posted. 758 * @param bool $wp_error Whether to return a WP_Error object instead of executing 759 * wp_die() or die() if a comment flood is occurring. 760 */ 761 do_action( 762 'check_comment_flood', 763 $commentdata['comment_author_IP'], 764 $commentdata['comment_author_email'], 765 $commentdata['comment_date_gmt'], 766 $wp_error 767 ); 768 769 /** 770 * Filters whether a comment is part of a comment flood. 771 * 772 * The default check is wp_check_comment_flood(). See check_comment_flood_db(). 773 * 774 * @since 4.7.0 775 * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. 776 * 777 * @param bool $is_flood Is a comment flooding occurring? Default false. 778 * @param string $comment_author_IP Comment author's IP address. 779 * @param string $comment_author_email Comment author's email. 780 * @param string $comment_date_gmt GMT date the comment was posted. 781 * @param bool $wp_error Whether to return a WP_Error object instead of executing 782 * wp_die() or die() if a comment flood is occurring. 783 */ 784 $is_flood = apply_filters( 785 'wp_is_comment_flood', 786 false, 787 $commentdata['comment_author_IP'], 788 $commentdata['comment_author_email'], 789 $commentdata['comment_date_gmt'], 790 $wp_error 791 ); 792 793 if ( $is_flood ) { 794 /** This filter is documented in wp-includes/comment-template.php */ 795 $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); 796 797 return new WP_Error( 'comment_flood', $comment_flood_message, 429 ); 798 } 799 800 if ( ! empty( $commentdata['user_id'] ) ) { 801 $user = get_userdata( $commentdata['user_id'] ); 802 $post_author = $wpdb->get_var( 803 $wpdb->prepare( 804 "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", 805 $commentdata['comment_post_ID'] 806 ) 807 ); 808 } 809 810 if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { 811 // The author and the admins get respect. 812 $approved = 1; 813 } else { 814 // Everyone else's comments will be checked. 815 if ( check_comment( 816 $commentdata['comment_author'], 817 $commentdata['comment_author_email'], 818 $commentdata['comment_author_url'], 819 $commentdata['comment_content'], 820 $commentdata['comment_author_IP'], 821 $commentdata['comment_agent'], 822 $commentdata['comment_type'] 823 ) ) { 824 $approved = 1; 825 } else { 826 $approved = 0; 827 } 828 829 if ( wp_check_comment_disallowed_list( 830 $commentdata['comment_author'], 831 $commentdata['comment_author_email'], 832 $commentdata['comment_author_url'], 833 $commentdata['comment_content'], 834 $commentdata['comment_author_IP'], 835 $commentdata['comment_agent'] 836 ) ) { 837 $approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam'; 838 } 839 } 840 841 /** 842 * Filters a comment's approval status before it is set. 843 * 844 * @since 2.1.0 845 * @since 4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion 846 * and allow skipping further processing. 847 * 848 * @param int|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam', 'trash', 849 * or WP_Error. 850 * @param array $commentdata Comment data. 851 */ 852 return apply_filters( 'pre_comment_approved', $approved, $commentdata ); 853 } 854 855 /** 856 * Hooks WP's native database-based comment-flood check. 857 * 858 * This wrapper maintains backward compatibility with plugins that expect to 859 * be able to unhook the legacy check_comment_flood_db() function from 860 * 'check_comment_flood' using remove_action(). 861 * 862 * @since 2.3.0 863 * @since 4.7.0 Converted to be an add_filter() wrapper. 864 */ 865 function check_comment_flood_db() { 866 add_filter( 'wp_is_comment_flood', 'wp_check_comment_flood', 10, 5 ); 867 } 868 869 /** 870 * Checks whether comment flooding is occurring. 871 * 872 * Won't run, if current user can manage options, so to not block 873 * administrators. 874 * 875 * @since 4.7.0 876 * 877 * @global wpdb $wpdb WordPress database abstraction object. 878 * 879 * @param bool $is_flood Is a comment flooding occurring? 880 * @param string $ip Comment author's IP address. 881 * @param string $email Comment author's email address. 882 * @param string $date MySQL time string. 883 * @param bool $avoid_die When true, a disallowed comment will result in the function 884 * returning without executing wp_die() or die(). Default false. 885 * @return bool Whether comment flooding is occurring. 886 */ 887 function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) { 888 889 global $wpdb; 890 891 // Another callback has declared a flood. Trust it. 892 if ( true === $is_flood ) { 893 return $is_flood; 894 } 895 896 // Don't throttle admins or moderators. 897 if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) { 898 return false; 899 } 900 901 $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); 902 903 if ( is_user_logged_in() ) { 904 $user = get_current_user_id(); 905 $check_column = '`user_id`'; 906 } else { 907 $user = $ip; 908 $check_column = '`comment_author_IP`'; 909 } 910 911 $sql = $wpdb->prepare( 912 "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", 913 $hour_ago, 914 $user, 915 $email 916 ); 917 918 $lasttime = $wpdb->get_var( $sql ); 919 920 if ( $lasttime ) { 921 $time_lastcomment = mysql2date( 'U', $lasttime, false ); 922 $time_newcomment = mysql2date( 'U', $date, false ); 923 924 /** 925 * Filters the comment flood status. 926 * 927 * @since 2.1.0 928 * 929 * @param bool $bool Whether a comment flood is occurring. Default false. 930 * @param int $time_lastcomment Timestamp of when the last comment was posted. 931 * @param int $time_newcomment Timestamp of when the new comment was posted. 932 */ 933 $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment ); 934 935 if ( $flood_die ) { 936 /** 937 * Fires before the comment flood message is triggered. 938 * 939 * @since 1.5.0 940 * 941 * @param int $time_lastcomment Timestamp of when the last comment was posted. 942 * @param int $time_newcomment Timestamp of when the new comment was posted. 943 */ 944 do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); 945 946 if ( $avoid_die ) { 947 return true; 948 } else { 949 /** 950 * Filters the comment flood error message. 951 * 952 * @since 5.2.0 953 * 954 * @param string $comment_flood_message Comment flood error message. 955 */ 956 $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); 957 958 if ( wp_doing_ajax() ) { 959 die( $comment_flood_message ); 960 } 961 962 wp_die( $comment_flood_message, 429 ); 963 } 964 } 965 } 966 967 return false; 968 } 969 970 /** 971 * Separates an array of comments into an array keyed by comment_type. 972 * 973 * @since 2.7.0 974 * 975 * @param WP_Comment[] $comments Array of comments 976 * @return WP_Comment[] Array of comments keyed by comment_type. 977 */ 978 function separate_comments( &$comments ) { 979 $comments_by_type = array( 980 'comment' => array(), 981 'trackback' => array(), 982 'pingback' => array(), 983 'pings' => array(), 984 ); 985 986 $count = count( $comments ); 987 988 for ( $i = 0; $i < $count; $i++ ) { 989 $type = $comments[ $i ]->comment_type; 990 991 if ( empty( $type ) ) { 992 $type = 'comment'; 993 } 994 995 $comments_by_type[ $type ][] = &$comments[ $i ]; 996 997 if ( 'trackback' === $type || 'pingback' === $type ) { 998 $comments_by_type['pings'][] = &$comments[ $i ]; 999 } 1000 } 1001 1002 return $comments_by_type; 1003 } 1004 1005 /** 1006 * Calculate the total number of comment pages. 1007 * 1008 * @since 2.7.0 1009 * 1010 * @uses Walker_Comment 1011 * 1012 * @global WP_Query $wp_query WordPress Query object. 1013 * 1014 * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to `$wp_query->comments`. 1015 * @param int $per_page Optional. Comments per page. 1016 * @param bool $threaded Optional. Control over flat or threaded comments. 1017 * @return int Number of comment pages. 1018 */ 1019 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { 1020 global $wp_query; 1021 1022 if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) { 1023 return $wp_query->max_num_comment_pages; 1024 } 1025 1026 if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) { 1027 $comments = $wp_query->comments; 1028 } 1029 1030 if ( empty( $comments ) ) { 1031 return 0; 1032 } 1033 1034 if ( ! get_option( 'page_comments' ) ) { 1035 return 1; 1036 } 1037 1038 if ( ! isset( $per_page ) ) { 1039 $per_page = (int) get_query_var( 'comments_per_page' ); 1040 } 1041 if ( 0 === $per_page ) { 1042 $per_page = (int) get_option( 'comments_per_page' ); 1043 } 1044 if ( 0 === $per_page ) { 1045 return 1; 1046 } 1047 1048 if ( ! isset( $threaded ) ) { 1049 $threaded = get_option( 'thread_comments' ); 1050 } 1051 1052 if ( $threaded ) { 1053 $walker = new Walker_Comment; 1054 $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); 1055 } else { 1056 $count = ceil( count( $comments ) / $per_page ); 1057 } 1058 1059 return $count; 1060 } 1061 1062 /** 1063 * Calculate what page number a comment will appear on for comment paging. 1064 * 1065 * @since 2.7.0 1066 * 1067 * @global wpdb $wpdb WordPress database abstraction object. 1068 * 1069 * @param int $comment_ID Comment ID. 1070 * @param array $args { 1071 * Array of optional arguments. 1072 * 1073 * @type string $type Limit paginated comments to those matching a given type. 1074 * Accepts 'comment', 'trackback', 'pingback', 'pings' 1075 * (trackbacks and pingbacks), or 'all'. Default 'all'. 1076 * @type int $per_page Per-page count to use when calculating pagination. 1077 * Defaults to the value of the 'comments_per_page' option. 1078 * @type int|string $max_depth If greater than 1, comment page will be determined 1079 * for the top-level parent `$comment_ID`. 1080 * Defaults to the value of the 'thread_comments_depth' option. 1081 * } * 1082 * @return int|null Comment page number or null on error. 1083 */ 1084 function get_page_of_comment( $comment_ID, $args = array() ) { 1085 global $wpdb; 1086 1087 $page = null; 1088 1089 $comment = get_comment( $comment_ID ); 1090 if ( ! $comment ) { 1091 return; 1092 } 1093 1094 $defaults = array( 1095 'type' => 'all', 1096 'page' => '', 1097 'per_page' => '', 1098 'max_depth' => '', 1099 ); 1100 $args = wp_parse_args( $args, $defaults ); 1101 $original_args = $args; 1102 1103 // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option. 1104 if ( get_option( 'page_comments' ) ) { 1105 if ( '' === $args['per_page'] ) { 1106 $args['per_page'] = get_query_var( 'comments_per_page' ); 1107 } 1108 1109 if ( '' === $args['per_page'] ) { 1110 $args['per_page'] = get_option( 'comments_per_page' ); 1111 } 1112 } 1113 1114 if ( empty( $args['per_page'] ) ) { 1115 $args['per_page'] = 0; 1116 $args['page'] = 0; 1117 } 1118 1119 if ( $args['per_page'] < 1 ) { 1120 $page = 1; 1121 } 1122 1123 if ( null === $page ) { 1124 if ( '' === $args['max_depth'] ) { 1125 if ( get_option( 'thread_comments' ) ) { 1126 $args['max_depth'] = get_option( 'thread_comments_depth' ); 1127 } else { 1128 $args['max_depth'] = -1; 1129 } 1130 } 1131 1132 // Find this comment's top-level parent if threading is enabled. 1133 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) { 1134 return get_page_of_comment( $comment->comment_parent, $args ); 1135 } 1136 1137 $comment_args = array( 1138 'type' => $args['type'], 1139 'post_id' => $comment->comment_post_ID, 1140 'fields' => 'ids', 1141 'count' => true, 1142 'status' => 'approve', 1143 'parent' => 0, 1144 'date_query' => array( 1145 array( 1146 'column' => "$wpdb->comments.comment_date_gmt", 1147 'before' => $comment->comment_date_gmt, 1148 ), 1149 ), 1150 ); 1151 1152 if ( is_user_logged_in() ) { 1153 $comment_args['include_unapproved'] = array( get_current_user_id() ); 1154 } else { 1155 $unapproved_email = wp_get_unapproved_comment_author_email(); 1156 1157 if ( $unapproved_email ) { 1158 $comment_args['include_unapproved'] = array( $unapproved_email ); 1159 } 1160 } 1161 1162 /** 1163 * Filters the arguments used to query comments in get_page_of_comment(). 1164 * 1165 * @since 5.5.0 1166 * 1167 * @see WP_Comment_Query::__construct() 1168 * 1169 * @param array $comment_args { 1170 * Array of WP_Comment_Query arguments. 1171 * 1172 * @type string $type Limit paginated comments to those matching a given type. 1173 * Accepts 'comment', 'trackback', 'pingback', 'pings' 1174 * (trackbacks and pingbacks), or 'all'. Default 'all'. 1175 * @type int $post_id ID of the post. 1176 * @type string $fields Comment fields to return. 1177 * @type bool $count Whether to return a comment count (true) or array 1178 * of comment objects (false). 1179 * @type string $status Comment status. 1180 * @type int $parent Parent ID of comment to retrieve children of. 1181 * @type array $date_query Date query clauses to limit comments by. See WP_Date_Query. 1182 * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments 1183 * will be included in paginated comments. 1184 * } 1185 */ 1186 $comment_args = apply_filters( 'get_page_of_comment_query_args', $comment_args ); 1187 1188 $comment_query = new WP_Comment_Query(); 1189 $older_comment_count = $comment_query->query( $comment_args ); 1190 1191 // No older comments? Then it's page #1. 1192 if ( 0 == $older_comment_count ) { 1193 $page = 1; 1194 1195 // Divide comments older than this one by comments per page to get this comment's page number. 1196 } else { 1197 $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] ); 1198 } 1199 } 1200 1201 /** 1202 * Filters the calculated page on which a comment appears. 1203 * 1204 * @since 4.4.0 1205 * @since 4.7.0 Introduced the `$comment_ID` parameter. 1206 * 1207 * @param int $page Comment page. 1208 * @param array $args { 1209 * Arguments used to calculate pagination. These include arguments auto-detected by the function, 1210 * based on query vars, system settings, etc. For pristine arguments passed to the function, 1211 * see `$original_args`. 1212 * 1213 * @type string $type Type of comments to count. 1214 * @type int $page Calculated current page. 1215 * @type int $per_page Calculated number of comments per page. 1216 * @type int $max_depth Maximum comment threading depth allowed. 1217 * } 1218 * @param array $original_args { 1219 * Array of arguments passed to the function. Some or all of these may not be set. 1220 * 1221 * @type string $type Type of comments to count. 1222 * @type int $page Current comment page. 1223 * @type int $per_page Number of comments per page. 1224 * @type int $max_depth Maximum comment threading depth allowed. 1225 * } 1226 * @param int $comment_ID ID of the comment. 1227 */ 1228 return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args, $comment_ID ); 1229 } 1230 1231 /** 1232 * Retrieves the maximum character lengths for the comment form fields. 1233 * 1234 * @since 4.5.0 1235 * 1236 * @global wpdb $wpdb WordPress database abstraction object. 1237 * 1238 * @return int[] Array of maximum lengths keyed by field name. 1239 */ 1240 function wp_get_comment_fields_max_lengths() { 1241 global $wpdb; 1242 1243 $lengths = array( 1244 'comment_author' => 245, 1245 'comment_author_email' => 100, 1246 'comment_author_url' => 200, 1247 'comment_content' => 65525, 1248 ); 1249 1250 if ( $wpdb->is_mysql ) { 1251 foreach ( $lengths as $column => $length ) { 1252 $col_length = $wpdb->get_col_length( $wpdb->comments, $column ); 1253 $max_length = 0; 1254 1255 // No point if we can't get the DB column lengths. 1256 if ( is_wp_error( $col_length ) ) { 1257 break; 1258 } 1259 1260 if ( ! is_array( $col_length ) && (int) $col_length > 0 ) { 1261 $max_length = (int) $col_length; 1262 } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && (int) $col_length['length'] > 0 ) { 1263 $max_length = (int) $col_length['length']; 1264 1265 if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) { 1266 $max_length = $max_length - 10; 1267 } 1268 } 1269 1270 if ( $max_length > 0 ) { 1271 $lengths[ $column ] = $max_length; 1272 } 1273 } 1274 } 1275 1276 /** 1277 * Filters the lengths for the comment form fields. 1278 * 1279 * @since 4.5.0 1280 * 1281 * @param int[] $lengths Array of maximum lengths keyed by field name. 1282 */ 1283 return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths ); 1284 } 1285 1286 /** 1287 * Compares the lengths of comment data against the maximum character limits. 1288 * 1289 * @since 4.7.0 1290 * 1291 * @param array $comment_data Array of arguments for inserting a comment. 1292 * @return WP_Error|true WP_Error when a comment field exceeds the limit, 1293 * otherwise true. 1294 */ 1295 function wp_check_comment_data_max_lengths( $comment_data ) { 1296 $max_lengths = wp_get_comment_fields_max_lengths(); 1297 1298 if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) { 1299 return new WP_Error( 'comment_author_column_length', __( '<strong>Error</strong>: Your name is too long.' ), 200 ); 1300 } 1301 1302 if ( isset( $comment_data['comment_author_email'] ) && strlen( $comment_data['comment_author_email'] ) > $max_lengths['comment_author_email'] ) { 1303 return new WP_Error( 'comment_author_email_column_length', __( '<strong>Error</strong>: Your email address is too long.' ), 200 ); 1304 } 1305 1306 if ( isset( $comment_data['comment_author_url'] ) && strlen( $comment_data['comment_author_url'] ) > $max_lengths['comment_author_url'] ) { 1307 return new WP_Error( 'comment_author_url_column_length', __( '<strong>Error</strong>: Your URL is too long.' ), 200 ); 1308 } 1309 1310 if ( isset( $comment_data['comment_content'] ) && mb_strlen( $comment_data['comment_content'], '8bit' ) > $max_lengths['comment_content'] ) { 1311 return new WP_Error( 'comment_content_column_length', __( '<strong>Error</strong>: Your comment is too long.' ), 200 ); 1312 } 1313 1314 return true; 1315 } 1316 1317 /** 1318 * Checks if a comment contains disallowed characters or words. 1319 * 1320 * @since 5.5.0 1321 * 1322 * @param string $author The author of the comment 1323 * @param string $email The email of the comment 1324 * @param string $url The url used in the comment 1325 * @param string $comment The comment content 1326 * @param string $user_ip The comment author's IP address 1327 * @param string $user_agent The author's browser user agent 1328 * @return bool True if comment contains disallowed content, false if comment does not 1329 */ 1330 function wp_check_comment_disallowed_list( $author, $email, $url, $comment, $user_ip, $user_agent ) { 1331 /** 1332 * Fires before the comment is tested for disallowed characters or words. 1333 * 1334 * @since 1.5.0 1335 * @deprecated 5.5.0 Use {@see 'wp_check_comment_disallowed_list'} instead. 1336 * 1337 * @param string $author Comment author. 1338 * @param string $email Comment author's email. 1339 * @param string $url Comment author's URL. 1340 * @param string $comment Comment content. 1341 * @param string $user_ip Comment author's IP address. 1342 * @param string $user_agent Comment author's browser user agent. 1343 */ 1344 do_action_deprecated( 1345 'wp_blacklist_check', 1346 array( $author, $email, $url, $comment, $user_ip, $user_agent ), 1347 '5.5.0', 1348 'wp_check_comment_disallowed_list', 1349 __( 'Please consider writing more inclusive code.' ) 1350 ); 1351 1352 /** 1353 * Fires before the comment is tested for disallowed characters or words. 1354 * 1355 * @since 5.5.0 1356 * 1357 * @param string $author Comment author. 1358 * @param string $email Comment author's email. 1359 * @param string $url Comment author's URL. 1360 * @param string $comment Comment content. 1361 * @param string $user_ip Comment author's IP address. 1362 * @param string $user_agent Comment author's browser user agent. 1363 */ 1364 do_action( 'wp_check_comment_disallowed_list', $author, $email, $url, $comment, $user_ip, $user_agent ); 1365 1366 $mod_keys = trim( get_option( 'disallowed_keys' ) ); 1367 if ( '' === $mod_keys ) { 1368 return false; // If moderation keys are empty. 1369 } 1370 1371 // Ensure HTML tags are not being used to bypass the list of disallowed characters and words. 1372 $comment_without_html = wp_strip_all_tags( $comment ); 1373 1374 $words = explode( "\n", $mod_keys ); 1375 1376 foreach ( (array) $words as $word ) { 1377 $word = trim( $word ); 1378 1379 // Skip empty lines. 1380 if ( empty( $word ) ) { 1381 continue; } 1382 1383 // Do some escaping magic so that '#' chars 1384 // in the spam words don't break things: 1385 $word = preg_quote( $word, '#' ); 1386 1387 $pattern = "#$word#i"; 1388 if ( preg_match( $pattern, $author ) 1389 || preg_match( $pattern, $email ) 1390 || preg_match( $pattern, $url ) 1391 || preg_match( $pattern, $comment ) 1392 || preg_match( $pattern, $comment_without_html ) 1393 || preg_match( $pattern, $user_ip ) 1394 || preg_match( $pattern, $user_agent ) 1395 ) { 1396 return true; 1397 } 1398 } 1399 return false; 1400 } 1401 1402 /** 1403 * Retrieves the total comment counts for the whole site or a single post. 1404 * 1405 * The comment stats are cached and then retrieved, if they already exist in the 1406 * cache. 1407 * 1408 * @see get_comment_count() Which handles fetching the live comment counts. 1409 * 1410 * @since 2.5.0 1411 * 1412 * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that 1413 * comment counts for the whole site will be retrieved. 1414 * @return stdClass { 1415 * The number of comments keyed by their status. 1416 * 1417 * @type int $approved The number of approved comments. 1418 * @type int $moderated The number of comments awaiting moderation (a.k.a. pending). 1419 * @type int $spam The number of spam comments. 1420 * @type int $trash The number of trashed comments. 1421 * @type int $post-trashed The number of comments for posts that are in the trash. 1422 * @type int $total_comments The total number of non-trashed comments, including spam. 1423 * @type int $all The total number of pending or approved comments. 1424 * } 1425 */ 1426 function wp_count_comments( $post_id = 0 ) { 1427 $post_id = (int) $post_id; 1428 1429 /** 1430 * Filters the comments count for a given post or the whole site. 1431 * 1432 * @since 2.7.0 1433 * 1434 * @param array|stdClass $count An empty array or an object containing comment counts. 1435 * @param int $post_id The post ID. Can be 0 to represent the whole site. 1436 */ 1437 $filtered = apply_filters( 'wp_count_comments', array(), $post_id ); 1438 if ( ! empty( $filtered ) ) { 1439 return $filtered; 1440 } 1441 1442 $count = wp_cache_get( "comments-{$post_id}", 'counts' ); 1443 if ( false !== $count ) { 1444 return $count; 1445 } 1446 1447 $stats = get_comment_count( $post_id ); 1448 $stats['moderated'] = $stats['awaiting_moderation']; 1449 unset( $stats['awaiting_moderation'] ); 1450 1451 $stats_object = (object) $stats; 1452 wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' ); 1453 1454 return $stats_object; 1455 } 1456 1457 /** 1458 * Trashes or deletes a comment. 1459 * 1460 * The comment is moved to Trash instead of permanently deleted unless Trash is 1461 * disabled, item is already in the Trash, or $force_delete is true. 1462 * 1463 * The post comment count will be updated if the comment was approved and has a 1464 * post ID available. 1465 * 1466 * @since 2.0.0 1467 * 1468 * @global wpdb $wpdb WordPress database abstraction object. 1469 * 1470 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1471 * @param bool $force_delete Whether to bypass Trash and force deletion. Default false. 1472 * @return bool True on success, false on failure. 1473 */ 1474 function wp_delete_comment( $comment_id, $force_delete = false ) { 1475 global $wpdb; 1476 $comment = get_comment( $comment_id ); 1477 if ( ! $comment ) { 1478 return false; 1479 } 1480 1481 if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ), true ) ) { 1482 return wp_trash_comment( $comment_id ); 1483 } 1484 1485 /** 1486 * Fires immediately before a comment is deleted from the database. 1487 * 1488 * @since 1.2.0 1489 * @since 4.9.0 Added the `$comment` parameter. 1490 * 1491 * @param int $comment_id The comment ID. 1492 * @param WP_Comment $comment The comment to be deleted. 1493 */ 1494 do_action( 'delete_comment', $comment->comment_ID, $comment ); 1495 1496 // Move children up a level. 1497 $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) ); 1498 if ( ! empty( $children ) ) { 1499 $wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) ); 1500 clean_comment_cache( $children ); 1501 } 1502 1503 // Delete metadata. 1504 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); 1505 foreach ( $meta_ids as $mid ) { 1506 delete_metadata_by_mid( 'comment', $mid ); 1507 } 1508 1509 if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) { 1510 return false; 1511 } 1512 1513 /** 1514 * Fires immediately after a comment is deleted from the database. 1515 * 1516 * @since 2.9.0 1517 * @since 4.9.0 Added the `$comment` parameter. 1518 * 1519 * @param int $comment_id The comment ID. 1520 * @param WP_Comment $comment The deleted comment. 1521 */ 1522 do_action( 'deleted_comment', $comment->comment_ID, $comment ); 1523 1524 $post_id = $comment->comment_post_ID; 1525 if ( $post_id && 1 == $comment->comment_approved ) { 1526 wp_update_comment_count( $post_id ); 1527 } 1528 1529 clean_comment_cache( $comment->comment_ID ); 1530 1531 /** This action is documented in wp-includes/comment.php */ 1532 do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' ); 1533 1534 wp_transition_comment_status( 'delete', $comment->comment_approved, $comment ); 1535 1536 return true; 1537 } 1538 1539 /** 1540 * Moves a comment to the Trash 1541 * 1542 * If Trash is disabled, comment is permanently deleted. 1543 * 1544 * @since 2.9.0 1545 * 1546 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1547 * @return bool True on success, false on failure. 1548 */ 1549 function wp_trash_comment( $comment_id ) { 1550 if ( ! EMPTY_TRASH_DAYS ) { 1551 return wp_delete_comment( $comment_id, true ); 1552 } 1553 1554 $comment = get_comment( $comment_id ); 1555 if ( ! $comment ) { 1556 return false; 1557 } 1558 1559 /** 1560 * Fires immediately before a comment is sent to the Trash. 1561 * 1562 * @since 2.9.0 1563 * @since 4.9.0 Added the `$comment` parameter. 1564 * 1565 * @param int $comment_id The comment ID. 1566 * @param WP_Comment $comment The comment to be trashed. 1567 */ 1568 do_action( 'trash_comment', $comment->comment_ID, $comment ); 1569 1570 if ( wp_set_comment_status( $comment, 'trash' ) ) { 1571 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); 1572 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); 1573 add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved ); 1574 add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() ); 1575 1576 /** 1577 * Fires immediately after a comment is sent to Trash. 1578 * 1579 * @since 2.9.0 1580 * @since 4.9.0 Added the `$comment` parameter. 1581 * 1582 * @param int $comment_id The comment ID. 1583 * @param WP_Comment $comment The trashed comment. 1584 */ 1585 do_action( 'trashed_comment', $comment->comment_ID, $comment ); 1586 1587 return true; 1588 } 1589 1590 return false; 1591 } 1592 1593 /** 1594 * Removes a comment from the Trash 1595 * 1596 * @since 2.9.0 1597 * 1598 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1599 * @return bool True on success, false on failure. 1600 */ 1601 function wp_untrash_comment( $comment_id ) { 1602 $comment = get_comment( $comment_id ); 1603 if ( ! $comment ) { 1604 return false; 1605 } 1606 1607 /** 1608 * Fires immediately before a comment is restored from the Trash. 1609 * 1610 * @since 2.9.0 1611 * @since 4.9.0 Added the `$comment` parameter. 1612 * 1613 * @param int $comment_id The comment ID. 1614 * @param WP_Comment $comment The comment to be untrashed. 1615 */ 1616 do_action( 'untrash_comment', $comment->comment_ID, $comment ); 1617 1618 $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ); 1619 if ( empty( $status ) ) { 1620 $status = '0'; 1621 } 1622 1623 if ( wp_set_comment_status( $comment, $status ) ) { 1624 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); 1625 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); 1626 1627 /** 1628 * Fires immediately after a comment is restored from the Trash. 1629 * 1630 * @since 2.9.0 1631 * @since 4.9.0 Added the `$comment` parameter. 1632 * 1633 * @param int $comment_id The comment ID. 1634 * @param WP_Comment $comment The untrashed comment. 1635 */ 1636 do_action( 'untrashed_comment', $comment->comment_ID, $comment ); 1637 1638 return true; 1639 } 1640 1641 return false; 1642 } 1643 1644 /** 1645 * Marks a comment as Spam 1646 * 1647 * @since 2.9.0 1648 * 1649 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1650 * @return bool True on success, false on failure. 1651 */ 1652 function wp_spam_comment( $comment_id ) { 1653 $comment = get_comment( $comment_id ); 1654 if ( ! $comment ) { 1655 return false; 1656 } 1657 1658 /** 1659 * Fires immediately before a comment is marked as Spam. 1660 * 1661 * @since 2.9.0 1662 * @since 4.9.0 Added the `$comment` parameter. 1663 * 1664 * @param int $comment_id The comment ID. 1665 * @param WP_Comment $comment The comment to be marked as spam. 1666 */ 1667 do_action( 'spam_comment', $comment->comment_ID, $comment ); 1668 1669 if ( wp_set_comment_status( $comment, 'spam' ) ) { 1670 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); 1671 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); 1672 add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved ); 1673 add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() ); 1674 1675 /** 1676 * Fires immediately after a comment is marked as Spam. 1677 * 1678 * @since 2.9.0 1679 * @since 4.9.0 Added the `$comment` parameter. 1680 * 1681 * @param int $comment_id The comment ID. 1682 * @param WP_Comment $comment The comment marked as spam. 1683 */ 1684 do_action( 'spammed_comment', $comment->comment_ID, $comment ); 1685 1686 return true; 1687 } 1688 1689 return false; 1690 } 1691 1692 /** 1693 * Removes a comment from the Spam 1694 * 1695 * @since 2.9.0 1696 * 1697 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1698 * @return bool True on success, false on failure. 1699 */ 1700 function wp_unspam_comment( $comment_id ) { 1701 $comment = get_comment( $comment_id ); 1702 if ( ! $comment ) { 1703 return false; 1704 } 1705 1706 /** 1707 * Fires immediately before a comment is unmarked as Spam. 1708 * 1709 * @since 2.9.0 1710 * @since 4.9.0 Added the `$comment` parameter. 1711 * 1712 * @param int $comment_id The comment ID. 1713 * @param WP_Comment $comment The comment to be unmarked as spam. 1714 */ 1715 do_action( 'unspam_comment', $comment->comment_ID, $comment ); 1716 1717 $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ); 1718 if ( empty( $status ) ) { 1719 $status = '0'; 1720 } 1721 1722 if ( wp_set_comment_status( $comment, $status ) ) { 1723 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); 1724 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); 1725 1726 /** 1727 * Fires immediately after a comment is unmarked as Spam. 1728 * 1729 * @since 2.9.0 1730 * @since 4.9.0 Added the `$comment` parameter. 1731 * 1732 * @param int $comment_id The comment ID. 1733 * @param WP_Comment $comment The comment unmarked as spam. 1734 */ 1735 do_action( 'unspammed_comment', $comment->comment_ID, $comment ); 1736 1737 return true; 1738 } 1739 1740 return false; 1741 } 1742 1743 /** 1744 * The status of a comment by ID. 1745 * 1746 * @since 1.0.0 1747 * 1748 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object 1749 * @return string|false Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure. 1750 */ 1751 function wp_get_comment_status( $comment_id ) { 1752 $comment = get_comment( $comment_id ); 1753 if ( ! $comment ) { 1754 return false; 1755 } 1756 1757 $approved = $comment->comment_approved; 1758 1759 if ( null == $approved ) { 1760 return false; 1761 } elseif ( '1' == $approved ) { 1762 return 'approved'; 1763 } elseif ( '0' == $approved ) { 1764 return 'unapproved'; 1765 } elseif ( 'spam' === $approved ) { 1766 return 'spam'; 1767 } elseif ( 'trash' === $approved ) { 1768 return 'trash'; 1769 } else { 1770 return false; 1771 } 1772 } 1773 1774 /** 1775 * Call hooks for when a comment status transition occurs. 1776 * 1777 * Calls hooks for comment status transitions. If the new comment status is not the same 1778 * as the previous comment status, then two hooks will be ran, the first is 1779 * {@see 'transition_comment_status'} with new status, old status, and comment data. 1780 * The next action called is {@see 'comment_$old_status_to_$new_status'}. It has 1781 * the comment data. 1782 * 1783 * The final action will run whether or not the comment statuses are the same. 1784 * The action is named {@see 'comment_$new_status_$comment->comment_type'}. 1785 * 1786 * @since 2.7.0 1787 * 1788 * @param string $new_status New comment status. 1789 * @param string $old_status Previous comment status. 1790 * @param WP_Comment $comment Comment object. 1791 */ 1792 function wp_transition_comment_status( $new_status, $old_status, $comment ) { 1793 /* 1794 * Translate raw statuses to human-readable formats for the hooks. 1795 * This is not a complete list of comment status, it's only the ones 1796 * that need to be renamed. 1797 */ 1798 $comment_statuses = array( 1799 0 => 'unapproved', 1800 'hold' => 'unapproved', // wp_set_comment_status() uses "hold". 1801 1 => 'approved', 1802 'approve' => 'approved', // wp_set_comment_status() uses "approve". 1803 ); 1804 if ( isset( $comment_statuses[ $new_status ] ) ) { 1805 $new_status = $comment_statuses[ $new_status ]; 1806 } 1807 if ( isset( $comment_statuses[ $old_status ] ) ) { 1808 $old_status = $comment_statuses[ $old_status ]; 1809 } 1810 1811 // Call the hooks. 1812 if ( $new_status != $old_status ) { 1813 /** 1814 * Fires when the comment status is in transition. 1815 * 1816 * @since 2.7.0 1817 * 1818 * @param int|string $new_status The new comment status. 1819 * @param int|string $old_status The old comment status. 1820 * @param WP_Comment $comment Comment object. 1821 */ 1822 do_action( 'transition_comment_status', $new_status, $old_status, $comment ); 1823 /** 1824 * Fires when the comment status is in transition from one specific status to another. 1825 * 1826 * The dynamic portions of the hook name, `$old_status`, and `$new_status`, 1827 * refer to the old and new comment statuses, respectively. 1828 * 1829 * @since 2.7.0 1830 * 1831 * @param WP_Comment $comment Comment object. 1832 */ 1833 do_action( "comment_{$old_status}_to_{$new_status}", $comment ); 1834 } 1835 /** 1836 * Fires when the status of a specific comment type is in transition. 1837 * 1838 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`, 1839 * refer to the new comment status, and the type of comment, respectively. 1840 * 1841 * Typical comment types include an empty string (standard comment), 'pingback', 1842 * or 'trackback'. 1843 * 1844 * @since 2.7.0 1845 * 1846 * @param int $comment_ID The comment ID. 1847 * @param WP_Comment $comment Comment object. 1848 */ 1849 do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment ); 1850 } 1851 1852 /** 1853 * Clear the lastcommentmodified cached value when a comment status is changed. 1854 * 1855 * Deletes the lastcommentmodified cache key when a comment enters or leaves 1856 * 'approved' status. 1857 * 1858 * @since 4.7.0 1859 * @access private 1860 * 1861 * @param string $new_status The new comment status. 1862 * @param string $old_status The old comment status. 1863 */ 1864 function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) { 1865 if ( 'approved' === $new_status || 'approved' === $old_status ) { 1866 foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 1867 wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); 1868 } 1869 } 1870 } 1871 1872 /** 1873 * Get current commenter's name, email, and URL. 1874 * 1875 * Expects cookies content to already be sanitized. User of this function might 1876 * wish to recheck the returned array for validity. 1877 * 1878 * @see sanitize_comment_cookies() Use to sanitize cookies 1879 * 1880 * @since 2.0.4 1881 * 1882 * @return array { 1883 * An array of current commenter variables. 1884 * 1885 * @type string $comment_author The name of the current commenter, or an empty string. 1886 * @type string $comment_author_email The email address of the current commenter, or an empty string. 1887 * @type string $comment_author_url The URL address of the current commenter, or an empty string. 1888 * } 1889 */ 1890 function wp_get_current_commenter() { 1891 // Cookies should already be sanitized. 1892 1893 $comment_author = ''; 1894 if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) { 1895 $comment_author = $_COOKIE[ 'comment_author_' . COOKIEHASH ]; 1896 } 1897 1898 $comment_author_email = ''; 1899 if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { 1900 $comment_author_email = $_COOKIE[ 'comment_author_email_' . COOKIEHASH ]; 1901 } 1902 1903 $comment_author_url = ''; 1904 if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) { 1905 $comment_author_url = $_COOKIE[ 'comment_author_url_' . COOKIEHASH ]; 1906 } 1907 1908 /** 1909 * Filters the current commenter's name, email, and URL. 1910 * 1911 * @since 3.1.0 1912 * 1913 * @param array $comment_author_data { 1914 * An array of current commenter variables. 1915 * 1916 * @type string $comment_author The name of the current commenter, or an empty string. 1917 * @type string $comment_author_email The email address of the current commenter, or an empty string. 1918 * @type string $comment_author_url The URL address of the current commenter, or an empty string. 1919 * } 1920 */ 1921 return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) ); 1922 } 1923 1924 /** 1925 * Get unapproved comment author's email. 1926 * 1927 * Used to allow the commenter to see their pending comment. 1928 * 1929 * @since 5.1.0 1930 * @since 5.7.0 The window within which the author email for an unapproved comment 1931 * can be retrieved was extended to 10 minutes. 1932 * 1933 * @return string The unapproved comment author's email (when supplied). 1934 */ 1935 function wp_get_unapproved_comment_author_email() { 1936 $commenter_email = ''; 1937 1938 if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) { 1939 $comment_id = (int) $_GET['unapproved']; 1940 $comment = get_comment( $comment_id ); 1941 1942 if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) { 1943 // The comment will only be viewable by the comment author for 10 minutes. 1944 $comment_preview_expires = strtotime( $comment->comment_date_gmt . '+10 minutes' ); 1945 1946 if ( time() < $comment_preview_expires ) { 1947 $commenter_email = $comment->comment_author_email; 1948 } 1949 } 1950 } 1951 1952 if ( ! $commenter_email ) { 1953 $commenter = wp_get_current_commenter(); 1954 $commenter_email = $commenter['comment_author_email']; 1955 } 1956 1957 return $commenter_email; 1958 } 1959 1960 /** 1961 * Inserts a comment into the database. 1962 * 1963 * @since 2.0.0 1964 * @since 4.4.0 Introduced the `$comment_meta` argument. 1965 * @since 5.5.0 Default value for `$comment_type` argument changed to `comment`. 1966 * 1967 * @global wpdb $wpdb WordPress database abstraction object. 1968 * 1969 * @param array $commentdata { 1970 * Array of arguments for inserting a new comment. 1971 * 1972 * @type string $comment_agent The HTTP user agent of the `$comment_author` when 1973 * the comment was submitted. Default empty. 1974 * @type int|string $comment_approved Whether the comment has been approved. Default 1. 1975 * @type string $comment_author The name of the author of the comment. Default empty. 1976 * @type string $comment_author_email The email address of the `$comment_author`. Default empty. 1977 * @type string $comment_author_IP The IP address of the `$comment_author`. Default empty. 1978 * @type string $comment_author_url The URL address of the `$comment_author`. Default empty. 1979 * @type string $comment_content The content of the comment. Default empty. 1980 * @type string $comment_date The date the comment was submitted. To set the date 1981 * manually, `$comment_date_gmt` must also be specified. 1982 * Default is the current time. 1983 * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone. 1984 * Default is `$comment_date` in the site's GMT timezone. 1985 * @type int $comment_karma The karma of the comment. Default 0. 1986 * @type int $comment_parent ID of this comment's parent, if any. Default 0. 1987 * @type int $comment_post_ID ID of the post that relates to the comment, if any. 1988 * Default 0. 1989 * @type string $comment_type Comment type. Default 'comment'. 1990 * @type array $comment_meta Optional. Array of key/value pairs to be stored in commentmeta for the 1991 * new comment. 1992 * @type int $user_id ID of the user who submitted the comment. Default 0. 1993 * } 1994 * @return int|false The new comment's ID on success, false on failure. 1995 */ 1996 function wp_insert_comment( $commentdata ) { 1997 global $wpdb; 1998 $data = wp_unslash( $commentdata ); 1999 2000 $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author']; 2001 $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email']; 2002 $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url']; 2003 $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP']; 2004 2005 $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date']; 2006 $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt']; 2007 2008 $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID']; 2009 $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content']; 2010 $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; 2011 $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; 2012 $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; 2013 $comment_type = empty( $data['comment_type'] ) ? 'comment' : $data['comment_type']; 2014 $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; 2015 2016 $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; 2017 2018 $compacted = 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' ); 2019 if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) { 2020 return false; 2021 } 2022 2023 $id = (int) $wpdb->insert_id; 2024 2025 if ( 1 == $comment_approved ) { 2026 wp_update_comment_count( $comment_post_ID ); 2027 2028 foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 2029 wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); 2030 } 2031 } 2032 2033 clean_comment_cache( $id ); 2034 2035 $comment = get_comment( $id ); 2036 2037 // If metadata is provided, store it. 2038 if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) { 2039 foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) { 2040 add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true ); 2041 } 2042 } 2043 2044 /** 2045 * Fires immediately after a comment is inserted into the database. 2046 * 2047 * @since 2.8.0 2048 * 2049 * @param int $id The comment ID. 2050 * @param WP_Comment $comment Comment object. 2051 */ 2052 do_action( 'wp_insert_comment', $id, $comment ); 2053 2054 return $id; 2055 } 2056 2057 /** 2058 * Filters and sanitizes comment data. 2059 * 2060 * Sets the comment data 'filtered' field to true when finished. This can be 2061 * checked as to whether the comment should be filtered and to keep from 2062 * filtering the same comment more than once. 2063 * 2064 * @since 2.0.0 2065 * 2066 * @param array $commentdata Contains information on the comment. 2067 * @return array Parsed comment information. 2068 */ 2069 function wp_filter_comment( $commentdata ) { 2070 if ( isset( $commentdata['user_ID'] ) ) { 2071 /** 2072 * Filters the comment author's user ID before it is set. 2073 * 2074 * The first time this filter is evaluated, 'user_ID' is checked 2075 * (for back-compat), followed by the standard 'user_id' value. 2076 * 2077 * @since 1.5.0 2078 * 2079 * @param int $user_ID The comment author's user ID. 2080 */ 2081 $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] ); 2082 } elseif ( isset( $commentdata['user_id'] ) ) { 2083 /** This filter is documented in wp-includes/comment.php */ 2084 $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] ); 2085 } 2086 2087 /** 2088 * Filters the comment author's browser user agent before it is set. 2089 * 2090 * @since 1.5.0 2091 * 2092 * @param string $comment_agent The comment author's browser user agent. 2093 */ 2094 $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) ); 2095 /** This filter is documented in wp-includes/comment.php */ 2096 $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] ); 2097 /** 2098 * Filters the comment content before it is set. 2099 * 2100 * @since 1.5.0 2101 * 2102 * @param string $comment_content The comment content. 2103 */ 2104 $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] ); 2105 /** 2106 * Filters the comment author's IP address before it is set. 2107 * 2108 * @since 1.5.0 2109 * 2110 * @param string $comment_author_ip The comment author's IP address. 2111 */ 2112 $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] ); 2113 /** This filter is documented in wp-includes/comment.php */ 2114 $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] ); 2115 /** This filter is documented in wp-includes/comment.php */ 2116 $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] ); 2117 $commentdata['filtered'] = true; 2118 return $commentdata; 2119 } 2120 2121 /** 2122 * Whether a comment should be blocked because of comment flood. 2123 * 2124 * @since 2.1.0 2125 * 2126 * @param bool $block Whether plugin has already blocked comment. 2127 * @param int $time_lastcomment Timestamp for last comment. 2128 * @param int $time_newcomment Timestamp for new comment. 2129 * @return bool Whether comment should be blocked. 2130 */ 2131 function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment ) { 2132 if ( $block ) { // A plugin has already blocked... we'll let that decision stand. 2133 return $block; 2134 } 2135 if ( ( $time_newcomment - $time_lastcomment ) < 15 ) { 2136 return true; 2137 } 2138 return false; 2139 } 2140 2141 /** 2142 * Adds a new comment to the database. 2143 * 2144 * Filters new comment to ensure that the fields are sanitized and valid before 2145 * inserting comment into database. Calls {@see 'comment_post'} action with comment ID 2146 * and whether comment is approved by WordPress. Also has {@see 'preprocess_comment'} 2147 * filter for processing the comment data before the function handles it. 2148 * 2149 * We use `REMOTE_ADDR` here directly. If you are behind a proxy, you should ensure 2150 * that it is properly set, such as in wp-config.php, for your environment. 2151 * 2152 * See {@link https://core.trac.wordpress.org/ticket/9235} 2153 * 2154 * @since 1.5.0 2155 * @since 4.3.0 Introduced the `comment_agent` and `comment_author_IP` arguments. 2156 * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function 2157 * to return a WP_Error object instead of dying. 2158 * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. 2159 * @since 5.5.0 Introduced the `comment_type` argument. 2160 * 2161 * @see wp_insert_comment() 2162 * @global wpdb $wpdb WordPress database abstraction object. 2163 * 2164 * @param array $commentdata { 2165 * Comment data. 2166 * 2167 * @type string $comment_author The name of the comment author. 2168 * @type string $comment_author_email The comment author email address. 2169 * @type string $comment_author_url The comment author URL. 2170 * @type string $comment_content The content of the comment. 2171 * @type string $comment_date The date the comment was submitted. Default is the current time. 2172 * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone. 2173 * Default is `$comment_date` in the GMT timezone. 2174 * @type string $comment_type Comment type. Default 'comment'. 2175 * @type int $comment_parent The ID of this comment's parent, if any. Default 0. 2176 * @type int $comment_post_ID The ID of the post that relates to the comment. 2177 * @type int $user_id The ID of the user who submitted the comment. Default 0. 2178 * @type int $user_ID Kept for backward-compatibility. Use `$user_id` instead. 2179 * @type string $comment_agent Comment author user agent. Default is the value of 'HTTP_USER_AGENT' 2180 * in the `$_SERVER` superglobal sent in the original request. 2181 * @type string $comment_author_IP Comment author IP address in IPv4 format. Default is the value of 2182 * 'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request. 2183 * } 2184 * @param bool $wp_error Should errors be returned as WP_Error objects instead of 2185 * executing wp_die()? Default false. 2186 * @return int|false|WP_Error The ID of the comment on success, false or WP_Error on failure. 2187 */ 2188 function wp_new_comment( $commentdata, $wp_error = false ) { 2189 global $wpdb; 2190 2191 if ( isset( $commentdata['user_ID'] ) ) { 2192 $commentdata['user_ID'] = (int) $commentdata['user_ID']; 2193 $commentdata['user_id'] = $commentdata['user_ID']; 2194 } 2195 2196 $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0; 2197 2198 if ( ! isset( $commentdata['comment_author_IP'] ) ) { 2199 $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; 2200 } 2201 2202 if ( ! isset( $commentdata['comment_agent'] ) ) { 2203 $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; 2204 } 2205 2206 /** 2207 * Filters a comment's data before it is sanitized and inserted into the database. 2208 * 2209 * @since 1.5.0 2210 * @since 5.6.0 Comment data includes the `comment_agent` and `comment_author_IP` values. 2211 * 2212 * @param array $commentdata Comment data. 2213 */ 2214 $commentdata = apply_filters( 'preprocess_comment', $commentdata ); 2215 2216 $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; 2217 if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) { 2218 $commentdata['user_ID'] = (int) $commentdata['user_ID']; 2219 $commentdata['user_id'] = $commentdata['user_ID']; 2220 } elseif ( isset( $commentdata['user_id'] ) ) { 2221 $commentdata['user_id'] = (int) $commentdata['user_id']; 2222 } 2223 2224 $commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0; 2225 2226 $parent_status = ( $commentdata['comment_parent'] > 0 ) ? wp_get_comment_status( $commentdata['comment_parent'] ) : ''; 2227 2228 $commentdata['comment_parent'] = ( 'approved' === $parent_status || 'unapproved' === $parent_status ) ? $commentdata['comment_parent'] : 0; 2229 2230 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); 2231 2232 $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); 2233 2234 if ( empty( $commentdata['comment_date'] ) ) { 2235 $commentdata['comment_date'] = current_time( 'mysql' ); 2236 } 2237 2238 if ( empty( $commentdata['comment_date_gmt'] ) ) { 2239 $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 ); 2240 } 2241 2242 if ( empty( $commentdata['comment_type'] ) ) { 2243 $commentdata['comment_type'] = 'comment'; 2244 } 2245 2246 $commentdata = wp_filter_comment( $commentdata ); 2247 2248 $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error ); 2249 if ( is_wp_error( $commentdata['comment_approved'] ) ) { 2250 return $commentdata['comment_approved']; 2251 } 2252 2253 $comment_ID = wp_insert_comment( $commentdata ); 2254 if ( ! $comment_ID ) { 2255 $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' ); 2256 2257 foreach ( $fields as $field ) { 2258 if ( isset( $commentdata[ $field ] ) ) { 2259 $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] ); 2260 } 2261 } 2262 2263 $commentdata = wp_filter_comment( $commentdata ); 2264 2265 $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error ); 2266 if ( is_wp_error( $commentdata['comment_approved'] ) ) { 2267 return $commentdata['comment_approved']; 2268 } 2269 2270 $comment_ID = wp_insert_comment( $commentdata ); 2271 if ( ! $comment_ID ) { 2272 return false; 2273 } 2274 } 2275 2276 /** 2277 * Fires immediately after a comment is inserted into the database. 2278 * 2279 * @since 1.2.0 2280 * @since 4.5.0 The `$commentdata` parameter was added. 2281 * 2282 * @param int $comment_ID The comment ID. 2283 * @param int|string $comment_approved 1 if the comment is approved, 0 if not, 'spam' if spam. 2284 * @param array $commentdata Comment data. 2285 */ 2286 do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'], $commentdata ); 2287 2288 return $comment_ID; 2289 } 2290 2291 /** 2292 * Send a comment moderation notification to the comment moderator. 2293 * 2294 * @since 4.4.0 2295 * 2296 * @param int $comment_ID ID of the comment. 2297 * @return bool True on success, false on failure. 2298 */ 2299 function wp_new_comment_notify_moderator( $comment_ID ) { 2300 $comment = get_comment( $comment_ID ); 2301 2302 // Only send notifications for pending comments. 2303 $maybe_notify = ( '0' == $comment->comment_approved ); 2304 2305 /** This filter is documented in wp-includes/comment.php */ 2306 $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_ID ); 2307 2308 if ( ! $maybe_notify ) { 2309 return false; 2310 } 2311 2312 return wp_notify_moderator( $comment_ID ); 2313 } 2314 2315 /** 2316 * Send a notification of a new comment to the post author. 2317 * 2318 * @since 4.4.0 2319 * 2320 * Uses the {@see 'notify_post_author'} filter to determine whether the post author 2321 * should be notified when a new comment is added, overriding site setting. 2322 * 2323 * @param int $comment_ID Comment ID. 2324 * @return bool True on success, false on failure. 2325 */ 2326 function wp_new_comment_notify_postauthor( $comment_ID ) { 2327 $comment = get_comment( $comment_ID ); 2328 2329 $maybe_notify = get_option( 'comments_notify' ); 2330 2331 /** 2332 * Filters whether to send the post author new comment notification emails, 2333 * overriding the site setting. 2334 * 2335 * @since 4.4.0 2336 * 2337 * @param bool $maybe_notify Whether to notify the post author about the new comment. 2338 * @param int $comment_ID The ID of the comment for the notification. 2339 */ 2340 $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID ); 2341 2342 /* 2343 * wp_notify_postauthor() checks if notifying the author of their own comment. 2344 * By default, it won't, but filters can override this. 2345 */ 2346 if ( ! $maybe_notify ) { 2347 return false; 2348 } 2349 2350 // Only send notifications for approved comments. 2351 if ( ! isset( $comment->comment_approved ) || '1' != $comment->comment_approved ) { 2352 return false; 2353 } 2354 2355 return wp_notify_postauthor( $comment_ID ); 2356 } 2357 2358 /** 2359 * Sets the status of a comment. 2360 * 2361 * The {@see 'wp_set_comment_status'} action is called after the comment is handled. 2362 * If the comment status is not in the list, then false is returned. 2363 * 2364 * @since 1.0.0 2365 * 2366 * @global wpdb $wpdb WordPress database abstraction object. 2367 * 2368 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 2369 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'. 2370 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default false. 2371 * @return bool|WP_Error True on success, false or WP_Error on failure. 2372 */ 2373 function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false ) { 2374 global $wpdb; 2375 2376 switch ( $comment_status ) { 2377 case 'hold': 2378 case '0': 2379 $status = '0'; 2380 break; 2381 case 'approve': 2382 case '1': 2383 $status = '1'; 2384 add_action( 'wp_set_comment_status', 'wp_new_comment_notify_postauthor' ); 2385 break; 2386 case 'spam': 2387 $status = 'spam'; 2388 break; 2389 case 'trash': 2390 $status = 'trash'; 2391 break; 2392 default: 2393 return false; 2394 } 2395 2396 $comment_old = clone get_comment( $comment_id ); 2397 2398 if ( ! $wpdb->update( $wpdb->comments, array( 'comment_approved' => $status ), array( 'comment_ID' => $comment_old->comment_ID ) ) ) { 2399 if ( $wp_error ) { 2400 return new WP_Error( 'db_update_error', __( 'Could not update comment status.' ), $wpdb->last_error ); 2401 } else { 2402 return false; 2403 } 2404 } 2405 2406 clean_comment_cache( $comment_old->comment_ID ); 2407 2408 $comment = get_comment( $comment_old->comment_ID ); 2409 2410 /** 2411 * Fires immediately after transitioning a comment's status from one to another in the database 2412 * and removing the comment from the object cache, but prior to all status transition hooks. 2413 * 2414 * @since 1.5.0 2415 * 2416 * @param int $comment_id Comment ID. 2417 * @param string $comment_status Current comment status. Possible values include 2418 * 'hold', '0', 'approve', '1', 'spam', and 'trash'. 2419 */ 2420 do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status ); 2421 2422 wp_transition_comment_status( $comment_status, $comment_old->comment_approved, $comment ); 2423 2424 wp_update_comment_count( $comment->comment_post_ID ); 2425 2426 return true; 2427 } 2428 2429 /** 2430 * Updates an existing comment in the database. 2431 * 2432 * Filters the comment and makes sure certain fields are valid before updating. 2433 * 2434 * @since 2.0.0 2435 * @since 4.9.0 Add updating comment meta during comment update. 2436 * @since 5.5.0 The `$wp_error` parameter was added. 2437 * @since 5.5.0 The return values for an invalid comment or post ID 2438 * were changed to false instead of 0. 2439 * 2440 * @global wpdb $wpdb WordPress database abstraction object. 2441 * 2442 * @param array $commentarr Contains information on the comment. 2443 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 2444 * @return int|false|WP_Error The value 1 if the comment was updated, 0 if not updated. 2445 * False or a WP_Error object on failure. 2446 */ 2447 function wp_update_comment( $commentarr, $wp_error = false ) { 2448 global $wpdb; 2449 2450 // First, get all of the original fields. 2451 $comment = get_comment( $commentarr['comment_ID'], ARRAY_A ); 2452 if ( empty( $comment ) ) { 2453 if ( $wp_error ) { 2454 return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) ); 2455 } else { 2456 return false; 2457 } 2458 } 2459 2460 // Make sure that the comment post ID is valid (if specified). 2461 if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { 2462 if ( $wp_error ) { 2463 return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) ); 2464 } else { 2465 return false; 2466 } 2467 } 2468 2469 // Escape data pulled from DB. 2470 $comment = wp_slash( $comment ); 2471 2472 $old_status = $comment['comment_approved']; 2473 2474 // Merge old and new fields with new fields overwriting old ones. 2475 $commentarr = array_merge( $comment, $commentarr ); 2476 2477 $commentarr = wp_filter_comment( $commentarr ); 2478 2479 // Now extract the merged array. 2480 $data = wp_unslash( $commentarr ); 2481 2482 /** 2483 * Filters the comment content before it is updated in the database. 2484 * 2485 * @since 1.5.0 2486 * 2487 * @param string $comment_content The comment data. 2488 */ 2489 $data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] ); 2490 2491 $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] ); 2492 2493 if ( ! isset( $data['comment_approved'] ) ) { 2494 $data['comment_approved'] = 1; 2495 } elseif ( 'hold' === $data['comment_approved'] ) { 2496 $data['comment_approved'] = 0; 2497 } elseif ( 'approve' === $data['comment_approved'] ) { 2498 $data['comment_approved'] = 1; 2499 } 2500 2501 $comment_ID = $data['comment_ID']; 2502 $comment_post_ID = $data['comment_post_ID']; 2503 2504 /** 2505 * Filters the comment data immediately before it is updated in the database. 2506 * 2507 * Note: data being passed to the filter is already unslashed. 2508 * 2509 * @since 4.7.0 2510 * @since 5.5.0 Returning a WP_Error value from the filter will short-circuit comment update 2511 * and allow skipping further processing. 2512 * 2513 * @param array|WP_Error $data The new, processed comment data, or WP_Error. 2514 * @param array $comment The old, unslashed comment data. 2515 * @param array $commentarr The new, raw comment data. 2516 */ 2517 $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr ); 2518 2519 // Do not carry on on failure. 2520 if ( is_wp_error( $data ) ) { 2521 if ( $wp_error ) { 2522 return $data; 2523 } else { 2524 return false; 2525 } 2526 } 2527 2528 $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id', 'comment_agent', 'comment_author_IP' ); 2529 $data = wp_array_slice_assoc( $data, $keys ); 2530 2531 $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); 2532 2533 if ( false === $rval ) { 2534 if ( $wp_error ) { 2535 return new WP_Error( 'db_update_error', __( 'Could not update comment in the database.' ), $wpdb->last_error ); 2536 } else { 2537 return false; 2538 } 2539 } 2540 2541 // If metadata is provided, store it. 2542 if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) { 2543 foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) { 2544 update_comment_meta( $comment_ID, $meta_key, $meta_value ); 2545 } 2546 } 2547 2548 clean_comment_cache( $comment_ID ); 2549 wp_update_comment_count( $comment_post_ID ); 2550 2551 /** 2552 * Fires immediately after a comment is updated in the database. 2553 * 2554 * The hook also fires immediately before comment status transition hooks are fired. 2555 * 2556 * @since 1.2.0 2557 * @since 4.6.0 Added the `$data` parameter. 2558 * 2559 * @param int $comment_ID The comment ID. 2560 * @param array $data Comment data. 2561 */ 2562 do_action( 'edit_comment', $comment_ID, $data ); 2563 2564 $comment = get_comment( $comment_ID ); 2565 2566 wp_transition_comment_status( $comment->comment_approved, $old_status, $comment ); 2567 2568 return $rval; 2569 } 2570 2571 /** 2572 * Whether to defer comment counting. 2573 * 2574 * When setting $defer to true, all post comment counts will not be updated 2575 * until $defer is set to false. When $defer is set to false, then all 2576 * previously deferred updated post comment counts will then be automatically 2577 * updated without having to call wp_update_comment_count() after. 2578 * 2579 * @since 2.5.0 2580 * 2581 * @param bool $defer 2582 * @return bool 2583 */ 2584 function wp_defer_comment_counting( $defer = null ) { 2585 static $_defer = false; 2586 2587 if ( is_bool( $defer ) ) { 2588 $_defer = $defer; 2589 // Flush any deferred counts. 2590 if ( ! $defer ) { 2591 wp_update_comment_count( null, true ); 2592 } 2593 } 2594 2595 return $_defer; 2596 } 2597 2598 /** 2599 * Updates the comment count for post(s). 2600 * 2601 * When $do_deferred is false (is by default) and the comments have been set to 2602 * be deferred, the post_id will be added to a queue, which will be updated at a 2603 * later date and only updated once per post ID. 2604 * 2605 * If the comments have not be set up to be deferred, then the post will be 2606 * updated. When $do_deferred is set to true, then all previous deferred post 2607 * IDs will be updated along with the current $post_id. 2608 * 2609 * @since 2.1.0 2610 * 2611 * @see wp_update_comment_count_now() For what could cause a false return value 2612 * 2613 * @param int|null $post_id Post ID. 2614 * @param bool $do_deferred Optional. Whether to process previously deferred 2615 * post comment counts. Default false. 2616 * @return bool|void True on success, false on failure or if post with ID does 2617 * not exist. 2618 */ 2619 function wp_update_comment_count( $post_id, $do_deferred = false ) { 2620 static $_deferred = array(); 2621 2622 if ( empty( $post_id ) && ! $do_deferred ) { 2623 return false; 2624 } 2625 2626 if ( $do_deferred ) { 2627 $_deferred = array_unique( $_deferred ); 2628 foreach ( $_deferred as $i => $_post_id ) { 2629 wp_update_comment_count_now( $_post_id ); 2630 unset( $_deferred[ $i ] ); 2631 /** @todo Move this outside of the foreach and reset $_deferred to an array instead */ 2632 } 2633 } 2634 2635 if ( wp_defer_comment_counting() ) { 2636 $_deferred[] = $post_id; 2637 return true; 2638 } elseif ( $post_id ) { 2639 return wp_update_comment_count_now( $post_id ); 2640 } 2641 2642 } 2643 2644 /** 2645 * Updates the comment count for the post. 2646 * 2647 * @since 2.5.0 2648 * 2649 * @global wpdb $wpdb WordPress database abstraction object. 2650 * 2651 * @param int $post_id Post ID 2652 * @return bool True on success, false if the post does not exist. 2653 */ 2654 function wp_update_comment_count_now( $post_id ) { 2655 global $wpdb; 2656 $post_id = (int) $post_id; 2657 if ( ! $post_id ) { 2658 return false; 2659 } 2660 2661 wp_cache_delete( 'comments-0', 'counts' ); 2662 wp_cache_delete( "comments-{$post_id}", 'counts' ); 2663 2664 $post = get_post( $post_id ); 2665 if ( ! $post ) { 2666 return false; 2667 } 2668 2669 $old = (int) $post->comment_count; 2670 2671 /** 2672 * Filters a post's comment count before it is updated in the database. 2673 * 2674 * @since 4.5.0 2675 * 2676 * @param int|null $new The new comment count. Default null. 2677 * @param int $old The old comment count. 2678 * @param int $post_id Post ID. 2679 */ 2680 $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id ); 2681 2682 if ( is_null( $new ) ) { 2683 $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) ); 2684 } else { 2685 $new = (int) $new; 2686 } 2687 2688 $wpdb->update( $wpdb->posts, array( 'comment_count' => $new ), array( 'ID' => $post_id ) ); 2689 2690 clean_post_cache( $post ); 2691 2692 /** 2693 * Fires immediately after a post's comment count is updated in the database. 2694 * 2695 * @since 2.3.0 2696 * 2697 * @param int $post_id Post ID. 2698 * @param int $new The new comment count. 2699 * @param int $old The old comment count. 2700 */ 2701 do_action( 'wp_update_comment_count', $post_id, $new, $old ); 2702 2703 /** This action is documented in wp-includes/post.php */ 2704 do_action( "edit_post_{$post->post_type}", $post_id, $post ); 2705 2706 /** This action is documented in wp-includes/post.php */ 2707 do_action( 'edit_post', $post_id, $post ); 2708 2709 return true; 2710 } 2711 2712 // 2713 // Ping and trackback functions. 2714 // 2715 2716 /** 2717 * Finds a pingback server URI based on the given URL. 2718 * 2719 * Checks the HTML for the rel="pingback" link and x-pingback headers. It does 2720 * a check for the x-pingback headers first and returns that, if available. The 2721 * check for the rel="pingback" has more overhead than just the header. 2722 * 2723 * @since 1.5.0 2724 * 2725 * @param string $url URL to ping. 2726 * @param string $deprecated Not Used. 2727 * @return string|false String containing URI on success, false on failure. 2728 */ 2729 function discover_pingback_server_uri( $url, $deprecated = '' ) { 2730 if ( ! empty( $deprecated ) ) { 2731 _deprecated_argument( __FUNCTION__, '2.7.0' ); 2732 } 2733 2734 $pingback_str_dquote = 'rel="pingback"'; 2735 $pingback_str_squote = 'rel=\'pingback\''; 2736 2737 /** @todo Should use Filter Extension or custom preg_match instead. */ 2738 $parsed_url = parse_url( $url ); 2739 2740 if ( ! isset( $parsed_url['host'] ) ) { // Not a URL. This should never happen. 2741 return false; 2742 } 2743 2744 // Do not search for a pingback server on our own uploads. 2745 $uploads_dir = wp_get_upload_dir(); 2746 if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) ) { 2747 return false; 2748 } 2749 2750 $response = wp_safe_remote_head( 2751 $url, 2752 array( 2753 'timeout' => 2, 2754 'httpversion' => '1.0', 2755 ) 2756 ); 2757 2758 if ( is_wp_error( $response ) ) { 2759 return false; 2760 } 2761 2762 if ( wp_remote_retrieve_header( $response, 'x-pingback' ) ) { 2763 return wp_remote_retrieve_header( $response, 'x-pingback' ); 2764 } 2765 2766 // Not an (x)html, sgml, or xml page, no use going further. 2767 if ( preg_match( '#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' ) ) ) { 2768 return false; 2769 } 2770 2771 // Now do a GET since we're going to look in the HTML headers (and we're sure it's not a binary file). 2772 $response = wp_safe_remote_get( 2773 $url, 2774 array( 2775 'timeout' => 2, 2776 'httpversion' => '1.0', 2777 ) 2778 ); 2779 2780 if ( is_wp_error( $response ) ) { 2781 return false; 2782 } 2783 2784 $contents = wp_remote_retrieve_body( $response ); 2785 2786 $pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote ); 2787 $pingback_link_offset_squote = strpos( $contents, $pingback_str_squote ); 2788 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { 2789 $quote = ( $pingback_link_offset_dquote ) ? '"' : '\''; 2790 $pingback_link_offset = ( '"' === $quote ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote; 2791 $pingback_href_pos = strpos( $contents, 'href=', $pingback_link_offset ); 2792 $pingback_href_start = $pingback_href_pos + 6; 2793 $pingback_href_end = strpos( $contents, $quote, $pingback_href_start ); 2794 $pingback_server_url_len = $pingback_href_end - $pingback_href_start; 2795 $pingback_server_url = substr( $contents, $pingback_href_start, $pingback_server_url_len ); 2796 2797 // We may find rel="pingback" but an incomplete pingback URL. 2798 if ( $pingback_server_url_len > 0 ) { // We got it! 2799 return $pingback_server_url; 2800 } 2801 } 2802 2803 return false; 2804 } 2805 2806 /** 2807 * Perform all pingbacks, enclosures, trackbacks, and send to pingback services. 2808 * 2809 * @since 2.1.0 2810 * @since 5.6.0 Introduced `do_all_pings` action hook for individual services. 2811 */ 2812 function do_all_pings() { 2813 /** 2814 * Fires immediately after the `do_pings` event to hook services individually. 2815 * 2816 * @since 5.6.0 2817 */ 2818 do_action( 'do_all_pings' ); 2819 } 2820 2821 /** 2822 * Perform all pingbacks. 2823 * 2824 * @since 5.6.0 2825 */ 2826 function do_all_pingbacks() { 2827 $pings = get_posts( 2828 array( 2829 'post_type' => get_post_types(), 2830 'suppress_filters' => false, 2831 'nopaging' => true, 2832 'meta_key' => '_pingme', 2833 'fields' => 'ids', 2834 ) 2835 ); 2836 2837 foreach ( $pings as $ping ) { 2838 delete_post_meta( $ping, '_pingme' ); 2839 pingback( null, $ping ); 2840 } 2841 } 2842 2843 /** 2844 * Perform all enclosures. 2845 * 2846 * @since 5.6.0 2847 */ 2848 function do_all_enclosures() { 2849 $enclosures = get_posts( 2850 array( 2851 'post_type' => get_post_types(), 2852 'suppress_filters' => false, 2853 'nopaging' => true, 2854 'meta_key' => '_encloseme', 2855 'fields' => 'ids', 2856 ) 2857 ); 2858 2859 foreach ( $enclosures as $enclosure ) { 2860 delete_post_meta( $enclosure, '_encloseme' ); 2861 do_enclose( null, $enclosure ); 2862 } 2863 } 2864 2865 /** 2866 * Perform all trackbacks. 2867 * 2868 * @since 5.6.0 2869 */ 2870 function do_all_trackbacks() { 2871 $trackbacks = get_posts( 2872 array( 2873 'post_type' => get_post_types(), 2874 'suppress_filters' => false, 2875 'nopaging' => true, 2876 'meta_key' => '_trackbackme', 2877 'fields' => 'ids', 2878 ) 2879 ); 2880 2881 foreach ( $trackbacks as $trackback ) { 2882 delete_post_meta( $trackback, '_trackbackme' ); 2883 do_trackbacks( $trackback ); 2884 } 2885 } 2886 2887 /** 2888 * Perform trackbacks. 2889 * 2890 * @since 1.5.0 2891 * @since 4.7.0 `$post_id` can be a WP_Post object. 2892 * 2893 * @global wpdb $wpdb WordPress database abstraction object. 2894 * 2895 * @param int|WP_Post $post_id Post object or ID to do trackbacks on. 2896 */ 2897 function do_trackbacks( $post_id ) { 2898 global $wpdb; 2899 $post = get_post( $post_id ); 2900 if ( ! $post ) { 2901 return false; 2902 } 2903 2904 $to_ping = get_to_ping( $post ); 2905 $pinged = get_pung( $post ); 2906 if ( empty( $to_ping ) ) { 2907 $wpdb->update( $wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) ); 2908 return; 2909 } 2910 2911 if ( empty( $post->post_excerpt ) ) { 2912 /** This filter is documented in wp-includes/post-template.php */ 2913 $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID ); 2914 } else { 2915 /** This filter is documented in wp-includes/post-template.php */ 2916 $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt ); 2917 } 2918 2919 $excerpt = str_replace( ']]>', ']]>', $excerpt ); 2920 $excerpt = wp_html_excerpt( $excerpt, 252, '…' ); 2921 2922 /** This filter is documented in wp-includes/post-template.php */ 2923 $post_title = apply_filters( 'the_title', $post->post_title, $post->ID ); 2924 $post_title = strip_tags( $post_title ); 2925 2926 if ( $to_ping ) { 2927 foreach ( (array) $to_ping as $tb_ping ) { 2928 $tb_ping = trim( $tb_ping ); 2929 if ( ! in_array( $tb_ping, $pinged, true ) ) { 2930 trackback( $tb_ping, $post_title, $excerpt, $post->ID ); 2931 $pinged[] = $tb_ping; 2932 } else { 2933 $wpdb->query( 2934 $wpdb->prepare( 2935 "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, 2936 '')) WHERE ID = %d", 2937 $tb_ping, 2938 $post->ID 2939 ) 2940 ); 2941 } 2942 } 2943 } 2944 } 2945 2946 /** 2947 * Sends pings to all of the ping site services. 2948 * 2949 * @since 1.2.0 2950 * 2951 * @param int $post_id Post ID. 2952 * @return int Same as Post ID from parameter 2953 */ 2954 function generic_ping( $post_id = 0 ) { 2955 $services = get_option( 'ping_sites' ); 2956 2957 $services = explode( "\n", $services ); 2958 foreach ( (array) $services as $service ) { 2959 $service = trim( $service ); 2960 if ( '' !== $service ) { 2961 weblog_ping( $service ); 2962 } 2963 } 2964 2965 return $post_id; 2966 } 2967 2968 /** 2969 * Pings back the links found in a post. 2970 * 2971 * @since 0.71 2972 * @since 4.7.0 `$post_id` can be a WP_Post object. 2973 * 2974 * @param string $content Post content to check for links. If empty will retrieve from post. 2975 * @param int|WP_Post $post_id Post Object or ID. 2976 */ 2977 function pingback( $content, $post_id ) { 2978 include_once ABSPATH . WPINC . '/class-IXR.php'; 2979 include_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php'; 2980 2981 // Original code by Mort (http://mort.mine.nu:8080). 2982 $post_links = array(); 2983 2984 $post = get_post( $post_id ); 2985 if ( ! $post ) { 2986 return; 2987 } 2988 2989 $pung = get_pung( $post ); 2990 2991 if ( empty( $content ) ) { 2992 $content = $post->post_content; 2993 } 2994 2995 /* 2996 * Step 1. 2997 * Parsing the post, external links (if any) are stored in the $post_links array. 2998 */ 2999 $post_links_temp = wp_extract_urls( $content ); 3000 3001 /* 3002 * Step 2. 3003 * Walking through the links array. 3004 * First we get rid of links pointing to sites, not to specific files. 3005 * Example: 3006 * http://dummy-weblog.org 3007 * http://dummy-weblog.org/ 3008 * http://dummy-weblog.org/post.php 3009 * We don't wanna ping first and second types, even if they have a valid <link/>. 3010 */ 3011 foreach ( (array) $post_links_temp as $link_test ) { 3012 // If we haven't pung it already and it isn't a link to itself. 3013 if ( ! in_array( $link_test, $pung, true ) && ( url_to_postid( $link_test ) != $post->ID ) 3014 // Also, let's never ping local attachments. 3015 && ! is_local_attachment( $link_test ) 3016 ) { 3017 $test = parse_url( $link_test ); 3018 if ( $test ) { 3019 if ( isset( $test['query'] ) ) { 3020 $post_links[] = $link_test; 3021 } elseif ( isset( $test['path'] ) && ( '/' !== $test['path'] ) && ( '' !== $test['path'] ) ) { 3022 $post_links[] = $link_test; 3023 } 3024 } 3025 } 3026 } 3027 3028 $post_links = array_unique( $post_links ); 3029 3030 /** 3031 * Fires just before pinging back links found in a post. 3032 * 3033 * @since 2.0.0 3034 * 3035 * @param string[] $post_links Array of link URLs to be checked (passed by reference). 3036 * @param string[] $pung Array of link URLs already pinged (passed by reference). 3037 * @param int $post_ID The post ID. 3038 */ 3039 do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) ); 3040 3041 foreach ( (array) $post_links as $pagelinkedto ) { 3042 $pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); 3043 3044 if ( $pingback_server_url ) { 3045 set_time_limit( 60 ); 3046 // Now, the RPC call. 3047 $pagelinkedfrom = get_permalink( $post ); 3048 3049 // Using a timeout of 3 seconds should be enough to cover slow servers. 3050 $client = new WP_HTTP_IXR_Client( $pingback_server_url ); 3051 $client->timeout = 3; 3052 /** 3053 * Filters the user agent sent when pinging-back a URL. 3054 * 3055 * @since 2.9.0 3056 * 3057 * @param string $concat_useragent The user agent concatenated with ' -- WordPress/' 3058 * and the WordPress version. 3059 * @param string $useragent The useragent. 3060 * @param string $pingback_server_url The server URL being linked to. 3061 * @param string $pagelinkedto URL of page linked to. 3062 * @param string $pagelinkedfrom URL of page linked from. 3063 */ 3064 $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . get_bloginfo( 'version' ), $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom ); 3065 // When set to true, this outputs debug messages by itself. 3066 $client->debug = false; 3067 3068 if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered. 3069 add_ping( $post, $pagelinkedto ); 3070 } 3071 } 3072 } 3073 } 3074 3075 /** 3076 * Check whether blog is public before returning sites. 3077 * 3078 * @since 2.1.0 3079 * 3080 * @param mixed $sites Will return if blog is public, will not return if not public. 3081 * @return mixed Empty string if blog is not public, returns $sites, if site is public. 3082 */ 3083 function privacy_ping_filter( $sites ) { 3084 if ( '0' != get_option( 'blog_public' ) ) { 3085 return $sites; 3086 } else { 3087 return ''; 3088 } 3089 } 3090 3091 /** 3092 * Send a Trackback. 3093 * 3094 * Updates database when sending trackback to prevent duplicates. 3095 * 3096 * @since 0.71 3097 * 3098 * @global wpdb $wpdb WordPress database abstraction object. 3099 * 3100 * @param string $trackback_url URL to send trackbacks. 3101 * @param string $title Title of post. 3102 * @param string $excerpt Excerpt of post. 3103 * @param int $ID Post ID. 3104 * @return int|false|void Database query from update. 3105 */ 3106 function trackback( $trackback_url, $title, $excerpt, $ID ) { 3107 global $wpdb; 3108 3109 if ( empty( $trackback_url ) ) { 3110 return; 3111 } 3112 3113 $options = array(); 3114 $options['timeout'] = 10; 3115 $options['body'] = array( 3116 'title' => $title, 3117 'url' => get_permalink( $ID ), 3118 'blog_name' => get_option( 'blogname' ), 3119 'excerpt' => $excerpt, 3120 ); 3121 3122 $response = wp_safe_remote_post( $trackback_url, $options ); 3123 3124 if ( is_wp_error( $response ) ) { 3125 return; 3126 } 3127 3128 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID ) ); 3129 return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID ) ); 3130 } 3131 3132 /** 3133 * Send a pingback. 3134 * 3135 * @since 1.2.0 3136 * 3137 * @param string $server Host of blog to connect to. 3138 * @param string $path Path to send the ping. 3139 */ 3140 function weblog_ping( $server = '', $path = '' ) { 3141 include_once ABSPATH . WPINC . '/class-IXR.php'; 3142 include_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php'; 3143 3144 // Using a timeout of 3 seconds should be enough to cover slow servers. 3145 $client = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' === $path ) ) ? false : $path ) ); 3146 $client->timeout = 3; 3147 $client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' ); 3148 3149 // When set to true, this outputs debug messages by itself. 3150 $client->debug = false; 3151 $home = trailingslashit( home_url() ); 3152 if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // Then try a normal ping. 3153 $client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home ); 3154 } 3155 } 3156 3157 /** 3158 * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI 3159 * 3160 * @since 3.5.1 3161 * 3162 * @see wp_http_validate_url() 3163 * 3164 * @param string $source_uri 3165 * @return string 3166 */ 3167 function pingback_ping_source_uri( $source_uri ) { 3168 return (string) wp_http_validate_url( $source_uri ); 3169 } 3170 3171 /** 3172 * Default filter attached to xmlrpc_pingback_error. 3173 * 3174 * Returns a generic pingback error code unless the error code is 48, 3175 * which reports that the pingback is already registered. 3176 * 3177 * @since 3.5.1 3178 * 3179 * @link https://www.hixie.ch/specs/pingback/pingback#TOC3 3180 * 3181 * @param IXR_Error $ixr_error 3182 * @return IXR_Error 3183 */ 3184 function xmlrpc_pingback_error( $ixr_error ) { 3185 if ( 48 === $ixr_error->code ) { 3186 return $ixr_error; 3187 } 3188 return new IXR_Error( 0, '' ); 3189 } 3190 3191 // 3192 // Cache. 3193 // 3194 3195 /** 3196 * Removes a comment from the object cache. 3197 * 3198 * @since 2.3.0 3199 * 3200 * @param int|array $ids Comment ID or an array of comment IDs to remove from cache. 3201 */ 3202 function clean_comment_cache( $ids ) { 3203 foreach ( (array) $ids as $id ) { 3204 wp_cache_delete( $id, 'comment' ); 3205 3206 /** 3207 * Fires immediately after a comment has been removed from the object cache. 3208 * 3209 * @since 4.5.0 3210 * 3211 * @param int $id Comment ID. 3212 */ 3213 do_action( 'clean_comment_cache', $id ); 3214 } 3215 3216 wp_cache_set( 'last_changed', microtime(), 'comment' ); 3217 } 3218 3219 /** 3220 * Updates the comment cache of given comments. 3221 * 3222 * Will add the comments in $comments to the cache. If comment ID already exists 3223 * in the comment cache then it will not be updated. The comment is added to the 3224 * cache using the comment group with the key using the ID of the comments. 3225 * 3226 * @since 2.3.0 3227 * @since 4.4.0 Introduced the `$update_meta_cache` parameter. 3228 * 3229 * @param WP_Comment[] $comments Array of comment objects 3230 * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. 3231 */ 3232 function update_comment_cache( $comments, $update_meta_cache = true ) { 3233 foreach ( (array) $comments as $comment ) { 3234 wp_cache_add( $comment->comment_ID, $comment, 'comment' ); 3235 } 3236 3237 if ( $update_meta_cache ) { 3238 // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. 3239 $comment_ids = array(); 3240 foreach ( $comments as $comment ) { 3241 $comment_ids[] = $comment->comment_ID; 3242 } 3243 update_meta_cache( 'comment', $comment_ids ); 3244 } 3245 } 3246 3247 /** 3248 * Adds any comments from the given IDs to the cache that do not already exist in cache. 3249 * 3250 * @since 4.4.0 3251 * @access private 3252 * 3253 * @see update_comment_cache() 3254 * @global wpdb $wpdb WordPress database abstraction object. 3255 * 3256 * @param int[] $comment_ids Array of comment IDs. 3257 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 3258 */ 3259 function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { 3260 global $wpdb; 3261 3262 $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' ); 3263 if ( ! empty( $non_cached_ids ) ) { 3264 $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); 3265 3266 update_comment_cache( $fresh_comments, $update_meta_cache ); 3267 } 3268 } 3269 3270 // 3271 // Internal. 3272 // 3273 3274 /** 3275 * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts. 3276 * 3277 * @since 2.7.0 3278 * @access private 3279 * 3280 * @param WP_Post $posts Post data object. 3281 * @param WP_Query $query Query object. 3282 * @return array 3283 */ 3284 function _close_comments_for_old_posts( $posts, $query ) { 3285 if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) { 3286 return $posts; 3287 } 3288 3289 /** 3290 * Filters the list of post types to automatically close comments for. 3291 * 3292 * @since 3.2.0 3293 * 3294 * @param string[] $post_types An array of post type names. 3295 */ 3296 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 3297 if ( ! in_array( $posts[0]->post_type, $post_types, true ) ) { 3298 return $posts; 3299 } 3300 3301 $days_old = (int) get_option( 'close_comments_days_old' ); 3302 if ( ! $days_old ) { 3303 return $posts; 3304 } 3305 3306 if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { 3307 $posts[0]->comment_status = 'closed'; 3308 $posts[0]->ping_status = 'closed'; 3309 } 3310 3311 return $posts; 3312 } 3313 3314 /** 3315 * Close comments on an old post. Hooked to comments_open and pings_open. 3316 * 3317 * @since 2.7.0 3318 * @access private 3319 * 3320 * @param bool $open Comments open or closed. 3321 * @param int $post_id Post ID. 3322 * @return bool $open 3323 */ 3324 function _close_comments_for_old_post( $open, $post_id ) { 3325 if ( ! $open ) { 3326 return $open; 3327 } 3328 3329 if ( ! get_option( 'close_comments_for_old_posts' ) ) { 3330 return $open; 3331 } 3332 3333 $days_old = (int) get_option( 'close_comments_days_old' ); 3334 if ( ! $days_old ) { 3335 return $open; 3336 } 3337 3338 $post = get_post( $post_id ); 3339 3340 /** This filter is documented in wp-includes/comment.php */ 3341 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 3342 if ( ! in_array( $post->post_type, $post_types, true ) ) { 3343 return $open; 3344 } 3345 3346 // Undated drafts should not show up as comments closed. 3347 if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { 3348 return $open; 3349 } 3350 3351 if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { 3352 return false; 3353 } 3354 3355 return $open; 3356 } 3357 3358 /** 3359 * Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form. 3360 * 3361 * This function expects unslashed data, as opposed to functions such as `wp_new_comment()` which 3362 * expect slashed data. 3363 * 3364 * @since 4.4.0 3365 * 3366 * @param array $comment_data { 3367 * Comment data. 3368 * 3369 * @type string|int $comment_post_ID The ID of the post that relates to the comment. 3370 * @type string $author The name of the comment author. 3371 * @type string $email The comment author email address. 3372 * @type string $url The comment author URL. 3373 * @type string $comment The content of the comment. 3374 * @type string|int $comment_parent The ID of this comment's parent, if any. Default 0. 3375 * @type string $_wp_unfiltered_html_comment The nonce value for allowing unfiltered HTML. 3376 * } 3377 * @return WP_Comment|WP_Error A WP_Comment object on success, a WP_Error object on failure. 3378 */ 3379 function wp_handle_comment_submission( $comment_data ) { 3380 3381 $comment_post_ID = 0; 3382 $comment_parent = 0; 3383 $user_ID = 0; 3384 $comment_author = null; 3385 $comment_author_email = null; 3386 $comment_author_url = null; 3387 $comment_content = null; 3388 3389 if ( isset( $comment_data['comment_post_ID'] ) ) { 3390 $comment_post_ID = (int) $comment_data['comment_post_ID']; 3391 } 3392 if ( isset( $comment_data['author'] ) && is_string( $comment_data['author'] ) ) { 3393 $comment_author = trim( strip_tags( $comment_data['author'] ) ); 3394 } 3395 if ( isset( $comment_data['email'] ) && is_string( $comment_data['email'] ) ) { 3396 $comment_author_email = trim( $comment_data['email'] ); 3397 } 3398 if ( isset( $comment_data['url'] ) && is_string( $comment_data['url'] ) ) { 3399 $comment_author_url = trim( $comment_data['url'] ); 3400 } 3401 if ( isset( $comment_data['comment'] ) && is_string( $comment_data['comment'] ) ) { 3402 $comment_content = trim( $comment_data['comment'] ); 3403 } 3404 if ( isset( $comment_data['comment_parent'] ) ) { 3405 $comment_parent = absint( $comment_data['comment_parent'] ); 3406 } 3407 3408 $post = get_post( $comment_post_ID ); 3409 3410 if ( empty( $post->comment_status ) ) { 3411 3412 /** 3413 * Fires when a comment is attempted on a post that does not exist. 3414 * 3415 * @since 1.5.0 3416 * 3417 * @param int $comment_post_ID Post ID. 3418 */ 3419 do_action( 'comment_id_not_found', $comment_post_ID ); 3420 3421 return new WP_Error( 'comment_id_not_found' ); 3422 3423 } 3424 3425 // get_post_status() will get the parent status for attachments. 3426 $status = get_post_status( $post ); 3427 3428 if ( ( 'private' === $status ) && ! current_user_can( 'read_post', $comment_post_ID ) ) { 3429 return new WP_Error( 'comment_id_not_found' ); 3430 } 3431 3432 $status_obj = get_post_status_object( $status ); 3433 3434 if ( ! comments_open( $comment_post_ID ) ) { 3435 3436 /** 3437 * Fires when a comment is attempted on a post that has comments closed. 3438 * 3439 * @since 1.5.0 3440 * 3441 * @param int $comment_post_ID Post ID. 3442 */ 3443 do_action( 'comment_closed', $comment_post_ID ); 3444 3445 return new WP_Error( 'comment_closed', __( 'Sorry, comments are closed for this item.' ), 403 ); 3446 3447 } elseif ( 'trash' === $status ) { 3448 3449 /** 3450 * Fires when a comment is attempted on a trashed post. 3451 * 3452 * @since 2.9.0 3453 * 3454 * @param int $comment_post_ID Post ID. 3455 */ 3456 do_action( 'comment_on_trash', $comment_post_ID ); 3457 3458 return new WP_Error( 'comment_on_trash' ); 3459 3460 } elseif ( ! $status_obj->public && ! $status_obj->private ) { 3461 3462 /** 3463 * Fires when a comment is attempted on a post in draft mode. 3464 * 3465 * @since 1.5.1 3466 * 3467 * @param int $comment_post_ID Post ID. 3468 */ 3469 do_action( 'comment_on_draft', $comment_post_ID ); 3470 3471 if ( current_user_can( 'read_post', $comment_post_ID ) ) { 3472 return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 ); 3473 } else { 3474 return new WP_Error( 'comment_on_draft' ); 3475 } 3476 } elseif ( post_password_required( $comment_post_ID ) ) { 3477 3478 /** 3479 * Fires when a comment is attempted on a password-protected post. 3480 * 3481 * @since 2.9.0 3482 * 3483 * @param int $comment_post_ID Post ID. 3484 */ 3485 do_action( 'comment_on_password_protected', $comment_post_ID ); 3486 3487 return new WP_Error( 'comment_on_password_protected' ); 3488 3489 } else { 3490 3491 /** 3492 * Fires before a comment is posted. 3493 * 3494 * @since 2.8.0 3495 * 3496 * @param int $comment_post_ID Post ID. 3497 */ 3498 do_action( 'pre_comment_on_post', $comment_post_ID ); 3499 3500 } 3501 3502 // If the user is logged in. 3503 $user = wp_get_current_user(); 3504 if ( $user->exists() ) { 3505 if ( empty( $user->display_name ) ) { 3506 $user->display_name = $user->user_login; 3507 } 3508 $comment_author = $user->display_name; 3509 $comment_author_email = $user->user_email; 3510 $comment_author_url = $user->user_url; 3511 $user_ID = $user->ID; 3512 if ( current_user_can( 'unfiltered_html' ) ) { 3513 if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] ) 3514 || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID ) 3515 ) { 3516 kses_remove_filters(); // Start with a clean slate. 3517 kses_init_filters(); // Set up the filters. 3518 remove_filter( 'pre_comment_content', 'wp_filter_post_kses' ); 3519 add_filter( 'pre_comment_content', 'wp_filter_kses' ); 3520 } 3521 } 3522 } else { 3523 if ( get_option( 'comment_registration' ) ) { 3524 return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to comment.' ), 403 ); 3525 } 3526 } 3527 3528 $comment_type = 'comment'; 3529 3530 if ( get_option( 'require_name_email' ) && ! $user->exists() ) { 3531 if ( '' == $comment_author_email || '' == $comment_author ) { 3532 return new WP_Error( 'require_name_email', __( '<strong>Error</strong>: Please fill the required fields (name, email).' ), 200 ); 3533 } elseif ( ! is_email( $comment_author_email ) ) { 3534 return new WP_Error( 'require_valid_email', __( '<strong>Error</strong>: Please enter a valid email address.' ), 200 ); 3535 } 3536 } 3537 3538 $commentdata = compact( 3539 'comment_post_ID', 3540 'comment_author', 3541 'comment_author_email', 3542 'comment_author_url', 3543 'comment_content', 3544 'comment_type', 3545 'comment_parent', 3546 'user_ID' 3547 ); 3548 3549 /** 3550 * Filters whether an empty comment should be allowed. 3551 * 3552 * @since 5.1.0 3553 * 3554 * @param bool $allow_empty_comment Whether to allow empty comments. Default false. 3555 * @param array $commentdata Array of comment data to be sent to wp_insert_comment(). 3556 */ 3557 $allow_empty_comment = apply_filters( 'allow_empty_comment', false, $commentdata ); 3558 if ( '' === $comment_content && ! $allow_empty_comment ) { 3559 return new WP_Error( 'require_valid_comment', __( '<strong>Error</strong>: Please type your comment text.' ), 200 ); 3560 } 3561 3562 $check_max_lengths = wp_check_comment_data_max_lengths( $commentdata ); 3563 if ( is_wp_error( $check_max_lengths ) ) { 3564 return $check_max_lengths; 3565 } 3566 3567 $comment_id = wp_new_comment( wp_slash( $commentdata ), true ); 3568 if ( is_wp_error( $comment_id ) ) { 3569 return $comment_id; 3570 } 3571 3572 if ( ! $comment_id ) { 3573 return new WP_Error( 'comment_save_error', __( '<strong>Error</strong>: The comment could not be saved. Please try again later.' ), 500 ); 3574 } 3575 3576 return get_comment( $comment_id ); 3577 } 3578 3579 /** 3580 * Registers the personal data exporter for comments. 3581 * 3582 * @since 4.9.6 3583 * 3584 * @param array $exporters An array of personal data exporters. 3585 * @return array An array of personal data exporters. 3586 */ 3587 function wp_register_comment_personal_data_exporter( $exporters ) { 3588 $exporters['wordpress-comments'] = array( 3589 'exporter_friendly_name' => __( 'WordPress Comments' ), 3590 'callback' => 'wp_comments_personal_data_exporter', 3591 ); 3592 3593 return $exporters; 3594 } 3595 3596 /** 3597 * Finds and exports personal data associated with an email address from the comments table. 3598 * 3599 * @since 4.9.6 3600 * 3601 * @param string $email_address The comment author email address. 3602 * @param int $page Comment page. 3603 * @return array An array of personal data. 3604 */ 3605 function wp_comments_personal_data_exporter( $email_address, $page = 1 ) { 3606 // Limit us to 500 comments at a time to avoid timing out. 3607 $number = 500; 3608 $page = (int) $page; 3609 3610 $data_to_export = array(); 3611 3612 $comments = get_comments( 3613 array( 3614 'author_email' => $email_address, 3615 'number' => $number, 3616 'paged' => $page, 3617 'order_by' => 'comment_ID', 3618 'order' => 'ASC', 3619 'update_comment_meta_cache' => false, 3620 ) 3621 ); 3622 3623 $comment_prop_to_export = array( 3624 'comment_author' => __( 'Comment Author' ), 3625 'comment_author_email' => __( 'Comment Author Email' ), 3626 'comment_author_url' => __( 'Comment Author URL' ), 3627 'comment_author_IP' => __( 'Comment Author IP' ), 3628 'comment_agent' => __( 'Comment Author User Agent' ), 3629 'comment_date' => __( 'Comment Date' ), 3630 'comment_content' => __( 'Comment Content' ), 3631 'comment_link' => __( 'Comment URL' ), 3632 ); 3633 3634 foreach ( (array) $comments as $comment ) { 3635 $comment_data_to_export = array(); 3636 3637 foreach ( $comment_prop_to_export as $key => $name ) { 3638 $value = ''; 3639 3640 switch ( $key ) { 3641 case 'comment_author': 3642 case 'comment_author_email': 3643 case 'comment_author_url': 3644 case 'comment_author_IP': 3645 case 'comment_agent': 3646 case 'comment_date': 3647 $value = $comment->{$key}; 3648 break; 3649 3650 case 'comment_content': 3651 $value = get_comment_text( $comment->comment_ID ); 3652 break; 3653 3654 case 'comment_link': 3655 $value = get_comment_link( $comment->comment_ID ); 3656 $value = sprintf( 3657 '<a href="%s" target="_blank" rel="noopener">%s</a>', 3658 esc_url( $value ), 3659 esc_html( $value ) 3660 ); 3661 break; 3662 } 3663 3664 if ( ! empty( $value ) ) { 3665 $comment_data_to_export[] = array( 3666 'name' => $name, 3667 'value' => $value, 3668 ); 3669 } 3670 } 3671 3672 $data_to_export[] = array( 3673 'group_id' => 'comments', 3674 'group_label' => __( 'Comments' ), 3675 'group_description' => __( 'User’s comment data.' ), 3676 'item_id' => "comment-{$comment->comment_ID}", 3677 'data' => $comment_data_to_export, 3678 ); 3679 } 3680 3681 $done = count( $comments ) < $number; 3682 3683 return array( 3684 'data' => $data_to_export, 3685 'done' => $done, 3686 ); 3687 } 3688 3689 /** 3690 * Registers the personal data eraser for comments. 3691 * 3692 * @since 4.9.6 3693 * 3694 * @param array $erasers An array of personal data erasers. 3695 * @return array An array of personal data erasers. 3696 */ 3697 function wp_register_comment_personal_data_eraser( $erasers ) { 3698 $erasers['wordpress-comments'] = array( 3699 'eraser_friendly_name' => __( 'WordPress Comments' ), 3700 'callback' => 'wp_comments_personal_data_eraser', 3701 ); 3702 3703 return $erasers; 3704 } 3705 3706 /** 3707 * Erases personal data associated with an email address from the comments table. 3708 * 3709 * @since 4.9.6 3710 * 3711 * @param string $email_address The comment author email address. 3712 * @param int $page Comment page. 3713 * @return array 3714 */ 3715 function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 3716 global $wpdb; 3717 3718 if ( empty( $email_address ) ) { 3719 return array( 3720 'items_removed' => false, 3721 'items_retained' => false, 3722 'messages' => array(), 3723 'done' => true, 3724 ); 3725 } 3726 3727 // Limit us to 500 comments at a time to avoid timing out. 3728 $number = 500; 3729 $page = (int) $page; 3730 $items_removed = false; 3731 $items_retained = false; 3732 3733 $comments = get_comments( 3734 array( 3735 'author_email' => $email_address, 3736 'number' => $number, 3737 'paged' => $page, 3738 'order_by' => 'comment_ID', 3739 'order' => 'ASC', 3740 'include_unapproved' => true, 3741 ) 3742 ); 3743 3744 /* translators: Name of a comment's author after being anonymized. */ 3745 $anon_author = __( 'Anonymous' ); 3746 $messages = array(); 3747 3748 foreach ( (array) $comments as $comment ) { 3749 $anonymized_comment = array(); 3750 $anonymized_comment['comment_agent'] = ''; 3751 $anonymized_comment['comment_author'] = $anon_author; 3752 $anonymized_comment['comment_author_email'] = ''; 3753 $anonymized_comment['comment_author_IP'] = wp_privacy_anonymize_data( 'ip', $comment->comment_author_IP ); 3754 $anonymized_comment['comment_author_url'] = ''; 3755 $anonymized_comment['user_id'] = 0; 3756 3757 $comment_id = (int) $comment->comment_ID; 3758 3759 /** 3760 * Filters whether to anonymize the comment. 3761 * 3762 * @since 4.9.6 3763 * 3764 * @param bool|string $anon_message Whether to apply the comment anonymization (bool) or a custom 3765 * message (string). Default true. 3766 * @param WP_Comment $comment WP_Comment object. 3767 * @param array $anonymized_comment Anonymized comment data. 3768 */ 3769 $anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment ); 3770 3771 if ( true !== $anon_message ) { 3772 if ( $anon_message && is_string( $anon_message ) ) { 3773 $messages[] = esc_html( $anon_message ); 3774 } else { 3775 /* translators: %d: Comment ID. */ 3776 $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id ); 3777 } 3778 3779 $items_retained = true; 3780 3781 continue; 3782 } 3783 3784 $args = array( 3785 'comment_ID' => $comment_id, 3786 ); 3787 3788 $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args ); 3789 3790 if ( $updated ) { 3791 $items_removed = true; 3792 clean_comment_cache( $comment_id ); 3793 } else { 3794 $items_retained = true; 3795 } 3796 } 3797 3798 $done = count( $comments ) < $number; 3799 3800 return array( 3801 'items_removed' => $items_removed, 3802 'items_retained' => $items_retained, 3803 'messages' => $messages, 3804 'done' => $done, 3805 ); 3806 } 3807 3808 /** 3809 * Sets the last changed time for the 'comment' cache group. 3810 * 3811 * @since 5.0.0 3812 */ 3813 function wp_cache_set_comments_last_changed() { 3814 wp_cache_set( 'last_changed', microtime(), 'comment' ); 3815 } 3816 3817 /** 3818 * Updates the comment type for a batch of comments. 3819 * 3820 * @since 5.5.0 3821 * 3822 * @global wpdb $wpdb WordPress database abstraction object. 3823 */ 3824 function _wp_batch_update_comment_type() { 3825 global $wpdb; 3826 3827 $lock_name = 'update_comment_type.lock'; 3828 3829 // Try to lock. 3830 $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) ); 3831 3832 if ( ! $lock_result ) { 3833 $lock_result = get_option( $lock_name ); 3834 3835 // Bail if we were unable to create a lock, or if the existing lock is still valid. 3836 if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { 3837 wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); 3838 return; 3839 } 3840 } 3841 3842 // Update the lock, as by this point we've definitely got a lock, just need to fire the actions. 3843 update_option( $lock_name, time() ); 3844 3845 // Check if there's still an empty comment type. 3846 $empty_comment_type = $wpdb->get_var( 3847 "SELECT comment_ID FROM $wpdb->comments 3848 WHERE comment_type = '' 3849 LIMIT 1" 3850 ); 3851 3852 // No empty comment type, we're done here. 3853 if ( ! $empty_comment_type ) { 3854 update_option( 'finished_updating_comment_type', true ); 3855 delete_option( $lock_name ); 3856 return; 3857 } 3858 3859 // Empty comment type found? We'll need to run this script again. 3860 wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); 3861 3862 /** 3863 * Filters the comment batch size for updating the comment type. 3864 * 3865 * @since 5.5.0 3866 * 3867 * @param int $comment_batch_size The comment batch size. Default 100. 3868 */ 3869 $comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 ); 3870 3871 // Get the IDs of the comments to update. 3872 $comment_ids = $wpdb->get_col( 3873 $wpdb->prepare( 3874 "SELECT comment_ID 3875 FROM {$wpdb->comments} 3876 WHERE comment_type = '' 3877 ORDER BY comment_ID DESC 3878 LIMIT %d", 3879 $comment_batch_size 3880 ) 3881 ); 3882 3883 if ( $comment_ids ) { 3884 $comment_id_list = implode( ',', $comment_ids ); 3885 3886 // Update the `comment_type` field value to be `comment` for the next batch of comments. 3887 $wpdb->query( 3888 "UPDATE {$wpdb->comments} 3889 SET comment_type = 'comment' 3890 WHERE comment_type = '' 3891 AND comment_ID IN ({$comment_id_list})" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 3892 ); 3893 3894 // Make sure to clean the comment cache. 3895 clean_comment_cache( $comment_ids ); 3896 } 3897 3898 delete_option( $lock_name ); 3899 } 3900 3901 /** 3902 * In order to avoid the _wp_batch_update_comment_type() job being accidentally removed, 3903 * check that it's still scheduled while we haven't finished updating comment types. 3904 * 3905 * @ignore 3906 * @since 5.5.0 3907 */ 3908 function _wp_check_for_scheduled_update_comment_type() { 3909 if ( ! get_option( 'finished_updating_comment_type' ) && ! wp_next_scheduled( 'wp_update_comment_type_batch' ) ) { 3910 wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_update_comment_type_batch' ); 3911 } 3912 }