[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 3 // We plan to gradually remove all of the disabled lint rules below. 4 // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash 5 // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized 6 // phpcs:disable WordPress.Security.NonceVerification.Missing 7 // phpcs:disable Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure 8 9 class Akismet { 10 const API_HOST = 'rest.akismet.com'; 11 const API_PORT = 80; 12 const MAX_DELAY_BEFORE_MODERATION_EMAIL = 86400; // One day in seconds 13 const ALERT_CODE_COMMERCIAL = 30001; 14 15 public static $limit_notices = array( 16 10501 => 'FIRST_MONTH_OVER_LIMIT', 17 10502 => 'SECOND_MONTH_OVER_LIMIT', 18 10504 => 'THIRD_MONTH_APPROACHING_LIMIT', 19 10508 => 'THIRD_MONTH_OVER_LIMIT', 20 10516 => 'FOUR_PLUS_MONTHS_OVER_LIMIT', 21 ); 22 23 private static $last_comment = ''; 24 private static $initiated = false; 25 private static $prevent_moderation_email_for_these_comments = array(); 26 private static $last_comment_result = null; 27 private static $comment_as_submitted_allowed_keys = array( 28 'blog' => '', 29 'blog_charset' => '', 30 'blog_lang' => '', 31 'blog_ua' => '', 32 'comment_agent' => '', 33 'comment_author' => '', 34 'comment_author_IP' => '', 35 'comment_author_email' => '', 36 'comment_author_url' => '', 37 'comment_content' => '', 38 'comment_date_gmt' => '', 39 'comment_tags' => '', 40 'comment_type' => '', 41 'guid' => '', 42 'is_test' => '', 43 'permalink' => '', 44 'reporter' => '', 45 'site_domain' => '', 46 'submit_referer' => '', 47 'submit_uri' => '', 48 'user_ID' => '', 49 'user_agent' => '', 50 'user_id' => '', 51 'user_ip' => '', 52 ); 53 54 public static function init() { 55 if ( ! self::$initiated ) { 56 self::init_hooks(); 57 } 58 } 59 60 /** 61 * Initializes WordPress hooks 62 */ 63 private static function init_hooks() { 64 self::$initiated = true; 65 66 add_action( 'wp_insert_comment', array( 'Akismet', 'auto_check_update_meta' ), 10, 2 ); 67 add_filter( 'preprocess_comment', array( 'Akismet', 'auto_check_comment' ), 1 ); 68 add_filter( 'rest_pre_insert_comment', array( 'Akismet', 'rest_auto_check_comment' ), 1 ); 69 70 add_action( 'comment_form', array( 'Akismet', 'load_form_js' ) ); 71 add_action( 'do_shortcode_tag', array( 'Akismet', 'load_form_js_via_filter' ), 10, 4 ); 72 73 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments' ) ); 74 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments_meta' ) ); 75 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_orphaned_commentmeta' ) ); 76 add_action( 'akismet_schedule_cron_recheck', array( 'Akismet', 'cron_recheck' ) ); 77 78 add_action( 'comment_form', array( 'Akismet', 'add_comment_nonce' ), 1 ); 79 add_action( 'comment_form', array( 'Akismet', 'output_custom_form_fields' ) ); 80 add_filter( 'script_loader_tag', array( 'Akismet', 'set_form_js_async' ), 10, 3 ); 81 82 add_filter( 'comment_moderation_recipients', array( 'Akismet', 'disable_moderation_emails_if_unreachable' ), 1000, 2 ); 83 add_filter( 'pre_comment_approved', array( 'Akismet', 'last_comment_status' ), 10, 2 ); 84 85 add_action( 'transition_comment_status', array( 'Akismet', 'transition_comment_status' ), 10, 3 ); 86 87 // Run this early in the pingback call, before doing a remote fetch of the source uri 88 add_action( 'xmlrpc_call', array( 'Akismet', 'pre_check_pingback' ), 10, 3 ); 89 90 // Jetpack compatibility 91 add_filter( 'jetpack_options_whitelist', array( 'Akismet', 'add_to_jetpack_options_whitelist' ) ); 92 add_filter( 'jetpack_contact_form_html', array( 'Akismet', 'inject_custom_form_fields' ) ); 93 add_filter( 'jetpack_contact_form_akismet_values', array( 'Akismet', 'prepare_custom_form_values' ) ); 94 95 // Gravity Forms 96 add_filter( 'gform_get_form_filter', array( 'Akismet', 'inject_custom_form_fields' ) ); 97 add_filter( 'gform_akismet_fields', array( 'Akismet', 'prepare_custom_form_values' ) ); 98 99 // Contact Form 7 100 add_filter( 'wpcf7_form_elements', array( 'Akismet', 'append_custom_form_fields' ) ); 101 add_filter( 'wpcf7_akismet_parameters', array( 'Akismet', 'prepare_custom_form_values' ) ); 102 103 // Formidable Forms 104 add_filter( 'frm_filter_final_form', array( 'Akismet', 'inject_custom_form_fields' ) ); 105 add_filter( 'frm_akismet_values', array( 'Akismet', 'prepare_custom_form_values' ) ); 106 107 // Fluent Forms 108 /* 109 * The Fluent Forms hook names were updated in version 5.0.0. The last version that supported 110 * the original hook names was 4.3.25, and version 4.3.25 was tested up to WordPress version 6.1. 111 * 112 * The legacy hooks are fired before the new hooks. See 113 * https://github.com/fluentform/fluentform/commit/cc45341afcae400f217470a7bbfb15efdd80454f 114 * 115 * The legacy Fluent Forms hooks will be removed when Akismet no longer supports WordPress version 6.1. 116 * This will provide compatibility with previous versions of Fluent Forms for a reasonable amount of time. 117 */ 118 add_filter( 'fluentform_form_element_start', array( 'Akismet', 'output_custom_form_fields' ) ); 119 add_filter( 'fluentform_akismet_fields', array( 'Akismet', 'prepare_custom_form_values' ), 10, 2 ); 120 // Current Fluent Form hooks. 121 add_filter( 'fluentform/form_element_start', array( 'Akismet', 'output_custom_form_fields' ) ); 122 add_filter( 'fluentform/akismet_fields', array( 'Akismet', 'prepare_custom_form_values' ), 10, 2 ); 123 124 add_action( 'update_option_wordpress_api_key', array( 'Akismet', 'updated_option' ), 10, 2 ); 125 add_action( 'add_option_wordpress_api_key', array( 'Akismet', 'added_option' ), 10, 2 ); 126 127 add_action( 'comment_form_after', array( 'Akismet', 'display_comment_form_privacy_notice' ) ); 128 } 129 130 public static function get_api_key() { 131 return apply_filters( 'akismet_get_api_key', defined( 'WPCOM_API_KEY' ) ? constant( 'WPCOM_API_KEY' ) : get_option( 'wordpress_api_key' ) ); 132 } 133 134 /** 135 * Exchange the API key for a token that can only be used to access stats pages. 136 * 137 * @return string 138 */ 139 public static function get_access_token() { 140 static $access_token = null; 141 142 if ( is_null( $access_token ) ) { 143 $request_args = array( 'api_key' => self::get_api_key() ); 144 145 $request_args = apply_filters( 'akismet_request_args', $request_args, 'token' ); 146 147 $response = self::http_post( self::build_query( $request_args ), 'token' ); 148 149 $access_token = $response[1]; 150 } 151 152 return $access_token; 153 } 154 155 public static function check_key_status( $key, $ip = null ) { 156 $request_args = array( 157 'key' => $key, 158 'blog' => get_option( 'home' ), 159 ); 160 161 $request_args = apply_filters( 'akismet_request_args', $request_args, 'verify-key' ); 162 163 return self::http_post( self::build_query( $request_args ), 'verify-key', $ip ); 164 } 165 166 public static function verify_key( $key, $ip = null ) { 167 // Shortcut for obviously invalid keys. 168 if ( strlen( $key ) != 12 ) { 169 return 'invalid'; 170 } 171 172 $response = self::check_key_status( $key, $ip ); 173 174 if ( $response[1] != 'valid' && $response[1] != 'invalid' ) { 175 return 'failed'; 176 } 177 178 return $response[1]; 179 } 180 181 public static function deactivate_key( $key ) { 182 $request_args = array( 183 'key' => $key, 184 'blog' => get_option( 'home' ), 185 ); 186 187 $request_args = apply_filters( 'akismet_request_args', $request_args, 'deactivate' ); 188 189 $response = self::http_post( self::build_query( $request_args ), 'deactivate' ); 190 191 if ( $response[1] != 'deactivated' ) { 192 return 'failed'; 193 } 194 195 return $response[1]; 196 } 197 198 /** 199 * Add the akismet option to the Jetpack options management whitelist. 200 * 201 * @param array $options The list of whitelisted option names. 202 * @return array The updated whitelist 203 */ 204 public static function add_to_jetpack_options_whitelist( $options ) { 205 $options[] = 'wordpress_api_key'; 206 return $options; 207 } 208 209 /** 210 * When the akismet option is updated, run the registration call. 211 * 212 * This should only be run when the option is updated from the Jetpack/WP.com 213 * API call, and only if the new key is different than the old key. 214 * 215 * @param mixed $old_value The old option value. 216 * @param mixed $value The new option value. 217 */ 218 public static function updated_option( $old_value, $value ) { 219 // Not an API call 220 if ( ! class_exists( 'WPCOM_JSON_API_Update_Option_Endpoint' ) ) { 221 return; 222 } 223 // Only run the registration if the old key is different. 224 if ( $old_value !== $value ) { 225 self::verify_key( $value ); 226 } 227 } 228 229 /** 230 * Treat the creation of an API key the same as updating the API key to a new value. 231 * 232 * @param mixed $option_name Will always be "wordpress_api_key", until something else hooks in here. 233 * @param mixed $value The option value. 234 */ 235 public static function added_option( $option_name, $value ) { 236 if ( 'wordpress_api_key' === $option_name ) { 237 return self::updated_option( '', $value ); 238 } 239 } 240 241 public static function rest_auto_check_comment( $commentdata ) { 242 return self::auto_check_comment( $commentdata, 'rest_api' ); 243 } 244 245 /** 246 * Check a comment for spam. 247 * 248 * @param array $commentdata 249 * @param string $context What kind of request triggered this comment check? Possible values are 'default', 'rest_api', and 'xml-rpc'. 250 * @return array|WP_Error Either the $commentdata array with additional entries related to its spam status 251 * or a WP_Error, if it's a REST API request and the comment should be discarded. 252 */ 253 public static function auto_check_comment( $commentdata, $context = 'default' ) { 254 // If no key is configured, then there's no point in doing any of this. 255 if ( ! self::get_api_key() ) { 256 return $commentdata; 257 } 258 259 if ( ! isset( $commentdata['comment_meta'] ) ) { 260 $commentdata['comment_meta'] = array(); 261 } 262 263 self::$last_comment_result = null; 264 265 // Skip the Akismet check if the comment matches the Disallowed Keys list. 266 if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { 267 $comment_author = isset( $commentdata['comment_author'] ) ? $commentdata['comment_author'] : ''; 268 $comment_author_email = isset( $commentdata['comment_author_email'] ) ? $commentdata['comment_author_email'] : ''; 269 $comment_author_url = isset( $commentdata['comment_author_url'] ) ? $commentdata['comment_author_url'] : ''; 270 $comment_content = isset( $commentdata['comment_content'] ) ? $commentdata['comment_content'] : ''; 271 $comment_author_ip = isset( $commentdata['comment_author_IP'] ) ? $commentdata['comment_author_IP'] : ''; 272 $comment_agent = isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : ''; 273 274 if ( wp_check_comment_disallowed_list( $comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_ip, $comment_agent ) ) { 275 $commentdata['akismet_result'] = 'skipped'; 276 $commentdata['comment_meta']['akismet_result'] = 'skipped'; 277 278 $commentdata['akismet_skipped_microtime'] = microtime( true ); 279 $commentdata['comment_meta']['akismet_skipped_microtime'] = $commentdata['akismet_skipped_microtime']; 280 281 self::set_last_comment( $commentdata ); 282 283 return $commentdata; 284 } 285 } 286 287 $comment = $commentdata; 288 289 $comment['user_ip'] = self::get_ip_address(); 290 $comment['user_agent'] = self::get_user_agent(); 291 $comment['referrer'] = self::get_referer(); 292 $comment['blog'] = get_option( 'home' ); 293 $comment['blog_lang'] = get_locale(); 294 $comment['blog_charset'] = get_option( 'blog_charset' ); 295 $comment['permalink'] = get_permalink( $comment['comment_post_ID'] ); 296 297 if ( ! empty( $comment['user_ID'] ) ) { 298 $comment['user_role'] = self::get_user_roles( $comment['user_ID'] ); 299 } 300 301 /** See filter documentation in init_hooks(). */ 302 $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) ); 303 $comment['akismet_comment_nonce'] = 'inactive'; 304 if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) { 305 $comment['akismet_comment_nonce'] = 'failed'; 306 if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) ) { 307 $comment['akismet_comment_nonce'] = 'passed'; 308 } 309 310 // comment reply in wp-admin 311 if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) ) { 312 $comment['akismet_comment_nonce'] = 'passed'; 313 } 314 } 315 316 if ( self::is_test_mode() ) { 317 $comment['is_test'] = 'true'; 318 } 319 320 foreach ( $_POST as $key => $value ) { 321 if ( is_string( $value ) ) { 322 $comment[ "POST_{$key}" ] = $value; 323 } 324 } 325 326 foreach ( $_SERVER as $key => $value ) { 327 if ( ! is_string( $value ) ) { 328 continue; 329 } 330 331 if ( preg_match( '/^HTTP_COOKIE/', $key ) ) { 332 continue; 333 } 334 335 // Send any potentially useful $_SERVER vars, but avoid sending junk we don't need. 336 if ( preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key ) ) { 337 $comment[ "$key" ] = $value; 338 } 339 } 340 341 $post = get_post( $comment['comment_post_ID'] ); 342 343 if ( ! is_null( $post ) ) { 344 // $post can technically be null, although in the past, it's always been an indicator of another plugin interfering. 345 $comment['comment_post_modified_gmt'] = $post->post_modified_gmt; 346 347 // Tags and categories are important context in which to consider the comment. 348 $comment['comment_context'] = array(); 349 350 $tag_names = wp_get_post_tags( $post->ID, array( 'fields' => 'names' ) ); 351 352 if ( $tag_names && ! is_wp_error( $tag_names ) ) { 353 foreach ( $tag_names as $tag_name ) { 354 $comment['comment_context'][] = $tag_name; 355 } 356 } 357 358 $category_names = wp_get_post_categories( $post->ID, array( 'fields' => 'names' ) ); 359 360 if ( $category_names && ! is_wp_error( $category_names ) ) { 361 foreach ( $category_names as $category_name ) { 362 $comment['comment_context'][] = $category_name; 363 } 364 } 365 } 366 367 /** 368 * Filter the data that is used to generate the request body for the API call. 369 * 370 * @since 5.3.1 371 * 372 * @param array $comment An array of request data. 373 * @param string $endpoint The API endpoint being requested. 374 */ 375 $comment = apply_filters( 'akismet_request_args', $comment, 'comment-check' ); 376 377 $response = self::http_post( self::build_query( $comment ), 'comment-check' ); 378 379 do_action( 'akismet_comment_check_response', $response ); 380 381 $commentdata['comment_as_submitted'] = array_intersect_key( $comment, self::$comment_as_submitted_allowed_keys ); 382 383 // Also include any form fields we inject into the comment form, like ak_js 384 foreach ( $_POST as $key => $value ) { 385 if ( is_string( $value ) && strpos( $key, 'ak_' ) === 0 ) { 386 $commentdata['comment_as_submitted'][ 'POST_' . $key ] = $value; 387 } 388 } 389 390 $commentdata['akismet_result'] = $response[1]; 391 392 if ( 'true' === $response[1] || 'false' === $response[1] ) { 393 $commentdata['comment_meta']['akismet_result'] = $response[1]; 394 } else { 395 $commentdata['comment_meta']['akismet_error'] = time(); 396 } 397 398 if ( isset( $response[0]['x-akismet-pro-tip'] ) ) { 399 $commentdata['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip']; 400 $commentdata['comment_meta']['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip']; 401 } 402 403 if ( isset( $response[0]['x-akismet-guid'] ) ) { 404 $commentdata['akismet_guid'] = $response[0]['x-akismet-guid']; 405 $commentdata['comment_meta']['akismet_guid'] = $response[0]['x-akismet-guid']; 406 } 407 408 $commentdata['comment_meta']['akismet_as_submitted'] = $commentdata['comment_as_submitted']; 409 410 if ( isset( $response[0]['x-akismet-error'] ) ) { 411 // An error occurred that we anticipated (like a suspended key) and want the user to act on. 412 // Send to moderation. 413 self::$last_comment_result = '0'; 414 } elseif ( 'true' == $response[1] ) { 415 // akismet_spam_count will be incremented later by comment_is_spam() 416 self::$last_comment_result = 'spam'; 417 418 $discard = ( isset( $commentdata['akismet_pro_tip'] ) && $commentdata['akismet_pro_tip'] === 'discard' && self::allow_discard() ); 419 420 do_action( 'akismet_spam_caught', $discard ); 421 422 if ( $discard ) { 423 // The spam is obvious, so we're bailing out early. 424 // akismet_result_spam() won't be called so bump the counter here 425 if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) { 426 update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr ); 427 } 428 429 if ( 'rest_api' === $context ) { 430 return new WP_Error( 'akismet_rest_comment_discarded', __( 'Comment discarded.', 'akismet' ) ); 431 } elseif ( 'xml-rpc' === $context ) { 432 // If this is a pingback that we're pre-checking, the discard behavior is the same as the normal spam response behavior. 433 return $commentdata; 434 } else { 435 // Redirect back to the previous page, or failing that, the post permalink, or failing that, the homepage of the blog. 436 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ( $post ? get_permalink( $post ) : home_url() ); 437 wp_safe_redirect( esc_url_raw( $redirect_to ) ); 438 die(); 439 } 440 } elseif ( 'rest_api' === $context ) { 441 // The way the REST API structures its calls, we can set the comment_approved value right away. 442 $commentdata['comment_approved'] = 'spam'; 443 } 444 } 445 446 // if the response is neither true nor false, hold the comment for moderation and schedule a recheck 447 if ( 'true' != $response[1] && 'false' != $response[1] ) { 448 if ( ! current_user_can( 'moderate_comments' ) ) { 449 // Comment status should be moderated 450 self::$last_comment_result = '0'; 451 } 452 453 if ( ! wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) { 454 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); 455 do_action( 'akismet_scheduled_recheck', 'invalid-response-' . $response[1] ); 456 } 457 458 self::$prevent_moderation_email_for_these_comments[] = $commentdata; 459 } 460 461 // Delete old comments daily 462 if ( ! wp_next_scheduled( 'akismet_scheduled_delete' ) ) { 463 wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' ); 464 } 465 466 self::set_last_comment( $commentdata ); 467 self::fix_scheduled_recheck(); 468 469 return $commentdata; 470 } 471 472 public static function get_last_comment() { 473 return self::$last_comment; 474 } 475 476 public static function set_last_comment( $comment ) { 477 if ( is_null( $comment ) ) { 478 // This never happens in our code. 479 self::$last_comment = null; 480 } else { 481 // We filter it here so that it matches the filtered comment data that we'll have to compare against later. 482 // wp_filter_comment expects comment_author_IP 483 self::$last_comment = wp_filter_comment( 484 array_merge( 485 array( 'comment_author_IP' => self::get_ip_address() ), 486 $comment 487 ) 488 ); 489 } 490 } 491 492 // this fires on wp_insert_comment. we can't update comment_meta when auto_check_comment() runs 493 // because we don't know the comment ID at that point. 494 public static function auto_check_update_meta( $id, $comment ) { 495 // wp_insert_comment() might be called in other contexts, so make sure this is the same comment 496 // as was checked by auto_check_comment 497 if ( is_object( $comment ) && ! empty( self::$last_comment ) && is_array( self::$last_comment ) ) { 498 if ( self::matches_last_comment_by_id( $id ) ) { 499 // normal result: true or false 500 if ( isset( self::$last_comment['akismet_result'] ) && self::$last_comment['akismet_result'] == 'true' ) { 501 self::update_comment_history( $comment->comment_ID, '', 'check-spam' ); 502 if ( $comment->comment_approved != 'spam' ) { 503 self::update_comment_history( 504 $comment->comment_ID, 505 '', 506 'status-changed-' . $comment->comment_approved 507 ); 508 } 509 } elseif ( isset( self::$last_comment['akismet_result'] ) && self::$last_comment['akismet_result'] == 'false' ) { 510 self::update_comment_history( $comment->comment_ID, '', 'check-ham' ); 511 // Status could be spam or trash, depending on the WP version and whether this change applies: 512 // https://core.trac.wordpress.org/changeset/34726 513 if ( $comment->comment_approved == 'spam' || $comment->comment_approved == 'trash' ) { 514 if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { 515 if ( wp_check_comment_disallowed_list( $comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent ) ) { 516 self::update_comment_history( $comment->comment_ID, '', 'wp-disallowed' ); 517 } else { 518 self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved ); 519 } 520 } else { 521 self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved ); 522 } 523 } 524 } elseif ( isset( self::$last_comment['akismet_result'] ) && 'skipped' == self::$last_comment['akismet_result'] ) { 525 // The comment wasn't sent to Akismet because it matched the disallowed comment keys. 526 self::update_comment_history( $comment->comment_ID, '', 'wp-disallowed' ); 527 self::update_comment_history( $comment->comment_ID, '', 'akismet-skipped-disallowed' ); 528 } else if ( ! isset( self::$last_comment['akismet_result'] ) ) { 529 // Add a generic skipped history item. 530 self::update_comment_history( $comment->comment_ID, '', 'akismet-skipped' ); 531 } else { 532 // abnormal result: error 533 self::update_comment_history( 534 $comment->comment_ID, 535 '', 536 'check-error', 537 array( 'response' => substr( self::$last_comment['akismet_result'], 0, 50 ) ) 538 ); 539 } 540 } 541 } 542 } 543 544 public static function delete_old_comments() { 545 global $wpdb; 546 547 /** 548 * Determines how many comments will be deleted in each batch. 549 * 550 * @param int The default, as defined by AKISMET_DELETE_LIMIT. 551 */ 552 $delete_limit = apply_filters( 'akismet_delete_comment_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 ); 553 $delete_limit = max( 1, intval( $delete_limit ) ); 554 555 /** 556 * Determines how many days a comment will be left in the Spam queue before being deleted. 557 * 558 * @param int The default number of days. 559 */ 560 $delete_interval = apply_filters( 'akismet_delete_comment_interval', 15 ); 561 $delete_interval = max( 1, intval( $delete_interval ) ); 562 563 while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL %d DAY) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", $delete_interval, $delete_limit ) ) ) { 564 if ( empty( $comment_ids ) ) { 565 return; 566 } 567 568 $wpdb->queries = array(); 569 570 $comments = array(); 571 572 foreach ( $comment_ids as $comment_id ) { 573 $comments[ $comment_id ] = get_comment( $comment_id ); 574 575 do_action( 'delete_comment', $comment_id, $comments[ $comment_id ] ); 576 do_action( 'akismet_batch_delete_count', __FUNCTION__ ); 577 } 578 579 // Prepared as strings since comment_id is an unsigned BIGINT, and using %d will constrain the value to the maximum signed BIGINT. 580 $format_string = implode( ', ', array_fill( 0, is_countable( $comment_ids ) ? count( $comment_ids ) : 0, '%s' ) ); 581 582 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE comment_id IN ( " . $format_string . ' )', $comment_ids ) ); 583 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( " . $format_string . ' )', $comment_ids ) ); 584 585 foreach ( $comment_ids as $comment_id ) { 586 do_action( 'deleted_comment', $comment_id, $comments[ $comment_id ] ); 587 unset( $comments[ $comment_id ] ); 588 } 589 590 clean_comment_cache( $comment_ids ); 591 do_action( 'akismet_delete_comment_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 ); 592 } 593 594 if ( apply_filters( 'akismet_optimize_table', ( mt_rand( 1, 5000 ) == 11 ), $wpdb->comments ) ) { // lucky number 595 $wpdb->query( "OPTIMIZE TABLE {$wpdb->comments}" ); 596 } 597 } 598 599 public static function delete_old_comments_meta() { 600 global $wpdb; 601 602 $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 ); 603 604 // enforce a minimum of 1 day 605 $interval = absint( $interval ); 606 if ( $interval < 1 ) { 607 $interval = 1; 608 } 609 610 // akismet_as_submitted meta values are large, so expire them 611 // after $interval days regardless of the comment status 612 while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT m.comment_id FROM {$wpdb->commentmeta} as m INNER JOIN {$wpdb->comments} as c USING(comment_id) WHERE m.meta_key = 'akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > c.comment_date_gmt LIMIT 10000", $interval ) ) ) { 613 if ( empty( $comment_ids ) ) { 614 return; 615 } 616 617 $wpdb->queries = array(); 618 619 foreach ( $comment_ids as $comment_id ) { 620 delete_comment_meta( $comment_id, 'akismet_as_submitted' ); 621 do_action( 'akismet_batch_delete_count', __FUNCTION__ ); 622 } 623 624 do_action( 'akismet_delete_commentmeta_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 ); 625 } 626 627 if ( apply_filters( 'akismet_optimize_table', ( mt_rand( 1, 5000 ) == 11 ), $wpdb->commentmeta ) ) { // lucky number 628 $wpdb->query( "OPTIMIZE TABLE {$wpdb->commentmeta}" ); 629 } 630 } 631 632 // Clear out comments meta that no longer have corresponding comments in the database 633 public static function delete_orphaned_commentmeta() { 634 global $wpdb; 635 636 $last_meta_id = 0; 637 $start_time = isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true ); 638 $max_exec_time = max( ini_get( 'max_execution_time' ) - 5, 3 ); 639 640 while ( $commentmeta_results = $wpdb->get_results( $wpdb->prepare( "SELECT m.meta_id, m.comment_id, m.meta_key FROM {$wpdb->commentmeta} as m LEFT JOIN {$wpdb->comments} as c USING(comment_id) WHERE c.comment_id IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT 1000", $last_meta_id ) ) ) { 641 if ( empty( $commentmeta_results ) ) { 642 return; 643 } 644 645 $wpdb->queries = array(); 646 647 $commentmeta_deleted = 0; 648 649 foreach ( $commentmeta_results as $commentmeta ) { 650 if ( 'akismet_' == substr( $commentmeta->meta_key, 0, 8 ) ) { 651 delete_comment_meta( $commentmeta->comment_id, $commentmeta->meta_key ); 652 do_action( 'akismet_batch_delete_count', __FUNCTION__ ); 653 ++$commentmeta_deleted; 654 } 655 656 $last_meta_id = $commentmeta->meta_id; 657 } 658 659 do_action( 'akismet_delete_commentmeta_batch', $commentmeta_deleted ); 660 661 // If we're getting close to max_execution_time, quit for this round. 662 if ( microtime( true ) - $start_time > $max_exec_time ) { 663 return; 664 } 665 } 666 667 if ( apply_filters( 'akismet_optimize_table', ( mt_rand( 1, 5000 ) == 11 ), $wpdb->commentmeta ) ) { // lucky number 668 $wpdb->query( "OPTIMIZE TABLE {$wpdb->commentmeta}" ); 669 } 670 } 671 672 // how many approved comments does this author have? 673 public static function get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) { 674 global $wpdb; 675 676 /** 677 * Which comment types should be ignored when counting a user's approved comments? 678 * 679 * Some plugins add entries to the comments table that are not actual 680 * comments that could have been checked by Akismet. Allow these comments 681 * to be excluded from the "approved comment count" query in order to 682 * avoid artificially inflating the approved comment count. 683 * 684 * @param array $comment_types An array of comment types that won't be considered 685 * when counting a user's approved comments. 686 * 687 * @since 4.2.2 688 */ 689 $excluded_comment_types = apply_filters( 'akismet_excluded_comment_types', array() ); 690 691 $comment_type_where = ''; 692 693 if ( is_array( $excluded_comment_types ) && ! empty( $excluded_comment_types ) ) { 694 $excluded_comment_types = array_unique( $excluded_comment_types ); 695 696 foreach ( $excluded_comment_types as $excluded_comment_type ) { 697 $comment_type_where .= $wpdb->prepare( ' AND comment_type <> %s ', $excluded_comment_type ); 698 } 699 } 700 701 if ( ! empty( $user_id ) ) { 702 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = 1" . $comment_type_where, $user_id ) ); 703 } 704 705 if ( ! empty( $comment_author_email ) ) { 706 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_author_email = %s AND comment_author = %s AND comment_author_url = %s AND comment_approved = 1" . $comment_type_where, $comment_author_email, $comment_author, $comment_author_url ) ); 707 } 708 709 return 0; 710 } 711 712 /** 713 * Get the full comment history for a given comment, as an array in reverse chronological order. 714 * Each entry will have an 'event', a 'time', and possible a 'message' member (if the entry is old enough). 715 * Some entries will also have a 'user' or 'meta' member. 716 * 717 * @param int $comment_id The relevant comment ID. 718 * @return array|bool An array of history events, or false if there is no history. 719 */ 720 public static function get_comment_history( $comment_id ) { 721 $history = get_comment_meta( $comment_id, 'akismet_history', false ); 722 if ( empty( $history ) || empty( $history[0] ) ) { 723 return false; 724 } 725 726 /* 727 // To see all variants when testing. 728 $history[] = array( 'time' => 445856401, 'message' => 'Old versions of Akismet stored the message as a literal string in the commentmeta.', 'event' => null ); 729 $history[] = array( 'time' => 445856402, 'event' => 'recheck-spam' ); 730 $history[] = array( 'time' => 445856403, 'event' => 'check-spam' ); 731 $history[] = array( 'time' => 445856404, 'event' => 'recheck-ham' ); 732 $history[] = array( 'time' => 445856405, 'event' => 'check-ham' ); 733 $history[] = array( 'time' => 445856406, 'event' => 'wp-blacklisted' ); 734 $history[] = array( 'time' => 445856406, 'event' => 'wp-disallowed' ); 735 $history[] = array( 'time' => 445856407, 'event' => 'report-spam' ); 736 $history[] = array( 'time' => 445856408, 'event' => 'report-spam', 'user' => 'sam' ); 737 $history[] = array( 'message' => 'sam reported this comment as spam (hardcoded message).', 'time' => 445856400, 'event' => 'report-spam', 'user' => 'sam' ); 738 $history[] = array( 'time' => 445856409, 'event' => 'report-ham', 'user' => 'sam' ); 739 $history[] = array( 'message' => 'sam reported this comment as ham (hardcoded message).', 'time' => 445856400, 'event' => 'report-ham', 'user' => 'sam' ); // 740 $history[] = array( 'time' => 445856410, 'event' => 'cron-retry-spam' ); 741 $history[] = array( 'time' => 445856411, 'event' => 'cron-retry-ham' ); 742 $history[] = array( 'time' => 445856412, 'event' => 'check-error' ); // 743 $history[] = array( 'time' => 445856413, 'event' => 'check-error', 'meta' => array( 'response' => 'The server was taking a nap.' ) ); 744 $history[] = array( 'time' => 445856414, 'event' => 'recheck-error' ); // Should not generate a message. 745 $history[] = array( 'time' => 445856415, 'event' => 'recheck-error', 'meta' => array( 'response' => 'The server was taking a nap.' ) ); 746 $history[] = array( 'time' => 445856416, 'event' => 'status-changedtrash' ); 747 $history[] = array( 'time' => 445856417, 'event' => 'status-changedspam' ); 748 $history[] = array( 'time' => 445856418, 'event' => 'status-changedhold' ); 749 $history[] = array( 'time' => 445856419, 'event' => 'status-changedapprove' ); 750 $history[] = array( 'time' => 445856420, 'event' => 'status-changed-trash' ); 751 $history[] = array( 'time' => 445856421, 'event' => 'status-changed-spam' ); 752 $history[] = array( 'time' => 445856422, 'event' => 'status-changed-hold' ); 753 $history[] = array( 'time' => 445856423, 'event' => 'status-changed-approve' ); 754 $history[] = array( 'time' => 445856424, 'event' => 'status-trash', 'user' => 'sam' ); 755 $history[] = array( 'time' => 445856425, 'event' => 'status-spam', 'user' => 'sam' ); 756 $history[] = array( 'time' => 445856426, 'event' => 'status-hold', 'user' => 'sam' ); 757 $history[] = array( 'time' => 445856427, 'event' => 'status-approve', 'user' => 'sam' ); 758 $history[] = array( 'time' => 445856427, 'event' => 'webhook-spam' ); 759 $history[] = array( 'time' => 445856427, 'event' => 'webhook-ham' ); 760 $history[] = array( 'time' => 445856427, 'event' => 'webhook-spam-noaction' ); 761 $history[] = array( 'time' => 445856427, 'event' => 'webhook-ham-noaction' ); 762 */ 763 764 usort( $history, array( 'Akismet', '_cmp_time' ) ); 765 return $history; 766 } 767 768 /** 769 * Log an event for a given comment, storing it in comment_meta. 770 * 771 * @param int $comment_id The ID of the relevant comment. 772 * @param string $message The string description of the event. No longer used. 773 * @param string $event The event code. 774 * @param array $meta Metadata about the history entry. e.g., the user that reported or changed the status of a given comment. 775 */ 776 public static function update_comment_history( $comment_id, $message, $event = null, $meta = null ) { 777 global $current_user; 778 779 $user = ''; 780 781 $event = array( 782 'time' => self::_get_microtime(), 783 'event' => $event, 784 ); 785 786 if ( is_object( $current_user ) && isset( $current_user->user_login ) ) { 787 $event['user'] = $current_user->user_login; 788 } 789 790 if ( ! empty( $meta ) ) { 791 $event['meta'] = $meta; 792 } 793 794 // $unique = false so as to allow multiple values per comment 795 $r = add_comment_meta( $comment_id, 'akismet_history', $event, false ); 796 } 797 798 public static function check_db_comment( $id, $recheck_reason = 'recheck_queue' ) { 799 global $wpdb; 800 801 if ( ! self::get_api_key() ) { 802 return new WP_Error( 'akismet-not-configured', __( 'Akismet is not configured. Please enter an API key.', 'akismet' ) ); 803 } 804 805 $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id ), ARRAY_A ); 806 807 if ( ! $c ) { 808 return new WP_Error( 'invalid-comment-id', __( 'Comment not found.', 'akismet' ) ); 809 } 810 811 $c['user_ip'] = $c['comment_author_IP']; 812 $c['user_agent'] = $c['comment_agent']; 813 $c['referrer'] = ''; 814 $c['blog'] = get_option( 'home' ); 815 $c['blog_lang'] = get_locale(); 816 $c['blog_charset'] = get_option( 'blog_charset' ); 817 $c['permalink'] = get_permalink( $c['comment_post_ID'] ); 818 $c['recheck_reason'] = $recheck_reason; 819 820 $c['user_role'] = ''; 821 if ( ! empty( $c['user_ID'] ) ) { 822 $c['user_role'] = self::get_user_roles( $c['user_ID'] ); 823 } 824 825 if ( self::is_test_mode() ) { 826 $c['is_test'] = 'true'; 827 } 828 829 $c = apply_filters( 'akismet_request_args', $c, 'comment-check' ); 830 831 $response = self::http_post( self::build_query( $c ), 'comment-check' ); 832 833 if ( ! empty( $response[1] ) ) { 834 return $response[1]; 835 } 836 837 return false; 838 } 839 840 public static function recheck_comment( $id, $recheck_reason = 'recheck_queue' ) { 841 add_comment_meta( $id, 'akismet_rechecking', true ); 842 843 $api_response = self::check_db_comment( $id, $recheck_reason ); 844 845 if ( is_wp_error( $api_response ) ) { 846 // Invalid comment ID. 847 } elseif ( 'true' === $api_response ) { 848 wp_set_comment_status( $id, 'spam' ); 849 update_comment_meta( $id, 'akismet_result', 'true' ); 850 delete_comment_meta( $id, 'akismet_error' ); 851 delete_comment_meta( $id, 'akismet_delayed_moderation_email' ); 852 self::update_comment_history( $id, '', 'recheck-spam' ); 853 } elseif ( 'false' === $api_response ) { 854 update_comment_meta( $id, 'akismet_result', 'false' ); 855 delete_comment_meta( $id, 'akismet_error' ); 856 delete_comment_meta( $id, 'akismet_delayed_moderation_email' ); 857 self::update_comment_history( $id, '', 'recheck-ham' ); 858 } else { 859 // abnormal result: error 860 update_comment_meta( $id, 'akismet_result', 'error' ); 861 self::update_comment_history( 862 $id, 863 '', 864 'recheck-error', 865 array( 'response' => substr( $api_response, 0, 50 ) ) 866 ); 867 } 868 869 delete_comment_meta( $id, 'akismet_rechecking' ); 870 871 return $api_response; 872 } 873 874 public static function transition_comment_status( $new_status, $old_status, $comment ) { 875 876 if ( $new_status == $old_status ) { 877 return; 878 } 879 880 if ( 'spam' === $new_status || 'spam' === $old_status ) { 881 // Clear the cache of the "X comments in your spam queue" count on the dashboard. 882 wp_cache_delete( 'akismet_spam_count', 'widget' ); 883 } 884 885 // we don't need to record a history item for deleted comments 886 if ( $new_status == 'delete' ) { 887 return; 888 } 889 890 if ( ! current_user_can( 'edit_post', $comment->comment_post_ID ) && ! current_user_can( 'moderate_comments' ) ) { 891 return; 892 } 893 894 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING == true ) { 895 return; 896 } 897 898 // if this is present, it means the status has been changed by a re-check, not an explicit user action 899 if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) ) { 900 return; 901 } 902 903 if ( function_exists( 'getallheaders' ) ) { 904 $request_headers = getallheaders(); 905 906 foreach ( $request_headers as $header => $value ) { 907 if ( strtolower( $header ) == 'x-akismet-webhook' ) { 908 // This change is due to a webhook request. 909 return; 910 } 911 } 912 } 913 914 // Assumption alert: 915 // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status 916 // is changed automatically by another plugin. Unfortunately WordPress doesn't provide an unambiguous way to 917 // determine why the transition_comment_status action was triggered. And there are several different ways by which 918 // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others. 919 // We'll assume that this is an explicit user action if certain POST/GET variables exist. 920 if ( 921 // status=spam: Marking as spam via the REST API or... 922 // status=unspam: I'm not sure. Maybe this used to be used instead of status=approved? Or the UI for removing from spam but not approving has been since removed?... 923 // status=approved: Unspamming via the REST API (Calypso) or... 924 ( isset( $_POST['status'] ) && in_array( $_POST['status'], array( 'spam', 'unspam', 'approved' ) ) ) 925 // spam=1: Clicking "Spam" underneath a comment in wp-admin and allowing the AJAX request to happen. 926 || ( isset( $_POST['spam'] ) && (int) $_POST['spam'] == 1 ) 927 // unspam=1: Clicking "Not Spam" underneath a comment in wp-admin and allowing the AJAX request to happen. Or, clicking "Undo" after marking something as spam. 928 || ( isset( $_POST['unspam'] ) && (int) $_POST['unspam'] == 1 ) 929 // comment_status=spam/unspam: It's unclear where this is happening. 930 || ( isset( $_POST['comment_status'] ) && in_array( $_POST['comment_status'], array( 'spam', 'unspam' ) ) ) 931 // action=spam: Choosing "Mark as Spam" from the Bulk Actions dropdown in wp-admin (or the "Spam it" link in notification emails). 932 // action=unspam: Choosing "Not Spam" from the Bulk Actions dropdown in wp-admin. 933 // action=spamcomment: Following the "Spam" link below a comment in wp-admin (not allowing AJAX request to happen). 934 // action=unspamcomment: Following the "Not Spam" link below a comment in wp-admin (not allowing AJAX request to happen). 935 || ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'spam', 'unspam', 'spamcomment', 'unspamcomment' ) ) ) 936 // action=editedcomment: Editing a comment via wp-admin (and possibly changing its status). 937 || ( isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'editedcomment' ) ) ) 938 // for=jetpack: Moderation via the WordPress app, Calypso, anything powered by the Jetpack connection. 939 || ( isset( $_GET['for'] ) && ( 'jetpack' == $_GET['for'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) ) 940 // Certain WordPress.com API requests 941 || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) 942 // WordPress.org REST API requests 943 || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) 944 ) { 945 if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || ! $old_status ) ) { 946 return self::submit_spam_comment( $comment->comment_ID ); 947 } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) { 948 return self::submit_nonspam_comment( $comment->comment_ID ); 949 } 950 } 951 952 self::update_comment_history( $comment->comment_ID, '', 'status-' . $new_status ); 953 } 954 955 public static function submit_spam_comment( $comment_id ) { 956 global $wpdb, $current_user, $current_site; 957 958 $comment_id = (int) $comment_id; 959 960 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ), ARRAY_A ); 961 962 if ( ! $comment ) { 963 // it was deleted 964 return; 965 } 966 967 if ( 'spam' != $comment['comment_approved'] ) { 968 return; 969 } 970 971 self::update_comment_history( $comment_id, '', 'report-spam' ); 972 973 // If the user hasn't configured Akismet, there's nothing else to do at this point. 974 if ( ! self::get_api_key() ) { 975 return; 976 } 977 978 // use the original version stored in comment_meta if available 979 $as_submitted = self::sanitize_comment_as_submitted( get_comment_meta( $comment_id, 'akismet_as_submitted', true ) ); 980 981 if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) { 982 $comment = array_merge( $comment, $as_submitted ); 983 } 984 985 $comment['blog'] = get_option( 'home' ); 986 $comment['blog_lang'] = get_locale(); 987 $comment['blog_charset'] = get_option( 'blog_charset' ); 988 $comment['permalink'] = get_permalink( $comment['comment_post_ID'] ); 989 990 if ( is_object( $current_user ) ) { 991 $comment['reporter'] = $current_user->user_login; 992 } 993 994 if ( is_object( $current_site ) ) { 995 $comment['site_domain'] = $current_site->domain; 996 } 997 998 $comment['user_role'] = ''; 999 if ( ! empty( $comment['user_ID'] ) ) { 1000 $comment['user_role'] = self::get_user_roles( $comment['user_ID'] ); 1001 } 1002 1003 if ( self::is_test_mode() ) { 1004 $comment['is_test'] = 'true'; 1005 } 1006 1007 $post = get_post( $comment['comment_post_ID'] ); 1008 1009 if ( ! is_null( $post ) ) { 1010 $comment['comment_post_modified_gmt'] = $post->post_modified_gmt; 1011 } 1012 1013 $comment['comment_check_response'] = self::last_comment_check_response( $comment_id ); 1014 1015 $comment = apply_filters( 'akismet_request_args', $comment, 'submit-spam' ); 1016 1017 $response = self::http_post( self::build_query( $comment ), 'submit-spam' ); 1018 1019 update_comment_meta( $comment_id, 'akismet_user_result', 'true' ); 1020 1021 if ( $comment['reporter'] ) { 1022 update_comment_meta( $comment_id, 'akismet_user', $comment['reporter'] ); 1023 } 1024 1025 do_action( 'akismet_submit_spam_comment', $comment_id, $response[1] ); 1026 } 1027 1028 public static function submit_nonspam_comment( $comment_id ) { 1029 global $wpdb, $current_user, $current_site; 1030 1031 $comment_id = (int) $comment_id; 1032 1033 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ), ARRAY_A ); 1034 1035 if ( ! $comment ) { 1036 // it was deleted 1037 return; 1038 } 1039 1040 self::update_comment_history( $comment_id, '', 'report-ham' ); 1041 1042 // If the user hasn't configured Akismet, there's nothing else to do at this point. 1043 if ( ! self::get_api_key() ) { 1044 return; 1045 } 1046 1047 // use the original version stored in comment_meta if available 1048 $as_submitted = self::sanitize_comment_as_submitted( get_comment_meta( $comment_id, 'akismet_as_submitted', true ) ); 1049 1050 if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) { 1051 $comment = array_merge( $comment, $as_submitted ); 1052 } 1053 1054 $comment['blog'] = get_option( 'home' ); 1055 $comment['blog_lang'] = get_locale(); 1056 $comment['blog_charset'] = get_option( 'blog_charset' ); 1057 $comment['permalink'] = get_permalink( $comment['comment_post_ID'] ); 1058 $comment['user_role'] = ''; 1059 1060 if ( is_object( $current_user ) ) { 1061 $comment['reporter'] = $current_user->user_login; 1062 } 1063 1064 if ( is_object( $current_site ) ) { 1065 $comment['site_domain'] = $current_site->domain; 1066 } 1067 1068 if ( ! empty( $comment['user_ID'] ) ) { 1069 $comment['user_role'] = self::get_user_roles( $comment['user_ID'] ); 1070 } 1071 1072 if ( self::is_test_mode() ) { 1073 $comment['is_test'] = 'true'; 1074 } 1075 1076 $post = get_post( $comment['comment_post_ID'] ); 1077 1078 if ( ! is_null( $post ) ) { 1079 $comment['comment_post_modified_gmt'] = $post->post_modified_gmt; 1080 } 1081 1082 $comment['comment_check_response'] = self::last_comment_check_response( $comment_id ); 1083 1084 $comment = apply_filters( 'akismet_request_args', $comment, 'submit-ham' ); 1085 1086 $response = self::http_post( self::build_query( $comment ), 'submit-ham' ); 1087 1088 update_comment_meta( $comment_id, 'akismet_user_result', 'false' ); 1089 1090 if ( $comment['reporter'] ) { 1091 update_comment_meta( $comment_id, 'akismet_user', $comment['reporter'] ); 1092 } 1093 1094 do_action( 'akismet_submit_nonspam_comment', $comment_id, $response[1] ); 1095 } 1096 1097 public static function cron_recheck() { 1098 global $wpdb; 1099 1100 $api_key = self::get_api_key(); 1101 1102 $status = self::verify_key( $api_key ); 1103 if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) { 1104 // since there is currently a problem with the key, reschedule a check for 6 hours hence 1105 wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' ); 1106 do_action( 'akismet_scheduled_recheck', 'key-problem-' . get_option( 'akismet_alert_code' ) . '-' . $status ); 1107 return false; 1108 } 1109 1110 delete_option( 'akismet_available_servers' ); 1111 1112 $comment_errors = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error' LIMIT 100" ); 1113 1114 foreach ( (array) $comment_errors as $comment_id ) { 1115 // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck 1116 $comment = get_comment( $comment_id ); 1117 1118 if ( 1119 ! $comment // Comment has been deleted 1120 || strtotime( $comment->comment_date_gmt ) < strtotime( '-15 days' ) // Comment is too old. 1121 || $comment->comment_approved !== '0' // Comment is no longer in the Pending queue 1122 ) { 1123 delete_comment_meta( $comment_id, 'akismet_error' ); 1124 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' ); 1125 continue; 1126 } 1127 1128 add_comment_meta( $comment_id, 'akismet_rechecking', true ); 1129 $status = self::check_db_comment( $comment_id, 'retry' ); 1130 1131 $event = ''; 1132 if ( $status == 'true' ) { 1133 $event = 'cron-retry-spam'; 1134 } elseif ( $status == 'false' ) { 1135 $event = 'cron-retry-ham'; 1136 } 1137 1138 // If we got back a legit response then update the comment history 1139 // other wise just bail now and try again later. No point in 1140 // re-trying all the comments once we hit one failure. 1141 if ( ! empty( $event ) ) { 1142 delete_comment_meta( $comment_id, 'akismet_error' ); 1143 self::update_comment_history( $comment_id, '', $event ); 1144 update_comment_meta( $comment_id, 'akismet_result', $status ); 1145 // make sure the comment status is still pending. if it isn't, that means the user has already moved it elsewhere. 1146 $comment = get_comment( $comment_id ); 1147 if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) { 1148 if ( $status == 'true' ) { 1149 wp_spam_comment( $comment_id ); 1150 } elseif ( $status == 'false' ) { 1151 // comment is good, but it's still in the pending queue. depending on the moderation settings 1152 // we may need to change it to approved. 1153 if ( check_comment( $comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent, $comment->comment_type ) ) { 1154 wp_set_comment_status( $comment_id, 1 ); 1155 } elseif ( get_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ) ) { 1156 wp_notify_moderator( $comment_id ); 1157 } 1158 } 1159 } 1160 1161 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' ); 1162 } else { 1163 // If this comment has been pending moderation for longer than MAX_DELAY_BEFORE_MODERATION_EMAIL, 1164 // send a moderation email now. 1165 if ( ( intval( gmdate( 'U' ) ) - strtotime( $comment->comment_date_gmt ) ) < self::MAX_DELAY_BEFORE_MODERATION_EMAIL ) { 1166 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' ); 1167 wp_notify_moderator( $comment_id ); 1168 } 1169 1170 delete_comment_meta( $comment_id, 'akismet_rechecking' ); 1171 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); 1172 do_action( 'akismet_scheduled_recheck', 'check-db-comment-' . $status ); 1173 return; 1174 } 1175 delete_comment_meta( $comment_id, 'akismet_rechecking' ); 1176 } 1177 1178 $remaining = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" ); 1179 if ( $remaining && ! wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) { 1180 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); 1181 do_action( 'akismet_scheduled_recheck', 'remaining' ); 1182 } 1183 } 1184 1185 public static function fix_scheduled_recheck() { 1186 $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' ); 1187 if ( ! $future_check ) { 1188 return; 1189 } 1190 1191 if ( get_option( 'akismet_alert_code' ) > 0 ) { 1192 return; 1193 } 1194 1195 $check_range = time() + 1200; 1196 if ( $future_check > $check_range ) { 1197 wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' ); 1198 wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' ); 1199 do_action( 'akismet_scheduled_recheck', 'fix-scheduled-recheck' ); 1200 } 1201 } 1202 1203 public static function add_comment_nonce( $post_id ) { 1204 /** 1205 * To disable the Akismet comment nonce, add a filter for the 'akismet_comment_nonce' tag 1206 * and return any string value that is not 'true' or '' (empty string). 1207 * 1208 * Don't return boolean false, because that implies that the 'akismet_comment_nonce' option 1209 * has not been set and that Akismet should just choose the default behavior for that 1210 * situation. 1211 */ 1212 1213 if ( ! self::get_api_key() ) { 1214 return; 1215 } 1216 1217 $akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) ); 1218 1219 if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' ) { 1220 echo '<p style="display: none;">'; 1221 wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', false ); 1222 echo '</p>'; 1223 } 1224 } 1225 1226 public static function is_test_mode() { 1227 return defined( 'AKISMET_TEST_MODE' ) && AKISMET_TEST_MODE; 1228 } 1229 1230 public static function allow_discard() { 1231 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 1232 return false; 1233 } 1234 if ( is_user_logged_in() ) { 1235 return false; 1236 } 1237 1238 return ( get_option( 'akismet_strictness' ) === '1' ); 1239 } 1240 1241 public static function get_ip_address() { 1242 return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null; 1243 } 1244 1245 /** 1246 * Using the unique values that we assign, do we consider these two comments 1247 * to be the same instance of a comment? 1248 * 1249 * The only fields that matter in $comment1 and $comment2 are akismet_guid and akismet_skipped_microtime. 1250 * We set both of these during the comment-check call, and if the comment has been saved to the DB, 1251 * we save them as comment meta and add them back into the comment array before comparing the comments. 1252 * 1253 * @param mixed $comment1 A comment object or array. 1254 * @param mixed $comment2 A comment object or array. 1255 * @return bool Whether the two comments should be treated as the same comment. 1256 */ 1257 private static function comments_match( $comment1, $comment2 ) { 1258 $comment1 = (array) $comment1; 1259 $comment2 = (array) $comment2; 1260 1261 if ( ! empty( $comment1['akismet_guid'] ) && ! empty( $comment2['akismet_guid'] ) ) { 1262 // If the comment got sent to the API and got a response, it will have a GUID. 1263 1264 return ( $comment1['akismet_guid'] == $comment2['akismet_guid'] ); 1265 } else if ( ! empty( $comment1['akismet_skipped_microtime'] ) && ! empty( $comment2['akismet_skipped_microtime'] ) ) { 1266 // It won't have a GUID if it didn't get sent to the API because it matched the disallowed list, 1267 // but it should have a microtimestamp to use here for matching against the comment DB entry it matches. 1268 return ( strval( $comment1['akismet_skipped_microtime'] ) == strval( $comment2['akismet_skipped_microtime'] ) ); 1269 } 1270 1271 return false; 1272 } 1273 1274 /** 1275 * Does the supplied comment match the details of the one most recently stored in self::$last_comment? 1276 * 1277 * @param array $comment 1278 * @return bool Whether the comment supplied as an argument is a match for the one we have stored in $last_comment. 1279 */ 1280 public static function matches_last_comment( $comment ) { 1281 if ( ! self::$last_comment ) { 1282 return false; 1283 } 1284 1285 return self::comments_match( $comment, self::$last_comment ); 1286 } 1287 1288 /** 1289 * Because of the order of operations, we don't always know the comment ID of the comment that we're checking, 1290 * so we have to be able to match the comment we cached locally with the comment from the DB. 1291 * 1292 * @param int $comment_id 1293 * @return bool Whether the comment represented by $comment_id is a match for the one we have stored in $last_comment. 1294 */ 1295 public static function matches_last_comment_by_id( $comment_id ) { 1296 return self::matches_last_comment( self::get_fields_for_comment_matching( $comment_id ) ); 1297 } 1298 1299 /** 1300 * Given a comment ID, retrieve the values that we use for matching comments together. 1301 * 1302 * @param int $comment_id 1303 * @return array An array containing akismet_guid and akismet_skipped_microtime. Either or both may be falsy, but we hope that at least one is a string. 1304 */ 1305 public static function get_fields_for_comment_matching( $comment_id ) { 1306 return array( 1307 'akismet_guid' => get_comment_meta( $comment_id, 'akismet_guid', true ), 1308 'akismet_skipped_microtime' => get_comment_meta( $comment_id, 'akismet_skipped_microtime', true ), 1309 ); 1310 } 1311 1312 private static function get_user_agent() { 1313 return isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null; 1314 } 1315 1316 private static function get_referer() { 1317 return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null; 1318 } 1319 1320 // return a comma-separated list of role names for the given user 1321 public static function get_user_roles( $user_id ) { 1322 $comment_user = null; 1323 $roles = false; 1324 1325 if ( ! class_exists( 'WP_User' ) ) { 1326 return false; 1327 } 1328 1329 if ( $user_id > 0 ) { 1330 $comment_user = new WP_User( $user_id ); 1331 if ( isset( $comment_user->roles ) ) { 1332 $roles = implode( ',', $comment_user->roles ); 1333 } 1334 } 1335 1336 if ( is_multisite() && is_super_admin( $user_id ) ) { 1337 if ( empty( $roles ) ) { 1338 $roles = 'super_admin'; 1339 } else { 1340 $comment_user->roles[] = 'super_admin'; 1341 $roles = implode( ',', $comment_user->roles ); 1342 } 1343 } 1344 1345 return $roles; 1346 } 1347 1348 // filter handler used to return a spam result to pre_comment_approved 1349 public static function last_comment_status( $approved, $comment ) { 1350 if ( is_null( self::$last_comment_result ) ) { 1351 // We didn't have reason to store the result of the last check. 1352 return $approved; 1353 } 1354 1355 // Only do this if it's the correct comment. 1356 if ( ! self::matches_last_comment( $comment ) ) { 1357 self::log( "comment_is_spam mismatched comment, returning unaltered $approved" ); 1358 return $approved; 1359 } 1360 1361 if ( 'trash' === $approved ) { 1362 // If the last comment we checked has had its approval set to 'trash', 1363 // then it failed the comment blacklist check. Let that blacklist override 1364 // the spam check, since users have the (valid) expectation that when 1365 // they fill out their blacklists, comments that match it will always 1366 // end up in the trash. 1367 return $approved; 1368 } 1369 1370 // bump the counter here instead of when the filter is added to reduce the possibility of overcounting 1371 if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) { 1372 update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr ); 1373 } 1374 1375 return self::$last_comment_result; 1376 } 1377 1378 /** 1379 * If Akismet is temporarily unreachable, we don't want to "spam" the blogger with 1380 * moderation emails for comments that will be automatically cleared or spammed on 1381 * the next retry. 1382 * 1383 * For comments that will be rechecked later, empty the list of email addresses that 1384 * the moderation email would be sent to. 1385 * 1386 * @param array $emails An array of email addresses that the moderation email will be sent to. 1387 * @param int $comment_id The ID of the relevant comment. 1388 * @return array An array of email addresses that the moderation email will be sent to. 1389 */ 1390 public static function disable_moderation_emails_if_unreachable( $emails, $comment_id ) { 1391 if ( ! empty( self::$prevent_moderation_email_for_these_comments ) && ! empty( $emails ) ) { 1392 $matching_fields = self::get_fields_for_comment_matching( $comment_id ); 1393 1394 // self::$prevent_moderation_email_for_these_comments is an array of $commentdata objects 1395 // saved immediately after the comment-check request completes. 1396 foreach ( self::$prevent_moderation_email_for_these_comments as $possible_match ) { 1397 if ( self::comments_match( $possible_match, $matching_fields ) ) { 1398 update_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ); 1399 return array(); 1400 } 1401 } 1402 } 1403 1404 return $emails; 1405 } 1406 1407 public static function _cmp_time( $a, $b ) { 1408 return $a['time'] > $b['time'] ? -1 : 1; 1409 } 1410 1411 public static function _get_microtime() { 1412 $mtime = explode( ' ', microtime() ); 1413 return $mtime[1] + $mtime[0]; 1414 } 1415 1416 /** 1417 * Make a POST request to the Akismet API. 1418 * 1419 * @param string $request The body of the request. 1420 * @param string $path The path for the request. 1421 * @param string $ip The specific IP address to hit. 1422 * @return array A two-member array consisting of the headers and the response body, both empty in the case of a failure. 1423 */ 1424 public static function http_post( $request, $path, $ip = null ) { 1425 1426 $akismet_ua = sprintf( 'WordPress/%s | Akismet/%s', $GLOBALS['wp_version'], constant( 'AKISMET_VERSION' ) ); 1427 $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua ); 1428 1429 $host = self::API_HOST; 1430 $api_key = self::get_api_key(); 1431 1432 if ( $api_key ) { 1433 $request = add_query_arg( 'api_key', $api_key, $request ); 1434 } 1435 1436 $http_host = $host; 1437 // use a specific IP if provided 1438 // needed by Akismet_Admin::check_server_connectivity() 1439 if ( $ip && long2ip( ip2long( $ip ) ) ) { 1440 $http_host = $ip; 1441 } 1442 1443 $http_args = array( 1444 'body' => $request, 1445 'headers' => array( 1446 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ), 1447 'Host' => $host, 1448 'User-Agent' => $akismet_ua, 1449 ), 1450 'httpversion' => '1.0', 1451 'timeout' => 15, 1452 ); 1453 1454 $akismet_url = $http_akismet_url = "http://{$http_host}/1.1/{$path}"; 1455 1456 /** 1457 * Try SSL first; if that fails, try without it and don't try it again for a while. 1458 */ 1459 1460 $ssl = $ssl_failed = false; 1461 1462 // Check if SSL requests were disabled fewer than X hours ago. 1463 $ssl_disabled = get_option( 'akismet_ssl_disabled' ); 1464 1465 if ( $ssl_disabled && $ssl_disabled < ( time() - 60 * 60 * 24 ) ) { // 24 hours 1466 $ssl_disabled = false; 1467 delete_option( 'akismet_ssl_disabled' ); 1468 } elseif ( $ssl_disabled ) { 1469 do_action( 'akismet_ssl_disabled' ); 1470 } 1471 1472 if ( ! $ssl_disabled && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) { 1473 $akismet_url = set_url_scheme( $akismet_url, 'https' ); 1474 1475 do_action( 'akismet_https_request_pre' ); 1476 } 1477 1478 $response = wp_remote_post( $akismet_url, $http_args ); 1479 1480 self::log( compact( 'akismet_url', 'http_args', 'response' ) ); 1481 1482 if ( $ssl && is_wp_error( $response ) ) { 1483 do_action( 'akismet_https_request_failure', $response ); 1484 1485 // Intermittent connection problems may cause the first HTTPS 1486 // request to fail and subsequent HTTP requests to succeed randomly. 1487 // Retry the HTTPS request once before disabling SSL for a time. 1488 $response = wp_remote_post( $akismet_url, $http_args ); 1489 1490 self::log( compact( 'akismet_url', 'http_args', 'response' ) ); 1491 1492 if ( is_wp_error( $response ) ) { 1493 $ssl_failed = true; 1494 1495 do_action( 'akismet_https_request_failure', $response ); 1496 1497 do_action( 'akismet_http_request_pre' ); 1498 1499 // Try the request again without SSL. 1500 $response = wp_remote_post( $http_akismet_url, $http_args ); 1501 1502 self::log( compact( 'http_akismet_url', 'http_args', 'response' ) ); 1503 } 1504 } 1505 1506 if ( is_wp_error( $response ) ) { 1507 do_action( 'akismet_request_failure', $response ); 1508 1509 return array( '', '' ); 1510 } 1511 1512 if ( $ssl_failed ) { 1513 // The request failed when using SSL but succeeded without it. Disable SSL for future requests. 1514 update_option( 'akismet_ssl_disabled', time() ); 1515 1516 do_action( 'akismet_https_disabled' ); 1517 } 1518 1519 $simplified_response = array( $response['headers'], $response['body'] ); 1520 1521 $alert_code_check_paths = array( 1522 'verify-key', 1523 'comment-check', 1524 'get-stats', 1525 ); 1526 1527 if ( in_array( $path, $alert_code_check_paths ) ) { 1528 self::update_alert( $simplified_response ); 1529 } 1530 1531 return $simplified_response; 1532 } 1533 1534 // given a response from an API call like check_key_status(), update the alert code options if an alert is present. 1535 public static function update_alert( $response ) { 1536 $alert_option_prefix = 'akismet_alert_'; 1537 $alert_header_prefix = 'x-akismet-alert-'; 1538 $alert_header_names = array( 1539 'code', 1540 'msg', 1541 'api-calls', 1542 'usage-limit', 1543 'upgrade-plan', 1544 'upgrade-url', 1545 'upgrade-type', 1546 'upgrade-via-support', 1547 ); 1548 1549 foreach ( $alert_header_names as $alert_header_name ) { 1550 $value = null; 1551 if ( isset( $response[0][ $alert_header_prefix . $alert_header_name ] ) ) { 1552 $value = $response[0][ $alert_header_prefix . $alert_header_name ]; 1553 } 1554 1555 $option_name = $alert_option_prefix . str_replace( '-', '_', $alert_header_name ); 1556 if ( $value != get_option( $option_name ) ) { 1557 if ( ! $value ) { 1558 delete_option( $option_name ); 1559 } else { 1560 update_option( $option_name, $value ); 1561 } 1562 } 1563 } 1564 } 1565 1566 /** 1567 * Mark akismet-frontend.js as deferred. Because nothing depends on it, it can run at any time 1568 * after it's loaded, and the browser won't have to wait for it to load to continue 1569 * parsing the rest of the page. 1570 */ 1571 public static function set_form_js_async( $tag, $handle, $src ) { 1572 if ( 'akismet-frontend' !== $handle ) { 1573 return $tag; 1574 } 1575 1576 return preg_replace( '/^<script /i', '<script defer ', $tag ); 1577 } 1578 1579 public static function get_akismet_form_fields() { 1580 $fields = ''; 1581 1582 $prefix = 'ak_'; 1583 1584 // Contact Form 7 uses _wpcf7 as a prefix to know which fields to exclude from comment_content. 1585 if ( 'wpcf7_form_elements' === current_filter() ) { 1586 $prefix = '_wpcf7_ak_'; 1587 } 1588 1589 $fields .= '<p style="display: none !important;" class="akismet-fields-container" data-prefix="' . esc_attr( $prefix ) . '">'; 1590 $fields .= '<label>Δ<textarea name="' . $prefix . 'hp_textarea" cols="45" rows="8" maxlength="100"></textarea></label>'; 1591 1592 if ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() ) { 1593 // Keep track of how many ak_js fields are in this page so that we don't re-use 1594 // the same ID. 1595 static $field_count = 0; 1596 1597 ++$field_count; 1598 1599 $fields .= '<input type="hidden" id="ak_js_' . $field_count . '" name="' . $prefix . 'js" value="' . mt_rand( 0, 250 ) . '"/>'; 1600 $fields .= '<script>document.getElementById( "ak_js_' . $field_count . '" ).setAttribute( "value", ( new Date() ).getTime() );</script>'; 1601 } 1602 1603 $fields .= '</p>'; 1604 1605 return $fields; 1606 } 1607 1608 public static function output_custom_form_fields( $post_id ) { 1609 if ( 'fluentform/form_element_start' === current_filter() && did_action( 'fluentform_form_element_start' ) ) { 1610 // Already did this via the legacy filter. 1611 return; 1612 } 1613 1614 // phpcs:ignore WordPress.Security.EscapeOutput 1615 echo self::get_akismet_form_fields(); 1616 } 1617 1618 public static function inject_custom_form_fields( $html ) { 1619 $html = str_replace( '</form>', self::get_akismet_form_fields() . '</form>', $html ); 1620 1621 return $html; 1622 } 1623 1624 public static function append_custom_form_fields( $html ) { 1625 $html .= self::get_akismet_form_fields(); 1626 1627 return $html; 1628 } 1629 1630 /** 1631 * Ensure that any Akismet-added form fields are included in the comment-check call. 1632 * 1633 * @param array $form 1634 * @param array $data Some plugins will supply the POST data via the filter, since they don't 1635 * read it directly from $_POST. 1636 * @return array $form 1637 */ 1638 public static function prepare_custom_form_values( $form, $data = null ) { 1639 if ( 'fluentform/akismet_fields' === current_filter() && did_filter( 'fluentform_akismet_fields' ) ) { 1640 // Already updated the form fields via the legacy filter. 1641 return $form; 1642 } 1643 1644 if ( is_null( $data ) ) { 1645 // phpcs:ignore WordPress.Security.NonceVerification.Missing 1646 $data = $_POST; 1647 } 1648 1649 $prefix = 'ak_'; 1650 1651 // Contact Form 7 uses _wpcf7 as a prefix to know which fields to exclude from comment_content. 1652 if ( 'wpcf7_akismet_parameters' === current_filter() ) { 1653 $prefix = '_wpcf7_ak_'; 1654 } 1655 1656 foreach ( $data as $key => $val ) { 1657 if ( 0 === strpos( $key, $prefix ) ) { 1658 $form[ 'POST_ak_' . substr( $key, strlen( $prefix ) ) ] = $val; 1659 } 1660 } 1661 1662 return $form; 1663 } 1664 1665 private static function bail_on_activation( $message, $deactivate = true ) { 1666 ?> 1667 <!doctype html> 1668 <html> 1669 <head> 1670 <meta charset="<?php bloginfo( 'charset' ); ?>" /> 1671 <style> 1672 * { 1673 text-align: center; 1674 margin: 0; 1675 padding: 0; 1676 font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; 1677 } 1678 p { 1679 margin-top: 1em; 1680 font-size: 18px; 1681 } 1682 </style> 1683 </head> 1684 <body> 1685 <p><?php echo esc_html( $message ); ?></p> 1686 </body> 1687 </html> 1688 <?php 1689 if ( $deactivate ) { 1690 $plugins = get_option( 'active_plugins' ); 1691 $akismet = plugin_basename( AKISMET__PLUGIN_DIR . 'akismet.php' ); 1692 $update = false; 1693 foreach ( $plugins as $i => $plugin ) { 1694 if ( $plugin === $akismet ) { 1695 $plugins[ $i ] = false; 1696 $update = true; 1697 } 1698 } 1699 1700 if ( $update ) { 1701 update_option( 'active_plugins', array_filter( $plugins ) ); 1702 } 1703 } 1704 exit; 1705 } 1706 1707 public static function view( $name, array $args = array() ) { 1708 $args = apply_filters( 'akismet_view_arguments', $args, $name ); 1709 1710 foreach ( $args as $key => $val ) { 1711 $$key = $val; 1712 } 1713 1714 $file = AKISMET__PLUGIN_DIR . 'views/' . basename( $name ) . '.php'; 1715 1716 if ( file_exists( $file ) ) { 1717 include $file; 1718 } 1719 } 1720 1721 /** 1722 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() 1723 * 1724 * @static 1725 */ 1726 public static function plugin_activation() { 1727 if ( version_compare( $GLOBALS['wp_version'], AKISMET__MINIMUM_WP_VERSION, '<' ) ) { 1728 $message = '<strong>' . 1729 /* translators: 1: Current Akismet version number, 2: Minimum WordPress version number required. */ 1730 sprintf( esc_html__( 'Akismet %1$s requires WordPress %2$s or higher.', 'akismet' ), AKISMET_VERSION, AKISMET__MINIMUM_WP_VERSION ) . '</strong> ' . 1731 /* translators: 1: WordPress documentation URL, 2: Akismet download URL. */ 1732 sprintf( __( 'Please <a href="%1$s">upgrade WordPress</a> to a current version, or <a href="%2$s">downgrade to version 2.4 of the Akismet plugin</a>.', 'akismet' ), 'https://codex.wordpress.org/Upgrading_WordPress', 'https://wordpress.org/plugins/akismet' ); 1733 1734 self::bail_on_activation( $message ); 1735 } elseif ( ! empty( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], '/wp-admin/plugins.php' ) ) { 1736 add_option( 'Activated_Akismet', true ); 1737 } 1738 } 1739 1740 /** 1741 * Removes all connection options 1742 * 1743 * @static 1744 */ 1745 public static function plugin_deactivation() { 1746 self::deactivate_key( self::get_api_key() ); 1747 1748 // Remove any scheduled cron jobs. 1749 $akismet_cron_events = array( 1750 'akismet_schedule_cron_recheck', 1751 'akismet_scheduled_delete', 1752 ); 1753 1754 foreach ( $akismet_cron_events as $akismet_cron_event ) { 1755 $timestamp = wp_next_scheduled( $akismet_cron_event ); 1756 1757 if ( $timestamp ) { 1758 wp_unschedule_event( $timestamp, $akismet_cron_event ); 1759 } 1760 } 1761 } 1762 1763 /** 1764 * Essentially a copy of WP's build_query but one that doesn't expect pre-urlencoded values. 1765 * 1766 * @param array $args An array of key => value pairs 1767 * @return string A string ready for use as a URL query string. 1768 */ 1769 public static function build_query( $args ) { 1770 return _http_build_query( $args, '', '&' ); 1771 } 1772 1773 /** 1774 * Log debugging info to the error log. 1775 * 1776 * Enabled when WP_DEBUG_LOG is enabled (and WP_DEBUG, since according to 1777 * core, "WP_DEBUG_DISPLAY and WP_DEBUG_LOG perform no function unless 1778 * WP_DEBUG is true), but can be disabled via the akismet_debug_log filter. 1779 * 1780 * @param mixed $akismet_debug The data to log. 1781 */ 1782 public static function log( $akismet_debug ) { 1783 if ( apply_filters( 'akismet_debug_log', defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG && defined( 'AKISMET_DEBUG' ) && AKISMET_DEBUG ) ) { 1784 error_log( print_r( compact( 'akismet_debug' ), true ) ); 1785 } 1786 } 1787 1788 /** 1789 * Check pingbacks for spam before they're saved to the DB. 1790 * 1791 * @param string $method The XML-RPC method that was called. 1792 * @param array $args This and the $server arg are marked as optional since plugins might still be 1793 * calling do_action( 'xmlrpc_action', [...] ) without the arguments that were added in WP 5.7. 1794 * @param wp_xmlrpc_server $server 1795 */ 1796 public static function pre_check_pingback( $method, $args = array(), $server = null ) { 1797 if ( $method !== 'pingback.ping' ) { 1798 return; 1799 } 1800 1801 /* 1802 * $args looks like this: 1803 * 1804 * Array 1805 * ( 1806 * [0] => http://www.example.net/?p=1 // Site that created the pingback. 1807 * [1] => https://www.example.com/?p=2 // Post being pingback'd on this site. 1808 * ) 1809 */ 1810 1811 if ( ! is_null( $server ) && ! empty( $args[1] ) ) { 1812 $is_multicall = false; 1813 $multicall_count = 0; 1814 1815 if ( 'system.multicall' === $server->message->methodName ) { 1816 $is_multicall = true; 1817 $multicall_count = is_countable( $server->message->params ) ? count( $server->message->params ) : 0; 1818 } 1819 1820 $post_id = url_to_postid( $args[1] ); 1821 1822 // If pingbacks aren't open on this post, we'll still check whether this request is part of a potential DDOS, 1823 // but indicate to the server that pingbacks are indeed closed so we don't include this request in the user's stats, 1824 // since the user has already done their part by disabling pingbacks. 1825 $pingbacks_closed = false; 1826 1827 $post = get_post( $post_id ); 1828 1829 if ( ! $post || ! pings_open( $post ) ) { 1830 $pingbacks_closed = true; 1831 } 1832 1833 $comment = array( 1834 'comment_author_url' => $args[0], 1835 'comment_post_ID' => $post_id, 1836 'comment_author' => '', 1837 'comment_author_email' => '', 1838 'comment_content' => '', 1839 'comment_type' => 'pingback', 1840 'akismet_pre_check' => '1', 1841 'comment_pingback_target' => $args[1], 1842 'pingbacks_closed' => $pingbacks_closed ? '1' : '0', 1843 'is_multicall' => $is_multicall, 1844 'multicall_count' => $multicall_count, 1845 ); 1846 1847 $comment = self::auto_check_comment( $comment, 'xml-rpc' ); 1848 1849 if ( isset( $comment['akismet_result'] ) && 'true' == $comment['akismet_result'] ) { 1850 // Sad: tightly coupled with the IXR classes. Unfortunately the action provides no context and no way to return anything. 1851 $server->error( new IXR_Error( 0, 'Invalid discovery target' ) ); 1852 1853 // Also note that if this was part of a multicall, a spam result will prevent the subsequent calls from being executed. 1854 // This is probably fine, but it raises the bar for what should be acceptable as a false positive. 1855 } 1856 } 1857 } 1858 1859 /** 1860 * Ensure that we are loading expected scalar values from akismet_as_submitted commentmeta. 1861 * 1862 * @param mixed $meta_value 1863 * @return mixed 1864 */ 1865 private static function sanitize_comment_as_submitted( $meta_value ) { 1866 if ( empty( $meta_value ) ) { 1867 return $meta_value; 1868 } 1869 1870 $meta_value = (array) $meta_value; 1871 1872 foreach ( $meta_value as $key => $value ) { 1873 if ( ! is_scalar( $value ) ) { 1874 unset( $meta_value[ $key ] ); 1875 } else { 1876 // These can change, so they're not explicitly listed in comment_as_submitted_allowed_keys. 1877 if ( strpos( $key, 'POST_ak_' ) === 0 ) { 1878 continue; 1879 } 1880 1881 if ( ! isset( self::$comment_as_submitted_allowed_keys[ $key ] ) ) { 1882 unset( $meta_value[ $key ] ); 1883 } 1884 } 1885 } 1886 1887 return $meta_value; 1888 } 1889 1890 public static function predefined_api_key() { 1891 if ( defined( 'WPCOM_API_KEY' ) ) { 1892 return true; 1893 } 1894 1895 return apply_filters( 'akismet_predefined_api_key', false ); 1896 } 1897 1898 /** 1899 * Controls the display of a privacy related notice underneath the comment 1900 * form using the `akismet_comment_form_privacy_notice` option and filter 1901 * respectively. 1902 * 1903 * Default is to not display the notice, leaving the choice to site admins, 1904 * or integrators. 1905 */ 1906 public static function display_comment_form_privacy_notice() { 1907 if ( 'display' !== apply_filters( 'akismet_comment_form_privacy_notice', get_option( 'akismet_comment_form_privacy_notice', 'hide' ) ) ) { 1908 return; 1909 } 1910 1911 echo apply_filters( 1912 'akismet_comment_form_privacy_notice_markup', 1913 '<p class="akismet_comment_form_privacy_notice">' . 1914 wp_kses( 1915 sprintf( 1916 /* translators: %s: Akismet privacy URL */ 1917 __( 'This site uses Akismet to reduce spam. <a href="%s" target="_blank" rel="nofollow noopener">Learn how your comment data is processed.</a>', 'akismet' ), 1918 'https://akismet.com/privacy/' 1919 ), 1920 array( 1921 'a' => array( 1922 'href' => array(), 1923 'target' => array(), 1924 'rel' => array(), 1925 ), 1926 ) 1927 ) . 1928 '</p>' 1929 ); 1930 } 1931 1932 public static function load_form_js() { 1933 if ( 1934 ! is_admin() 1935 && ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() ) 1936 && self::get_api_key() 1937 ) { 1938 wp_register_script( 'akismet-frontend', plugin_dir_url( __FILE__ ) . '_inc/akismet-frontend.js', array(), filemtime( plugin_dir_path( __FILE__ ) . '_inc/akismet-frontend.js' ), true ); 1939 wp_enqueue_script( 'akismet-frontend' ); 1940 } 1941 } 1942 1943 /** 1944 * Add the form JavaScript when we detect that a supported form shortcode is being parsed. 1945 */ 1946 public static function load_form_js_via_filter( $return_value, $tag, $attr, $m ) { 1947 if ( in_array( $tag, array( 'contact-form', 'gravityform', 'contact-form-7', 'formidable', 'fluentform' ) ) ) { 1948 self::load_form_js(); 1949 } 1950 1951 return $return_value; 1952 } 1953 1954 /** 1955 * Was the last entry in the comment history created by Akismet? 1956 * 1957 * @param int $comment_id The ID of the comment. 1958 * @return bool 1959 */ 1960 public static function last_comment_status_change_came_from_akismet( $comment_id ) { 1961 $history = self::get_comment_history( $comment_id ); 1962 1963 if ( empty( $history ) ) { 1964 return false; 1965 } 1966 1967 $most_recent_history_event = $history[0]; 1968 1969 if ( ! isset( $most_recent_history_event['event'] ) ) { 1970 return false; 1971 } 1972 1973 $akismet_history_events = array( 1974 'check-error', 1975 'cron-retry-ham', 1976 'cron-retry-spam', 1977 'check-ham', 1978 'check-spam', 1979 'recheck-error', 1980 'recheck-ham', 1981 'recheck-spam', 1982 'webhook-ham', 1983 'webhook-spam', 1984 ); 1985 1986 if ( in_array( $most_recent_history_event['event'], $akismet_history_events ) ) { 1987 return true; 1988 } 1989 1990 return false; 1991 } 1992 1993 /** 1994 * Check the comment history to find out what the most recent comment-check 1995 * response said about this comment. 1996 * 1997 * This value is then included in submit-ham and submit-spam requests to allow 1998 * us to know whether the comment is actually a missed spam/ham or if it's 1999 * just being reclassified after either never being checked or being mistakenly 2000 * marked as ham/spam. 2001 * 2002 * @param int $comment_id The comment ID. 2003 * @return string 'true', 'false', or an empty string if we don't have a record 2004 * of comment-check being called. 2005 */ 2006 public static function last_comment_check_response( $comment_id ) { 2007 $history = self::get_comment_history( $comment_id ); 2008 2009 if ( $history ) { 2010 $history = array_reverse( $history ); 2011 2012 foreach ( $history as $akismet_history_entry ) { 2013 // We've always been consistent in how history entries are formatted 2014 // but comment_meta is writable by everyone, so don't assume that all 2015 // entries contain the expected parts. 2016 2017 if ( ! is_array( $akismet_history_entry ) ) { 2018 continue; 2019 } 2020 2021 if ( ! isset( $akismet_history_entry['event'] ) ) { 2022 continue; 2023 } 2024 2025 if ( in_array( 2026 $akismet_history_entry['event'], 2027 array( 2028 'recheck-spam', 2029 'check-spam', 2030 'cron-retry-spam', 2031 'webhook-spam', 2032 'webhook-spam-noaction', 2033 ), 2034 true 2035 ) ) { 2036 return 'true'; 2037 } elseif ( in_array( 2038 $akismet_history_entry['event'], 2039 array( 2040 'recheck-ham', 2041 'check-ham', 2042 'cron-retry-ham', 2043 'webhook-ham', 2044 'webhook-ham-noaction', 2045 ), 2046 true 2047 ) ) { 2048 return 'false'; 2049 } 2050 } 2051 } 2052 2053 return ''; 2054 } 2055 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Wed May 14 08:20:01 2025 | Cross-referenced by PHPXref |