33
33
add_action( 'activity_box_end', array( 'Akismet_Admin', 'dashboard_stats' ) );
34
34
add_action( 'rightnow_end', array( 'Akismet_Admin', 'rightnow_stats' ) );
35
35
add_action( 'manage_comments_nav', array( 'Akismet_Admin', 'check_for_spam_button' ) );
36
add_action( 'transition_comment_status', array( 'Akismet_Admin', 'transition_comment_status' ), 10, 3 );
37
36
add_action( 'admin_action_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
38
37
add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
39
38
add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
332
333
$link = add_query_arg( array( 'page' => 'akismet-admin', 'recheckqueue' => 'true', 'noheader' => 'true' ), admin_url( 'edit-comments.php' ) );
334
echo '</div><div class="alignleft"><a class="button-secondary checkforspam" href="' . esc_url( $link ) . '">' . esc_html__('Check for Spam', 'akismet') . '</a>';
335
echo '<img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" class="checkforspam-spinner" />';
338
public static function transition_comment_status( $new_status, $old_status, $comment ) {
339
if ( $new_status == $old_status )
342
# we don't need to record a history item for deleted comments
343
if ( $new_status == 'delete' )
349
if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
352
if ( defined('WP_IMPORTING') && WP_IMPORTING == true )
355
// if this is present, it means the status has been changed by a re-check, not an explicit user action
356
if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) )
359
global $current_user;
361
if ( is_object( $current_user ) )
362
$reporter = $current_user->user_login;
365
// We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
366
// is changed automatically by another plugin. Unfortunately WordPress doesn't provide an unambiguous way to
367
// determine why the transition_comment_status action was triggered. And there are several different ways by which
368
// to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
369
// We'll assume that this is an explicit user action if POST or GET has an 'action' key.
370
if ( isset($_POST['action']) || isset($_GET['action']) ) {
371
if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) {
372
return self::submit_spam_comment( $comment->comment_ID );
373
} elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
374
return self::submit_nonspam_comment( $comment->comment_ID );
378
Akismet::update_comment_history( $comment->comment_ID, sprintf( __('%1$s changed the comment status to %2$s', 'akismet'), $reporter, $new_status ), 'status-' . $new_status );
335
echo '</div><div class="alignleft"><a class="button-secondary checkforspam" href="' . esc_url( $link ) . '">' . esc_html__('Check for Spam', 'akismet') . '</a><span class="checkforspam-spinner"></span>';
381
338
public static function recheck_queue() {
515
472
echo '<span class="akismet-status" commentid="'.$comment->comment_ID.'"><a href="comment.php?action=editcomment&c='.$comment->comment_ID.'#akismet-status" title="' . esc_attr__( 'View comment history' , 'akismet') . '">'.esc_html( $desc ).'</a></span>';
517
if ( apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') ) ) {
474
$show_user_comments = apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') );
475
$show_user_comments = $show_user_comments === 'false' ? false : $show_user_comments; //option used to be saved as 'false' / 'true'
477
if ( $show_user_comments ) {
518
478
$comment_count = Akismet::get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
519
479
$comment_count = intval( $comment_count );
520
480
echo '<span class="akismet-user-comment-count" commentid="'.$comment->comment_ID.'" style="display:none;"><br><span class="akismet-user-comment-counts">'. sprintf( esc_html( _n( '%s approved', '%s approved', $comment_count , 'akismet') ), number_format_i18n( $comment_count ) ) . '</span></span>';
557
517
return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', array( 'Akismet_Admin', 'text_add_link_callback' ), $comment_text );
560
public static function submit_spam_comment( $comment_id ) {
561
global $wpdb, $current_user, $current_site;
563
$comment_id = (int) $comment_id;
565
$comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) );
567
if ( !$comment ) // it was deleted
570
if ( 'spam' != $comment->comment_approved )
573
// use the original version stored in comment_meta if available
574
$as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
576
if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) )
577
$comment = (object) array_merge( (array)$comment, $as_submitted );
579
$comment->blog = get_bloginfo('url');
580
$comment->blog_lang = get_locale();
581
$comment->blog_charset = get_option('blog_charset');
582
$comment->permalink = get_permalink($comment->comment_post_ID);
584
if ( is_object($current_user) )
585
$comment->reporter = $current_user->user_login;
587
if ( is_object($current_site) )
588
$comment->site_domain = $current_site->domain;
590
$comment->user_role = '';
591
if ( isset( $comment->user_ID ) )
592
$comment->user_role = Akismet::get_user_roles( $comment->user_ID );
594
if ( Akismet::is_test_mode() )
595
$comment->is_test = 'true';
597
$post = get_post( $comment->comment_post_ID );
598
$comment->comment_post_modified_gmt = $post->post_modified_gmt;
600
$response = Akismet::http_post( http_build_query( $comment ), 'submit-spam' );
601
if ( $comment->reporter ) {
602
Akismet::update_comment_history( $comment_id, sprintf( __('%s reported this comment as spam', 'akismet'), $comment->reporter ), 'report-spam' );
603
update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
604
update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
607
do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
610
public static function submit_nonspam_comment( $comment_id ) {
611
global $wpdb, $current_user, $current_site;
613
$comment_id = (int) $comment_id;
615
$comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) );
616
if ( !$comment ) // it was deleted
619
// use the original version stored in comment_meta if available
620
$as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
622
if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) )
623
$comment = (object) array_merge( (array)$comment, $as_submitted );
625
$comment->blog = get_bloginfo('url');
626
$comment->blog_lang = get_locale();
627
$comment->blog_charset = get_option('blog_charset');
628
$comment->permalink = get_permalink( $comment->comment_post_ID );
629
$comment->user_role = '';
631
if ( is_object($current_user) )
632
$comment->reporter = $current_user->user_login;
634
if ( is_object($current_site) )
635
$comment->site_domain = $current_site->domain;
637
if ( isset( $comment->user_ID ) )
638
$comment->user_role = Akismet::get_user_roles($comment->user_ID);
640
if ( Akismet::is_test_mode() )
641
$comment->is_test = 'true';
643
$post = get_post( $comment->comment_post_ID );
644
$comment->comment_post_modified_gmt = $post->post_modified_gmt;
646
$response = Akismet::http_post( http_build_query( $comment ), 'submit-ham' );
647
if ( $comment->reporter ) {
648
Akismet::update_comment_history( $comment_id, sprintf( __('%s reported this comment as not spam', 'akismet'), $comment->reporter ), 'report-ham' );
649
update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
650
update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
653
do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
656
520
// Total spam in queue
657
521
// get_option( 'akismet_spam_count' ) is the total caught ever
658
522
public static function get_spam_count( $type = false ) {
673
537
} elseif ( 'comments' == $type || 'comment' == $type ) { // comments
675
} else { // pingback, trackback, ...
676
$type = $wpdb->escape( $type );
679
return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type='$type'");
541
return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type = %s", $type ) );
682
544
// Check connectivity between the WordPress blog and Akismet's servers.
683
545
// Returns an associative array of server IP addresses, where the key is the IP address, and value is true (available) or false (unable to connect).
684
public static function check_server_connectivity() {
685
$test_host = 'rest.akismet.com';
687
// Some web hosts may disable one or both functions
688
if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
691
$ips = gethostbynamel( $test_host );
692
if ( !$ips || !is_array($ips) || !count($ips) )
695
$api_key = Akismet::get_api_key();
698
foreach ( $ips as $ip ) {
699
$response = Akismet::verify_key( $api_key, $ip );
700
// even if the key is invalid, at least we know we have connectivity
701
if ( $response == 'valid' || $response == 'invalid' )
702
$servers[$ip] = true;
704
$servers[$ip] = false;
546
public static function check_server_ip_connectivity() {
548
$servers = $ips = array();
550
// Some web hosts may disable this function
551
if ( function_exists('gethostbynamel') ) {
553
$ips = gethostbynamel( 'rest.akismet.com' );
554
if ( $ips && is_array($ips) && count($ips) ) {
555
$api_key = Akismet::get_api_key();
557
foreach ( $ips as $ip ) {
558
$response = Akismet::verify_key( $api_key, $ip );
559
// even if the key is invalid, at least we know we have connectivity
560
if ( $response == 'valid' || $response == 'invalid' )
561
$servers[$ip] = 'connected';
563
$servers[$ip] = $response ? $response : 'unable to connect';
709
// Check the server connectivity and store the results in an option.
710
// Cached results will be used if not older than the specified timeout in seconds; use $cache_timeout = 0 to force an update.
711
// Returns the same associative array as check_server_connectivity()
712
public static function get_server_connectivity( $cache_timeout = 86400 ) {
571
// Simpler connectivity check
572
public static function check_server_connectivity($cache_timeout = 86400) {
575
$debug[ 'PHP_VERSION' ] = PHP_VERSION;
576
$debug[ 'WORDPRESS_VERSION' ] = $GLOBALS['wp_version'];
577
$debug[ 'AKISMET_VERSION' ] = AKISMET_VERSION;
578
$debug[ 'AKISMET__PLUGIN_DIR' ] = AKISMET__PLUGIN_DIR;
579
$debug[ 'SITE_URL' ] = site_url();
580
$debug[ 'HOME_URL' ] = home_url();
713
582
$servers = get_option('akismet_available_servers');
714
if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false )
583
if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false ) {
584
$servers = self::check_server_ip_connectivity();
585
update_option('akismet_available_servers', $servers);
586
update_option('akismet_connectivity_time', time());
589
$response = wp_remote_get( 'http://rest.akismet.com/1.1/test' );
591
$debug[ 'gethostbynamel' ] = function_exists('gethostbynamel') ? 'exists' : 'not here';
592
$debug[ 'Servers' ] = $servers;
593
$debug[ 'Test Connection' ] = $response;
595
Akismet::log( $debug );
597
if ( $response && 'connected' == wp_remote_retrieve_body( $response ) )
717
// There's a race condition here but the effect is harmless.
718
$servers = self::check_server_connectivity();
719
update_option('akismet_available_servers', $servers);
720
update_option('akismet_connectivity_time', time());
603
// Check the server connectivity and store the available servers in an option.
604
public static function get_server_connectivity($cache_timeout = 86400) {
605
return self::check_server_connectivity( $cache_timeout );
724
608
public static function get_number_spam_waiting() {