[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/plugins/akismet/ -> class.akismet.php (source)

   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                  load_plugin_textdomain( 'akismet' );
 500  
 501                  // normal result: true or false
 502                  if ( isset( self::$last_comment['akismet_result'] ) && self::$last_comment['akismet_result'] == 'true' ) {
 503                      self::update_comment_history( $comment->comment_ID, '', 'check-spam' );
 504                      if ( $comment->comment_approved != 'spam' ) {
 505                          self::update_comment_history(
 506                              $comment->comment_ID,
 507                              '',
 508                              'status-changed-' . $comment->comment_approved
 509                          );
 510                      }
 511                  } elseif ( isset( self::$last_comment['akismet_result'] ) && self::$last_comment['akismet_result'] == 'false' ) {
 512                      self::update_comment_history( $comment->comment_ID, '', 'check-ham' );
 513                      // Status could be spam or trash, depending on the WP version and whether this change applies:
 514                      // https://core.trac.wordpress.org/changeset/34726
 515                      if ( $comment->comment_approved == 'spam' || $comment->comment_approved == 'trash' ) {
 516                          if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
 517                              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 ) ) {
 518                                  self::update_comment_history( $comment->comment_ID, '', 'wp-disallowed' );
 519                              } else {
 520                                  self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved );
 521                              }
 522                          } else {
 523                              self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved );
 524                          }
 525                      }
 526                  } elseif ( isset( self::$last_comment['akismet_result'] ) && 'skipped' == self::$last_comment['akismet_result'] ) {
 527                      // The comment wasn't sent to Akismet because it matched the disallowed comment keys.
 528                      self::update_comment_history( $comment->comment_ID, '', 'wp-disallowed' );
 529                      self::update_comment_history( $comment->comment_ID, '', 'akismet-skipped-disallowed' );
 530                  } else if ( ! isset( self::$last_comment['akismet_result'] ) ) {
 531                      // Add a generic skipped history item.
 532                      self::update_comment_history( $comment->comment_ID, '', 'akismet-skipped' );
 533                  } else {
 534                      // abnormal result: error
 535                      self::update_comment_history(
 536                          $comment->comment_ID,
 537                          '',
 538                          'check-error',
 539                          array( 'response' => substr( self::$last_comment['akismet_result'], 0, 50 ) )
 540                      );
 541                  }
 542              }
 543          }
 544      }
 545  
 546  	public static function delete_old_comments() {
 547          global $wpdb;
 548  
 549          /**
 550           * Determines how many comments will be deleted in each batch.
 551           *
 552           * @param int The default, as defined by AKISMET_DELETE_LIMIT.
 553           */
 554          $delete_limit = apply_filters( 'akismet_delete_comment_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 );
 555          $delete_limit = max( 1, intval( $delete_limit ) );
 556  
 557          /**
 558           * Determines how many days a comment will be left in the Spam queue before being deleted.
 559           *
 560           * @param int The default number of days.
 561           */
 562          $delete_interval = apply_filters( 'akismet_delete_comment_interval', 15 );
 563          $delete_interval = max( 1, intval( $delete_interval ) );
 564  
 565          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 ) ) ) {
 566              if ( empty( $comment_ids ) ) {
 567                  return;
 568              }
 569  
 570              $wpdb->queries = array();
 571  
 572              $comments = array();
 573  
 574              foreach ( $comment_ids as $comment_id ) {
 575                  $comments[ $comment_id ] = get_comment( $comment_id );
 576  
 577                  do_action( 'delete_comment', $comment_id, $comments[ $comment_id ] );
 578                  do_action( 'akismet_batch_delete_count', __FUNCTION__ );
 579              }
 580  
 581              // Prepared as strings since comment_id is an unsigned BIGINT, and using %d will constrain the value to the maximum signed BIGINT.
 582              $format_string = implode( ', ', array_fill( 0, is_countable( $comment_ids ) ? count( $comment_ids ) : 0, '%s' ) );
 583  
 584              $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE comment_id IN ( " . $format_string . ' )', $comment_ids ) );
 585              $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( " . $format_string . ' )', $comment_ids ) );
 586  
 587              foreach ( $comment_ids as $comment_id ) {
 588                  do_action( 'deleted_comment', $comment_id, $comments[ $comment_id ] );
 589                  unset( $comments[ $comment_id ] );
 590              }
 591  
 592              clean_comment_cache( $comment_ids );
 593              do_action( 'akismet_delete_comment_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 );
 594          }
 595  
 596          if ( apply_filters( 'akismet_optimize_table', ( mt_rand( 1, 5000 ) == 11 ), $wpdb->comments ) ) { // lucky number
 597              $wpdb->query( "OPTIMIZE TABLE {$wpdb->comments}" );
 598          }
 599      }
 600  
 601  	public static function delete_old_comments_meta() {
 602          global $wpdb;
 603  
 604          $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 );
 605  
 606          // enforce a minimum of 1 day
 607          $interval = absint( $interval );
 608          if ( $interval < 1 ) {
 609              $interval = 1;
 610          }
 611  
 612          // akismet_as_submitted meta values are large, so expire them
 613          // after $interval days regardless of the comment status
 614          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 ) ) ) {
 615              if ( empty( $comment_ids ) ) {
 616                  return;
 617              }
 618  
 619              $wpdb->queries = array();
 620  
 621              foreach ( $comment_ids as $comment_id ) {
 622                  delete_comment_meta( $comment_id, 'akismet_as_submitted' );
 623                  do_action( 'akismet_batch_delete_count', __FUNCTION__ );
 624              }
 625  
 626              do_action( 'akismet_delete_commentmeta_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 );
 627          }
 628  
 629          if ( apply_filters( 'akismet_optimize_table', ( mt_rand( 1, 5000 ) == 11 ), $wpdb->commentmeta ) ) { // lucky number
 630              $wpdb->query( "OPTIMIZE TABLE {$wpdb->commentmeta}" );
 631          }
 632      }
 633  
 634      // Clear out comments meta that no longer have corresponding comments in the database
 635  	public static function delete_orphaned_commentmeta() {
 636          global $wpdb;
 637  
 638          $last_meta_id  = 0;
 639          $start_time    = isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true );
 640          $max_exec_time = max( ini_get( 'max_execution_time' ) - 5, 3 );
 641  
 642          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 ) ) ) {
 643              if ( empty( $commentmeta_results ) ) {
 644                  return;
 645              }
 646  
 647              $wpdb->queries = array();
 648  
 649              $commentmeta_deleted = 0;
 650  
 651              foreach ( $commentmeta_results as $commentmeta ) {
 652                  if ( 'akismet_' == substr( $commentmeta->meta_key, 0, 8 ) ) {
 653                      delete_comment_meta( $commentmeta->comment_id, $commentmeta->meta_key );
 654                      do_action( 'akismet_batch_delete_count', __FUNCTION__ );
 655                      ++$commentmeta_deleted;
 656                  }
 657  
 658                  $last_meta_id = $commentmeta->meta_id;
 659              }
 660  
 661              do_action( 'akismet_delete_commentmeta_batch', $commentmeta_deleted );
 662  
 663              // If we're getting close to max_execution_time, quit for this round.
 664              if ( microtime( true ) - $start_time > $max_exec_time ) {
 665                  return;
 666              }
 667          }
 668  
 669          if ( apply_filters( 'akismet_optimize_table', ( mt_rand( 1, 5000 ) == 11 ), $wpdb->commentmeta ) ) { // lucky number
 670              $wpdb->query( "OPTIMIZE TABLE {$wpdb->commentmeta}" );
 671          }
 672      }
 673  
 674      // how many approved comments does this author have?
 675  	public static function get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) {
 676          global $wpdb;
 677  
 678          /**
 679           * Which comment types should be ignored when counting a user's approved comments?
 680           *
 681           * Some plugins add entries to the comments table that are not actual
 682           * comments that could have been checked by Akismet. Allow these comments
 683           * to be excluded from the "approved comment count" query in order to
 684           * avoid artificially inflating the approved comment count.
 685           *
 686           * @param array $comment_types An array of comment types that won't be considered
 687           *                             when counting a user's approved comments.
 688           *
 689           * @since 4.2.2
 690           */
 691          $excluded_comment_types = apply_filters( 'akismet_excluded_comment_types', array() );
 692  
 693          $comment_type_where = '';
 694  
 695          if ( is_array( $excluded_comment_types ) && ! empty( $excluded_comment_types ) ) {
 696              $excluded_comment_types = array_unique( $excluded_comment_types );
 697  
 698              foreach ( $excluded_comment_types as $excluded_comment_type ) {
 699                  $comment_type_where .= $wpdb->prepare( ' AND comment_type <> %s ', $excluded_comment_type );
 700              }
 701          }
 702  
 703          if ( ! empty( $user_id ) ) {
 704              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 ) );
 705          }
 706  
 707          if ( ! empty( $comment_author_email ) ) {
 708              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 ) );
 709          }
 710  
 711          return 0;
 712      }
 713  
 714      /**
 715       * Get the full comment history for a given comment, as an array in reverse chronological order.
 716       * Each entry will have an 'event', a 'time', and possible a 'message' member (if the entry is old enough).
 717       * Some entries will also have a 'user' or 'meta' member.
 718       *
 719       * @param int $comment_id The relevant comment ID.
 720       * @return array|bool An array of history events, or false if there is no history.
 721       */
 722  	public static function get_comment_history( $comment_id ) {
 723          $history = get_comment_meta( $comment_id, 'akismet_history', false );
 724          if ( empty( $history ) || empty( $history[0] ) ) {
 725              return false;
 726          }
 727  
 728          /*
 729          // To see all variants when testing.
 730          $history[] = array( 'time' => 445856401, 'message' => 'Old versions of Akismet stored the message as a literal string in the commentmeta.', 'event' => null );
 731          $history[] = array( 'time' => 445856402, 'event' => 'recheck-spam' );
 732          $history[] = array( 'time' => 445856403, 'event' => 'check-spam' );
 733          $history[] = array( 'time' => 445856404, 'event' => 'recheck-ham' );
 734          $history[] = array( 'time' => 445856405, 'event' => 'check-ham' );
 735          $history[] = array( 'time' => 445856406, 'event' => 'wp-blacklisted' );
 736          $history[] = array( 'time' => 445856406, 'event' => 'wp-disallowed' );
 737          $history[] = array( 'time' => 445856407, 'event' => 'report-spam' );
 738          $history[] = array( 'time' => 445856408, 'event' => 'report-spam', 'user' => 'sam' );
 739          $history[] = array( 'message' => 'sam reported this comment as spam (hardcoded message).', 'time' => 445856400, 'event' => 'report-spam', 'user' => 'sam' );
 740          $history[] = array( 'time' => 445856409, 'event' => 'report-ham', 'user' => 'sam' );
 741          $history[] = array( 'message' => 'sam reported this comment as ham (hardcoded message).', 'time' => 445856400, 'event' => 'report-ham', 'user' => 'sam' ); //
 742          $history[] = array( 'time' => 445856410, 'event' => 'cron-retry-spam' );
 743          $history[] = array( 'time' => 445856411, 'event' => 'cron-retry-ham' );
 744          $history[] = array( 'time' => 445856412, 'event' => 'check-error' ); //
 745          $history[] = array( 'time' => 445856413, 'event' => 'check-error', 'meta' => array( 'response' => 'The server was taking a nap.' ) );
 746          $history[] = array( 'time' => 445856414, 'event' => 'recheck-error' ); // Should not generate a message.
 747          $history[] = array( 'time' => 445856415, 'event' => 'recheck-error', 'meta' => array( 'response' => 'The server was taking a nap.' ) );
 748          $history[] = array( 'time' => 445856416, 'event' => 'status-changedtrash' );
 749          $history[] = array( 'time' => 445856417, 'event' => 'status-changedspam' );
 750          $history[] = array( 'time' => 445856418, 'event' => 'status-changedhold' );
 751          $history[] = array( 'time' => 445856419, 'event' => 'status-changedapprove' );
 752          $history[] = array( 'time' => 445856420, 'event' => 'status-changed-trash' );
 753          $history[] = array( 'time' => 445856421, 'event' => 'status-changed-spam' );
 754          $history[] = array( 'time' => 445856422, 'event' => 'status-changed-hold' );
 755          $history[] = array( 'time' => 445856423, 'event' => 'status-changed-approve' );
 756          $history[] = array( 'time' => 445856424, 'event' => 'status-trash', 'user' => 'sam' );
 757          $history[] = array( 'time' => 445856425, 'event' => 'status-spam', 'user' => 'sam' );
 758          $history[] = array( 'time' => 445856426, 'event' => 'status-hold', 'user' => 'sam' );
 759          $history[] = array( 'time' => 445856427, 'event' => 'status-approve', 'user' => 'sam' );
 760          $history[] = array( 'time' => 445856427, 'event' => 'webhook-spam' );
 761          $history[] = array( 'time' => 445856427, 'event' => 'webhook-ham' );
 762          $history[] = array( 'time' => 445856427, 'event' => 'webhook-spam-noaction' );
 763          $history[] = array( 'time' => 445856427, 'event' => 'webhook-ham-noaction' );
 764          */
 765  
 766          usort( $history, array( 'Akismet', '_cmp_time' ) );
 767          return $history;
 768      }
 769  
 770      /**
 771       * Log an event for a given comment, storing it in comment_meta.
 772       *
 773       * @param int    $comment_id The ID of the relevant comment.
 774       * @param string $message The string description of the event. No longer used.
 775       * @param string $event The event code.
 776       * @param array  $meta Metadata about the history entry. e.g., the user that reported or changed the status of a given comment.
 777       */
 778  	public static function update_comment_history( $comment_id, $message, $event = null, $meta = null ) {
 779          global $current_user;
 780  
 781          $user = '';
 782  
 783          $event = array(
 784              'time'  => self::_get_microtime(),
 785              'event' => $event,
 786          );
 787  
 788          if ( is_object( $current_user ) && isset( $current_user->user_login ) ) {
 789              $event['user'] = $current_user->user_login;
 790          }
 791  
 792          if ( ! empty( $meta ) ) {
 793              $event['meta'] = $meta;
 794          }
 795  
 796          // $unique = false so as to allow multiple values per comment
 797          $r = add_comment_meta( $comment_id, 'akismet_history', $event, false );
 798      }
 799  
 800  	public static function check_db_comment( $id, $recheck_reason = 'recheck_queue' ) {
 801          global $wpdb;
 802  
 803          if ( ! self::get_api_key() ) {
 804              return new WP_Error( 'akismet-not-configured', __( 'Akismet is not configured. Please enter an API key.', 'akismet' ) );
 805          }
 806  
 807          $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id ), ARRAY_A );
 808  
 809          if ( ! $c ) {
 810              return new WP_Error( 'invalid-comment-id', __( 'Comment not found.', 'akismet' ) );
 811          }
 812  
 813          $c['user_ip']        = $c['comment_author_IP'];
 814          $c['user_agent']     = $c['comment_agent'];
 815          $c['referrer']       = '';
 816          $c['blog']           = get_option( 'home' );
 817          $c['blog_lang']      = get_locale();
 818          $c['blog_charset']   = get_option( 'blog_charset' );
 819          $c['permalink']      = get_permalink( $c['comment_post_ID'] );
 820          $c['recheck_reason'] = $recheck_reason;
 821  
 822          $c['user_role'] = '';
 823          if ( ! empty( $c['user_ID'] ) ) {
 824              $c['user_role'] = self::get_user_roles( $c['user_ID'] );
 825          }
 826  
 827          if ( self::is_test_mode() ) {
 828              $c['is_test'] = 'true';
 829          }
 830  
 831          $c = apply_filters( 'akismet_request_args', $c, 'comment-check' );
 832  
 833          $response = self::http_post( self::build_query( $c ), 'comment-check' );
 834  
 835          if ( ! empty( $response[1] ) ) {
 836              return $response[1];
 837          }
 838  
 839          return false;
 840      }
 841  
 842  	public static function recheck_comment( $id, $recheck_reason = 'recheck_queue' ) {
 843          add_comment_meta( $id, 'akismet_rechecking', true );
 844  
 845          $api_response = self::check_db_comment( $id, $recheck_reason );
 846  
 847          if ( is_wp_error( $api_response ) ) {
 848              // Invalid comment ID.
 849          } elseif ( 'true' === $api_response ) {
 850              wp_set_comment_status( $id, 'spam' );
 851              update_comment_meta( $id, 'akismet_result', 'true' );
 852              delete_comment_meta( $id, 'akismet_error' );
 853              delete_comment_meta( $id, 'akismet_delayed_moderation_email' );
 854              self::update_comment_history( $id, '', 'recheck-spam' );
 855          } elseif ( 'false' === $api_response ) {
 856              update_comment_meta( $id, 'akismet_result', 'false' );
 857              delete_comment_meta( $id, 'akismet_error' );
 858              delete_comment_meta( $id, 'akismet_delayed_moderation_email' );
 859              self::update_comment_history( $id, '', 'recheck-ham' );
 860          } else {
 861              // abnormal result: error
 862              update_comment_meta( $id, 'akismet_result', 'error' );
 863              self::update_comment_history(
 864                  $id,
 865                  '',
 866                  'recheck-error',
 867                  array( 'response' => substr( $api_response, 0, 50 ) )
 868              );
 869          }
 870  
 871          delete_comment_meta( $id, 'akismet_rechecking' );
 872  
 873          return $api_response;
 874      }
 875  
 876  	public static function transition_comment_status( $new_status, $old_status, $comment ) {
 877  
 878          if ( $new_status == $old_status ) {
 879              return;
 880          }
 881  
 882          if ( 'spam' === $new_status || 'spam' === $old_status ) {
 883              // Clear the cache of the "X comments in your spam queue" count on the dashboard.
 884              wp_cache_delete( 'akismet_spam_count', 'widget' );
 885          }
 886  
 887          // we don't need to record a history item for deleted comments
 888          if ( $new_status == 'delete' ) {
 889              return;
 890          }
 891  
 892          if ( ! current_user_can( 'edit_post', $comment->comment_post_ID ) && ! current_user_can( 'moderate_comments' ) ) {
 893              return;
 894          }
 895  
 896          if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING == true ) {
 897              return;
 898          }
 899  
 900          // if this is present, it means the status has been changed by a re-check, not an explicit user action
 901          if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) ) {
 902              return;
 903          }
 904  
 905          if ( function_exists( 'getallheaders' ) ) {
 906              $request_headers = getallheaders();
 907  
 908              foreach ( $request_headers as $header => $value ) {
 909                  if ( strtolower( $header ) == 'x-akismet-webhook' ) {
 910                      // This change is due to a webhook request.
 911                      return;
 912                  }
 913              }
 914          }
 915  
 916          // Assumption alert:
 917          // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
 918          // is changed automatically by another plugin.  Unfortunately WordPress doesn't provide an unambiguous way to
 919          // determine why the transition_comment_status action was triggered.  And there are several different ways by which
 920          // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
 921          // We'll assume that this is an explicit user action if certain POST/GET variables exist.
 922          if (
 923              // status=spam: Marking as spam via the REST API or...
 924              // 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?...
 925              // status=approved: Unspamming via the REST API (Calypso) or...
 926              ( isset( $_POST['status'] ) && in_array( $_POST['status'], array( 'spam', 'unspam', 'approved' ) ) )
 927              // spam=1: Clicking "Spam" underneath a comment in wp-admin and allowing the AJAX request to happen.
 928              || ( isset( $_POST['spam'] ) && (int) $_POST['spam'] == 1 )
 929              // 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.
 930              || ( isset( $_POST['unspam'] ) && (int) $_POST['unspam'] == 1 )
 931              // comment_status=spam/unspam: It's unclear where this is happening.
 932              || ( isset( $_POST['comment_status'] ) && in_array( $_POST['comment_status'], array( 'spam', 'unspam' ) ) )
 933              // action=spam: Choosing "Mark as Spam" from the Bulk Actions dropdown in wp-admin (or the "Spam it" link in notification emails).
 934              // action=unspam: Choosing "Not Spam" from the Bulk Actions dropdown in wp-admin.
 935              // action=spamcomment: Following the "Spam" link below a comment in wp-admin (not allowing AJAX request to happen).
 936              // action=unspamcomment: Following the "Not Spam" link below a comment in wp-admin (not allowing AJAX request to happen).
 937              || ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'spam', 'unspam', 'spamcomment', 'unspamcomment' ) ) )
 938              // action=editedcomment: Editing a comment via wp-admin (and possibly changing its status).
 939              || ( isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'editedcomment' ) ) )
 940              // for=jetpack: Moderation via the WordPress app, Calypso, anything powered by the Jetpack connection.
 941              || ( isset( $_GET['for'] ) && ( 'jetpack' == $_GET['for'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) )
 942              // Certain WordPress.com API requests
 943              || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST )
 944              // WordPress.org REST API requests
 945              || ( defined( 'REST_REQUEST' ) && REST_REQUEST )
 946          ) {
 947              if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || ! $old_status ) ) {
 948                  return self::submit_spam_comment( $comment->comment_ID );
 949              } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
 950                  return self::submit_nonspam_comment( $comment->comment_ID );
 951              }
 952          }
 953  
 954          self::update_comment_history( $comment->comment_ID, '', 'status-' . $new_status );
 955      }
 956  
 957  	public static function submit_spam_comment( $comment_id ) {
 958          global $wpdb, $current_user, $current_site;
 959  
 960          $comment_id = (int) $comment_id;
 961  
 962          $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ), ARRAY_A );
 963  
 964          if ( ! $comment ) {
 965              // it was deleted
 966              return;
 967          }
 968  
 969          if ( 'spam' != $comment['comment_approved'] ) {
 970              return;
 971          }
 972  
 973          self::update_comment_history( $comment_id, '', 'report-spam' );
 974  
 975          // If the user hasn't configured Akismet, there's nothing else to do at this point.
 976          if ( ! self::get_api_key() ) {
 977              return;
 978          }
 979  
 980          // use the original version stored in comment_meta if available
 981          $as_submitted = self::sanitize_comment_as_submitted( get_comment_meta( $comment_id, 'akismet_as_submitted', true ) );
 982  
 983          if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) {
 984              $comment = array_merge( $comment, $as_submitted );
 985          }
 986  
 987          $comment['blog']         = get_option( 'home' );
 988          $comment['blog_lang']    = get_locale();
 989          $comment['blog_charset'] = get_option( 'blog_charset' );
 990          $comment['permalink']    = get_permalink( $comment['comment_post_ID'] );
 991  
 992          if ( is_object( $current_user ) ) {
 993              $comment['reporter'] = $current_user->user_login;
 994          }
 995  
 996          if ( is_object( $current_site ) ) {
 997              $comment['site_domain'] = $current_site->domain;
 998          }
 999  
1000          $comment['user_role'] = '';
1001          if ( ! empty( $comment['user_ID'] ) ) {
1002              $comment['user_role'] = self::get_user_roles( $comment['user_ID'] );
1003          }
1004  
1005          if ( self::is_test_mode() ) {
1006              $comment['is_test'] = 'true';
1007          }
1008  
1009          $post = get_post( $comment['comment_post_ID'] );
1010  
1011          if ( ! is_null( $post ) ) {
1012              $comment['comment_post_modified_gmt'] = $post->post_modified_gmt;
1013          }
1014  
1015          $comment['comment_check_response'] = self::last_comment_check_response( $comment_id );
1016  
1017          $comment = apply_filters( 'akismet_request_args', $comment, 'submit-spam' );
1018  
1019          $response = self::http_post( self::build_query( $comment ), 'submit-spam' );
1020  
1021          update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
1022  
1023          if ( $comment['reporter'] ) {
1024              update_comment_meta( $comment_id, 'akismet_user', $comment['reporter'] );
1025          }
1026  
1027          do_action( 'akismet_submit_spam_comment', $comment_id, $response[1] );
1028      }
1029  
1030  	public static function submit_nonspam_comment( $comment_id ) {
1031          global $wpdb, $current_user, $current_site;
1032  
1033          $comment_id = (int) $comment_id;
1034  
1035          $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ), ARRAY_A );
1036  
1037          if ( ! $comment ) {
1038              // it was deleted
1039              return;
1040          }
1041  
1042          self::update_comment_history( $comment_id, '', 'report-ham' );
1043  
1044          // If the user hasn't configured Akismet, there's nothing else to do at this point.
1045          if ( ! self::get_api_key() ) {
1046              return;
1047          }
1048  
1049          // use the original version stored in comment_meta if available
1050          $as_submitted = self::sanitize_comment_as_submitted( get_comment_meta( $comment_id, 'akismet_as_submitted', true ) );
1051  
1052          if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) {
1053              $comment = array_merge( $comment, $as_submitted );
1054          }
1055  
1056          $comment['blog']         = get_option( 'home' );
1057          $comment['blog_lang']    = get_locale();
1058          $comment['blog_charset'] = get_option( 'blog_charset' );
1059          $comment['permalink']    = get_permalink( $comment['comment_post_ID'] );
1060          $comment['user_role']    = '';
1061  
1062          if ( is_object( $current_user ) ) {
1063              $comment['reporter'] = $current_user->user_login;
1064          }
1065  
1066          if ( is_object( $current_site ) ) {
1067              $comment['site_domain'] = $current_site->domain;
1068          }
1069  
1070          if ( ! empty( $comment['user_ID'] ) ) {
1071              $comment['user_role'] = self::get_user_roles( $comment['user_ID'] );
1072          }
1073  
1074          if ( self::is_test_mode() ) {
1075              $comment['is_test'] = 'true';
1076          }
1077  
1078          $post = get_post( $comment['comment_post_ID'] );
1079  
1080          if ( ! is_null( $post ) ) {
1081              $comment['comment_post_modified_gmt'] = $post->post_modified_gmt;
1082          }
1083  
1084          $comment['comment_check_response'] = self::last_comment_check_response( $comment_id );
1085  
1086          $comment = apply_filters( 'akismet_request_args', $comment, 'submit-ham' );
1087  
1088          $response = self::http_post( self::build_query( $comment ), 'submit-ham' );
1089  
1090          update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
1091  
1092          if ( $comment['reporter'] ) {
1093              update_comment_meta( $comment_id, 'akismet_user', $comment['reporter'] );
1094          }
1095  
1096          do_action( 'akismet_submit_nonspam_comment', $comment_id, $response[1] );
1097      }
1098  
1099  	public static function cron_recheck() {
1100          global $wpdb;
1101  
1102          $api_key = self::get_api_key();
1103  
1104          $status = self::verify_key( $api_key );
1105          if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) {
1106              // since there is currently a problem with the key, reschedule a check for 6 hours hence
1107              wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' );
1108              do_action( 'akismet_scheduled_recheck', 'key-problem-' . get_option( 'akismet_alert_code' ) . '-' . $status );
1109              return false;
1110          }
1111  
1112          delete_option( 'akismet_available_servers' );
1113  
1114          $comment_errors = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'    LIMIT 100" );
1115  
1116          load_plugin_textdomain( 'akismet' );
1117  
1118          foreach ( (array) $comment_errors as $comment_id ) {
1119              // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck
1120              $comment = get_comment( $comment_id );
1121  
1122              if (
1123                  ! $comment // Comment has been deleted
1124                  || strtotime( $comment->comment_date_gmt ) < strtotime( '-15 days' ) // Comment is too old.
1125                  || $comment->comment_approved !== '0' // Comment is no longer in the Pending queue
1126                  ) {
1127                  delete_comment_meta( $comment_id, 'akismet_error' );
1128                  delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
1129                  continue;
1130              }
1131  
1132              add_comment_meta( $comment_id, 'akismet_rechecking', true );
1133              $status = self::check_db_comment( $comment_id, 'retry' );
1134  
1135              $event = '';
1136              if ( $status == 'true' ) {
1137                  $event = 'cron-retry-spam';
1138              } elseif ( $status == 'false' ) {
1139                  $event = 'cron-retry-ham';
1140              }
1141  
1142              // If we got back a legit response then update the comment history
1143              // other wise just bail now and try again later.  No point in
1144              // re-trying all the comments once we hit one failure.
1145              if ( ! empty( $event ) ) {
1146                  delete_comment_meta( $comment_id, 'akismet_error' );
1147                  self::update_comment_history( $comment_id, '', $event );
1148                  update_comment_meta( $comment_id, 'akismet_result', $status );
1149                  // make sure the comment status is still pending.  if it isn't, that means the user has already moved it elsewhere.
1150                  $comment = get_comment( $comment_id );
1151                  if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) {
1152                      if ( $status == 'true' ) {
1153                          wp_spam_comment( $comment_id );
1154                      } elseif ( $status == 'false' ) {
1155                          // comment is good, but it's still in the pending queue.  depending on the moderation settings
1156                          // we may need to change it to approved.
1157                          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 ) ) {
1158                              wp_set_comment_status( $comment_id, 1 );
1159                          } elseif ( get_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ) ) {
1160                              wp_notify_moderator( $comment_id );
1161                          }
1162                      }
1163                  }
1164  
1165                  delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
1166              } else {
1167                  // If this comment has been pending moderation for longer than MAX_DELAY_BEFORE_MODERATION_EMAIL,
1168                  // send a moderation email now.
1169                  if ( ( intval( gmdate( 'U' ) ) - strtotime( $comment->comment_date_gmt ) ) < self::MAX_DELAY_BEFORE_MODERATION_EMAIL ) {
1170                      delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
1171                      wp_notify_moderator( $comment_id );
1172                  }
1173  
1174                  delete_comment_meta( $comment_id, 'akismet_rechecking' );
1175                  wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
1176                  do_action( 'akismet_scheduled_recheck', 'check-db-comment-' . $status );
1177                  return;
1178              }
1179              delete_comment_meta( $comment_id, 'akismet_rechecking' );
1180          }
1181  
1182          $remaining = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" );
1183          if ( $remaining && ! wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
1184              wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
1185              do_action( 'akismet_scheduled_recheck', 'remaining' );
1186          }
1187      }
1188  
1189  	public static function fix_scheduled_recheck() {
1190          $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' );
1191          if ( ! $future_check ) {
1192              return;
1193          }
1194  
1195          if ( get_option( 'akismet_alert_code' ) > 0 ) {
1196              return;
1197          }
1198  
1199          $check_range = time() + 1200;
1200          if ( $future_check > $check_range ) {
1201              wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' );
1202              wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' );
1203              do_action( 'akismet_scheduled_recheck', 'fix-scheduled-recheck' );
1204          }
1205      }
1206  
1207  	public static function add_comment_nonce( $post_id ) {
1208          /**
1209           * To disable the Akismet comment nonce, add a filter for the 'akismet_comment_nonce' tag
1210           * and return any string value that is not 'true' or '' (empty string).
1211           *
1212           * Don't return boolean false, because that implies that the 'akismet_comment_nonce' option
1213           * has not been set and that Akismet should just choose the default behavior for that
1214           * situation.
1215           */
1216  
1217          if ( ! self::get_api_key() ) {
1218              return;
1219          }
1220  
1221          $akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
1222  
1223          if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' ) {
1224              echo '<p style="display: none;">';
1225              wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', false );
1226              echo '</p>';
1227          }
1228      }
1229  
1230  	public static function is_test_mode() {
1231          return defined( 'AKISMET_TEST_MODE' ) && AKISMET_TEST_MODE;
1232      }
1233  
1234  	public static function allow_discard() {
1235          if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1236              return false;
1237          }
1238          if ( is_user_logged_in() ) {
1239              return false;
1240          }
1241  
1242          return ( get_option( 'akismet_strictness' ) === '1' );
1243      }
1244  
1245  	public static function get_ip_address() {
1246          return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
1247      }
1248  
1249      /**
1250       * Using the unique values that we assign, do we consider these two comments
1251       * to be the same instance of a comment?
1252       *
1253       * The only fields that matter in $comment1 and $comment2 are akismet_guid and akismet_skipped_microtime.
1254       * We set both of these during the comment-check call, and if the comment has been saved to the DB,
1255       * we save them as comment meta and add them back into the comment array before comparing the comments.
1256       *
1257       * @param mixed $comment1 A comment object or array.
1258       * @param mixed $comment2 A comment object or array.
1259       * @return bool Whether the two comments should be treated as the same comment.
1260       */
1261  	private static function comments_match( $comment1, $comment2 ) {
1262          $comment1 = (array) $comment1;
1263          $comment2 = (array) $comment2;
1264  
1265          if ( ! empty( $comment1['akismet_guid'] ) && ! empty( $comment2['akismet_guid'] ) ) {
1266              // If the comment got sent to the API and got a response, it will have a GUID.
1267  
1268              return ( $comment1['akismet_guid'] == $comment2['akismet_guid'] );
1269          } else if ( ! empty( $comment1['akismet_skipped_microtime'] ) && ! empty( $comment2['akismet_skipped_microtime'] ) ) {
1270              // It won't have a GUID if it didn't get sent to the API because it matched the disallowed list,
1271              // but it should have a microtimestamp to use here for matching against the comment DB entry it matches.
1272              return ( strval( $comment1['akismet_skipped_microtime'] ) == strval( $comment2['akismet_skipped_microtime'] ) );
1273          }
1274  
1275          return false;
1276      }
1277  
1278      /**
1279       * Does the supplied comment match the details of the one most recently stored in self::$last_comment?
1280       *
1281       * @param array $comment
1282       * @return bool Whether the comment supplied as an argument is a match for the one we have stored in $last_comment.
1283       */
1284  	public static function matches_last_comment( $comment ) {
1285          if ( ! self::$last_comment ) {
1286              return false;
1287          }
1288  
1289          return self::comments_match( $comment, self::$last_comment );
1290      }
1291  
1292      /**
1293       * Because of the order of operations, we don't always know the comment ID of the comment that we're checking,
1294       * so we have to be able to match the comment we cached locally with the comment from the DB.
1295       *
1296       * @param int $comment_id
1297       * @return bool Whether the comment represented by $comment_id is a match for the one we have stored in $last_comment.
1298       */
1299  	public static function matches_last_comment_by_id( $comment_id ) {
1300          return self::matches_last_comment( self::get_fields_for_comment_matching( $comment_id ) );
1301      }
1302  
1303      /**
1304       * Given a comment ID, retrieve the values that we use for matching comments together.
1305       *
1306       * @param int $comment_id
1307       * @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.
1308       */
1309  	public static function get_fields_for_comment_matching( $comment_id ) {
1310          return array(
1311              'akismet_guid' => get_comment_meta( $comment_id, 'akismet_guid', true ),
1312              'akismet_skipped_microtime' => get_comment_meta( $comment_id, 'akismet_skipped_microtime', true ),
1313          );
1314      }
1315  
1316  	private static function get_user_agent() {
1317          return isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null;
1318      }
1319  
1320  	private static function get_referer() {
1321          return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null;
1322      }
1323  
1324      // return a comma-separated list of role names for the given user
1325  	public static function get_user_roles( $user_id ) {
1326          $comment_user = null;
1327          $roles        = false;
1328  
1329          if ( ! class_exists( 'WP_User' ) ) {
1330              return false;
1331          }
1332  
1333          if ( $user_id > 0 ) {
1334              $comment_user = new WP_User( $user_id );
1335              if ( isset( $comment_user->roles ) ) {
1336                  $roles = implode( ',', $comment_user->roles );
1337              }
1338          }
1339  
1340          if ( is_multisite() && is_super_admin( $user_id ) ) {
1341              if ( empty( $roles ) ) {
1342                  $roles = 'super_admin';
1343              } else {
1344                  $comment_user->roles[] = 'super_admin';
1345                  $roles                 = implode( ',', $comment_user->roles );
1346              }
1347          }
1348  
1349          return $roles;
1350      }
1351  
1352      // filter handler used to return a spam result to pre_comment_approved
1353  	public static function last_comment_status( $approved, $comment ) {
1354          if ( is_null( self::$last_comment_result ) ) {
1355              // We didn't have reason to store the result of the last check.
1356              return $approved;
1357          }
1358  
1359          // Only do this if it's the correct comment.
1360          if ( ! self::matches_last_comment( $comment ) ) {
1361              self::log( "comment_is_spam mismatched comment, returning unaltered $approved" );
1362              return $approved;
1363          }
1364  
1365          if ( 'trash' === $approved ) {
1366              // If the last comment we checked has had its approval set to 'trash',
1367              // then it failed the comment blacklist check. Let that blacklist override
1368              // the spam check, since users have the (valid) expectation that when
1369              // they fill out their blacklists, comments that match it will always
1370              // end up in the trash.
1371              return $approved;
1372          }
1373  
1374          // bump the counter here instead of when the filter is added to reduce the possibility of overcounting
1375          if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) {
1376              update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr );
1377          }
1378  
1379          return self::$last_comment_result;
1380      }
1381  
1382      /**
1383       * If Akismet is temporarily unreachable, we don't want to "spam" the blogger with
1384       * moderation emails for comments that will be automatically cleared or spammed on
1385       * the next retry.
1386       *
1387       * For comments that will be rechecked later, empty the list of email addresses that
1388       * the moderation email would be sent to.
1389       *
1390       * @param array $emails An array of email addresses that the moderation email will be sent to.
1391       * @param int   $comment_id The ID of the relevant comment.
1392       * @return array An array of email addresses that the moderation email will be sent to.
1393       */
1394  	public static function disable_moderation_emails_if_unreachable( $emails, $comment_id ) {
1395          if ( ! empty( self::$prevent_moderation_email_for_these_comments ) && ! empty( $emails ) ) {
1396              $matching_fields = self::get_fields_for_comment_matching( $comment_id );
1397  
1398              // self::$prevent_moderation_email_for_these_comments is an array of $commentdata objects
1399              // saved immediately after the comment-check request completes.
1400              foreach ( self::$prevent_moderation_email_for_these_comments as $possible_match ) {
1401                  if ( self::comments_match( $possible_match, $matching_fields ) ) {
1402                      update_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true );
1403                      return array();
1404                  }
1405              }
1406          }
1407  
1408          return $emails;
1409      }
1410  
1411  	public static function _cmp_time( $a, $b ) {
1412          return $a['time'] > $b['time'] ? -1 : 1;
1413      }
1414  
1415  	public static function _get_microtime() {
1416          $mtime = explode( ' ', microtime() );
1417          return $mtime[1] + $mtime[0];
1418      }
1419  
1420      /**
1421       * Make a POST request to the Akismet API.
1422       *
1423       * @param string $request The body of the request.
1424       * @param string $path The path for the request.
1425       * @param string $ip The specific IP address to hit.
1426       * @return array A two-member array consisting of the headers and the response body, both empty in the case of a failure.
1427       */
1428  	public static function http_post( $request, $path, $ip = null ) {
1429  
1430          $akismet_ua = sprintf( 'WordPress/%s | Akismet/%s', $GLOBALS['wp_version'], constant( 'AKISMET_VERSION' ) );
1431          $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua );
1432  
1433          $host    = self::API_HOST;
1434          $api_key = self::get_api_key();
1435  
1436          if ( $api_key ) {
1437              $request = add_query_arg( 'api_key', $api_key, $request );
1438          }
1439  
1440          $http_host = $host;
1441          // use a specific IP if provided
1442          // needed by Akismet_Admin::check_server_connectivity()
1443          if ( $ip && long2ip( ip2long( $ip ) ) ) {
1444              $http_host = $ip;
1445          }
1446  
1447          $http_args = array(
1448              'body'        => $request,
1449              'headers'     => array(
1450                  'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
1451                  'Host'         => $host,
1452                  'User-Agent'   => $akismet_ua,
1453              ),
1454              'httpversion' => '1.0',
1455              'timeout'     => 15,
1456          );
1457  
1458          $akismet_url = $http_akismet_url = "http://{$http_host}/1.1/{$path}";
1459  
1460          /**
1461           * Try SSL first; if that fails, try without it and don't try it again for a while.
1462           */
1463  
1464          $ssl = $ssl_failed = false;
1465  
1466          // Check if SSL requests were disabled fewer than X hours ago.
1467          $ssl_disabled = get_option( 'akismet_ssl_disabled' );
1468  
1469          if ( $ssl_disabled && $ssl_disabled < ( time() - 60 * 60 * 24 ) ) { // 24 hours
1470              $ssl_disabled = false;
1471              delete_option( 'akismet_ssl_disabled' );
1472          } elseif ( $ssl_disabled ) {
1473              do_action( 'akismet_ssl_disabled' );
1474          }
1475  
1476          if ( ! $ssl_disabled && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) {
1477              $akismet_url = set_url_scheme( $akismet_url, 'https' );
1478  
1479              do_action( 'akismet_https_request_pre' );
1480          }
1481  
1482          $response = wp_remote_post( $akismet_url, $http_args );
1483  
1484          self::log( compact( 'akismet_url', 'http_args', 'response' ) );
1485  
1486          if ( $ssl && is_wp_error( $response ) ) {
1487              do_action( 'akismet_https_request_failure', $response );
1488  
1489              // Intermittent connection problems may cause the first HTTPS
1490              // request to fail and subsequent HTTP requests to succeed randomly.
1491              // Retry the HTTPS request once before disabling SSL for a time.
1492              $response = wp_remote_post( $akismet_url, $http_args );
1493  
1494              self::log( compact( 'akismet_url', 'http_args', 'response' ) );
1495  
1496              if ( is_wp_error( $response ) ) {
1497                  $ssl_failed = true;
1498  
1499                  do_action( 'akismet_https_request_failure', $response );
1500  
1501                  do_action( 'akismet_http_request_pre' );
1502  
1503                  // Try the request again without SSL.
1504                  $response = wp_remote_post( $http_akismet_url, $http_args );
1505  
1506                  self::log( compact( 'http_akismet_url', 'http_args', 'response' ) );
1507              }
1508          }
1509  
1510          if ( is_wp_error( $response ) ) {
1511              do_action( 'akismet_request_failure', $response );
1512  
1513              return array( '', '' );
1514          }
1515  
1516          if ( $ssl_failed ) {
1517              // The request failed when using SSL but succeeded without it. Disable SSL for future requests.
1518              update_option( 'akismet_ssl_disabled', time() );
1519  
1520              do_action( 'akismet_https_disabled' );
1521          }
1522  
1523          $simplified_response = array( $response['headers'], $response['body'] );
1524  
1525          $alert_code_check_paths = array(
1526              'verify-key',
1527              'comment-check',
1528              'get-stats',
1529          );
1530  
1531          if ( in_array( $path, $alert_code_check_paths ) ) {
1532              self::update_alert( $simplified_response );
1533          }
1534  
1535          return $simplified_response;
1536      }
1537  
1538      // given a response from an API call like check_key_status(), update the alert code options if an alert is present.
1539  	public static function update_alert( $response ) {
1540          $alert_option_prefix = 'akismet_alert_';
1541          $alert_header_prefix = 'x-akismet-alert-';
1542          $alert_header_names  = array(
1543              'code',
1544              'msg',
1545              'api-calls',
1546              'usage-limit',
1547              'upgrade-plan',
1548              'upgrade-url',
1549              'upgrade-type',
1550              'upgrade-via-support',
1551          );
1552  
1553          foreach ( $alert_header_names as $alert_header_name ) {
1554              $value = null;
1555              if ( isset( $response[0][ $alert_header_prefix . $alert_header_name ] ) ) {
1556                  $value = $response[0][ $alert_header_prefix . $alert_header_name ];
1557              }
1558  
1559              $option_name = $alert_option_prefix . str_replace( '-', '_', $alert_header_name );
1560              if ( $value != get_option( $option_name ) ) {
1561                  if ( ! $value ) {
1562                      delete_option( $option_name );
1563                  } else {
1564                      update_option( $option_name, $value );
1565                  }
1566              }
1567          }
1568      }
1569  
1570      /**
1571       * Mark akismet-frontend.js as deferred. Because nothing depends on it, it can run at any time
1572       * after it's loaded, and the browser won't have to wait for it to load to continue
1573       * parsing the rest of the page.
1574       */
1575  	public static function set_form_js_async( $tag, $handle, $src ) {
1576          if ( 'akismet-frontend' !== $handle ) {
1577              return $tag;
1578          }
1579  
1580          return preg_replace( '/^<script /i', '<script defer ', $tag );
1581      }
1582  
1583  	public static function get_akismet_form_fields() {
1584          $fields = '';
1585  
1586          $prefix = 'ak_';
1587  
1588          // Contact Form 7 uses _wpcf7 as a prefix to know which fields to exclude from comment_content.
1589          if ( 'wpcf7_form_elements' === current_filter() ) {
1590              $prefix = '_wpcf7_ak_';
1591          }
1592  
1593          $fields .= '<p style="display: none !important;" class="akismet-fields-container" data-prefix="' . esc_attr( $prefix ) . '">';
1594          $fields .= '<label>&#916;<textarea name="' . $prefix . 'hp_textarea" cols="45" rows="8" maxlength="100"></textarea></label>';
1595  
1596          if ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() ) {
1597              // Keep track of how many ak_js fields are in this page so that we don't re-use
1598              // the same ID.
1599              static $field_count = 0;
1600  
1601              ++$field_count;
1602  
1603              $fields .= '<input type="hidden" id="ak_js_' . $field_count . '" name="' . $prefix . 'js" value="' . mt_rand( 0, 250 ) . '"/>';
1604              $fields .= '<script>document.getElementById( "ak_js_' . $field_count . '" ).setAttribute( "value", ( new Date() ).getTime() );</script>';
1605          }
1606  
1607          $fields .= '</p>';
1608  
1609          return $fields;
1610      }
1611  
1612  	public static function output_custom_form_fields( $post_id ) {
1613          if ( 'fluentform/form_element_start' === current_filter() && did_action( 'fluentform_form_element_start' ) ) {
1614              // Already did this via the legacy filter.
1615              return;
1616          }
1617  
1618          // phpcs:ignore WordPress.Security.EscapeOutput
1619          echo self::get_akismet_form_fields();
1620      }
1621  
1622  	public static function inject_custom_form_fields( $html ) {
1623          $html = str_replace( '</form>', self::get_akismet_form_fields() . '</form>', $html );
1624  
1625          return $html;
1626      }
1627  
1628  	public static function append_custom_form_fields( $html ) {
1629          $html .= self::get_akismet_form_fields();
1630  
1631          return $html;
1632      }
1633  
1634      /**
1635       * Ensure that any Akismet-added form fields are included in the comment-check call.
1636       *
1637       * @param array $form
1638       * @param array $data Some plugins will supply the POST data via the filter, since they don't
1639       *                    read it directly from $_POST.
1640       * @return array $form
1641       */
1642  	public static function prepare_custom_form_values( $form, $data = null ) {
1643          if ( 'fluentform/akismet_fields' === current_filter() && did_filter( 'fluentform_akismet_fields' ) ) {
1644              // Already updated the form fields via the legacy filter.
1645              return $form;
1646          }
1647  
1648          if ( is_null( $data ) ) {
1649              // phpcs:ignore WordPress.Security.NonceVerification.Missing
1650              $data = $_POST;
1651          }
1652  
1653          $prefix = 'ak_';
1654  
1655          // Contact Form 7 uses _wpcf7 as a prefix to know which fields to exclude from comment_content.
1656          if ( 'wpcf7_akismet_parameters' === current_filter() ) {
1657              $prefix = '_wpcf7_ak_';
1658          }
1659  
1660          foreach ( $data as $key => $val ) {
1661              if ( 0 === strpos( $key, $prefix ) ) {
1662                  $form[ 'POST_ak_' . substr( $key, strlen( $prefix ) ) ] = $val;
1663              }
1664          }
1665  
1666          return $form;
1667      }
1668  
1669  	private static function bail_on_activation( $message, $deactivate = true ) {
1670          ?>
1671  <!doctype html>
1672  <html>
1673  <head>
1674  <meta charset="<?php bloginfo( 'charset' ); ?>" />
1675  <style>
1676  * {
1677      text-align: center;
1678      margin: 0;
1679      padding: 0;
1680      font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
1681  }
1682  p {
1683      margin-top: 1em;
1684      font-size: 18px;
1685  }
1686  </style>
1687  </head>
1688  <body>
1689  <p><?php echo esc_html( $message ); ?></p>
1690  </body>
1691  </html>
1692          <?php
1693          if ( $deactivate ) {
1694              $plugins = get_option( 'active_plugins' );
1695              $akismet = plugin_basename( AKISMET__PLUGIN_DIR . 'akismet.php' );
1696              $update  = false;
1697              foreach ( $plugins as $i => $plugin ) {
1698                  if ( $plugin === $akismet ) {
1699                      $plugins[ $i ] = false;
1700                      $update        = true;
1701                  }
1702              }
1703  
1704              if ( $update ) {
1705                  update_option( 'active_plugins', array_filter( $plugins ) );
1706              }
1707          }
1708          exit;
1709      }
1710  
1711  	public static function view( $name, array $args = array() ) {
1712          $args = apply_filters( 'akismet_view_arguments', $args, $name );
1713  
1714          foreach ( $args as $key => $val ) {
1715              $$key = $val;
1716          }
1717  
1718          load_plugin_textdomain( 'akismet' );
1719  
1720          $file = AKISMET__PLUGIN_DIR . 'views/' . basename( $name ) . '.php';
1721  
1722          if ( file_exists( $file ) ) {
1723              include $file;
1724          }
1725      }
1726  
1727      /**
1728       * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
1729       *
1730       * @static
1731       */
1732  	public static function plugin_activation() {
1733          if ( version_compare( $GLOBALS['wp_version'], AKISMET__MINIMUM_WP_VERSION, '<' ) ) {
1734              load_plugin_textdomain( 'akismet' );
1735  
1736              $message = '<strong>' .
1737                  /* translators: 1: Current Akismet version number, 2: Minimum WordPress version number required. */
1738                  sprintf( esc_html__( 'Akismet %1$s requires WordPress %2$s or higher.', 'akismet' ), AKISMET_VERSION, AKISMET__MINIMUM_WP_VERSION ) . '</strong> ' .
1739                  /* translators: 1: WordPress documentation URL, 2: Akismet download URL. */
1740                  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' );
1741  
1742              self::bail_on_activation( $message );
1743          } elseif ( ! empty( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], '/wp-admin/plugins.php' ) ) {
1744              add_option( 'Activated_Akismet', true );
1745          }
1746      }
1747  
1748      /**
1749       * Removes all connection options
1750       *
1751       * @static
1752       */
1753  	public static function plugin_deactivation() {
1754          self::deactivate_key( self::get_api_key() );
1755  
1756          // Remove any scheduled cron jobs.
1757          $akismet_cron_events = array(
1758              'akismet_schedule_cron_recheck',
1759              'akismet_scheduled_delete',
1760          );
1761  
1762          foreach ( $akismet_cron_events as $akismet_cron_event ) {
1763              $timestamp = wp_next_scheduled( $akismet_cron_event );
1764  
1765              if ( $timestamp ) {
1766                  wp_unschedule_event( $timestamp, $akismet_cron_event );
1767              }
1768          }
1769      }
1770  
1771      /**
1772       * Essentially a copy of WP's build_query but one that doesn't expect pre-urlencoded values.
1773       *
1774       * @param array $args An array of key => value pairs
1775       * @return string A string ready for use as a URL query string.
1776       */
1777  	public static function build_query( $args ) {
1778          return _http_build_query( $args, '', '&' );
1779      }
1780  
1781      /**
1782       * Log debugging info to the error log.
1783       *
1784       * Enabled when WP_DEBUG_LOG is enabled (and WP_DEBUG, since according to
1785       * core, "WP_DEBUG_DISPLAY and WP_DEBUG_LOG perform no function unless
1786       * WP_DEBUG is true), but can be disabled via the akismet_debug_log filter.
1787       *
1788       * @param mixed $akismet_debug The data to log.
1789       */
1790  	public static function log( $akismet_debug ) {
1791          if ( apply_filters( 'akismet_debug_log', defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG && defined( 'AKISMET_DEBUG' ) && AKISMET_DEBUG ) ) {
1792              error_log( print_r( compact( 'akismet_debug' ), true ) );
1793          }
1794      }
1795  
1796      /**
1797       * Check pingbacks for spam before they're saved to the DB.
1798       *
1799       * @param string           $method The XML-RPC method that was called.
1800       * @param array            $args This and the $server arg are marked as optional since plugins might still be
1801       *                               calling do_action( 'xmlrpc_action', [...] ) without the arguments that were added in WP 5.7.
1802       * @param wp_xmlrpc_server $server
1803       */
1804  	public static function pre_check_pingback( $method, $args = array(), $server = null ) {
1805          if ( $method !== 'pingback.ping' ) {
1806              return;
1807          }
1808  
1809          /*
1810           * $args looks like this:
1811           *
1812           * Array
1813           * (
1814           *     [0] => http://www.example.net/?p=1 // Site that created the pingback.
1815           *     [1] => https://www.example.com/?p=2 // Post being pingback'd on this site.
1816           * )
1817           */
1818  
1819          if ( ! is_null( $server ) && ! empty( $args[1] ) ) {
1820              $is_multicall    = false;
1821              $multicall_count = 0;
1822  
1823              if ( 'system.multicall' === $server->message->methodName ) {
1824                  $is_multicall    = true;
1825                  $multicall_count = is_countable( $server->message->params ) ? count( $server->message->params ) : 0;
1826              }
1827  
1828              $post_id = url_to_postid( $args[1] );
1829  
1830              // If pingbacks aren't open on this post, we'll still check whether this request is part of a potential DDOS,
1831              // but indicate to the server that pingbacks are indeed closed so we don't include this request in the user's stats,
1832              // since the user has already done their part by disabling pingbacks.
1833              $pingbacks_closed = false;
1834  
1835              $post = get_post( $post_id );
1836  
1837              if ( ! $post || ! pings_open( $post ) ) {
1838                  $pingbacks_closed = true;
1839              }
1840  
1841              $comment = array(
1842                  'comment_author_url'      => $args[0],
1843                  'comment_post_ID'         => $post_id,
1844                  'comment_author'          => '',
1845                  'comment_author_email'    => '',
1846                  'comment_content'         => '',
1847                  'comment_type'            => 'pingback',
1848                  'akismet_pre_check'       => '1',
1849                  'comment_pingback_target' => $args[1],
1850                  'pingbacks_closed'        => $pingbacks_closed ? '1' : '0',
1851                  'is_multicall'            => $is_multicall,
1852                  'multicall_count'         => $multicall_count,
1853              );
1854  
1855              $comment = self::auto_check_comment( $comment, 'xml-rpc' );
1856  
1857              if ( isset( $comment['akismet_result'] ) && 'true' == $comment['akismet_result'] ) {
1858                  // Sad: tightly coupled with the IXR classes. Unfortunately the action provides no context and no way to return anything.
1859                  $server->error( new IXR_Error( 0, 'Invalid discovery target' ) );
1860  
1861                  // Also note that if this was part of a multicall, a spam result will prevent the subsequent calls from being executed.
1862                  // This is probably fine, but it raises the bar for what should be acceptable as a false positive.
1863              }
1864          }
1865      }
1866  
1867      /**
1868       * Ensure that we are loading expected scalar values from akismet_as_submitted commentmeta.
1869       *
1870       * @param mixed $meta_value
1871       * @return mixed
1872       */
1873  	private static function sanitize_comment_as_submitted( $meta_value ) {
1874          if ( empty( $meta_value ) ) {
1875              return $meta_value;
1876          }
1877  
1878          $meta_value = (array) $meta_value;
1879  
1880          foreach ( $meta_value as $key => $value ) {
1881              if ( ! is_scalar( $value ) ) {
1882                  unset( $meta_value[ $key ] );
1883              } else {
1884                  // These can change, so they're not explicitly listed in comment_as_submitted_allowed_keys.
1885                  if ( strpos( $key, 'POST_ak_' ) === 0 ) {
1886                      continue;
1887                  }
1888  
1889                  if ( ! isset( self::$comment_as_submitted_allowed_keys[ $key ] ) ) {
1890                      unset( $meta_value[ $key ] );
1891                  }
1892              }
1893          }
1894  
1895          return $meta_value;
1896      }
1897  
1898  	public static function predefined_api_key() {
1899          if ( defined( 'WPCOM_API_KEY' ) ) {
1900              return true;
1901          }
1902  
1903          return apply_filters( 'akismet_predefined_api_key', false );
1904      }
1905  
1906      /**
1907       * Controls the display of a privacy related notice underneath the comment
1908       * form using the `akismet_comment_form_privacy_notice` option and filter
1909       * respectively.
1910       *
1911       * Default is to not display the notice, leaving the choice to site admins,
1912       * or integrators.
1913       */
1914  	public static function display_comment_form_privacy_notice() {
1915          if ( 'display' !== apply_filters( 'akismet_comment_form_privacy_notice', get_option( 'akismet_comment_form_privacy_notice', 'hide' ) ) ) {
1916              return;
1917          }
1918  
1919          echo apply_filters(
1920              'akismet_comment_form_privacy_notice_markup',
1921              '<p class="akismet_comment_form_privacy_notice">' .
1922                  wp_kses(
1923                      sprintf(
1924                          /* translators: %s: Akismet privacy URL */
1925                          __( 'This site uses Akismet to reduce spam. <a href="%s" target="_blank" rel="nofollow noopener">Learn how your comment data is processed.</a>', 'akismet' ),
1926                          'https://akismet.com/privacy/'
1927                      ),
1928                      array(
1929                          'a' => array(
1930                              'href' => array(),
1931                              'target' => array(),
1932                              'rel' => array(),
1933                          ),
1934                      )
1935                  ) .
1936              '</p>'
1937          );
1938      }
1939  
1940  	public static function load_form_js() {
1941          if (
1942              ! is_admin()
1943              && ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() )
1944              && self::get_api_key()
1945              ) {
1946              wp_register_script( 'akismet-frontend', plugin_dir_url( __FILE__ ) . '_inc/akismet-frontend.js', array(), filemtime( plugin_dir_path( __FILE__ ) . '_inc/akismet-frontend.js' ), true );
1947              wp_enqueue_script( 'akismet-frontend' );
1948          }
1949      }
1950  
1951      /**
1952       * Add the form JavaScript when we detect that a supported form shortcode is being parsed.
1953       */
1954  	public static function load_form_js_via_filter( $return_value, $tag, $attr, $m ) {
1955          if ( in_array( $tag, array( 'contact-form', 'gravityform', 'contact-form-7', 'formidable', 'fluentform' ) ) ) {
1956              self::load_form_js();
1957          }
1958  
1959          return $return_value;
1960      }
1961  
1962      /**
1963       * Was the last entry in the comment history created by Akismet?
1964       *
1965       * @param int $comment_id The ID of the comment.
1966       * @return bool
1967       */
1968  	public static function last_comment_status_change_came_from_akismet( $comment_id ) {
1969          $history = self::get_comment_history( $comment_id );
1970  
1971          if ( empty( $history ) ) {
1972              return false;
1973          }
1974  
1975          $most_recent_history_event = $history[0];
1976  
1977          if ( ! isset( $most_recent_history_event['event'] ) ) {
1978              return false;
1979          }
1980  
1981          $akismet_history_events = array(
1982              'check-error',
1983              'cron-retry-ham',
1984              'cron-retry-spam',
1985              'check-ham',
1986              'check-spam',
1987              'recheck-error',
1988              'recheck-ham',
1989              'recheck-spam',
1990              'webhook-ham',
1991              'webhook-spam',
1992          );
1993  
1994          if ( in_array( $most_recent_history_event['event'], $akismet_history_events ) ) {
1995              return true;
1996          }
1997  
1998          return false;
1999      }
2000  
2001      /**
2002       * Check the comment history to find out what the most recent comment-check
2003       * response said about this comment.
2004       *
2005       * This value is then included in submit-ham and submit-spam requests to allow
2006       * us to know whether the comment is actually a missed spam/ham or if it's
2007       * just being reclassified after either never being checked or being mistakenly
2008       * marked as ham/spam.
2009       *
2010       * @param int $comment_id The comment ID.
2011       * @return string 'true', 'false', or an empty string if we don't have a record
2012       *                of comment-check being called.
2013       */
2014  	public static function last_comment_check_response( $comment_id ) {
2015          $history = self::get_comment_history( $comment_id );
2016  
2017          if ( $history ) {
2018              $history = array_reverse( $history );
2019  
2020              foreach ( $history as $akismet_history_entry ) {
2021                  // We've always been consistent in how history entries are formatted
2022                  // but comment_meta is writable by everyone, so don't assume that all
2023                  // entries contain the expected parts.
2024  
2025                  if ( ! is_array( $akismet_history_entry ) ) {
2026                      continue;
2027                  }
2028  
2029                  if ( ! isset( $akismet_history_entry['event'] ) ) {
2030                      continue;
2031                  }
2032  
2033                  if ( in_array(
2034                      $akismet_history_entry['event'],
2035                      array(
2036                          'recheck-spam',
2037                          'check-spam',
2038                          'cron-retry-spam',
2039                          'webhook-spam',
2040                          'webhook-spam-noaction',
2041                      ),
2042                      true
2043                  ) ) {
2044                      return 'true';
2045                  } elseif ( in_array(
2046                      $akismet_history_entry['event'],
2047                      array(
2048                          'recheck-ham',
2049                          'check-ham',
2050                          'cron-retry-ham',
2051                          'webhook-ham',
2052                          'webhook-ham-noaction',
2053                      ),
2054                      true
2055                  ) ) {
2056                      return 'false';
2057                  }
2058              }
2059          }
2060  
2061          return '';
2062      }
2063  }


Generated : Fri Feb 21 08:20:01 2025 Cross-referenced by PHPXref