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