- // wp_insert_comment() might be called in other contexts, so make sure this is the same comment
- // as was checked by akismet_auto_check_comment
- if ( is_object($comment) && !empty($akismet_last_comment) && is_array($akismet_last_comment) ) {
- if ( intval($akismet_last_comment['comment_post_ID']) == intval($comment->comment_post_ID)
- && $akismet_last_comment['comment_author'] == $comment->comment_author
- && $akismet_last_comment['comment_author_email'] == $comment->comment_author_email ) {
- // normal result: true or false
- if ( $akismet_last_comment['akismet_result'] == 'true' ) {
- update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' );
- akismet_update_comment_history( $comment->comment_ID, __('Akismet caught this comment as spam'), 'check-spam' );
- if ( $comment->comment_approved != 'spam' )
- akismet_update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s'), $comment->comment_approved), 'status-changed'.$comment->comment_approved );
- } elseif ( $akismet_last_comment['akismet_result'] == 'false' ) {
- update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' );
- akismet_update_comment_history( $comment->comment_ID, __('Akismet cleared this comment'), 'check-ham' );
- if ( $comment->comment_approved == 'spam' ) {
- if ( wp_blacklist_check($comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent) )
- akismet_update_comment_history( $comment->comment_ID, __('Comment was caught by wp_blacklist_check'), 'wp-blacklisted' );
- else
- akismet_update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s'), $comment->comment_approved), 'status-changed-'.$comment->comment_approved );
- }
- // abnormal result: error
- } else {
- update_comment_meta( $comment->comment_ID, 'akismet_error', time() );
- akismet_update_comment_history( $comment->comment_ID, sprintf( __('Akismet was unable to check this comment (response: %s), will automatically retry again later.'), $akismet_last_comment['akismet_result']), 'check-error' );
- }
-
- // record the complete original data as submitted for checking
- if ( isset($akismet_last_comment['comment_as_submitted']) )
- update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', $akismet_last_comment['comment_as_submitted'] );
- }
- }
-}
-
-add_action( 'wp_insert_comment', 'akismet_auto_check_update_meta', 10, 2 );
-
-
-function akismet_auto_check_comment( $commentdata ) {
- global $akismet_api_host, $akismet_api_port, $akismet_last_comment;
-
- $comment = $commentdata;
- $comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
- $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
- $comment['referrer'] = $_SERVER['HTTP_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']);
-
- $comment['user_role'] = akismet_get_user_roles($comment['user_ID']);
-
- $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 ( akismet_test_mode() )
- $comment['is_test'] = 'true';
-
- foreach ($_POST as $key => $value ) {
- if ( is_string($value) )
- $comment["POST_{$key}"] = $value;
- }
-
- $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
-
- foreach ( $_SERVER as $key => $value ) {
- if ( !in_array( $key, $ignore ) && is_string($value) )
- $comment["$key"] = $value;
- else
- $comment["$key"] = '';
- }
-
- $query_string = '';
- foreach ( $comment as $key => $data )
- $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
-
- $commentdata['comment_as_submitted'] = $comment;
-
- $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
- $commentdata['akismet_result'] = $response[1];
- if ( 'true' == $response[1] ) {
- // akismet_spam_count will be incremented later by akismet_result_spam()
- add_filter('pre_comment_approved', 'akismet_result_spam');
-
- do_action( 'akismet_spam_caught' );
-
- $post = get_post( $comment['comment_post_ID'] );
- $last_updated = strtotime( $post->post_modified_gmt );
- $diff = time() - $last_updated;
- $diff = $diff / 86400;
-
- if ( $post->post_type == 'post' && $diff > 30 && get_option( 'akismet_discard_month' ) == 'true' && empty($comment['user_ID']) ) {
- // akismet_result_spam() won't be called so bump the counter here
- if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
- update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
- wp_redirect( $_SERVER['HTTP_REFERER'] );
- die();
- }
- }
-
- // if the response is neither true nor false, hold the comment for moderation and schedule a recheck
- if ( 'true' != $response[1] && 'false' != $response[1] ) {
- add_filter('pre_comment_approved', 'akismet_result_hold');
- wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
- }
-
- if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_event') ) {
- // WP 2.1+: delete old comments daily
- if ( !wp_next_scheduled('akismet_scheduled_delete') )
- wp_schedule_event(time(), 'daily', 'akismet_scheduled_delete');
- } elseif ( (mt_rand(1, 10) == 3) ) {
- // WP 2.0: run this one time in ten
- akismet_delete_old();
- }
- $akismet_last_comment = $commentdata;
- return $commentdata;
-}
-
-add_action('preprocess_comment', 'akismet_auto_check_comment', 1);
-
-function akismet_delete_old() {
- global $wpdb;
- $now_gmt = current_time('mysql', 1);
- $comment_ids = $wpdb->get_col("SELECT comment_id FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'");
- if ( empty( $comment_ids ) )
- return;
-
- $comma_comment_ids = implode( ', ', array_map('intval', $comment_ids) );
-
- do_action( 'delete_comment', $comment_ids );
- $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_id IN ( $comma_comment_ids )");
- $wpdb->query("DELETE FROM $wpdb->commentmeta WHERE comment_id IN ( $comma_comment_ids )");
- clean_comment_cache( $comment_ids );
- $n = mt_rand(1, 5000);
- if ( apply_filters('akismet_optimize_table', ($n == 11)) ) // lucky number
- $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
-
-}
-
-add_action('akismet_scheduled_delete', 'akismet_delete_old');
-
-function akismet_check_db_comment( $id, $recheck_reason = 'recheck_queue' ) {
- global $wpdb, $akismet_api_host, $akismet_api_port;
-
- $id = (int) $id;
- $c = $wpdb->get_row( "SELECT * FROM $wpdb->comments WHERE comment_ID = '$id'", ARRAY_A );
- if ( !$c )
- return;
-
- $c['user_ip'] = $c['comment_author_IP'];
- $c['user_agent'] = $c['comment_agent'];
- $c['referrer'] = '';
- $c['blog'] = get_option('home');
- $c['blog_lang'] = get_locale();
- $c['blog_charset'] = get_option('blog_charset');
- $c['permalink'] = get_permalink($c['comment_post_ID']);
- $id = $c['comment_ID'];
- if ( akismet_test_mode() )
- $c['is_test'] = 'true';
- $c['recheck_reason'] = $recheck_reason;
-
- $query_string = '';
- foreach ( $c as $key => $data )
- $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
-
- $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
- return $response[1];
-}
-
-function akismet_cron_recheck() {
- global $wpdb;
-
- delete_option('akismet_available_servers');
-
- $comment_errors = $wpdb->get_col( "
- SELECT comment_id
- FROM {$wpdb->prefix}commentmeta
- WHERE meta_key = 'akismet_error'
- LIMIT 100
- " );
-
- foreach ( (array) $comment_errors as $comment_id ) {
- // if the comment no longer exists, remove the meta entry from the queue to avoid getting stuck
- if ( !get_comment( $comment_id ) ) {
- delete_comment_meta( $comment_id, 'akismet_error' );
- continue;
- }
-
- add_comment_meta( $comment_id, 'akismet_rechecking', true );
- $status = akismet_check_db_comment( $comment_id, 'retry' );
-
- $msg = '';
- if ( $status == 'true' ) {
- $msg = __( 'Akismet caught this comment as spam during an automatic retry.' );
- } elseif ( $status == 'false' ) {
- $msg = __( 'Akismet cleared this comment during an automatic retry.' );
- }
-
- // If we got back a legit response then update the comment history
- // other wise just bail now and try again later. No point in
- // re-trying all the comments once we hit one failure.
- if ( !empty( $msg ) ) {
- delete_comment_meta( $comment_id, 'akismet_error' );
- akismet_update_comment_history( $comment_id, $msg, 'cron-retry' );
- update_comment_meta( $comment_id, 'akismet_result', $status );
- // make sure the comment status is still pending. if it isn't, that means the user has already moved it elsewhere.
- $comment = get_comment( $comment_id );
- if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) {
- if ( $status == 'true' ) {
- wp_spam_comment( $comment_id );
- } elseif ( $status == 'false' ) {
- // comment is good, but it's still in the pending queue. depending on the moderation settings
- // we may need to change it to approved.
- 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) )
- wp_set_comment_status( $comment_id, 1 );
- }
- }
- } else {
- delete_comment_meta( $comment_id, 'akismet_rechecking' );
- wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
- return;
- }
- }
-
- $remaining = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'akismet_error'" ) );
- if ( $remaining && !wp_next_scheduled('akismet_schedule_cron_recheck') ) {
- wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
- }
-}
-add_action( 'akismet_schedule_cron_recheck', 'akismet_cron_recheck' );
-
-function akismet_add_comment_nonce( $post_id ) {
- echo '<p style="display: none;">';
- wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', FALSE );
- echo '</p>';
-}
-
-$akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
-
-if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' )
- add_action( 'comment_form', 'akismet_add_comment_nonce' );
-
-if ( '3.0.5' == $wp_version ) {
- remove_filter( 'comment_text', 'wp_kses_data' );
- if ( is_admin() )
- add_filter( 'comment_text', 'wp_kses_post' );
-}