• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / Akismet::auto_check_comment() – Check a comment for spam.

You appear to be a bot. Output may be restricted

Description

Check a comment for spam.

Usage

$array|WP_Error = Akismet::auto_check_comment( $commentdata, $context );

Parameters

$commentdata
( array ) required –
$context
( string ) optional default: default – What kind of request triggered this comment check? Possible values are 'default', 'rest_api', and 'xml-rpc'.

Returns

array|WP_Error Either the $commentdata array with additional entries related to its spam status or a WP_Error, if it's a REST API request and the comment should be discarded.

Source

File name: akismet/class.akismet.php
Lines:

1 to 100 of 147
  public static function auto_check_comment( $commentdata, $context = 'default' ) {
    // If no key is configured, then there's no point in doing any of this.
    if ( ! self::get_api_key() ) {
      return $commentdata;
    }

    self::$last_comment_result = null;

    $comment = $commentdata;

    $comment['user_ip']      = self::get_ip_address();
    $comment['user_agent']   = self::get_user_agent();
    $comment['referrer']     = self::get_referer();
    $comment['blog']         = get_option( 'home' );
    $comment['blog_lang']    = get_locale();
    $comment['blog_charset'] = get_option('blog_charset');
    $comment['permalink']    = get_permalink( $comment['comment_post_ID'] );

    if ( ! empty( $comment['user_ID'] ) ) {
      $comment['user_role'] = Akismet::get_user_roles( $comment['user_ID'] );
    }

    
/** See filter documentation in init_hooks(). */
    $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
    $comment['akismet_comment_nonce'] = 'inactive';
    if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) {
      $comment['akismet_comment_nonce'] = 'failed';
      if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) )
        $comment['akismet_comment_nonce'] = 'passed';

      // comment reply in wp-admin
      if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) )
        $comment['akismet_comment_nonce'] = 'passed';

    }

    if ( self::is_test_mode() )
      $comment['is_test'] = 'true';

    foreach( $_POST as $key => $value ) {
      if ( is_string( $value ) )
        $comment["POST_{$key}"] = $value;
    }

    foreach ( $_SERVER as $key => $value ) {
      if ( ! is_string( $value ) ) {
        continue;
      }

      if ( preg_match( "/^HTTP_COOKIE/", $key ) ) {
        continue;
      }

      // Send any potentially useful $_SERVER vars, but avoid sending junk we don't need.
      if ( preg_match( "/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/", $key ) ) {
        $comment[ "$key" ] = $value;
      }
    }

    $post = get_post( $comment['comment_post_ID'] );

    if ( ! is_null( $post ) ) {
      // $post can technically be null, although in the past, it's always been an indicator of another plugin interfering.
      $comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt;
    }

    $response = self::http_post( Akismet::build_query( $comment ), 'comment-check' );

    do_action( 'akismet_comment_check_response', $response );

    $commentdata['comment_as_submitted'] = array_intersect_key( $comment, self::$comment_as_submitted_allowed_keys );

    // Also include any form fields we inject into the comment form, like ak_js
    foreach ( $_POST as $key => $value ) {
      if ( is_string( $value ) && strpos( $key, 'ak_' ) === 0 ) {
        $commentdata['comment_as_submitted'][ 'POST_' . $key ] = $value;
      }
    }

    $commentdata['akismet_result'] = $response[1];

    if ( isset( $response[0]['x-akismet-pro-tip'] ) )
          $commentdata['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip'];

    if ( isset( $response[0]['x-akismet-error'] ) ) {
      // An error occurred that we anticipated (like a suspended key) and want the user to act on.
      // Send to moderation.
      self::$last_comment_result = '0';
    }
    else if ( 'true' == $response[1] ) {
      // akismet_spam_count will be incremented later by comment_is_spam()
      self::$last_comment_result = 'spam';

      $discard = ( isset( $commentdata['akismet_pro_tip'] ) && $commentdata['akismet_pro_tip'] === 'discard' && self::allow_discard() );

      do_action( 'akismet_spam_caught', $discard );

      if ( $discard ) {
        // The spam is obvious, so we're bailing out early. 
        // akismet_result_spam() won't be called so bump the counter here
 
[1] [2] Next »

 View on GitHub View on Trac

Published: 19th December 2019 | Last updated: 12th September 2021

Primary Sidebar

Information

Function name: Akismet::auto_check_comment
Class ref: Akismet
Plugin ref: Akismet
Version: 5.0.1
Sourcefile: class.akismet.php
File ref: class.akismet.php
Deprecated?: No
API Letters: A,C

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 6.1.1. PHP: 8.0.28
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2023. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites