4 const NONCE = 'akismet-update-key';
6 private static $initiated = false;
7 private static $notices = array();
9 public static function init() {
10 if ( ! self::$initiated ) {
14 if ( isset( $_POST['action'] ) && $_POST['action'] == 'enter-key' ) {
15 self::enter_api_key();
19 public static function init_hooks() {
20 // The standalone stats page was removed in 3.0 for an all-in-one config and stats page.
21 // Redirect any links that might have been bookmarked or in browser history.
22 if ( isset( $_GET['page'] ) && 'akismet-stats-display' == $_GET['page'] ) {
23 wp_safe_redirect( esc_url_raw( self::get_page_url( 'stats' ) ), 301 );
27 self::$initiated = true;
29 add_action( 'admin_init', array( 'Akismet_Admin', 'admin_init' ) );
30 add_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); # Priority 5, so it's called before Jetpack's admin_menu.
31 add_action( 'admin_notices', array( 'Akismet_Admin', 'display_notice' ) );
32 add_action( 'admin_enqueue_scripts', array( 'Akismet_Admin', 'load_resources' ) );
33 add_action( 'activity_box_end', array( 'Akismet_Admin', 'dashboard_stats' ) );
34 add_action( 'rightnow_end', array( 'Akismet_Admin', 'rightnow_stats' ) );
35 add_action( 'manage_comments_nav', array( 'Akismet_Admin', 'check_for_spam_button' ) );
36 add_action( 'admin_action_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
37 add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
38 add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
39 add_action( 'wp_ajax_comment_author_reurl', array( 'Akismet_Admin', 'add_comment_author_url' ) );
41 add_filter( 'plugin_action_links', array( 'Akismet_Admin', 'plugin_action_links' ), 10, 2 );
42 add_filter( 'comment_row_actions', array( 'Akismet_Admin', 'comment_row_action' ), 10, 2 );
43 add_filter( 'comment_text', array( 'Akismet_Admin', 'text_add_link_class' ) );
45 add_filter( 'plugin_action_links_'.plugin_basename( plugin_dir_path( __FILE__ ) . 'akismet.php'), array( 'Akismet_Admin', 'admin_plugin_settings_link' ) );
47 add_filter( 'wxr_export_skip_commentmeta', array( 'Akismet_Admin', 'exclude_commentmeta_from_export' ), 10, 3 );
50 public static function admin_init() {
51 load_plugin_textdomain( 'akismet' );
52 add_meta_box( 'akismet-status', __('Comment History', 'akismet'), array( 'Akismet_Admin', 'comment_status_meta_box' ), 'comment', 'normal' );
55 public static function admin_menu() {
56 if ( class_exists( 'Jetpack' ) )
57 add_action( 'jetpack_admin_menu', array( 'Akismet_Admin', 'load_menu' ) );
62 public static function admin_head() {
63 if ( !current_user_can( 'manage_options' ) )
67 public static function admin_plugin_settings_link( $links ) {
68 $settings_link = '<a href="'.esc_url( self::get_page_url() ).'">'.__('Settings', 'akismet').'</a>';
69 array_unshift( $links, $settings_link );
73 public static function load_menu() {
74 if ( class_exists( 'Jetpack' ) )
75 $hook = add_submenu_page( 'jetpack', __( 'Akismet' , 'akismet'), __( 'Akismet' , 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
77 $hook = add_options_page( __('Akismet', 'akismet'), __('Akismet', 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
79 if ( version_compare( $GLOBALS['wp_version'], '3.3', '>=' ) ) {
80 add_action( "load-$hook", array( 'Akismet_Admin', 'admin_help' ) );
84 public static function load_resources() {
87 if ( in_array( $hook_suffix, array(
88 'index.php', # dashboard
92 'settings_page_akismet-key-config',
93 'jetpack_page_akismet-key-config',
95 wp_register_style( 'akismet.css', AKISMET__PLUGIN_URL . '_inc/akismet.css', array(), AKISMET_VERSION );
96 wp_enqueue_style( 'akismet.css');
98 wp_register_script( 'akismet.js', AKISMET__PLUGIN_URL . '_inc/akismet.js', array('jquery','postbox'), AKISMET_VERSION );
99 wp_enqueue_script( 'akismet.js' );
100 wp_localize_script( 'akismet.js', 'WPAkismet', array(
101 'comment_author_url_nonce' => wp_create_nonce( 'comment_author_url_nonce' ),
103 'Remove this URL' => __( 'Remove this URL' , 'akismet'),
104 'Removing...' => __( 'Removing...' , 'akismet'),
105 'URL removed' => __( 'URL removed' , 'akismet'),
106 '(undo)' => __( '(undo)' , 'akismet'),
107 'Re-adding...' => __( 'Re-adding...' , 'akismet'),
114 * Add help to the Akismet page
116 * @return false if not the Akismet page
118 public static function admin_help() {
119 $current_screen = get_current_screen();
122 if ( current_user_can( 'manage_options' ) ) {
123 if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) ) {
125 $current_screen->add_help_tab(
128 'title' => __( 'Overview' , 'akismet'),
130 '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
131 '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
132 '<p>' . esc_html__( 'On this page, you are able to setup the Akismet plugin.' , 'akismet') . '</p>',
136 $current_screen->add_help_tab(
138 'id' => 'setup-signup',
139 'title' => __( 'New to Akismet' , 'akismet'),
141 '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
142 '<p>' . esc_html__( 'You need to enter an API key to activate the Akismet service on your site.' , 'akismet') . '</p>' .
143 '<p>' . sprintf( __( 'Signup for an account on %s to get an API Key.' , 'akismet'), '<a href="https://akismet.com/plugin-signup/" target="_blank">Akismet.com</a>' ) . '</p>',
147 $current_screen->add_help_tab(
149 'id' => 'setup-manual',
150 'title' => __( 'Enter an API Key' , 'akismet'),
152 '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
153 '<p>' . esc_html__( 'If you already have an API key' , 'akismet') . '</p>' .
155 '<li>' . esc_html__( 'Copy and paste the API key into the text field.' , 'akismet') . '</li>' .
156 '<li>' . esc_html__( 'Click the Use this Key button.' , 'akismet') . '</li>' .
161 elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' ) {
163 $current_screen->add_help_tab(
166 'title' => __( 'Overview' , 'akismet'),
168 '<p><strong>' . esc_html__( 'Akismet Stats' , 'akismet') . '</strong></p>' .
169 '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
170 '<p>' . esc_html__( 'On this page, you are able to view stats on spam filtered on your site.' , 'akismet') . '</p>',
176 $current_screen->add_help_tab(
179 'title' => __( 'Overview' , 'akismet'),
181 '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
182 '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
183 '<p>' . esc_html__( 'On this page, you are able to enter/remove an API key, view account information and view spam stats.' , 'akismet') . '</p>',
187 $current_screen->add_help_tab(
190 'title' => __( 'Settings' , 'akismet'),
192 '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
193 '<p><strong>' . esc_html__( 'API Key' , 'akismet') . '</strong> - ' . esc_html__( 'Enter/remove an API key.' , 'akismet') . '</p>' .
194 '<p><strong>' . esc_html__( 'Comments' , 'akismet') . '</strong> - ' . esc_html__( 'Show the number of approved comments beside each comment author in the comments list page.' , 'akismet') . '</p>' .
195 '<p><strong>' . esc_html__( 'Strictness' , 'akismet') . '</strong> - ' . esc_html__( 'Choose to either discard the worst spam automatically or to always put all spam in spam folder.' , 'akismet') . '</p>',
199 $current_screen->add_help_tab(
202 'title' => __( 'Account' , 'akismet'),
204 '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
205 '<p><strong>' . esc_html__( 'Subscription Type' , 'akismet') . '</strong> - ' . esc_html__( 'The Akismet subscription plan' , 'akismet') . '</p>' .
206 '<p><strong>' . esc_html__( 'Status' , 'akismet') . '</strong> - ' . esc_html__( 'The subscription status - active, cancelled or suspended' , 'akismet') . '</p>',
213 $current_screen->set_help_sidebar(
214 '<p><strong>' . esc_html__( 'For more information:' , 'akismet') . '</strong></p>' .
215 '<p><a href="https://akismet.com/faq/" target="_blank">' . esc_html__( 'Akismet FAQ' , 'akismet') . '</a></p>' .
216 '<p><a href="https://akismet.com/support/" target="_blank">' . esc_html__( 'Akismet Support' , 'akismet') . '</a></p>'
220 public static function enter_api_key() {
221 if ( function_exists('current_user_can') && !current_user_can('manage_options') )
222 die(__('Cheatin’ uh?', 'akismet'));
224 if ( !wp_verify_nonce( $_POST['_wpnonce'], self::NONCE ) )
227 foreach( array( 'akismet_strictness', 'akismet_show_user_comments_approved' ) as $option ) {
228 update_option( $option, isset( $_POST[$option] ) && (int) $_POST[$option] == 1 ? '1' : '0' );
231 if ( defined( 'WPCOM_API_KEY' ) )
232 return false; //shouldn't have option to save key if already defined
234 $new_key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
235 $old_key = Akismet::get_api_key();
237 if ( empty( $new_key ) ) {
238 if ( !empty( $old_key ) ) {
239 delete_option( 'wordpress_api_key' );
240 self::$notices[] = 'new-key-empty';
243 elseif ( $new_key != $old_key ) {
244 self::save_key( $new_key );
250 public static function save_key( $api_key ) {
251 $key_status = Akismet::verify_key( $api_key );
253 if ( $key_status == 'valid' ) {
254 $akismet_user = self::get_akismet_user( $api_key );
256 if ( $akismet_user ) {
257 if ( in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) ) )
258 update_option( 'wordpress_api_key', $api_key );
260 if ( $akismet_user->status == 'active' )
261 self::$notices['status'] = 'new-key-valid';
263 self::$notices['status'] = $akismet_user->status;
266 self::$notices['status'] = 'new-key-invalid';
268 elseif ( in_array( $key_status, array( 'invalid', 'failed' ) ) )
269 self::$notices['status'] = 'new-key-'.$key_status;
272 public static function dashboard_stats() {
273 if ( !function_exists('did_action') || did_action( 'rightnow_end' ) )
274 return; // We already displayed this info in the "Right Now" section
276 if ( !$count = get_option('akismet_spam_count') )
281 echo '<h3>' . esc_html( _x( 'Spam', 'comments' , 'akismet') ) . '</h3>';
283 echo '<p>'.sprintf( _n(
284 '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comment</a>.',
285 '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.',
287 , 'akismet'), 'https://akismet.com/wordpress/', esc_url( add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( isset( $submenu['edit-comments.php'] ) ? 'edit-comments.php' : 'edit.php' ) ) ), number_format_i18n($count) ).'</p>';
291 public static function rightnow_stats() {
292 global $submenu, $wp_db_version;
294 if ( 8645 < $wp_db_version ) // 2.7
295 $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
296 elseif ( isset( $submenu['edit-comments.php'] ) )
297 $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit-comments.php' ) );
299 $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit.php' ) );
301 if ( $count = get_option('akismet_spam_count') ) {
302 $intro = sprintf( _n(
303 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
304 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
306 , 'akismet'), 'https://akismet.com/wordpress/', number_format_i18n( $count ) );
308 $intro = sprintf( __('<a href="%s">Akismet</a> blocks spam from getting to your blog. ', 'akismet'), 'https://akismet.com/wordpress/' );
311 $link = function_exists( 'esc_url' ) ? esc_url( $link ) : clean_url( $link );
312 if ( $queue_count = self::get_spam_count() ) {
313 $queue_text = sprintf( _n(
314 'There’s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
315 'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
317 , 'akismet'), number_format_i18n( $queue_count ), $link );
319 $queue_text = sprintf( __( "There’s nothing in your <a href='%s'>spam queue</a> at the moment." , 'akismet'), $link );
322 $text = $intro . '<br />' . $queue_text;
323 echo "<p class='akismet-right-now'>$text</p>\n";
326 public static function check_for_spam_button( $comment_status ) {
327 if ( 'approved' == $comment_status )
330 if ( function_exists('plugins_url') )
331 $link = add_query_arg( array( 'action' => 'akismet_recheck_queue' ), admin_url( 'admin.php' ) );
333 $link = add_query_arg( array( 'page' => 'akismet-admin', 'recheckqueue' => 'true', 'noheader' => 'true' ), admin_url( 'edit-comments.php' ) );
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>';
338 public static function recheck_queue() {
341 Akismet::fix_scheduled_recheck();
343 if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
347 if ( isset( $_POST['limit'] ) && isset( $_POST['offset'] ) ) {
348 $paginate = $wpdb->prepare( " LIMIT %d OFFSET %d", array( $_POST['limit'], $_POST['offset'] ) );
350 $moderation = $wpdb->get_results( "SELECT * FROM {$wpdb->comments} WHERE comment_approved = '0'{$paginate}", ARRAY_A );
352 foreach ( (array) $moderation as $c ) {
353 $c['user_ip'] = $c['comment_author_IP'];
354 $c['user_agent'] = $c['comment_agent'];
356 $c['blog'] = get_bloginfo('url');
357 $c['blog_lang'] = get_locale();
358 $c['blog_charset'] = get_option('blog_charset');
359 $c['permalink'] = get_permalink($c['comment_post_ID']);
361 $c['user_role'] = '';
362 if ( isset( $c['user_ID'] ) )
363 $c['user_role'] = Akismet::get_user_roles($c['user_ID']);
365 if ( Akismet::is_test_mode() )
366 $c['is_test'] = 'true';
368 add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
370 $response = Akismet::http_post( Akismet::build_query( $c ), 'comment-check' );
372 if ( 'true' == $response[1] ) {
373 wp_set_comment_status( $c['comment_ID'], 'spam' );
374 update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
375 delete_comment_meta( $c['comment_ID'], 'akismet_error' );
376 delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
377 Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam', 'akismet'), 'check-spam' );
379 } elseif ( 'false' == $response[1] ) {
380 update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
381 delete_comment_meta( $c['comment_ID'], 'akismet_error' );
382 delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
383 Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment', 'akismet'), 'check-ham' );
384 // abnormal result: error
386 update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
387 Akismet::update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)', 'akismet'), substr($response[1], 0, 50)), 'check-error' );
390 delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' );
392 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
394 'processed' => count((array) $moderation),
398 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url( 'edit-comments.php' );
399 wp_safe_redirect( $redirect_to );
404 // Adds an 'x' link next to author URLs, clicking will remove the author URL and show an undo link
405 public static function remove_comment_author_url() {
406 if ( !empty( $_POST['id'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
407 $comment = get_comment( intval( $_POST['id'] ), ARRAY_A );
408 if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
409 $comment['comment_author_url'] = '';
410 do_action( 'comment_remove_author_url' );
411 print( wp_update_comment( $comment ) );
417 public static function add_comment_author_url() {
418 if ( !empty( $_POST['id'] ) && !empty( $_POST['url'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
419 $comment = get_comment( intval( $_POST['id'] ), ARRAY_A );
420 if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
421 $comment['comment_author_url'] = esc_url( $_POST['url'] );
422 do_action( 'comment_add_author_url' );
423 print( wp_update_comment( $comment ) );
429 public static function comment_row_action( $a, $comment ) {
431 // failsafe for old WP versions
432 if ( !function_exists('add_comment_meta') )
435 $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
436 $akismet_error = get_comment_meta( $comment->comment_ID, 'akismet_error', true );
437 $user_result = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
438 $comment_status = wp_get_comment_status( $comment->comment_ID );
440 if ( $akismet_error ) {
441 $desc = __( 'Awaiting spam check' , 'akismet');
442 } elseif ( !$user_result || $user_result == $akismet_result ) {
443 // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
444 if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
445 $desc = __( 'Flagged as spam by Akismet' , 'akismet');
446 elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
447 $desc = __( 'Cleared by Akismet' , 'akismet');
449 $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
450 if ( $user_result == 'true' )
451 $desc = sprintf( __('Flagged as spam by %s', 'akismet'), $who );
453 $desc = sprintf( __('Un-spammed by %s', 'akismet'), $who );
456 // add a History item to the hover links, just after Edit
457 if ( $akismet_result ) {
459 foreach ( $a as $k => $item ) {
463 || ( $k == 'unspam' && $GLOBALS['wp_version'] >= 3.4 )
465 $b['history'] = '<a href="comment.php?action=editcomment&c='.$comment->comment_ID.'#akismet-status" title="'. esc_attr__( 'View comment history' , 'akismet') . '"> '. esc_html__('History', 'akismet') . '</a>';
473 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>';
475 $show_user_comments = apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') );
476 $show_user_comments = $show_user_comments === 'false' ? false : $show_user_comments; //option used to be saved as 'false' / 'true'
478 if ( $show_user_comments ) {
479 $comment_count = Akismet::get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
480 $comment_count = intval( $comment_count );
481 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>';
487 public static function comment_status_meta_box( $comment ) {
488 $history = Akismet::get_comment_history( $comment->comment_ID );
491 echo '<div class="akismet-history" style="margin: 13px;">';
492 foreach ( $history as $row ) {
493 $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
494 echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( esc_html__('%s ago', 'akismet'), human_time_diff( $row['time'] ) ) . '</span> - ';
495 echo esc_html( $row['message'] ) . '</div>';
501 public static function plugin_action_links( $links, $file ) {
502 if ( $file == plugin_basename( AKISMET__PLUGIN_URL . '/akismet.php' ) ) {
503 $links[] = '<a href="' . esc_url( self::get_page_url() ) . '">'.esc_html__( 'Settings' , 'akismet').'</a>';
509 public static function text_add_link_callback( $m ) {
511 if ( $m[4] == $m[2] )
512 return '<a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a>';
514 return '<span title="'.$m[2].'" class="comment-link"><a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a></span>';
517 public static function text_add_link_class( $comment_text ) {
518 return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', array( 'Akismet_Admin', 'text_add_link_callback' ), $comment_text );
521 // Total spam in queue
522 // get_option( 'akismet_spam_count' ) is the total caught ever
523 public static function get_spam_count( $type = false ) {
526 if ( !$type ) { // total
527 $count = wp_cache_get( 'akismet_spam_count', 'widget' );
528 if ( false === $count ) {
529 if ( function_exists('wp_count_comments') ) {
530 $count = wp_count_comments();
531 $count = $count->spam;
533 $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam'");
535 wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
538 } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
542 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type = %s", $type ) );
545 // Check connectivity between the WordPress blog and Akismet's servers.
546 // 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).
547 public static function check_server_ip_connectivity() {
549 $servers = $ips = array();
551 // Some web hosts may disable this function
552 if ( function_exists('gethostbynamel') ) {
554 $ips = gethostbynamel( 'rest.akismet.com' );
555 if ( $ips && is_array($ips) && count($ips) ) {
556 $api_key = Akismet::get_api_key();
558 foreach ( $ips as $ip ) {
559 $response = Akismet::verify_key( $api_key, $ip );
560 // even if the key is invalid, at least we know we have connectivity
561 if ( $response == 'valid' || $response == 'invalid' )
562 $servers[$ip] = 'connected';
564 $servers[$ip] = $response ? $response : 'unable to connect';
572 // Simpler connectivity check
573 public static function check_server_connectivity($cache_timeout = 86400) {
576 $debug[ 'PHP_VERSION' ] = PHP_VERSION;
577 $debug[ 'WORDPRESS_VERSION' ] = $GLOBALS['wp_version'];
578 $debug[ 'AKISMET_VERSION' ] = AKISMET_VERSION;
579 $debug[ 'AKISMET__PLUGIN_DIR' ] = AKISMET__PLUGIN_DIR;
580 $debug[ 'SITE_URL' ] = site_url();
581 $debug[ 'HOME_URL' ] = home_url();
583 $servers = get_option('akismet_available_servers');
584 if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false ) {
585 $servers = self::check_server_ip_connectivity();
586 update_option('akismet_available_servers', $servers);
587 update_option('akismet_connectivity_time', time());
590 $response = wp_remote_get( 'http://rest.akismet.com/1.1/test' );
592 $debug[ 'gethostbynamel' ] = function_exists('gethostbynamel') ? 'exists' : 'not here';
593 $debug[ 'Servers' ] = $servers;
594 $debug[ 'Test Connection' ] = $response;
596 Akismet::log( $debug );
598 if ( $response && 'connected' == wp_remote_retrieve_body( $response ) )
604 // Check the server connectivity and store the available servers in an option.
605 public static function get_server_connectivity($cache_timeout = 86400) {
606 return self::check_server_connectivity( $cache_timeout );
609 public static function get_number_spam_waiting() {
611 return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" );
614 public static function get_page_url( $page = 'config' ) {
616 $args = array( 'page' => 'akismet-key-config' );
618 if ( $page == 'stats' )
619 $args = array( 'page' => 'akismet-key-config', 'view' => 'stats' );
620 elseif ( $page == 'delete_key' )
621 $args = array( 'page' => 'akismet-key-config', 'view' => 'start', 'action' => 'delete-key', '_wpnonce' => wp_create_nonce( self::NONCE ) );
623 $url = add_query_arg( $args, class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'options-general.php' ) );
628 public static function get_akismet_user( $api_key ) {
629 $akismet_user = Akismet::http_post( Akismet::build_query( array( 'key' => $api_key ) ), 'get-subscription' );
631 if ( ! empty( $akismet_user[1] ) )
632 $akismet_user = json_decode( $akismet_user[1] );
634 $akismet_user = false;
636 return $akismet_user;
639 public static function get_stats( $api_key ) {
640 $stat_totals = array();
642 foreach( array( '6-months', 'all' ) as $interval ) {
643 $response = Akismet::http_post( Akismet::build_query( array( 'blog' => urlencode( get_bloginfo('url') ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
645 if ( ! empty( $response[1] ) ) {
646 $stat_totals[$interval] = json_decode( $response[1] );
652 public static function verify_wpcom_key( $api_key, $user_id, $token = '' ) {
653 $akismet_account = Akismet::http_post( Akismet::build_query( array(
654 'user_id' => $user_id,
655 'api_key' => $api_key,
657 'get_account_type' => 'true'
658 ) ), 'verify-wpcom-key' );
660 if ( ! empty( $akismet_account[1] ) )
661 $akismet_account = json_decode( $akismet_account[1] );
663 Akismet::log( compact( 'akismet_account' ) );
665 return $akismet_account;
668 public static function display_alert() {
669 Akismet::view( 'notice', array(
671 'code' => (int) get_option( 'akismet_alert_code' ),
672 'msg' => get_option( 'akismet_alert_msg' )
676 public static function display_spam_check_warning() {
677 Akismet::fix_scheduled_recheck();
679 if ( wp_next_scheduled('akismet_schedule_cron_recheck') > time() && self::get_number_spam_waiting() > 0 ) {
680 $link_text = apply_filters( 'akismet_spam_check_warning_link_text', sprintf( __( 'Please check your <a href="%s">Akismet configuration</a> and contact your web host if problems persist.', 'akismet'), esc_url( self::get_page_url() ) ) );
681 Akismet::view( 'notice', array( 'type' => 'spam-check', 'link_text' => $link_text ) );
685 public static function display_invalid_version() {
686 Akismet::view( 'notice', array( 'type' => 'version' ) );
689 public static function display_api_key_warning() {
690 Akismet::view( 'notice', array( 'type' => 'plugin' ) );
693 public static function display_page() {
694 if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) )
695 self::display_start_page();
696 elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' )
697 self::display_stats_page();
699 self::display_configuration_page();
702 public static function display_start_page() {
703 if ( isset( $_GET['action'] ) ) {
704 if ( $_GET['action'] == 'delete-key' ) {
705 if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], self::NONCE ) )
706 delete_option( 'wordpress_api_key' );
710 if ( $api_key = Akismet::get_api_key() ) {
711 self::display_configuration_page();
715 //the user can choose to auto connect their API key by clicking a button on the akismet done page
716 //if jetpack, get verified api key by using connected wpcom user id
717 //if no jetpack, get verified api key by using an akismet token
719 $akismet_user = false;
721 if ( isset( $_GET['token'] ) && preg_match('/^(\d+)-[0-9a-f]{20}$/', $_GET['token'] ) )
722 $akismet_user = self::verify_wpcom_key( '', '', $_GET['token'] );
723 elseif ( $jetpack_user = self::get_jetpack_user() )
724 $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'] );
726 if ( isset( $_GET['action'] ) ) {
727 if ( $_GET['action'] == 'save-key' ) {
728 if ( is_object( $akismet_user ) ) {
729 self::save_key( $akismet_user->api_key );
730 self::display_notice();
731 self::display_configuration_page();
737 echo '<h2 class="ak-header">'.esc_html__('Akismet', 'akismet').'</h2>';
739 self::display_status();
741 Akismet::view( 'start', compact( 'akismet_user' ) );
744 public static function display_stats_page() {
745 Akismet::view( 'stats' );
748 public static function display_configuration_page() {
749 $api_key = Akismet::get_api_key();
750 $akismet_user = self::get_akismet_user( $api_key );
751 $stat_totals = self::get_stats( $api_key );
753 // If unset, create the new strictness option using the old discard option to determine its default
754 if ( get_option( 'akismet_strictness' ) === false )
755 add_option( 'akismet_strictness', (get_option('akismet_discard_month') === 'true' ? '1' : '0') );
757 if ( empty( self::$notices ) ) {
759 if ( ! empty( $stat_totals['all'] ) && isset( $stat_totals['all']->time_saved ) && $akismet_user->status == 'active' && $akismet_user->account_type == 'free-api-key' ) {
763 if ( $stat_totals['all']->time_saved > 1800 ) {
764 $total_in_minutes = round( $stat_totals['all']->time_saved / 60 );
765 $total_in_hours = round( $total_in_minutes / 60 );
766 $total_in_days = round( $total_in_hours / 8 );
767 $cleaning_up = __( 'Cleaning up spam takes time.' , 'akismet');
769 if ( $total_in_days > 1 )
770 $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %s days!' , 'akismet'), number_format_i18n( $total_in_days ) );
771 elseif ( $total_in_hours > 1 )
772 $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %d hours!' , 'akismet'), $total_in_hours );
773 elseif ( $total_in_minutes >= 30 )
774 $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %d minutes!' , 'akismet'), $total_in_minutes );
777 Akismet::view( 'notice', array( 'type' => 'active-notice', 'time_saved' => $time_saved ) );
780 if ( !empty( $akismet_user->limit_reached ) && in_array( $akismet_user->limit_reached, array( 'yellow', 'red' ) ) ) {
781 Akismet::view( 'notice', array( 'type' => 'limit-reached', 'level' => $akismet_user->limit_reached ) );
785 if ( !isset( self::$notices['status'] ) && in_array( $akismet_user->status, array( 'cancelled', 'suspended', 'missing', 'no-sub' ) ) )
786 Akismet::view( 'notice', array( 'type' => $akismet_user->status ) );
788 Akismet::log( compact( 'stat_totals', 'akismet_user' ) );
789 Akismet::view( 'config', compact( 'api_key', 'akismet_user', 'stat_totals' ) );
792 public static function display_notice() {
795 if ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config', 'edit-comments.php' ) ) && (int) get_option( 'akismet_alert_code' ) > 0 ) {
796 self::display_alert();
798 elseif ( $hook_suffix == 'plugins.php' && !Akismet::get_api_key() ) {
799 self::display_api_key_warning();
801 elseif ( $hook_suffix == 'edit-comments.php' && wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
802 self::display_spam_check_warning();
804 elseif ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config' ) ) && Akismet::get_api_key() ) {
805 self::display_status();
809 public static function display_status() {
812 if ( !self::get_server_connectivity() )
813 $type = 'servers-be-down';
815 if ( !empty( $type ) )
816 Akismet::view( 'notice', compact( 'type' ) );
817 elseif ( !empty( self::$notices ) ) {
818 foreach ( self::$notices as $type )
819 Akismet::view( 'notice', compact( 'type' ) );
823 private static function get_jetpack_user() {
824 if ( !class_exists('Jetpack') )
827 Jetpack::load_xml_rpc_client();
828 $xml = new Jetpack_IXR_ClientMulticall( array( 'user_id' => get_current_user_id() ) );
830 $xml->addCall( 'wpcom.getUserID' );
831 $xml->addCall( 'akismet.getAPIKey' );
834 Akismet::log( compact( 'xml' ) );
836 if ( !$xml->isError() ) {
837 $responses = $xml->getResponse();
838 if ( count( $responses ) > 1 ) {
839 $api_key = array_shift( $responses[0] );
840 $user_id = (int) array_shift( $responses[1] );
841 return compact( 'api_key', 'user_id' );
848 * Some commentmeta isn't useful in an export file. Suppress it (when supported).
850 * @param bool $exclude
851 * @param string $key The meta key
852 * @param object $meta The meta object
853 * @return bool Whether to exclude this meta entry from the export.
855 public static function exclude_commentmeta_from_export( $exclude, $key, $meta ) {
856 if ( in_array( $key, array( 'akismet_as_submitted', 'akismet_rechecking', 'akismet_delayed_moderation_email' ) ) ) {