WordPress.org

Plugin Directory

Changeset 1431102


Ignore:
Timestamp:
06/06/16 04:09:10 (4 months ago)
Author:
cfinke
Message:

When check_db_comment fails because of an invalid comment ID, return a WP_Error.

This way, we can tell the difference between a problem communicating with the Akismet API and just bad data being passed in. This lets us be smarter about when to interrupt a batch process if Akismet is unreachable.

Location:
akismet/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • akismet/trunk/class.akismet-admin.php

    r1431089 r1431102  
    399399 
    400400        foreach ( $moderation as $comment_id ) { 
    401             $is_spam = Akismet::recheck_comment( $comment_id, 'recheck_queue' ); 
    402  
    403             if ( 'true' == $is_spam ) { 
     401            $api_response = Akismet::recheck_comment( $comment_id, 'recheck_queue' ); 
     402 
     403            if ( 'true' === $api_response ) { 
    404404                ++$result_counts['spam']; 
    405405            } 
    406             elseif ( 'false' == $is_spam ) { 
     406            elseif ( 'false' === $api_response ) { 
    407407                ++$result_counts['ham']; 
    408408            } 
  • akismet/trunk/class.akismet-cli.php

    r1431095 r1431102  
    2727            if ( isset( $assoc_args['noaction'] ) ) { 
    2828                // Check the comment, but don't reclassify it. 
    29                 $is_spam = Akismet::check_db_comment( $comment_id, 'wp-cli' ); 
     29                $api_response = Akismet::check_db_comment( $comment_id, 'wp-cli' ); 
    3030            } 
    3131            else { 
    32                 $is_spam = Akismet::recheck_comment( $comment_id, 'wp-cli' ); 
     32                $api_response = Akismet::recheck_comment( $comment_id, 'wp-cli' ); 
    3333            } 
    3434             
    35             if ( 'true' === $is_spam ) { 
     35            if ( 'true' === $api_response ) { 
    3636                WP_CLI::line( sprintf( __( "Comment #%d is spam.", 'akismet' ), $comment_id ) ); 
    3737            } 
    38             else if ( 'false' === $is_spam ) { 
     38            else if ( 'false' === $api_response ) { 
    3939                WP_CLI::line( sprintf( __( "Comment #%d is not spam.", 'akismet' ), $comment_id ) ); 
    4040            } 
    4141            else { 
    42                 WP_CLI::warning( sprintf( __( "Comment #%d could not be checked.", 'akismet' ), $comment_id ) ); 
     42                if ( false === $api_response ) { 
     43                    WP_CLI::error( __( "Failed to connect to Akismet.", 'akismet' ) ); 
     44                } 
     45                else if ( is_wp_error( $api_response ) ) { 
     46                    WP_CLI::warning( sprintf( __( "Comment #%d could not be checked.", 'akismet' ), $comment_id ) ); 
     47                } 
    4348            } 
    4449        } 
  • akismet/trunk/class.akismet.php

    r1431089 r1431102  
    450450 
    451451        $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id ), ARRAY_A ); 
    452         if ( !$c ) 
    453             return; 
     452         
     453        if ( ! $c ) { 
     454            return new WP_Error( 'invalid-comment-id', __( 'Comment not found.', 'akismet' ) ); 
     455        } 
    454456 
    455457        $c['user_ip']        = $c['comment_author_IP']; 
     
    462464        $c['recheck_reason'] = $recheck_reason; 
    463465 
    464         if ( self::is_test_mode() ) 
    465             $c['is_test'] = 'true'; 
    466  
    467         $response = self::http_post( Akismet::build_query( $c ), 'comment-check' ); 
    468  
    469         return ( is_array( $response ) && ! empty( $response[1] ) ) ? $response[1] : false; 
    470     } 
    471      
    472     public static function recheck_comment( $id, $recheck_reason = 'recheck_queue' ) { 
    473         $is_spam = self::check_db_comment( $id ); 
    474  
    475         $c = get_comment( $id, ARRAY_A ); 
    476          
    477         if ( ! $c ) { 
    478             return false; 
    479         } 
    480  
    481         $c['user_ip']      = $c['comment_author_IP']; 
    482         $c['user_agent']   = $c['comment_agent']; 
    483         $c['referrer']     = ''; 
    484         $c['blog']         = get_option( 'home' ); 
    485         $c['blog_lang']    = get_locale(); 
    486         $c['blog_charset'] = get_option('blog_charset'); 
    487         $c['permalink']    = get_permalink($c['comment_post_ID']); 
    488  
    489466        $c['user_role'] = ''; 
    490467        if ( isset( $c['user_ID'] ) ) 
    491468            $c['user_role'] = Akismet::get_user_roles($c['user_ID']); 
    492469 
    493         if ( Akismet::is_test_mode() ) 
     470        if ( self::is_test_mode() ) 
    494471            $c['is_test'] = 'true'; 
    495472 
    496         add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true ); 
    497  
    498         $response = Akismet::http_post( Akismet::build_query( $c ), 'comment-check' ); 
    499  
    500         if ( 'true' == $response[1] ) { 
    501             wp_set_comment_status( $c['comment_ID'], 'spam' ); 
    502             update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' ); 
    503             delete_comment_meta( $c['comment_ID'], 'akismet_error' ); 
    504             delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' ); 
    505             Akismet::update_comment_history( $c['comment_ID'], '', 'recheck-spam' ); 
    506         } elseif ( 'false' == $response[1] ) { 
    507             update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' ); 
    508             delete_comment_meta( $c['comment_ID'], 'akismet_error' ); 
    509             delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' ); 
    510             Akismet::update_comment_history( $c['comment_ID'], '', 'recheck-ham' ); 
    511         } else { 
     473        $response = self::http_post( Akismet::build_query( $c ), 'comment-check' ); 
     474 
     475        if ( ! empty( $response[1] ) ) { 
     476            return $response[1]; 
     477        } 
     478 
     479        return false; 
     480    } 
     481     
     482    public static function recheck_comment( $id, $recheck_reason = 'recheck_queue' ) { 
     483        add_comment_meta( $id, 'akismet_rechecking', true ); 
     484         
     485        $api_response = self::check_db_comment( $id, $recheck_reason ); 
     486 
     487        delete_comment_meta( $id, 'akismet_rechecking' ); 
     488 
     489        if ( is_wp_error( $api_response ) ) { 
     490            // Invalid comment ID. 
     491        } 
     492        else if ( 'true' === $api_response ) { 
     493            wp_set_comment_status( $id, 'spam' ); 
     494            update_comment_meta( $id, 'akismet_result', 'true' ); 
     495            delete_comment_meta( $id, 'akismet_error' ); 
     496            delete_comment_meta( $id, 'akismet_delayed_moderation_email' ); 
     497            Akismet::update_comment_history( $id, '', 'recheck-spam' ); 
     498        } 
     499        elseif ( 'false' === $api_response ) { 
     500            update_comment_meta( $id, 'akismet_result', 'false' ); 
     501            delete_comment_meta( $id, 'akismet_error' ); 
     502            delete_comment_meta( $id, 'akismet_delayed_moderation_email' ); 
     503            Akismet::update_comment_history( $id, '', 'recheck-ham' ); 
     504        } 
     505        else { 
    512506            // abnormal result: error 
    513             update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' ); 
     507            update_comment_meta( $id, 'akismet_result', 'error' ); 
    514508            Akismet::update_comment_history( 
    515                 $c['comment_ID'], 
     509                $id, 
    516510                '', 
    517511                'recheck-error', 
    518                 array( 'response' => substr( $response[1], 0, 50 ) ) 
     512                array( 'response' => substr( $api_response, 0, 50 ) ) 
    519513            ); 
    520514        } 
    521515 
    522         delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' ); 
    523  
    524         return $response[1]; 
     516        return $api_response; 
    525517    } 
    526518 
Note: See TracChangeset for help on using the changeset viewer.