| [ 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.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 continue 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|null 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 return null; 2835 } 2836 2837 /** 2838 * Updates the comment count for the post. 2839 * 2840 * @since 2.5.0 2841 * 2842 * @global wpdb $wpdb WordPress database abstraction object. 2843 * 2844 * @param int $post_id Post ID 2845 * @return bool True on success, false if the post does not exist. 2846 */ 2847 function wp_update_comment_count_now( $post_id ) { 2848 global $wpdb; 2849 2850 $post_id = (int) $post_id; 2851 2852 if ( ! $post_id ) { 2853 return false; 2854 } 2855 2856 wp_cache_delete( 'comments-0', 'counts' ); 2857 wp_cache_delete( "comments-{$post_id}", 'counts' ); 2858 2859 $post = get_post( $post_id ); 2860 2861 if ( ! $post ) { 2862 return false; 2863 } 2864 2865 $old = (int) $post->comment_count; 2866 2867 /** 2868 * Filters a post's comment count before it is updated in the database. 2869 * 2870 * @since 4.5.0 2871 * 2872 * @param int|null $new The new comment count. Default null. 2873 * @param int $old The old comment count. 2874 * @param int $post_id Post ID. 2875 */ 2876 $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id ); 2877 2878 if ( is_null( $new ) ) { 2879 $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 ) ); 2880 } else { 2881 $new = (int) $new; 2882 } 2883 2884 $wpdb->update( $wpdb->posts, array( 'comment_count' => $new ), array( 'ID' => $post_id ) ); 2885 2886 clean_post_cache( $post ); 2887 2888 /** 2889 * Fires immediately after a post's comment count is updated in the database. 2890 * 2891 * @since 2.3.0 2892 * 2893 * @param int $post_id Post ID. 2894 * @param int $new The new comment count. 2895 * @param int $old The old comment count. 2896 */ 2897 do_action( 'wp_update_comment_count', $post_id, $new, $old ); 2898 2899 /** This action is documented in wp-includes/post.php */ 2900 do_action( "edit_post_{$post->post_type}", $post_id, $post ); 2901 2902 /** This action is documented in wp-includes/post.php */ 2903 do_action( 'edit_post', $post_id, $post ); 2904 2905 return true; 2906 } 2907 2908 // 2909 // Ping and trackback functions. 2910 // 2911 2912 /** 2913 * Finds a pingback server URI based on the given URL. 2914 * 2915 * Checks the HTML for the rel="pingback" link and X-Pingback headers. It does 2916 * a check for the X-Pingback headers first and returns that, if available. 2917 * The check for the rel="pingback" has more overhead than just the header. 2918 * 2919 * @since 1.5.0 2920 * 2921 * @param string $url URL to ping. 2922 * @param string $deprecated Not Used. 2923 * @return string|false String containing URI on success, false on failure. 2924 */ 2925 function discover_pingback_server_uri( $url, $deprecated = '' ) { 2926 if ( ! empty( $deprecated ) ) { 2927 _deprecated_argument( __FUNCTION__, '2.7.0' ); 2928 } 2929 2930 $pingback_str_dquote = 'rel="pingback"'; 2931 $pingback_str_squote = 'rel=\'pingback\''; 2932 2933 /** @todo Should use Filter Extension or custom preg_match instead. */ 2934 $parsed_url = parse_url( $url ); 2935 2936 if ( ! isset( $parsed_url['host'] ) ) { // Not a URL. This should never happen. 2937 return false; 2938 } 2939 2940 // Do not search for a pingback server on our own uploads. 2941 $uploads_dir = wp_get_upload_dir(); 2942 if ( str_starts_with( $url, $uploads_dir['baseurl'] ) ) { 2943 return false; 2944 } 2945 2946 $response = wp_safe_remote_head( 2947 $url, 2948 array( 2949 'timeout' => 2, 2950 'httpversion' => '1.0', 2951 ) 2952 ); 2953 2954 if ( is_wp_error( $response ) ) { 2955 return false; 2956 } 2957 2958 if ( wp_remote_retrieve_header( $response, 'X-Pingback' ) ) { 2959 return wp_remote_retrieve_header( $response, 'X-Pingback' ); 2960 } 2961 2962 // Not an (x)html, sgml, or xml page, no use going further. 2963 if ( preg_match( '#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'Content-Type' ) ) ) { 2964 return false; 2965 } 2966 2967 // Now do a GET since we're going to look in the HTML headers (and we're sure it's not a binary file). 2968 $response = wp_safe_remote_get( 2969 $url, 2970 array( 2971 'timeout' => 2, 2972 'httpversion' => '1.0', 2973 ) 2974 ); 2975 2976 if ( is_wp_error( $response ) ) { 2977 return false; 2978 } 2979 2980 $contents = wp_remote_retrieve_body( $response ); 2981 2982 $pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote ); 2983 $pingback_link_offset_squote = strpos( $contents, $pingback_str_squote ); 2984 2985 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { 2986 $quote = ( $pingback_link_offset_dquote ) ? '"' : '\''; 2987 $pingback_link_offset = ( '"' === $quote ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote; 2988 $pingback_href_pos = strpos( $contents, 'href=', $pingback_link_offset ); 2989 $pingback_href_start = $pingback_href_pos + 6; 2990 $pingback_href_end = strpos( $contents, $quote, $pingback_href_start ); 2991 $pingback_server_url_len = $pingback_href_end - $pingback_href_start; 2992 $pingback_server_url = substr( $contents, $pingback_href_start, $pingback_server_url_len ); 2993 2994 // We may find rel="pingback" but an incomplete pingback URL. 2995 if ( $pingback_server_url_len > 0 ) { // We got it! 2996 return $pingback_server_url; 2997 } 2998 } 2999 3000 return false; 3001 } 3002 3003 /** 3004 * Performs all pingbacks, enclosures, trackbacks, and sends to pingback services. 3005 * 3006 * @since 2.1.0 3007 * @since 5.6.0 Introduced `do_all_pings` action hook for individual services. 3008 */ 3009 function do_all_pings() { 3010 /** 3011 * Fires immediately after the `do_pings` event to hook services individually. 3012 * 3013 * @since 5.6.0 3014 */ 3015 do_action( 'do_all_pings' ); 3016 } 3017 3018 /** 3019 * Performs all pingbacks. 3020 * 3021 * @since 5.6.0 3022 */ 3023 function do_all_pingbacks() { 3024 $pings = get_posts( 3025 array( 3026 'post_type' => get_post_types(), 3027 'suppress_filters' => false, 3028 'nopaging' => true, 3029 'meta_key' => '_pingme', 3030 'fields' => 'ids', 3031 ) 3032 ); 3033 3034 foreach ( $pings as $ping ) { 3035 delete_post_meta( $ping, '_pingme' ); 3036 pingback( null, $ping ); 3037 } 3038 } 3039 3040 /** 3041 * Performs all enclosures. 3042 * 3043 * @since 5.6.0 3044 */ 3045 function do_all_enclosures() { 3046 $enclosures = get_posts( 3047 array( 3048 'post_type' => get_post_types(), 3049 'suppress_filters' => false, 3050 'nopaging' => true, 3051 'meta_key' => '_encloseme', 3052 'fields' => 'ids', 3053 ) 3054 ); 3055 3056 foreach ( $enclosures as $enclosure ) { 3057 delete_post_meta( $enclosure, '_encloseme' ); 3058 do_enclose( null, $enclosure ); 3059 } 3060 } 3061 3062 /** 3063 * Performs all trackbacks. 3064 * 3065 * @since 5.6.0 3066 */ 3067 function do_all_trackbacks() { 3068 $trackbacks = get_posts( 3069 array( 3070 'post_type' => get_post_types(), 3071 'suppress_filters' => false, 3072 'nopaging' => true, 3073 'meta_key' => '_trackbackme', 3074 'fields' => 'ids', 3075 ) 3076 ); 3077 3078 foreach ( $trackbacks as $trackback ) { 3079 delete_post_meta( $trackback, '_trackbackme' ); 3080 do_trackbacks( $trackback ); 3081 } 3082 } 3083 3084 /** 3085 * Performs trackbacks. 3086 * 3087 * @since 1.5.0 3088 * @since 4.7.0 `$post` can be a WP_Post object. 3089 * 3090 * @global wpdb $wpdb WordPress database abstraction object. 3091 * 3092 * @param int|WP_Post $post Post ID or object to do trackbacks on. 3093 * @return void|false Returns false on failure. 3094 */ 3095 function do_trackbacks( $post ) { 3096 global $wpdb; 3097 3098 $post = get_post( $post ); 3099 3100 if ( ! $post ) { 3101 return false; 3102 } 3103 3104 $to_ping = get_to_ping( $post ); 3105 $pinged = get_pung( $post ); 3106 3107 if ( empty( $to_ping ) ) { 3108 $wpdb->update( $wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) ); 3109 return; 3110 } 3111 3112 if ( empty( $post->post_excerpt ) ) { 3113 /** This filter is documented in wp-includes/post-template.php */ 3114 $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID ); 3115 } else { 3116 /** This filter is documented in wp-includes/post-template.php */ 3117 $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt ); 3118 } 3119 3120 $excerpt = str_replace( ']]>', ']]>', $excerpt ); 3121 $excerpt = wp_html_excerpt( $excerpt, 252, '…' ); 3122 3123 /** This filter is documented in wp-includes/post-template.php */ 3124 $post_title = apply_filters( 'the_title', $post->post_title, $post->ID ); 3125 $post_title = strip_tags( $post_title ); 3126 3127 foreach ( (array) $to_ping as $tb_ping ) { 3128 $tb_ping = trim( $tb_ping ); 3129 if ( ! in_array( $tb_ping, $pinged, true ) ) { 3130 trackback( $tb_ping, $post_title, $excerpt, $post->ID ); 3131 $pinged[] = $tb_ping; 3132 } else { 3133 $wpdb->query( 3134 $wpdb->prepare( 3135 "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", 3136 $tb_ping, 3137 $post->ID 3138 ) 3139 ); 3140 } 3141 } 3142 } 3143 3144 /** 3145 * Sends pings to all of the ping site services. 3146 * 3147 * @since 1.2.0 3148 * 3149 * @param int $post_id Post ID. 3150 * @return int Same post ID as provided. 3151 */ 3152 function generic_ping( $post_id = 0 ) { 3153 $services = get_option( 'ping_sites' ); 3154 3155 $services = explode( "\n", $services ); 3156 foreach ( (array) $services as $service ) { 3157 $service = trim( $service ); 3158 if ( '' !== $service ) { 3159 weblog_ping( $service ); 3160 } 3161 } 3162 3163 return $post_id; 3164 } 3165 3166 /** 3167 * Determines whether pings should be disabled for the current environment. 3168 * 3169 * By default, all pings (outgoing pingbacks, trackbacks, and ping service 3170 * notifications, as well as incoming pingbacks and trackbacks) are disabled 3171 * for non-production environments ('local', 'development', 'staging'). 3172 * 3173 * @since 7.1.0 3174 * 3175 * @return bool True if pings should be disabled, false otherwise. 3176 */ 3177 function wp_should_disable_pings_for_environment() { 3178 $environment_type = wp_get_environment_type(); 3179 $should_disable = 'production' !== $environment_type; 3180 3181 /** 3182 * Filters whether pings should be disabled for the current environment. 3183 * 3184 * Returning false re-enables pings in non-production environments. 3185 * Returning true disables pings even in production. 3186 * 3187 * @since 7.1.0 3188 * 3189 * @param bool $should_disable Whether pings should be disabled. Default true 3190 * for non-production environments, false for production. 3191 * @param string $environment_type The current environment type as returned by 3192 * wp_get_environment_type(). 3193 */ 3194 return apply_filters( 'wp_should_disable_pings_for_environment', $should_disable, $environment_type ); 3195 } 3196 3197 /** 3198 * Removes outgoing ping callbacks in non-production environments. 3199 * 3200 * Hooked to `do_all_pings` at priority 1 so it runs before the default 3201 * priority 10 callbacks. Does not remove `do_all_enclosures`. 3202 * 3203 * @since 7.1.0 3204 */ 3205 function wp_maybe_disable_outgoing_pings_for_environment() { 3206 if ( wp_should_disable_pings_for_environment() ) { 3207 remove_action( 'do_all_pings', 'do_all_pingbacks' ); 3208 remove_action( 'do_all_pings', 'do_all_trackbacks' ); 3209 remove_action( 'do_all_pings', 'generic_ping' ); 3210 } 3211 } 3212 3213 /** 3214 * Rejects incoming trackbacks in non-production environments. 3215 * 3216 * Hooked to `pre_trackback_post` which fires in `wp-trackback.php` before the 3217 * trackback is processed. Calls `trackback_response()` which sends an XML error 3218 * response and terminates the request. 3219 * 3220 * @since 7.1.0 3221 */ 3222 function wp_maybe_disable_trackback_for_environment() { 3223 if ( wp_should_disable_pings_for_environment() ) { 3224 trackback_response( 1, __( 'Trackbacks are disabled in non-production environments.' ) ); 3225 } 3226 } 3227 3228 /** 3229 * Removes the pingback XML-RPC method in non-production environments. 3230 * 3231 * @since 7.1.0 3232 * 3233 * @param string[] $methods An array of XML-RPC methods, keyed by their methodName. 3234 * @return string[] Modified array of XML-RPC methods. 3235 */ 3236 function wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ) { 3237 if ( wp_should_disable_pings_for_environment() ) { 3238 unset( $methods['pingback.ping'] ); 3239 } 3240 3241 return $methods; 3242 } 3243 3244 /** 3245 * Pings back the links found in a post. 3246 * 3247 * @since 0.71 3248 * @since 4.7.0 `$post` can be a WP_Post object. 3249 * @since 6.8.0 Returns an array of pingback statuses indexed by link. 3250 * 3251 * @param string $content Post content to check for links. If empty will retrieve from post. 3252 * @param int|WP_Post $post Post ID or object. 3253 * @return array<string, bool> An array of pingback statuses indexed by link. 3254 */ 3255 function pingback( $content, $post ) { 3256 require_once ABSPATH . WPINC . '/class-IXR.php'; 3257 require_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php'; 3258 3259 // Original code by Mort (http://mort.mine.nu:8080). 3260 $post_links = array(); 3261 3262 $post = get_post( $post ); 3263 3264 if ( ! $post ) { 3265 return array(); 3266 } 3267 3268 $pung = get_pung( $post ); 3269 3270 if ( empty( $content ) ) { 3271 $content = $post->post_content; 3272 } 3273 3274 /* 3275 * Step 1. 3276 * Parsing the post, external links (if any) are stored in the $post_links array. 3277 */ 3278 $post_links_temp = wp_extract_urls( $content ); 3279 3280 $ping_status = array(); 3281 /* 3282 * Step 2. 3283 * Walking through the links array. 3284 * First we get rid of links pointing to sites, not to specific files. 3285 * Example: 3286 * http://dummy-weblog.org 3287 * http://dummy-weblog.org/ 3288 * http://dummy-weblog.org/post.php 3289 * We don't wanna ping first and second types, even if they have a valid <link/>. 3290 */ 3291 foreach ( (array) $post_links_temp as $link_test ) { 3292 // If we haven't pung it already and it isn't a link to itself. 3293 if ( ! in_array( $link_test, $pung, true ) && ( url_to_postid( $link_test ) !== $post->ID ) 3294 // Also, let's never ping local attachments. 3295 && ! is_local_attachment( $link_test ) 3296 ) { 3297 $test = parse_url( $link_test ); 3298 if ( $test ) { 3299 if ( isset( $test['query'] ) ) { 3300 $post_links[] = $link_test; 3301 } elseif ( isset( $test['path'] ) && ( '/' !== $test['path'] ) && ( '' !== $test['path'] ) ) { 3302 $post_links[] = $link_test; 3303 } 3304 } 3305 } 3306 } 3307 3308 $post_links = array_unique( $post_links ); 3309 3310 /** 3311 * Fires just before pinging back links found in a post. 3312 * 3313 * @since 2.0.0 3314 * 3315 * @param string[] $post_links Array of link URLs to be checked (passed by reference). 3316 * @param string[] $pung Array of link URLs already pinged (passed by reference). 3317 * @param int $post_id The post ID. 3318 */ 3319 do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) ); 3320 3321 foreach ( (array) $post_links as $pagelinkedto ) { 3322 $pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); 3323 3324 if ( $pingback_server_url ) { 3325 // Allow an additional 60 seconds for each pingback to complete. 3326 if ( function_exists( 'set_time_limit' ) ) { 3327 set_time_limit( 60 ); 3328 } 3329 3330 // Now, the RPC call. 3331 $pagelinkedfrom = get_permalink( $post ); 3332 3333 // Using a timeout of 3 seconds should be enough to cover slow servers. 3334 $client = new WP_HTTP_IXR_Client( $pingback_server_url ); 3335 $client->timeout = 3; 3336 /** 3337 * Filters the user agent sent when pinging-back a URL. 3338 * 3339 * @since 2.9.0 3340 * 3341 * @param string $concat_useragent The user agent concatenated with ' -- WordPress/' 3342 * and the WordPress version. 3343 * @param string $useragent The useragent. 3344 * @param string $pingback_server_url The server URL being linked to. 3345 * @param string $pagelinkedto URL of page linked to. 3346 * @param string $pagelinkedfrom URL of page linked from. 3347 */ 3348 $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . get_bloginfo( 'version' ), $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom ); 3349 // When set to true, this outputs debug messages by itself. 3350 $client->debug = false; 3351 3352 $status = $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ); 3353 3354 if ( $status // Ping registered. 3355 || ( isset( $client->error->code ) && 48 === $client->error->code ) // Already registered. 3356 ) { 3357 add_ping( $post, $pagelinkedto ); 3358 } 3359 $ping_status[ $pagelinkedto ] = $status; 3360 } 3361 } 3362 3363 return $ping_status; 3364 } 3365 3366 /** 3367 * Checks whether blog is public before returning sites. 3368 * 3369 * @since 2.1.0 3370 * 3371 * @param mixed $sites Will return if blog is public, will not return if not public. 3372 * @return mixed Empty string if blog is not public, returns $sites, if site is public. 3373 */ 3374 function privacy_ping_filter( $sites ) { 3375 if ( '0' !== get_option( 'blog_public' ) ) { 3376 return $sites; 3377 } else { 3378 return ''; 3379 } 3380 } 3381 3382 /** 3383 * Sends a Trackback. 3384 * 3385 * Updates database when sending trackback to prevent duplicates. 3386 * 3387 * @since 0.71 3388 * 3389 * @global wpdb $wpdb WordPress database abstraction object. 3390 * 3391 * @param string $trackback_url URL to send trackbacks. 3392 * @param string $title Title of post. 3393 * @param string $excerpt Excerpt of post. 3394 * @param int $post_id Post ID. 3395 * @return int|false|null Database query from update. 3396 */ 3397 function trackback( $trackback_url, $title, $excerpt, $post_id ) { 3398 global $wpdb; 3399 3400 if ( empty( $trackback_url ) ) { 3401 return null; 3402 } 3403 3404 $options = array(); 3405 $options['timeout'] = 10; 3406 $options['body'] = array( 3407 'title' => $title, 3408 'url' => get_permalink( $post_id ), 3409 'blog_name' => get_option( 'blogname' ), 3410 'excerpt' => $excerpt, 3411 ); 3412 3413 $response = wp_safe_remote_post( $trackback_url, $options ); 3414 3415 if ( is_wp_error( $response ) ) { 3416 return null; 3417 } 3418 3419 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $post_id ) ); 3420 return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $post_id ) ); 3421 } 3422 3423 /** 3424 * Sends a pingback. 3425 * 3426 * @since 1.2.0 3427 * 3428 * @param string $server Host of blog to connect to. 3429 * @param string $path Path to send the ping. 3430 */ 3431 function weblog_ping( $server = '', $path = '' ) { 3432 require_once ABSPATH . WPINC . '/class-IXR.php'; 3433 require_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php'; 3434 3435 // Using a timeout of 3 seconds should be enough to cover slow servers. 3436 $client = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' === $path ) ) ? false : $path ) ); 3437 $client->timeout = 3; 3438 $client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' ); 3439 3440 // When set to true, this outputs debug messages by itself. 3441 $client->debug = false; 3442 $home = trailingslashit( home_url() ); 3443 if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // Then try a normal ping. 3444 $client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home ); 3445 } 3446 } 3447 3448 /** 3449 * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI. 3450 * 3451 * @since 3.5.1 3452 * 3453 * @see wp_http_validate_url() 3454 * 3455 * @param string $source_uri 3456 * @return string Validated source URI. 3457 */ 3458 function pingback_ping_source_uri( $source_uri ) { 3459 return (string) wp_http_validate_url( $source_uri ); 3460 } 3461 3462 /** 3463 * Default filter attached to xmlrpc_pingback_error. 3464 * 3465 * Returns a generic pingback error code unless the error code is 48, 3466 * which reports that the pingback is already registered. 3467 * 3468 * @since 3.5.1 3469 * 3470 * @link https://www.hixie.ch/specs/pingback/pingback#TOC3 3471 * 3472 * @param IXR_Error $ixr_error 3473 * @return IXR_Error 3474 */ 3475 function xmlrpc_pingback_error( $ixr_error ) { 3476 if ( 48 === $ixr_error->code ) { 3477 return $ixr_error; 3478 } 3479 return new IXR_Error( 0, '' ); 3480 } 3481 3482 // 3483 // Cache. 3484 // 3485 3486 /** 3487 * Removes a comment from the object cache. 3488 * 3489 * @since 2.3.0 3490 * 3491 * @param int|array $ids Comment ID or an array of comment IDs to remove from cache. 3492 */ 3493 function clean_comment_cache( $ids ) { 3494 $comment_ids = (array) $ids; 3495 wp_cache_delete_multiple( $comment_ids, 'comment' ); 3496 foreach ( $comment_ids as $id ) { 3497 /** 3498 * Fires immediately after a comment has been removed from the object cache. 3499 * 3500 * @since 4.5.0 3501 * 3502 * @param int $id Comment ID. 3503 */ 3504 do_action( 'clean_comment_cache', $id ); 3505 } 3506 3507 wp_cache_set_comments_last_changed(); 3508 } 3509 3510 /** 3511 * Updates the comment cache of given comments. 3512 * 3513 * Will add the comments in $comments to the cache. If comment ID already exists 3514 * in the comment cache then it will not be updated. The comment is added to the 3515 * cache using the comment group with the key using the ID of the comments. 3516 * 3517 * @since 2.3.0 3518 * @since 4.4.0 Introduced the `$update_meta_cache` parameter. 3519 * 3520 * @param WP_Comment[] $comments Array of comment objects 3521 * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. 3522 */ 3523 function update_comment_cache( $comments, $update_meta_cache = true ) { 3524 $data = array(); 3525 foreach ( (array) $comments as $comment ) { 3526 $data[ $comment->comment_ID ] = $comment; 3527 } 3528 wp_cache_add_multiple( $data, 'comment' ); 3529 3530 if ( $update_meta_cache ) { 3531 // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. 3532 $comment_ids = array(); 3533 foreach ( $comments as $comment ) { 3534 $comment_ids[] = $comment->comment_ID; 3535 } 3536 update_meta_cache( 'comment', $comment_ids ); 3537 } 3538 } 3539 3540 /** 3541 * Adds any comments from the given IDs to the cache that do not already exist in cache. 3542 * 3543 * @since 4.4.0 3544 * @since 6.1.0 This function is no longer marked as "private". 3545 * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta. 3546 * 3547 * @see update_comment_cache() 3548 * @global wpdb $wpdb WordPress database abstraction object. 3549 * 3550 * @param int[] $comment_ids Array of comment IDs. 3551 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 3552 */ 3553 function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { 3554 global $wpdb; 3555 3556 $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' ); 3557 if ( ! empty( $non_cached_ids ) ) { 3558 $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); 3559 3560 update_comment_cache( $fresh_comments, false ); 3561 } 3562 3563 if ( $update_meta_cache ) { 3564 wp_lazyload_comment_meta( $comment_ids ); 3565 } 3566 } 3567 3568 // 3569 // Internal. 3570 // 3571 3572 /** 3573 * Closes comments on old posts on the fly, without any extra DB queries. Hooked to the_posts. 3574 * 3575 * @since 2.7.0 3576 * @access private 3577 * 3578 * @param WP_Post[] $posts Array of post objects. 3579 * @param WP_Query $query Query object. 3580 * @return WP_Post[] 3581 */ 3582 function _close_comments_for_old_posts( $posts, $query ) { 3583 if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) { 3584 return $posts; 3585 } 3586 3587 /** 3588 * Filters the list of post types to automatically close comments for. 3589 * 3590 * @since 3.2.0 3591 * 3592 * @param string[] $post_types An array of post type names. 3593 */ 3594 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 3595 if ( ! in_array( $posts[0]->post_type, $post_types, true ) ) { 3596 return $posts; 3597 } 3598 3599 $days_old = (int) get_option( 'close_comments_days_old' ); 3600 if ( ! $days_old ) { 3601 return $posts; 3602 } 3603 3604 if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { 3605 $posts[0]->comment_status = 'closed'; 3606 $posts[0]->ping_status = 'closed'; 3607 } 3608 3609 return $posts; 3610 } 3611 3612 /** 3613 * Closes comments on an old post. Hooked to comments_open and pings_open. 3614 * 3615 * @since 2.7.0 3616 * @access private 3617 * 3618 * @param bool $open Comments open or closed. 3619 * @param int $post_id Post ID. 3620 * @return bool $open 3621 */ 3622 function _close_comments_for_old_post( $open, $post_id ) { 3623 if ( ! $open ) { 3624 return $open; 3625 } 3626 3627 if ( ! get_option( 'close_comments_for_old_posts' ) ) { 3628 return $open; 3629 } 3630 3631 $days_old = (int) get_option( 'close_comments_days_old' ); 3632 if ( ! $days_old ) { 3633 return $open; 3634 } 3635 3636 $post = get_post( $post_id ); 3637 3638 /** This filter is documented in wp-includes/comment.php */ 3639 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 3640 if ( ! in_array( $post->post_type, $post_types, true ) ) { 3641 return $open; 3642 } 3643 3644 // Undated drafts should not show up as comments closed. 3645 if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { 3646 return $open; 3647 } 3648 3649 if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { 3650 return false; 3651 } 3652 3653 return $open; 3654 } 3655 3656 /** 3657 * Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form. 3658 * 3659 * This function expects unslashed data, as opposed to functions such as `wp_new_comment()` which 3660 * expect slashed data. 3661 * 3662 * @since 4.4.0 3663 * 3664 * @param array $comment_data { 3665 * Comment data. 3666 * 3667 * @type string|int $comment_post_ID The ID of the post that relates to the comment. 3668 * @type string $author The name of the comment author. 3669 * @type string $email The comment author email address. 3670 * @type string $url The comment author URL. 3671 * @type string $comment The content of the comment. 3672 * @type string|int $comment_parent The ID of this comment's parent, if any. Default 0. 3673 * @type string $_wp_unfiltered_html_comment The nonce value for allowing unfiltered HTML. 3674 * } 3675 * @return WP_Comment|WP_Error A WP_Comment object on success, a WP_Error object on failure. 3676 */ 3677 function wp_handle_comment_submission( $comment_data ) { 3678 $comment_post_id = 0; 3679 $comment_author = ''; 3680 $comment_author_email = ''; 3681 $comment_author_url = ''; 3682 $comment_content = ''; 3683 $comment_parent = 0; 3684 $user_id = 0; 3685 3686 if ( isset( $comment_data['comment_post_ID'] ) ) { 3687 $comment_post_id = (int) $comment_data['comment_post_ID']; 3688 } 3689 if ( isset( $comment_data['author'] ) && is_string( $comment_data['author'] ) ) { 3690 $comment_author = trim( strip_tags( $comment_data['author'] ) ); 3691 } 3692 if ( isset( $comment_data['email'] ) && is_string( $comment_data['email'] ) ) { 3693 $comment_author_email = trim( $comment_data['email'] ); 3694 } 3695 if ( isset( $comment_data['url'] ) && is_string( $comment_data['url'] ) ) { 3696 $comment_author_url = trim( $comment_data['url'] ); 3697 } 3698 if ( isset( $comment_data['comment'] ) && is_string( $comment_data['comment'] ) ) { 3699 $comment_content = trim( $comment_data['comment'] ); 3700 } 3701 if ( isset( $comment_data['comment_parent'] ) ) { 3702 $comment_parent = absint( $comment_data['comment_parent'] ); 3703 $comment_parent_object = get_comment( $comment_parent ); 3704 3705 if ( 3706 0 !== $comment_parent && 3707 ( 3708 ! $comment_parent_object instanceof WP_Comment || 3709 0 === (int) $comment_parent_object->comment_approved 3710 ) 3711 ) { 3712 /** 3713 * Fires when a comment reply is attempted to an unapproved comment. 3714 * 3715 * @since 6.2.0 3716 * 3717 * @param int $comment_post_id Post ID. 3718 * @param int $comment_parent Parent comment ID. 3719 */ 3720 do_action( 'comment_reply_to_unapproved_comment', $comment_post_id, $comment_parent ); 3721 3722 return new WP_Error( 'comment_reply_to_unapproved_comment', __( 'Sorry, replies to unapproved comments are not allowed.' ), 403 ); 3723 } 3724 } 3725 3726 $post = get_post( $comment_post_id ); 3727 3728 if ( empty( $post->comment_status ) ) { 3729 3730 /** 3731 * Fires when a comment is attempted on a post that does not exist. 3732 * 3733 * @since 1.5.0 3734 * 3735 * @param int $comment_post_id Post ID. 3736 */ 3737 do_action( 'comment_id_not_found', $comment_post_id ); 3738 3739 return new WP_Error( 'comment_id_not_found' ); 3740 3741 } 3742 3743 // get_post_status() will get the parent status for attachments. 3744 $status = get_post_status( $post ); 3745 3746 if ( ( 'private' === $status ) && ! current_user_can( 'read_post', $comment_post_id ) ) { 3747 return new WP_Error( 'comment_id_not_found' ); 3748 } 3749 3750 $status_obj = get_post_status_object( $status ); 3751 3752 if ( ! comments_open( $comment_post_id ) ) { 3753 3754 /** 3755 * Fires when a comment is attempted on a post that has comments closed. 3756 * 3757 * @since 1.5.0 3758 * 3759 * @param int $comment_post_id Post ID. 3760 */ 3761 do_action( 'comment_closed', $comment_post_id ); 3762 3763 return new WP_Error( 'comment_closed', __( 'Sorry, comments are closed for this item.' ), 403 ); 3764 3765 } elseif ( 'trash' === $status ) { 3766 3767 /** 3768 * Fires when a comment is attempted on a trashed post. 3769 * 3770 * @since 2.9.0 3771 * 3772 * @param int $comment_post_id Post ID. 3773 */ 3774 do_action( 'comment_on_trash', $comment_post_id ); 3775 3776 return new WP_Error( 'comment_on_trash' ); 3777 3778 } elseif ( ! $status_obj->public && ! $status_obj->private ) { 3779 3780 /** 3781 * Fires when a comment is attempted on a post in draft mode. 3782 * 3783 * @since 1.5.1 3784 * 3785 * @param int $comment_post_id Post ID. 3786 */ 3787 do_action( 'comment_on_draft', $comment_post_id ); 3788 3789 if ( current_user_can( 'read_post', $comment_post_id ) ) { 3790 return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 ); 3791 } else { 3792 return new WP_Error( 'comment_on_draft' ); 3793 } 3794 } elseif ( post_password_required( $comment_post_id ) ) { 3795 3796 /** 3797 * Fires when a comment is attempted on a password-protected post. 3798 * 3799 * @since 2.9.0 3800 * 3801 * @param int $comment_post_id Post ID. 3802 */ 3803 do_action( 'comment_on_password_protected', $comment_post_id ); 3804 3805 return new WP_Error( 'comment_on_password_protected' ); 3806 3807 } else { 3808 /** 3809 * Fires before a comment is posted. 3810 * 3811 * @since 2.8.0 3812 * 3813 * @param int $comment_post_id Post ID. 3814 */ 3815 do_action( 'pre_comment_on_post', $comment_post_id ); 3816 } 3817 3818 // If the user is logged in. 3819 $user = wp_get_current_user(); 3820 if ( $user->exists() ) { 3821 if ( empty( $user->display_name ) ) { 3822 $user->display_name = $user->user_login; 3823 } 3824 3825 $comment_author = $user->display_name; 3826 $comment_author_email = $user->user_email; 3827 $comment_author_url = $user->user_url; 3828 $user_id = $user->ID; 3829 3830 if ( current_user_can( 'unfiltered_html' ) ) { 3831 if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] ) 3832 || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_id ) 3833 ) { 3834 kses_remove_filters(); // Start with a clean slate. 3835 kses_init_filters(); // Set up the filters. 3836 remove_filter( 'pre_comment_content', 'wp_filter_post_kses' ); 3837 add_filter( 'pre_comment_content', 'wp_filter_kses' ); 3838 } 3839 } 3840 } else { 3841 if ( get_option( 'comment_registration' ) ) { 3842 return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to comment.' ), 403 ); 3843 } 3844 } 3845 3846 $comment_type = 'comment'; 3847 3848 if ( get_option( 'require_name_email' ) && ! $user->exists() ) { 3849 if ( '' === $comment_author_email || '' === $comment_author ) { 3850 return new WP_Error( 'require_name_email', __( '<strong>Error:</strong> Please fill the required fields.' ), 200 ); 3851 } elseif ( ! is_email( $comment_author_email ) ) { 3852 return new WP_Error( 'require_valid_email', __( '<strong>Error:</strong> Please enter a valid email address.' ), 200 ); 3853 } 3854 } 3855 3856 $commentdata = array( 3857 'comment_post_ID' => $comment_post_id, 3858 ); 3859 3860 $commentdata += compact( 3861 'comment_author', 3862 'comment_author_email', 3863 'comment_author_url', 3864 'comment_content', 3865 'comment_type', 3866 'comment_parent', 3867 'user_id' 3868 ); 3869 3870 /** 3871 * Filters whether an empty comment should be allowed. 3872 * 3873 * @since 5.1.0 3874 * 3875 * @param bool $allow_empty_comment Whether to allow empty comments. Default false. 3876 * @param array $commentdata Array of comment data to be sent to wp_insert_comment(). 3877 */ 3878 $allow_empty_comment = apply_filters( 'allow_empty_comment', false, $commentdata ); 3879 if ( '' === $comment_content && ! $allow_empty_comment ) { 3880 return new WP_Error( 'require_valid_comment', __( '<strong>Error:</strong> Please type your comment text.' ), 200 ); 3881 } 3882 3883 $check_max_lengths = wp_check_comment_data_max_lengths( $commentdata ); 3884 if ( is_wp_error( $check_max_lengths ) ) { 3885 return $check_max_lengths; 3886 } 3887 3888 $comment_id = wp_new_comment( wp_slash( $commentdata ), true ); 3889 if ( is_wp_error( $comment_id ) ) { 3890 return $comment_id; 3891 } 3892 3893 if ( ! $comment_id ) { 3894 return new WP_Error( 'comment_save_error', __( '<strong>Error:</strong> The comment could not be saved. Please try again later.' ), 500 ); 3895 } 3896 3897 return get_comment( $comment_id ); 3898 } 3899 3900 /** 3901 * Registers the personal data exporter for comments. 3902 * 3903 * @since 4.9.6 3904 * 3905 * @param array[] $exporters An array of personal data exporters. 3906 * @return array[] An array of personal data exporters. 3907 */ 3908 function wp_register_comment_personal_data_exporter( $exporters ) { 3909 $exporters['wordpress-comments'] = array( 3910 'exporter_friendly_name' => __( 'WordPress Comments' ), 3911 'callback' => 'wp_comments_personal_data_exporter', 3912 ); 3913 3914 return $exporters; 3915 } 3916 3917 /** 3918 * Finds and exports personal data associated with an email address from the comments table. 3919 * 3920 * @since 4.9.6 3921 * 3922 * @param string $email_address The comment author email address. 3923 * @param int $page Comment page number. 3924 * @return array { 3925 * An array of personal data. 3926 * 3927 * @type array[] $data An array of personal data arrays. 3928 * @type bool $done Whether the exporter is finished. 3929 * } 3930 */ 3931 function wp_comments_personal_data_exporter( $email_address, $page = 1 ) { 3932 // Limit us to 500 comments at a time to avoid timing out. 3933 $number = 500; 3934 $page = (int) $page; 3935 3936 $data_to_export = array(); 3937 3938 $comments = get_comments( 3939 array( 3940 'author_email' => $email_address, 3941 'number' => $number, 3942 'paged' => $page, 3943 'orderby' => 'comment_ID', 3944 'order' => 'ASC', 3945 'update_comment_meta_cache' => false, 3946 ) 3947 ); 3948 3949 $comment_prop_to_export = array( 3950 'comment_author' => __( 'Comment Author' ), 3951 'comment_author_email' => __( 'Comment Author Email' ), 3952 'comment_author_url' => __( 'Comment Author URL' ), 3953 'comment_author_IP' => __( 'Comment Author IP' ), 3954 'comment_agent' => __( 'Comment Author User Agent' ), 3955 'comment_date' => __( 'Comment Date' ), 3956 'comment_content' => __( 'Comment Content' ), 3957 'comment_link' => __( 'Comment URL' ), 3958 ); 3959 3960 foreach ( (array) $comments as $comment ) { 3961 $comment_data_to_export = array(); 3962 3963 foreach ( $comment_prop_to_export as $key => $name ) { 3964 $value = ''; 3965 3966 switch ( $key ) { 3967 case 'comment_author': 3968 case 'comment_author_email': 3969 case 'comment_author_url': 3970 case 'comment_author_IP': 3971 case 'comment_agent': 3972 case 'comment_date': 3973 $value = $comment->{$key}; 3974 break; 3975 3976 case 'comment_content': 3977 $value = get_comment_text( $comment->comment_ID ); 3978 break; 3979 3980 case 'comment_link': 3981 $value = get_comment_link( $comment->comment_ID ); 3982 $value = sprintf( 3983 '<a href="%s" target="_blank">%s</a>', 3984 esc_url( $value ), 3985 esc_html( $value ) 3986 ); 3987 break; 3988 } 3989 3990 if ( ! empty( $value ) ) { 3991 $comment_data_to_export[] = array( 3992 'name' => $name, 3993 'value' => $value, 3994 ); 3995 } 3996 } 3997 3998 $data_to_export[] = array( 3999 'group_id' => 'comments', 4000 'group_label' => __( 'Comments' ), 4001 'group_description' => __( 'User’s comment data.' ), 4002 'item_id' => "comment-{$comment->comment_ID}", 4003 'data' => $comment_data_to_export, 4004 ); 4005 } 4006 4007 $done = count( $comments ) < $number; 4008 4009 return array( 4010 'data' => $data_to_export, 4011 'done' => $done, 4012 ); 4013 } 4014 4015 /** 4016 * Registers the personal data eraser for comments. 4017 * 4018 * @since 4.9.6 4019 * 4020 * @param array $erasers An array of personal data erasers. 4021 * @return array An array of personal data erasers. 4022 */ 4023 function wp_register_comment_personal_data_eraser( $erasers ) { 4024 $erasers['wordpress-comments'] = array( 4025 'eraser_friendly_name' => __( 'WordPress Comments' ), 4026 'callback' => 'wp_comments_personal_data_eraser', 4027 ); 4028 4029 return $erasers; 4030 } 4031 4032 /** 4033 * Erases personal data associated with an email address from the comments table. 4034 * 4035 * @since 4.9.6 4036 * 4037 * @global wpdb $wpdb WordPress database abstraction object. 4038 * 4039 * @param string $email_address The comment author email address. 4040 * @param int $page Comment page number. 4041 * @return array { 4042 * Data removal results. 4043 * 4044 * @type bool $items_removed Whether items were actually removed. 4045 * @type bool $items_retained Whether items were retained. 4046 * @type string[] $messages An array of messages to add to the personal data export file. 4047 * @type bool $done Whether the eraser is finished. 4048 * } 4049 */ 4050 function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 4051 global $wpdb; 4052 4053 if ( empty( $email_address ) ) { 4054 return array( 4055 'items_removed' => false, 4056 'items_retained' => false, 4057 'messages' => array(), 4058 'done' => true, 4059 ); 4060 } 4061 4062 // Limit us to 500 comments at a time to avoid timing out. 4063 $number = 500; 4064 $page = (int) $page; 4065 $items_removed = false; 4066 $items_retained = false; 4067 4068 $comments = get_comments( 4069 array( 4070 'author_email' => $email_address, 4071 'number' => $number, 4072 'paged' => $page, 4073 'orderby' => 'comment_ID', 4074 'order' => 'ASC', 4075 'include_unapproved' => true, 4076 ) 4077 ); 4078 4079 /* translators: Name of a comment's author after being anonymized. */ 4080 $anon_author = __( 'Anonymous' ); 4081 $messages = array(); 4082 4083 foreach ( (array) $comments as $comment ) { 4084 $anonymized_comment = array(); 4085 $anonymized_comment['comment_agent'] = ''; 4086 $anonymized_comment['comment_author'] = $anon_author; 4087 $anonymized_comment['comment_author_email'] = ''; 4088 $anonymized_comment['comment_author_IP'] = wp_privacy_anonymize_data( 'ip', $comment->comment_author_IP ); 4089 $anonymized_comment['comment_author_url'] = ''; 4090 $anonymized_comment['user_id'] = 0; 4091 4092 $comment_id = (int) $comment->comment_ID; 4093 4094 /** 4095 * Filters whether to anonymize the comment. 4096 * 4097 * @since 4.9.6 4098 * 4099 * @param bool|string $anon_message Whether to apply the comment anonymization (bool) or a custom 4100 * message (string). Default true. 4101 * @param WP_Comment $comment WP_Comment object. 4102 * @param array $anonymized_comment Anonymized comment data. 4103 */ 4104 $anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment ); 4105 4106 if ( true !== $anon_message ) { 4107 if ( $anon_message && is_string( $anon_message ) ) { 4108 $messages[] = esc_html( $anon_message ); 4109 } else { 4110 /* translators: %d: Comment ID. */ 4111 $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id ); 4112 } 4113 4114 $items_retained = true; 4115 4116 continue; 4117 } 4118 4119 $args = array( 4120 'comment_ID' => $comment_id, 4121 ); 4122 4123 $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args ); 4124 4125 if ( $updated ) { 4126 $items_removed = true; 4127 clean_comment_cache( $comment_id ); 4128 } else { 4129 $items_retained = true; 4130 } 4131 } 4132 4133 $done = count( $comments ) < $number; 4134 4135 return array( 4136 'items_removed' => $items_removed, 4137 'items_retained' => $items_retained, 4138 'messages' => $messages, 4139 'done' => $done, 4140 ); 4141 } 4142 4143 /** 4144 * Sets the last changed time for the 'comment' cache group. 4145 * 4146 * @since 5.0.0 4147 */ 4148 function wp_cache_set_comments_last_changed() { 4149 wp_cache_set_last_changed( 'comment' ); 4150 } 4151 4152 /** 4153 * Updates the comment type for a batch of comments. 4154 * 4155 * @since 5.5.0 4156 * 4157 * @global wpdb $wpdb WordPress database abstraction object. 4158 */ 4159 function _wp_batch_update_comment_type() { 4160 global $wpdb; 4161 4162 $lock_name = 'update_comment_type.lock'; 4163 4164 // Try to lock. 4165 $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) ); 4166 4167 if ( ! $lock_result ) { 4168 $lock_result = get_option( $lock_name ); 4169 4170 // Bail if we were unable to create a lock, or if the existing lock is still valid. 4171 if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { 4172 wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); 4173 return; 4174 } 4175 } 4176 4177 // Update the lock, as by this point we've definitely got a lock, just need to fire the actions. 4178 update_option( $lock_name, time() ); 4179 4180 // Check if there's still an empty comment type. 4181 $empty_comment_type = $wpdb->get_var( 4182 "SELECT comment_ID FROM $wpdb->comments 4183 WHERE comment_type = '' 4184 LIMIT 1" 4185 ); 4186 4187 // No empty comment type, we're done here. 4188 if ( ! $empty_comment_type ) { 4189 update_option( 'finished_updating_comment_type', true ); 4190 delete_option( $lock_name ); 4191 return; 4192 } 4193 4194 // Empty comment type found? We'll need to run this script again. 4195 wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); 4196 4197 /** 4198 * Filters the comment batch size for updating the comment type. 4199 * 4200 * @since 5.5.0 4201 * 4202 * @param int $comment_batch_size The comment batch size. Default 100. 4203 */ 4204 $comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 ); 4205 4206 // Get the IDs of the comments to update. 4207 $comment_ids = $wpdb->get_col( 4208 $wpdb->prepare( 4209 "SELECT comment_ID 4210 FROM {$wpdb->comments} 4211 WHERE comment_type = '' 4212 ORDER BY comment_ID DESC 4213 LIMIT %d", 4214 $comment_batch_size 4215 ) 4216 ); 4217 4218 if ( $comment_ids ) { 4219 $comment_id_list = implode( ',', $comment_ids ); 4220 4221 // Update the `comment_type` field value to be `comment` for the next batch of comments. 4222 $wpdb->query( 4223 "UPDATE {$wpdb->comments} 4224 SET comment_type = 'comment' 4225 WHERE comment_type = '' 4226 AND comment_ID IN ({$comment_id_list})" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 4227 ); 4228 4229 // Make sure to clean the comment cache. 4230 clean_comment_cache( $comment_ids ); 4231 } 4232 4233 delete_option( $lock_name ); 4234 } 4235 4236 /** 4237 * In order to avoid the _wp_batch_update_comment_type() job being accidentally removed, 4238 * check that it's still scheduled while we haven't finished updating comment types. 4239 * 4240 * @ignore 4241 * @since 5.5.0 4242 */ 4243 function _wp_check_for_scheduled_update_comment_type() { 4244 if ( ! get_option( 'finished_updating_comment_type' ) && ! wp_next_scheduled( 'wp_update_comment_type_batch' ) ) { 4245 wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_update_comment_type_batch' ); 4246 } 4247 } 4248 4249 /** 4250 * Register initial note status meta. 4251 * 4252 * @since 6.9.0 4253 */ 4254 function wp_create_initial_comment_meta() { 4255 register_meta( 4256 'comment', 4257 '_wp_note_status', 4258 array( 4259 'type' => 'string', 4260 'description' => __( 'Note resolution status' ), 4261 'single' => true, 4262 'show_in_rest' => array( 4263 'schema' => array( 4264 'type' => 'string', 4265 'enum' => array( 'resolved', 'reopen' ), 4266 ), 4267 ), 4268 'auth_callback' => function ( $allowed, $meta_key, $object_id ) { 4269 return current_user_can( 'edit_comment', $object_id ); 4270 }, 4271 ) 4272 ); 4273 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Jun 14 08:20:09 2026 | Cross-referenced by PHPXref |