2 add_action( 'admin_menu', 'akismet_admin_menu' );
4 akismet_admin_warnings();
6 function akismet_admin_init() {
9 // all admin functions are disabled in old versions
10 if ( !function_exists('is_multisite') && version_compare( $wp_version, '3.0', '<' ) ) {
12 function akismet_version_warning() {
14 <div id="akismet-warning" class="updated fade"><p><strong>'.sprintf(__('Akismet %s requires WordPress 3.0 or higher.'), AKISMET_VERSION) .'</strong> '.sprintf(__('Please <a href="%s">upgrade WordPress</a> to a current version, or <a href="%s">downgrade to version 2.4 of the Akismet plugin</a>.'), 'http://codex.wordpress.org/Upgrading_WordPress', 'http://wordpress.org/extend/plugins/akismet/download/'). '</p></div>
17 add_action('admin_notices', 'akismet_version_warning');
22 if ( function_exists( 'get_plugin_page_hook' ) )
23 $hook = get_plugin_page_hook( 'akismet-stats-display', 'index.php' );
25 $hook = 'dashboard_page_akismet-stats-display';
26 add_meta_box('akismet-status', __('Comment History'), 'akismet_comment_status_meta_box', 'comment', 'normal');
28 add_action('admin_init', 'akismet_admin_init');
30 add_action( 'admin_enqueue_scripts', 'akismet_load_js_and_css' );
31 function akismet_load_js_and_css() {
34 if ( in_array( $hook_suffix, array(
35 'index.php', # dashboard
39 'plugins_page_akismet-key-config',
40 'jetpack_page_akismet-key-config',
42 wp_register_style( 'akismet.css', AKISMET_PLUGIN_URL . 'akismet.css', array(), '2.5.4.4' );
43 wp_enqueue_style( 'akismet.css');
45 wp_register_script( 'akismet.js', AKISMET_PLUGIN_URL . 'akismet.js', array('jquery'), '2.5.4.6' );
46 wp_enqueue_script( 'akismet.js' );
47 wp_localize_script( 'akismet.js', 'WPAkismet', array(
48 'comment_author_url_nonce' => wp_create_nonce( 'comment_author_url_nonce' )
54 function akismet_nonce_field($action = -1) { return wp_nonce_field($action); }
55 $akismet_nonce = 'akismet-update-key';
57 function akismet_plugin_action_links( $links, $file ) {
58 if ( $file == plugin_basename( dirname(__FILE__).'/akismet.php' ) ) {
59 $links[] = '<a href="' . admin_url( 'admin.php?page=akismet-key-config' ) . '">'.__( 'Settings' ).'</a>';
65 add_filter( 'plugin_action_links', 'akismet_plugin_action_links', 10, 2 );
67 function akismet_conf() {
68 global $akismet_nonce, $current_user;
70 $new_key_link = 'https://akismet.com/get/';
71 $api_key = akismet_get_key();
72 $show_key_form = $api_key;
73 $key_status = 'empty';
78 if ( isset( $_POST['submit'] ) ) {
79 if ( function_exists('current_user_can') && !current_user_can('manage_options') )
80 die(__('Cheatin’ uh?'));
82 $show_key_form = true;
84 check_admin_referer( $akismet_nonce );
85 $key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
86 $home_url = parse_url( get_bloginfo('url') );
88 if ( empty( $home_url['host'] ) )
89 $ms[] = 'bad_home_url';
91 if ( empty( $key ) ) {
93 delete_option('wordpress_api_key');
95 $ms[] = 'new_key_empty';
101 $key_status = akismet_verify_key( $key );
103 if ( $key != $api_key && $key_status == 'valid' ) {
104 update_option('wordpress_api_key', $key);
105 $ms[] = 'new_key_valid';
107 elseif ( $key_status == 'invalid' )
108 $ms[] = 'new_key_invalid';
109 elseif ( $key_status == 'failed' )
110 $ms[] = 'new_key_failed';
112 $api_key = $key_status == 'valid' ? $key : false;
114 if ( isset( $_POST['akismet_discard_month'] ) )
115 update_option( 'akismet_discard_month', 'true' );
117 update_option( 'akismet_discard_month', 'false' );
119 if ( isset( $_POST['akismet_show_user_comments_approved'] ) )
120 update_option( 'akismet_show_user_comments_approved', 'true' );
122 update_option( 'akismet_show_user_comments_approved', 'false' );
128 elseif ( isset( $_POST['check'] ) ) {
129 $show_key_form = true;
130 check_admin_referer( $akismet_nonce );
131 akismet_get_server_connectivity(0);
134 if ( $show_key_form ) {
135 //check current key status
136 //only get this if showing the key form otherwise takes longer for page to load for new user
137 //no need to get it if we already know it and its valid
138 if ( in_array( $key_status, array( 'invalid', 'failed', 'empty' ) ) ) {
139 $key = get_option('wordpress_api_key');
140 if ( empty( $key ) ) {
141 //no key saved yet - maybe connection to Akismet down?
142 if ( in_array( $key_status, array( 'invalid', 'empty' ) ) ) {
143 if ( akismet_verify_key( '1234567890ab' ) == 'failed' )
144 $ms[] = 'no_connection';
148 $key_status = akismet_verify_key( $key );
151 if ( !isset( $_POST['submit'] ) ) {
152 if ( $key_status == 'invalid' )
153 $ms[] = 'key_invalid';
154 elseif ( !empty( $key ) && $key_status == 'failed' )
155 $ms[] = 'key_failed';
160 'new_key_empty' => array( 'class' => 'updated fade', 'text' => __('Your key has been cleared.' ) ),
161 'new_key_valid' => array( 'class' => 'updated fade', 'text' => __('Your Akismet account has been successfully set up and activated. Happy blogging!' ) ),
162 'new_key_invalid' => array( 'class' => 'error', 'text' => __('The key you entered is invalid. Please double-check it.' ) ),
163 'new_key_failed' => array( 'class' => 'error', 'text' => __('The key you entered could not be verified because a connection to akismet.com could not be established. Please check your server configuration.' ) ),
164 'no_connection' => array( 'class' => 'error', 'text' => __('There was a problem connecting to the Akismet server. Please check your server configuration.' ) ),
165 'key_empty' => array( 'class' => 'updated fade', 'text' => __('Please enter an API key' ) ),
166 'key_invalid' => array( 'class' => 'error', 'text' => __('This key is invalid.' ) ),
167 'key_failed' => array( 'class' => 'error', 'text' => __('The key below was previously validated but a connection to akismet.com can not be established at this time. Please check your server configuration.' ) ),
168 'bad_home_url' => array( 'class' => 'error', 'text' => sprintf( __('Your WordPress home URL %s is invalid. Please fix the <a href="%s">home option</a>.'), esc_html( get_bloginfo('url') ), admin_url('options.php#home') ) )
174 <?php if ( !$api_key ) : ?>
175 <h2 class="ak-header"><?php _e('Akismet'); ?></h2>
177 <h2 class="ak-header"><?php printf( __( 'Akismet <a href="%s" class="add-new-h2">Stats</a>' ), esc_url( add_query_arg( array( 'page' => 'akismet-stats-display' ), class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'index.php' ) ) ) ); ?></h2>
179 <div class="no-key <?php echo $show_key_form ? 'hidden' : '';?>">
180 <p><?php _e('Akismet eliminates the comment and trackback spam you get on your site. To use Akismet you may need to sign up for an API key. Click the button below to get started.'); ?></p>
181 <form name="akismet_activate" action="https://akismet.com/get/" method="POST">
182 <input type="hidden" name="return" value="1"/>
183 <input type="hidden" name="jetpack" value="<?php echo (string) class_exists( 'Jetpack' );?>"/>
184 <input type="hidden" name="user" value="<?php echo esc_attr( $current_user->user_login );?>"/>
185 <input type="submit" class="button button-primary" value="<?php echo esc_attr( __('Create a new Akismet Key') ); ?>"/>
188 <a href="#" class="switch-have-key"><?php _e('I already have a key'); ?></a>
190 <div class="have-key <?php echo $show_key_form ? '' : 'hidden';?>">
191 <?php if ( !empty($_POST['submit'] ) && $saved_ok ) : ?>
192 <div id="message" class="updated fade"><p><strong><?php _e('Settings saved.') ?></strong></p></div>
194 <?php if ( isset($_GET['message']) && $_GET['message'] == 'success' ) : ?>
195 <div id="message" class="updated fade"><p><?php _e('<strong>Sign up success!</strong> Please check your email for your Akismet API Key and enter it below.') ?></p></div>
197 <?php foreach( $ms as $m ) : ?>
198 <div class="<?php echo $messages[$m]['class']; ?>"><p><strong><?php echo $messages[$m]['text']; ?></strong></p></div>
200 <form action="" method="post" id="akismet-conf">
201 <table class="form-table">
204 <th><label for="key"><?php _e('Akismet API Key');?></label></th>
206 <input id="key" name="key" type="text" size="15" maxlength="12" value="<?php echo esc_html( get_option('wordpress_api_key') ); ?>" class="regular-text code <?php echo $key_status;?>"><div class="under-input key-status <?php echo $key_status;?>"><?php echo ucfirst( $key_status );?></div>
207 <p class="need-key description"><?php printf( __('You must enter a valid Akismet API key here. If you need an API key, you can <a href="%s">create one here</a>'), '#' );?></p>
210 <?php if ( $api_key ):?>
212 <th scope="row"><?php _e('Settings');?></th>
214 <fieldset><legend class="screen-reader-text"><span><?php _e('Settings');?></span></legend>
215 <label for="akismet_discard_month" title="<?php echo esc_attr( __( 'Auto-detete old spam' ) ); ?>"><input name="akismet_discard_month" id="akismet_discard_month" value="true" type="checkbox" <?php echo get_option('akismet_discard_month') == 'true' ? 'checked="checked"':''; ?>> <span><?php _e('Auto-delete spam submitted on posts more than a month old.'); ?></span></label><br>
216 <label for="akismet_show_user_comments_approved" title="<?php echo esc_attr( __( 'Show approved comments' ) ); ?>"><input name="akismet_show_user_comments_approved" id="akismet_show_user_comments_approved" value="true" type="checkbox" <?php echo get_option('akismet_show_user_comments_approved') == 'true' ? 'checked="checked"':''; ?>> <span><?php _e('Show the number of comments you\'ve approved beside each comment author.'); ?></span></label>
223 <?php akismet_nonce_field($akismet_nonce) ?>
225 <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes');?>">
229 <?php if ( $api_key ) : ?>
230 <h3><?php _e('Server Connectivity'); ?></h3>
231 <form action="" method="post" id="akismet-connectivity">
232 <table class="form-table">
235 <th><label for="key"><?php _e('Server Status');?></label></th>
237 <?php if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') ) : ?>
238 <p class="key-status failed"><?php _e('Network functions are disabled.'); ?></p>
239 <p class="description"><?php echo sprintf( __('Your web host or server administrator has disabled PHP\'s <code>fsockopen</code> or <code>gethostbynamel</code> functions. <strong>Akismet cannot work correctly until this is fixed.</strong> Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet\'s system requirements</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
241 $servers = akismet_get_server_connectivity();
242 $fail_count = count( $servers ) - count( array_filter( $servers ) );
243 if ( is_array( $servers ) && count( $servers ) > 0 ) {
244 if ( $fail_count > 0 && $fail_count < count( $servers ) ) { // some connections work, some fail ?>
245 <p class="key-status some"><?php _e('Unable to reach some Akismet servers.'); ?></p>
246 <p class="description"><?php echo sprintf( __('A network problem or firewall is blocking some connections from your web server to Akismet.com. Akismet is working but this may cause problems during times of network congestion. Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
247 <?php } elseif ( $fail_count > 0 ) { // all connections fail ?>
248 <p class="key-status failed"><?php _e('Unable to reach any Akismet servers.'); ?></p>
249 <p class="description"><?php echo sprintf( __('A network problem or firewall is blocking all connections from your web server to Akismet.com. <strong>Akismet cannot work correctly until this is fixed.</strong> Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
250 <?php } else { // all connections work ?>
251 <p class="key-status valid"><?php _e('All Akismet servers are available.'); ?></p>
252 <p class="description"><?php _e('Akismet is working correctly. All servers are accessible.'); ?></p>
254 } else { //can't connect to any server ?>
255 <p class="key-status failed"><?php _e('Unable to find Akismet servers.'); ?></p>
256 <p class="description"><?php echo sprintf( __('A DNS problem or firewall is preventing all access from your web server to Akismet.com. <strong>Akismet cannot work correctly until this is fixed.</strong> Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
261 <?php if ( !empty( $servers ) ) : ?>
263 <th scope="row"><?php _e('Network Status');?></th>
265 <table class="network-status">
267 <th><?php _e('Akismet server'); ?></th><th><?php _e('Network Status'); ?></th>
272 foreach ( $servers as $ip => $status ) : ?>
274 <td align="center"><?php echo esc_html( $ip ); ?></td>
275 <td class="key-status <?php echo $status ? 'valid' : 'failed'; ?>"><?php echo $status ? __('Accessible') : __('Re-trying'); ?></td>
281 <input type="submit" name="check" id="submit" class="button" style="margin-left: 13.3em;" value="<?php _e('Check Network Status');?>">
286 <th scope="row"><?php _e('Last Checked');?></th>
288 <p><strong><?php echo get_option('akismet_connectivity_time') ? sprintf( __('%s Ago'), ucwords( human_time_diff( get_option('akismet_connectivity_time') ) ) ) : __( 'Not yet' ); ?></strong></p>
289 <p class="description"><?php printf( __('You can confirm that Akismet.com is up by <a href="%s" target="_blank">clicking here</a>.'), 'http://status.automattic.com/9931/136079/Akismet-API' ); ?></p>
293 <?php akismet_nonce_field($akismet_nonce) ?>
301 function akismet_stats_display() {
302 global $akismet_api_host, $akismet_api_port;
304 $blog = urlencode( get_bloginfo('url') );
305 $api_key = akismet_get_key();?>
307 <div class="wrap"><?php
309 <div id="akismet-warning" class="updated fade"><p><strong><?php _e('Akismet is almost ready.');?></strong> <?php printf( __( 'You must <a href="%1$s">enter your Akismet API key</a> for it to work.' ), esc_url( add_query_arg( array( 'page' => 'akismet-key-config' ), admin_url( 'admin.php' ) ) ) );?></p></div><?php
311 <iframe src="<?php echo esc_url( sprintf( '%s://akismet.com/web/1.0/user-stats.php?blog=%s&api_key=%s', is_ssl()?'https':'http', $blog, $api_key ) ); ?>" width="100%" height="2500px" frameborder="0" id="akismet-stats-frame"></iframe><?php
316 function akismet_stats() {
317 if ( !function_exists('did_action') || did_action( 'rightnow_end' ) ) // We already displayed this info in the "Right Now" section
319 if ( !$count = get_option('akismet_spam_count') )
321 $path = plugin_basename(__FILE__);
322 echo '<h3>' . _x( 'Spam', 'comments' ) . '</h3>';
324 if ( isset( $submenu['edit-comments.php'] ) )
325 $link = 'edit-comments.php';
328 echo '<p>'.sprintf( _n( '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.', '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.', $count ), 'http://akismet.com/?return=true', clean_url("$link?page=akismet-admin"), number_format_i18n($count) ).'</p>';
330 add_action('activity_box_end', 'akismet_stats');
332 function akismet_admin_warnings() {
333 global $wpcom_api_key, $pagenow;
336 $pagenow == 'edit-comments.php'
337 || ( !empty( $_GET['page'] ) && $_GET['page'] == 'akismet-key-config' )
338 || ( !empty( $_GET['page'] ) && $_GET['page'] == 'akismet-stats-display' )
340 if ( get_option( 'akismet_alert_code' ) ) {
341 function akismet_alert() {
343 'code' => (int) get_option( 'akismet_alert_code' ),
344 'msg' => get_option( 'akismet_alert_msg' )
348 <p><strong><?php _e( 'Akismet Error Code');?>: <?php echo $alert['code']; ?></strong></p>
349 <p><?php esc_html_e( $alert['msg'] ); ?></p>
350 <p><?php //FIXME: need to revert this to using __() in next version
351 printf( translate( 'For more information:' ) . ' <a href="%s">%s</a>' , 'https://akismet.com/errors/'.$alert['code'], 'https://akismet.com/errors/'.$alert['code'] );?>
357 add_action( 'admin_notices', 'akismet_alert' );
361 if ( !get_option('wordpress_api_key') && !$wpcom_api_key && !isset($_POST['submit']) ) {
362 function akismet_warning() {
363 global $hook_suffix, $current_user;
365 if ( $hook_suffix == 'plugins.php' ) {
367 <div class="updated" style="padding: 0; margin: 0; border: none; background: none;">
368 <style type="text/css">
369 .akismet_activate{min-width:825px;border:1px solid #4F800D;padding:5px;margin:15px 0;background:#83AF24;background-image:-webkit-gradient(linear,0% 0,80% 100%,from(#83AF24),to(#4F800D));background-image:-moz-linear-gradient(80% 100% 120deg,#4F800D,#83AF24);-moz-border-radius:3px;border-radius:3px;-webkit-border-radius:3px;position:relative;overflow:hidden}.akismet_activate .aa_a{position:absolute;top:-5px;right:10px;font-size:140px;color:#769F33;font-family:Georgia, "Times New Roman", Times, serif;z-index:1}.akismet_activate .aa_button{font-weight:bold;border:1px solid #029DD6;border-top:1px solid #06B9FD;font-size:15px;text-align:center;padding:9px 0 8px 0;color:#FFF;background:#029DD6;background-image:-webkit-gradient(linear,0% 0,0% 100%,from(#029DD6),to(#0079B1));background-image:-moz-linear-gradient(0% 100% 90deg,#0079B1,#029DD6);-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px}.akismet_activate .aa_button:hover{text-decoration:none !important;border:1px solid #029DD6;border-bottom:1px solid #00A8EF;font-size:15px;text-align:center;padding:9px 0 8px 0;color:#F0F8FB;background:#0079B1;background-image:-webkit-gradient(linear,0% 0,0% 100%,from(#0079B1),to(#0092BF));background-image:-moz-linear-gradient(0% 100% 90deg,#0092BF,#0079B1);-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px}.akismet_activate .aa_button_border{border:1px solid #006699;-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px;background:#029DD6;background-image:-webkit-gradient(linear,0% 0,0% 100%,from(#029DD6),to(#0079B1));background-image:-moz-linear-gradient(0% 100% 90deg,#0079B1,#029DD6)}.akismet_activate .aa_button_container{cursor:pointer;display:inline-block;background:#DEF1B8;padding:5px;-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px;width:266px}.akismet_activate .aa_description{position:absolute;top:22px;left:285px;margin-left:25px;color:#E5F2B1;font-size:15px;z-index:1000}.akismet_activate .aa_description strong{color:#FFF;font-weight:normal}
371 <form name="akismet_activate" action="https://akismet.com/get/" method="POST">
372 <input type="hidden" name="return" value="1"/>
373 <input type="hidden" name="jetpack" value="'.(string) class_exists( 'Jetpack' ).'"/>
374 <input type="hidden" name="user" value="'.esc_attr( $current_user->user_login ).'"/>
375 <div class="akismet_activate">
376 <div class="aa_a">A</div>
377 <div class="aa_button_container" onclick="document.akismet_activate.submit();">
378 <div class="aa_button_border">
379 <div class="aa_button">Activate your Akismet account</div>
382 <div class="aa_description"><strong>Almost done</strong> - activate your account and say goodbye to comment spam.</div>
390 add_action('admin_notices', 'akismet_warning');
392 } elseif ( ( empty($_SERVER['SCRIPT_FILENAME']) || basename($_SERVER['SCRIPT_FILENAME']) == 'edit-comments.php' ) && wp_next_scheduled('akismet_schedule_cron_recheck') ) {
393 function akismet_warning() {
395 akismet_fix_scheduled_recheck();
396 $waiting = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'akismet_error'" );
397 $next_check = wp_next_scheduled('akismet_schedule_cron_recheck');
398 if ( $waiting > 0 && $next_check > time() )
400 <div id="akismet-warning" class="updated fade"><p><strong>'.__('Akismet has detected a problem.').'</strong> '.sprintf(__('Some comments have not yet been checked for spam by Akismet. They have been temporarily held for moderation. Please check your <a href="%s">Akismet configuration</a> and contact your web host if problems persist.'), 'admin.php?page=akismet-key-config').'</p></div>
403 add_action('admin_notices', 'akismet_warning');
410 function akismet_comment_row_action( $a, $comment ) {
412 // failsafe for old WP versions
413 if ( !function_exists('add_comment_meta') )
416 $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
417 $akismet_error = get_comment_meta( $comment->comment_ID, 'akismet_error', true );
418 $user_result = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
419 $comment_status = wp_get_comment_status( $comment->comment_ID );
421 if ( $akismet_error ) {
422 $desc = __( 'Awaiting spam check' );
423 } elseif ( !$user_result || $user_result == $akismet_result ) {
424 // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
425 if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
426 $desc = __( 'Flagged as spam by Akismet' );
427 elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
428 $desc = __( 'Cleared by Akismet' );
430 $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
431 if ( $user_result == 'true' )
432 $desc = sprintf( __('Flagged as spam by %s'), $who );
434 $desc = sprintf( __('Un-spammed by %s'), $who );
437 // add a History item to the hover links, just after Edit
438 if ( $akismet_result ) {
440 foreach ( $a as $k => $item ) {
444 || ( $k == 'unspam' && $GLOBALS['wp_version'] >= 3.4 )
446 $b['history'] = '<a href="comment.php?action=editcomment&c='.$comment->comment_ID.'#akismet-status" title="'. esc_attr__( 'View comment history' ) . '"> '. __('History') . '</a>';
454 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' ) . '">'.esc_html( $desc ).'</a></span>';
456 if ( apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') ) == 'true' ) {
457 $comment_count = akismet_get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
458 $comment_count = intval( $comment_count );
459 echo '<span class="akismet-user-comment-count" commentid="'.$comment->comment_ID.'" style="display:none;"><br><span class="akismet-user-comment-counts">'.sprintf( _n( '%s approved', '%s approved', $comment_count ), number_format_i18n( $comment_count ) ) . '</span></span>';
465 add_filter( 'comment_row_actions', 'akismet_comment_row_action', 10, 2 );
467 function akismet_comment_status_meta_box($comment) {
468 $history = akismet_get_comment_history( $comment->comment_ID );
471 echo '<div class="akismet-history" style="margin: 13px;">';
472 foreach ( $history as $row ) {
473 $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
474 echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</span> - ';
475 echo esc_html( $row['message'] ) . '</div>';
484 // add an extra column header to the comments screen
485 function akismet_comments_columns( $columns ) {
486 $columns[ 'akismet' ] = __( 'Akismet' );
490 #add_filter( 'manage_edit-comments_columns', 'akismet_comments_columns' );
492 // Show stuff in the extra column
493 function akismet_comment_column_row( $column, $comment_id ) {
494 if ( $column != 'akismet' )
497 $history = akismet_get_comment_history( $comment_id );
500 echo '<dl class="akismet-history">';
501 foreach ( $history as $row ) {
502 echo '<dt>' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</dt>';
503 echo '<dd>' . esc_html( $row['message'] ) . '</dd>';
510 #add_action( 'manage_comments_custom_column', 'akismet_comment_column_row', 10, 2 );
514 // call out URLS in comments
515 function akismet_text_add_link_callback( $m ) {
517 if ( $m[4] == $m[2] )
518 return '<a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a>';
520 return '<span title="'.$m[2].'" class="comment-link"><a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a></span>';
523 function akismet_text_add_link_class( $comment_text ) {
524 return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', 'akismet_text_add_link_callback', $comment_text );
527 add_filter('comment_text', 'akismet_text_add_link_class');
531 function akismet_rightnow() {
532 global $submenu, $wp_db_version;
534 if ( 8645 < $wp_db_version ) // 2.7
535 $link = 'edit-comments.php?comment_status=spam';
536 elseif ( isset( $submenu['edit-comments.php'] ) )
537 $link = 'edit-comments.php?page=akismet-admin';
539 $link = 'edit.php?page=akismet-admin';
541 if ( $count = get_option('akismet_spam_count') ) {
542 $intro = sprintf( _n(
543 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
544 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
546 ), 'http://akismet.com/?return=true', number_format_i18n( $count ) );
548 $intro = sprintf( __('<a href="%1$s">Akismet</a> blocks spam from getting to your blog. '), 'http://akismet.com/?return=true' );
551 $link = function_exists( 'esc_url' ) ? esc_url( $link ) : clean_url( $link );
552 if ( $queue_count = akismet_spam_count() ) {
553 $queue_text = sprintf( _n(
554 'There\'s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
555 'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
557 ), number_format_i18n( $queue_count ), $link );
559 $queue_text = sprintf( __( "There's nothing in your <a href='%1\$s'>spam queue</a> at the moment." ), $link );
562 $text = $intro . '<br />' . $queue_text;
563 echo "<p class='akismet-right-now'>$text</p>\n";
566 add_action('rightnow_end', 'akismet_rightnow');
570 function akismet_check_for_spam_button($comment_status) {
571 if ( 'approved' == $comment_status )
573 if ( function_exists('plugins_url') )
574 $link = 'admin.php?action=akismet_recheck_queue';
576 $link = 'edit-comments.php?page=akismet-admin&recheckqueue=true&noheader=true';
577 echo "</div><div class='alignleft'><a class='button-secondary checkforspam' href='$link'>" . __('Check for Spam') . "</a>";
579 add_action('manage_comments_nav', 'akismet_check_for_spam_button');
581 function akismet_submit_nonspam_comment ( $comment_id ) {
582 global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
583 $comment_id = (int) $comment_id;
585 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
586 if ( !$comment ) // it was deleted
589 // use the original version stored in comment_meta if available
590 $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
591 if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
592 $comment = (object) array_merge( (array)$comment, $as_submitted );
595 $comment->blog = get_bloginfo('url');
596 $comment->blog_lang = get_locale();
597 $comment->blog_charset = get_option('blog_charset');
598 $comment->permalink = get_permalink($comment->comment_post_ID);
599 if ( is_object($current_user) ) {
600 $comment->reporter = $current_user->user_login;
602 if ( is_object($current_site) ) {
603 $comment->site_domain = $current_site->domain;
606 $comment->user_role = '';
607 if ( isset( $comment->user_ID ) )
608 $comment->user_role = akismet_get_user_roles($comment->user_ID);
610 if ( akismet_test_mode() )
611 $comment->is_test = 'true';
613 $post = get_post( $comment->comment_post_ID );
614 $comment->comment_post_modified_gmt = $post->post_modified_gmt;
617 foreach ( $comment as $key => $data )
618 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
620 $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-ham", $akismet_api_port);
621 if ( $comment->reporter ) {
622 akismet_update_comment_history( $comment_id, sprintf( __('%s reported this comment as not spam'), $comment->reporter ), 'report-ham' );
623 update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
624 update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
627 do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
630 function akismet_submit_spam_comment ( $comment_id ) {
631 global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
632 $comment_id = (int) $comment_id;
634 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
635 if ( !$comment ) // it was deleted
637 if ( 'spam' != $comment->comment_approved )
640 // use the original version stored in comment_meta if available
641 $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
642 if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
643 $comment = (object) array_merge( (array)$comment, $as_submitted );
646 $comment->blog = get_bloginfo('url');
647 $comment->blog_lang = get_locale();
648 $comment->blog_charset = get_option('blog_charset');
649 $comment->permalink = get_permalink($comment->comment_post_ID);
650 if ( is_object($current_user) ) {
651 $comment->reporter = $current_user->user_login;
653 if ( is_object($current_site) ) {
654 $comment->site_domain = $current_site->domain;
657 $comment->user_role = '';
658 if ( isset( $comment->user_ID ) )
659 $comment->user_role = akismet_get_user_roles($comment->user_ID);
661 if ( akismet_test_mode() )
662 $comment->is_test = 'true';
664 $post = get_post( $comment->comment_post_ID );
665 $comment->comment_post_modified_gmt = $post->post_modified_gmt;
668 foreach ( $comment as $key => $data )
669 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
671 $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-spam", $akismet_api_port);
672 if ( $comment->reporter ) {
673 akismet_update_comment_history( $comment_id, sprintf( __('%s reported this comment as spam'), $comment->reporter ), 'report-spam' );
674 update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
675 update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
677 do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
681 function akismet_transition_comment_status( $new_status, $old_status, $comment ) {
682 if ( $new_status == $old_status )
685 # we don't need to record a history item for deleted comments
686 if ( $new_status == 'delete' )
692 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
695 if ( defined('WP_IMPORTING') && WP_IMPORTING == true )
698 // if this is present, it means the status has been changed by a re-check, not an explicit user action
699 if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) )
702 global $current_user;
704 if ( is_object( $current_user ) )
705 $reporter = $current_user->user_login;
708 // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
709 // is changed automatically by another plugin. Unfortunately WordPress doesn't provide an unambiguous way to
710 // determine why the transition_comment_status action was triggered. And there are several different ways by which
711 // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
712 // We'll assume that this is an explicit user action if POST or GET has an 'action' key.
713 if ( isset($_POST['action']) || isset($_GET['action']) ) {
714 if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) {
715 return akismet_submit_spam_comment( $comment->comment_ID );
716 } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
717 return akismet_submit_nonspam_comment( $comment->comment_ID );
721 akismet_update_comment_history( $comment->comment_ID, sprintf( __('%s changed the comment status to %s'), $reporter, $new_status ), 'status-' . $new_status );
724 add_action( 'transition_comment_status', 'akismet_transition_comment_status', 10, 3 );
726 // Total spam in queue
727 // get_option( 'akismet_spam_count' ) is the total caught ever
728 function akismet_spam_count( $type = false ) {
731 if ( !$type ) { // total
732 $count = wp_cache_get( 'akismet_spam_count', 'widget' );
733 if ( false === $count ) {
734 if ( function_exists('wp_count_comments') ) {
735 $count = wp_count_comments();
736 $count = $count->spam;
738 $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
740 wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
743 } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
745 } else { // pingback, trackback, ...
746 $type = $wpdb->escape( $type );
749 return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='$type'");
753 function akismet_recheck_queue() {
754 global $wpdb, $akismet_api_host, $akismet_api_port;
756 akismet_fix_scheduled_recheck();
758 if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
761 $moderation = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '0'", ARRAY_A );
762 foreach ( (array) $moderation as $c ) {
763 $c['user_ip'] = $c['comment_author_IP'];
764 $c['user_agent'] = $c['comment_agent'];
766 $c['blog'] = get_bloginfo('url');
767 $c['blog_lang'] = get_locale();
768 $c['blog_charset'] = get_option('blog_charset');
769 $c['permalink'] = get_permalink($c['comment_post_ID']);
771 $c['user_role'] = '';
772 if ( isset( $c['user_ID'] ) )
773 $c['user_role'] = akismet_get_user_roles($c['user_ID']);
775 if ( akismet_test_mode() )
776 $c['is_test'] = 'true';
778 $id = (int) $c['comment_ID'];
781 foreach ( $c as $key => $data )
782 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
784 add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
785 $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
786 if ( 'true' == $response[1] ) {
787 wp_set_comment_status($c['comment_ID'], 'spam');
788 update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
789 delete_comment_meta( $c['comment_ID'], 'akismet_error' );
790 akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam'), 'check-spam' );
792 } elseif ( 'false' == $response[1] ) {
793 update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
794 delete_comment_meta( $c['comment_ID'], 'akismet_error' );
795 akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment'), 'check-ham' );
796 // abnormal result: error
798 update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
799 akismet_update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)'), substr($response[1], 0, 50)), 'check-error' );
802 delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' );
804 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url( 'edit-comments.php' );
805 wp_safe_redirect( $redirect_to );
809 add_action('admin_action_akismet_recheck_queue', 'akismet_recheck_queue');
811 // Adds an 'x' link next to author URLs, clicking will remove the author URL and show an undo link
812 function akismet_remove_comment_author_url() {
813 if ( !empty($_POST['id'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
815 $comment = get_comment( intval($_POST['id']), ARRAY_A );
816 if (current_user_can('edit_comment', $comment['comment_ID'])) {
817 $comment['comment_author_url'] = '';
818 do_action( 'comment_remove_author_url' );
819 print(wp_update_comment( $comment ));
825 add_action('wp_ajax_comment_author_deurl', 'akismet_remove_comment_author_url');
827 function akismet_add_comment_author_url() {
828 if ( !empty( $_POST['id'] ) && !empty( $_POST['url'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
830 $comment = get_comment( intval($_POST['id']), ARRAY_A );
831 if (current_user_can('edit_comment', $comment['comment_ID'])) {
832 $comment['comment_author_url'] = esc_url($_POST['url']);
833 do_action( 'comment_add_author_url' );
834 print(wp_update_comment( $comment ));
840 add_action('wp_ajax_comment_author_reurl', 'akismet_add_comment_author_url');
842 // Check connectivity between the WordPress blog and Akismet's servers.
843 // 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).
844 function akismet_check_server_connectivity() {
845 global $akismet_api_host, $akismet_api_port, $wpcom_api_key;
847 $test_host = 'rest.akismet.com';
849 // Some web hosts may disable one or both functions
850 if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
853 $ips = gethostbynamel($test_host);
854 if ( !$ips || !is_array($ips) || !count($ips) )
858 foreach ( $ips as $ip ) {
859 $response = akismet_verify_key( akismet_get_key(), $ip );
860 // even if the key is invalid, at least we know we have connectivity
861 if ( $response == 'valid' || $response == 'invalid' )
862 $servers[$ip] = true;
864 $servers[$ip] = false;
870 // Check the server connectivity and store the results in an option.
871 // Cached results will be used if not older than the specified timeout in seconds; use $cache_timeout = 0 to force an update.
872 // Returns the same associative array as akismet_check_server_connectivity()
873 function akismet_get_server_connectivity( $cache_timeout = 86400 ) {
874 $servers = get_option('akismet_available_servers');
875 if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false )
878 // There's a race condition here but the effect is harmless.
879 $servers = akismet_check_server_connectivity();
880 update_option('akismet_available_servers', $servers);
881 update_option('akismet_connectivity_time', time());
885 // Returns true if server connectivity was OK at the last check, false if there was a problem that needs to be fixed.
886 function akismet_server_connectivity_ok() {
887 // skip the check on WPMU because the status page is hidden
888 global $wpcom_api_key;
889 if ( $wpcom_api_key )
891 $servers = akismet_get_server_connectivity();
892 return !( empty($servers) || !count($servers) || count( array_filter($servers) ) < count($servers) );
895 function akismet_admin_menu() {
896 if ( class_exists( 'Jetpack' ) ) {
897 add_action( 'jetpack_admin_menu', 'akismet_load_menu' );
903 function akismet_load_menu() {
904 if ( class_exists( 'Jetpack' ) ) {
905 add_submenu_page( 'jetpack', __( 'Akismet' ), __( 'Akismet' ), 'manage_options', 'akismet-key-config', 'akismet_conf' );
906 add_submenu_page( 'jetpack', __( 'Akismet Stats' ), __( 'Akismet Stats' ), 'manage_options', 'akismet-stats-display', 'akismet_stats_display' );
908 add_submenu_page('plugins.php', __('Akismet'), __('Akismet'), 'manage_options', 'akismet-key-config', 'akismet_conf');
909 add_submenu_page('index.php', __('Akismet Stats'), __('Akismet Stats'), 'manage_options', 'akismet-stats-display', 'akismet_stats_display');