home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-content / plugins / akismet / class.akismet-admin.php < prev    next >
Encoding:
PHP Script  |  2017-12-13  |  43.0 KB  |  1,132 lines

  1. <?php
  2.  
  3. class Akismet_Admin {
  4.     const NONCE = 'akismet-update-key';
  5.  
  6.     private static $initiated = false;
  7.     private static $notices   = array();
  8.     private static $allowed   = array(
  9.         'a' => array(
  10.             'href' => true,
  11.             'title' => true,
  12.         ),
  13.         'b' => array(),
  14.         'code' => array(),
  15.         'del' => array(
  16.             'datetime' => true,
  17.         ),
  18.         'em' => array(),
  19.         'i' => array(),
  20.         'q' => array(
  21.             'cite' => true,
  22.         ),
  23.         'strike' => array(),
  24.         'strong' => array(),
  25.     );
  26.  
  27.     public static function init() {
  28.         if ( ! self::$initiated ) {
  29.             self::init_hooks();
  30.         }
  31.  
  32.         if ( isset( $_POST['action'] ) && $_POST['action'] == 'enter-key' ) {
  33.             self::enter_api_key();
  34.         }
  35.     }
  36.  
  37.     public static function init_hooks() {
  38.         // The standalone stats page was removed in 3.0 for an all-in-one config and stats page.
  39.         // Redirect any links that might have been bookmarked or in browser history.
  40.         if ( isset( $_GET['page'] ) && 'akismet-stats-display' == $_GET['page'] ) {
  41.             wp_safe_redirect( esc_url_raw( self::get_page_url( 'stats' ) ), 301 );
  42.             die;
  43.         }
  44.  
  45.         self::$initiated = true;
  46.  
  47.         add_action( 'admin_init', array( 'Akismet_Admin', 'admin_init' ) );
  48.         add_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); # Priority 5, so it's called before Jetpack's admin_menu.
  49.         add_action( 'admin_notices', array( 'Akismet_Admin', 'display_notice' ) );
  50.         add_action( 'admin_enqueue_scripts', array( 'Akismet_Admin', 'load_resources' ) );
  51.         add_action( 'activity_box_end', array( 'Akismet_Admin', 'dashboard_stats' ) );
  52.         add_action( 'rightnow_end', array( 'Akismet_Admin', 'rightnow_stats' ) );
  53.         add_action( 'manage_comments_nav', array( 'Akismet_Admin', 'check_for_spam_button' ) );
  54.         add_action( 'admin_action_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
  55.         add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
  56.         add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
  57.         add_action( 'wp_ajax_comment_author_reurl', array( 'Akismet_Admin', 'add_comment_author_url' ) );
  58.         add_action( 'jetpack_auto_activate_akismet', array( 'Akismet_Admin', 'connect_jetpack_user' ) );
  59.  
  60.         add_filter( 'plugin_action_links', array( 'Akismet_Admin', 'plugin_action_links' ), 10, 2 );
  61.         add_filter( 'comment_row_actions', array( 'Akismet_Admin', 'comment_row_action' ), 10, 2 );
  62.         
  63.         add_filter( 'plugin_action_links_'.plugin_basename( plugin_dir_path( __FILE__ ) . 'akismet.php'), array( 'Akismet_Admin', 'admin_plugin_settings_link' ) );
  64.         
  65.         add_filter( 'wxr_export_skip_commentmeta', array( 'Akismet_Admin', 'exclude_commentmeta_from_export' ), 10, 3 );
  66.         
  67.         add_filter( 'all_plugins', array( 'Akismet_Admin', 'modify_plugin_description' ) );
  68.     }
  69.  
  70.     public static function admin_init() {
  71.         load_plugin_textdomain( 'akismet' );
  72.         add_meta_box( 'akismet-status', __('Comment History', 'akismet'), array( 'Akismet_Admin', 'comment_status_meta_box' ), 'comment', 'normal' );
  73.     }
  74.  
  75.     public static function admin_menu() {
  76.         if ( class_exists( 'Jetpack' ) )
  77.             add_action( 'jetpack_admin_menu', array( 'Akismet_Admin', 'load_menu' ) );
  78.         else
  79.             self::load_menu();
  80.     }
  81.  
  82.     public static function admin_head() {
  83.         if ( !current_user_can( 'manage_options' ) )
  84.             return;
  85.     }
  86.     
  87.     public static function admin_plugin_settings_link( $links ) { 
  88.           $settings_link = '<a href="'.esc_url( self::get_page_url() ).'">'.__('Settings', 'akismet').'</a>';
  89.           array_unshift( $links, $settings_link ); 
  90.           return $links; 
  91.     }
  92.  
  93.     public static function load_menu() {
  94.         if ( class_exists( 'Jetpack' ) ) {
  95.             $hook = add_submenu_page( 'jetpack', __( 'Akismet Anti-Spam' , 'akismet'), __( 'Akismet Anti-Spam' , 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
  96.         }
  97.         else {
  98.             $hook = add_options_page( __('Akismet Anti-Spam', 'akismet'), __('Akismet Anti-Spam', 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
  99.         }
  100.         
  101.         if ( $hook ) {
  102.             add_action( "load-$hook", array( 'Akismet_Admin', 'admin_help' ) );
  103.         }
  104.     }
  105.  
  106.     public static function load_resources() {
  107.         global $hook_suffix;
  108.  
  109.         if ( in_array( $hook_suffix, apply_filters( 'akismet_admin_page_hook_suffixes', array(
  110.             'index.php', # dashboard
  111.             'edit-comments.php',
  112.             'comment.php',
  113.             'post.php',
  114.             'settings_page_akismet-key-config',
  115.             'jetpack_page_akismet-key-config',
  116.             'plugins.php',
  117.         ) ) ) ) {
  118.             wp_register_style( 'akismet.css', plugin_dir_url( __FILE__ ) . '_inc/akismet.css', array(), AKISMET_VERSION );
  119.             wp_enqueue_style( 'akismet.css');
  120.  
  121.             wp_register_script( 'akismet.js', plugin_dir_url( __FILE__ ) . '_inc/akismet.js', array('jquery'), AKISMET_VERSION );
  122.             wp_enqueue_script( 'akismet.js' );
  123.             
  124.             $inline_js = array(
  125.                 'comment_author_url_nonce' => wp_create_nonce( 'comment_author_url_nonce' ),
  126.                 'strings' => array(
  127.                     'Remove this URL' => __( 'Remove this URL' , 'akismet'),
  128.                     'Removing...'     => __( 'Removing...' , 'akismet'),
  129.                     'URL removed'     => __( 'URL removed' , 'akismet'),
  130.                     '(undo)'          => __( '(undo)' , 'akismet'),
  131.                     'Re-adding...'    => __( 'Re-adding...' , 'akismet'),
  132.                 )
  133.             );
  134.  
  135.             if ( isset( $_GET['akismet_recheck'] ) && wp_verify_nonce( $_GET['akismet_recheck'], 'akismet_recheck' ) ) {
  136.                 $inline_js['start_recheck'] = true;
  137.             }
  138.  
  139.             wp_localize_script( 'akismet.js', 'WPAkismet', $inline_js );
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Add help to the Akismet page
  145.      *
  146.      * @return false if not the Akismet page
  147.      */
  148.     public static function admin_help() {
  149.         $current_screen = get_current_screen();
  150.  
  151.         // Screen Content
  152.         if ( current_user_can( 'manage_options' ) ) {
  153.             if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) ) {
  154.                 //setup page
  155.                 $current_screen->add_help_tab(
  156.                     array(
  157.                         'id'        => 'overview',
  158.                         'title'        => __( 'Overview' , 'akismet'),
  159.                         'content'    =>
  160.                             '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
  161.                             '<p>' . esc_html__( 'Akismet filters out spam, so you can focus on more important things.' , 'akismet') . '</p>' .
  162.                             '<p>' . esc_html__( 'On this page, you are able to set up the Akismet plugin.' , 'akismet') . '</p>',
  163.                     )
  164.                 );
  165.  
  166.                 $current_screen->add_help_tab(
  167.                     array(
  168.                         'id'        => 'setup-signup',
  169.                         'title'        => __( 'New to Akismet' , 'akismet'),
  170.                         'content'    =>
  171.                             '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
  172.                             '<p>' . esc_html__( 'You need to enter an API key to activate the Akismet service on your site.' , 'akismet') . '</p>' .
  173.                             '<p>' . sprintf( __( 'Sign up for an account on %s to get an API Key.' , 'akismet'), '<a href="https://akismet.com/plugin-signup/" target="_blank">Akismet.com</a>' ) . '</p>',
  174.                     )
  175.                 );
  176.  
  177.                 $current_screen->add_help_tab(
  178.                     array(
  179.                         'id'        => 'setup-manual',
  180.                         'title'        => __( 'Enter an API Key' , 'akismet'),
  181.                         'content'    =>
  182.                             '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
  183.                             '<p>' . esc_html__( 'If you already have an API key' , 'akismet') . '</p>' .
  184.                             '<ol>' .
  185.                                 '<li>' . esc_html__( 'Copy and paste the API key into the text field.' , 'akismet') . '</li>' .
  186.                                 '<li>' . esc_html__( 'Click the Use this Key button.' , 'akismet') . '</li>' .
  187.                             '</ol>',
  188.                     )
  189.                 );
  190.             }
  191.             elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' ) {
  192.                 //stats page
  193.                 $current_screen->add_help_tab(
  194.                     array(
  195.                         'id'        => 'overview',
  196.                         'title'        => __( 'Overview' , 'akismet'),
  197.                         'content'    =>
  198.                             '<p><strong>' . esc_html__( 'Akismet Stats' , 'akismet') . '</strong></p>' .
  199.                             '<p>' . esc_html__( 'Akismet filters out spam, so you can focus on more important things.' , 'akismet') . '</p>' .
  200.                             '<p>' . esc_html__( 'On this page, you are able to view stats on spam filtered on your site.' , 'akismet') . '</p>',
  201.                     )
  202.                 );
  203.             }
  204.             else {
  205.                 //configuration page
  206.                 $current_screen->add_help_tab(
  207.                     array(
  208.                         'id'        => 'overview',
  209.                         'title'        => __( 'Overview' , 'akismet'),
  210.                         'content'    =>
  211.                             '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
  212.                             '<p>' . esc_html__( 'Akismet filters out spam, so you can focus on more important things.' , 'akismet') . '</p>' .
  213.                             '<p>' . esc_html__( 'On this page, you are able to update your Akismet settings and view spam stats.' , 'akismet') . '</p>',
  214.                     )
  215.                 );
  216.  
  217.                 $current_screen->add_help_tab(
  218.                     array(
  219.                         'id'        => 'settings',
  220.                         'title'        => __( 'Settings' , 'akismet'),
  221.                         'content'    =>
  222.                             '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
  223.                             ( Akismet::predefined_api_key() ? '' : '<p><strong>' . esc_html__( 'API Key' , 'akismet') . '</strong> - ' . esc_html__( 'Enter/remove an API key.' , 'akismet') . '</p>' ) .
  224.                             '<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>' .
  225.                             '<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>',
  226.                     )
  227.                 );
  228.  
  229.                 if ( ! Akismet::predefined_api_key() ) {
  230.                     $current_screen->add_help_tab(
  231.                         array(
  232.                             'id'        => 'account',
  233.                             'title'        => __( 'Account' , 'akismet'),
  234.                             'content'    =>
  235.                                 '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
  236.                                 '<p><strong>' . esc_html__( 'Subscription Type' , 'akismet') . '</strong> - ' . esc_html__( 'The Akismet subscription plan' , 'akismet') . '</p>' .
  237.                                 '<p><strong>' . esc_html__( 'Status' , 'akismet') . '</strong> - ' . esc_html__( 'The subscription status - active, cancelled or suspended' , 'akismet') . '</p>',
  238.                         )
  239.                     );
  240.                 }
  241.             }
  242.         }
  243.  
  244.         // Help Sidebar
  245.         $current_screen->set_help_sidebar(
  246.             '<p><strong>' . esc_html__( 'For more information:' , 'akismet') . '</strong></p>' .
  247.             '<p><a href="https://akismet.com/faq/" target="_blank">'     . esc_html__( 'Akismet FAQ' , 'akismet') . '</a></p>' .
  248.             '<p><a href="https://akismet.com/support/" target="_blank">' . esc_html__( 'Akismet Support' , 'akismet') . '</a></p>'
  249.         );
  250.     }
  251.  
  252.     public static function enter_api_key() {
  253.         if ( ! current_user_can( 'manage_options' ) ) {
  254.             die( __( 'Cheatin’ uh?', 'akismet' ) );
  255.         }
  256.  
  257.         if ( !wp_verify_nonce( $_POST['_wpnonce'], self::NONCE ) )
  258.             return false;
  259.  
  260.         foreach( array( 'akismet_strictness', 'akismet_show_user_comments_approved' ) as $option ) {
  261.             update_option( $option, isset( $_POST[$option] ) && (int) $_POST[$option] == 1 ? '1' : '0' );
  262.         }
  263.         
  264.         if ( Akismet::predefined_api_key() ) {
  265.             return false; //shouldn't have option to save key if already defined
  266.         }
  267.         
  268.         $new_key = preg_replace( '/[^a-f0-9]/i', '', $_POST['key'] );
  269.         $old_key = Akismet::get_api_key();
  270.  
  271.         if ( empty( $new_key ) ) {
  272.             if ( !empty( $old_key ) ) {
  273.                 delete_option( 'wordpress_api_key' );
  274.                 self::$notices[] = 'new-key-empty';
  275.             }
  276.         }
  277.         elseif ( $new_key != $old_key ) {
  278.             self::save_key( $new_key );
  279.         }
  280.  
  281.         return true;
  282.     }
  283.  
  284.     public static function save_key( $api_key ) {
  285.         $key_status = Akismet::verify_key( $api_key );
  286.  
  287.         if ( $key_status == 'valid' ) {
  288.             $akismet_user = self::get_akismet_user( $api_key );
  289.             
  290.             if ( $akismet_user ) {                
  291.                 if ( in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) ) )
  292.                     update_option( 'wordpress_api_key', $api_key );
  293.                 
  294.                 if ( $akismet_user->status == 'active' )
  295.                     self::$notices['status'] = 'new-key-valid';
  296.                 elseif ( $akismet_user->status == 'notice' )
  297.                     self::$notices['status'] = $akismet_user;
  298.                 else
  299.                     self::$notices['status'] = $akismet_user->status;
  300.             }
  301.             else
  302.                 self::$notices['status'] = 'new-key-invalid';
  303.         }
  304.         elseif ( in_array( $key_status, array( 'invalid', 'failed' ) ) )
  305.             self::$notices['status'] = 'new-key-'.$key_status;
  306.     }
  307.  
  308.     public static function dashboard_stats() {
  309.         if ( did_action( 'rightnow_end' ) ) {
  310.             return; // We already displayed this info in the "Right Now" section
  311.         }
  312.  
  313.         if ( !$count = get_option('akismet_spam_count') )
  314.             return;
  315.  
  316.         global $submenu;
  317.  
  318.         echo '<h3>' . esc_html( _x( 'Spam', 'comments' , 'akismet') ) . '</h3>';
  319.  
  320.         echo '<p>'.sprintf( _n(
  321.                 '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comment</a>.',
  322.                 '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.',
  323.                 $count
  324.             , '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>';
  325.     }
  326.  
  327.     // WP 2.5+
  328.     public static function rightnow_stats() {
  329.         if ( $count = get_option('akismet_spam_count') ) {
  330.             $intro = sprintf( _n(
  331.                 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
  332.                 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
  333.                 $count
  334.             , 'akismet'), 'https://akismet.com/wordpress/', number_format_i18n( $count ) );
  335.         } else {
  336.             $intro = sprintf( __('<a href="%s">Akismet</a> blocks spam from getting to your blog. ', 'akismet'), 'https://akismet.com/wordpress/' );
  337.         }
  338.  
  339.         $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
  340.  
  341.         if ( $queue_count = self::get_spam_count() ) {
  342.             $queue_text = sprintf( _n(
  343.                 'There’s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
  344.                 'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
  345.                 $queue_count
  346.             , 'akismet'), number_format_i18n( $queue_count ), esc_url( $link ) );
  347.         } else {
  348.             $queue_text = sprintf( __( "There’s nothing in your <a href='%s'>spam queue</a> at the moment." , 'akismet'), esc_url( $link ) );
  349.         }
  350.  
  351.         $text = $intro . '<br />' . $queue_text;
  352.         echo "<p class='akismet-right-now'>$text</p>\n";
  353.     }
  354.  
  355.     public static function check_for_spam_button( $comment_status ) {
  356.         // The "Check for Spam" button should only appear when the page might be showing
  357.         // a comment with comment_approved=0, which means an un-trashed, un-spammed,
  358.         // not-yet-moderated comment.
  359.         if ( 'all' != $comment_status && 'moderated' != $comment_status ) {
  360.             return;
  361.         }
  362.  
  363.         $link = add_query_arg( array( 'action' => 'akismet_recheck_queue' ), admin_url( 'admin.php' ) );
  364.  
  365.         $comments_count = wp_count_comments();
  366.         
  367.         echo '</div>';
  368.         echo '<div class="alignleft">';
  369.         echo '<a
  370.                 class="button-secondary checkforspam"
  371.                 href="' . esc_url( $link ) . '"
  372.                 data-active-label="' . esc_attr( __( 'Checking for Spam', 'akismet' ) ) . '"
  373.                 data-progress-label-format="' . esc_attr( __( '(%1$s%)', 'akismet' ) ) . '"
  374.                 data-success-url="' . esc_attr( remove_query_arg( 'akismet_recheck', add_query_arg( array( 'akismet_recheck_complete' => 1, 'recheck_count' => urlencode( '__recheck_count__' ), 'spam_count' => urlencode( '__spam_count__' ) ) ) ) ) . '"
  375.                 data-pending-comment-count="' . esc_attr( $comments_count->moderated ) . '"
  376.                 >';
  377.             echo '<span class="akismet-label">' . esc_html__('Check for Spam', 'akismet') . '</span>';
  378.             echo '<span class="checkforspam-progress"></span>';
  379.         echo '</a>';
  380.         echo '<span class="checkforspam-spinner"></span>';
  381.  
  382.     }
  383.  
  384.     public static function recheck_queue() {
  385.         global $wpdb;
  386.  
  387.         Akismet::fix_scheduled_recheck();
  388.  
  389.         if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) ) {
  390.             return;
  391.         }
  392.  
  393.         $result_counts = self::recheck_queue_portion( empty( $_POST['offset'] ) ? 0 : $_POST['offset'], empty( $_POST['limit'] ) ? 100 : $_POST['limit'] );
  394.  
  395.         if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  396.             wp_send_json( array(
  397.                 'counts' => $result_counts,
  398.             ));
  399.         }
  400.         else {
  401.             $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url( 'edit-comments.php' );
  402.             wp_safe_redirect( $redirect_to );
  403.             exit;
  404.         }
  405.     }
  406.     
  407.     public static function recheck_queue_portion( $start = 0, $limit = 100 ) {
  408.         global $wpdb;
  409.         
  410.         $paginate = '';
  411.  
  412.         if ( $limit <= 0 ) {
  413.             $limit = 100;
  414.         }
  415.  
  416.         if ( $start < 0 ) {
  417.             $start = 0;
  418.         }
  419.  
  420.         $moderation = $wpdb->get_col( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_approved = '0' LIMIT %d OFFSET %d", $limit, $start ) );
  421.  
  422.         $result_counts = array(
  423.             'processed' => count( $moderation ),
  424.             'spam' => 0,
  425.             'ham' => 0,
  426.             'error' => 0,
  427.         );
  428.  
  429.         foreach ( $moderation as $comment_id ) {
  430.             $api_response = Akismet::recheck_comment( $comment_id, 'recheck_queue' );
  431.  
  432.             if ( 'true' === $api_response ) {
  433.                 ++$result_counts['spam'];
  434.             }
  435.             elseif ( 'false' === $api_response ) {
  436.                 ++$result_counts['ham'];
  437.             }
  438.             else {
  439.                 ++$result_counts['error'];
  440.             }
  441.         }
  442.  
  443.         return $result_counts;
  444.     }
  445.  
  446.     // Adds an 'x' link next to author URLs, clicking will remove the author URL and show an undo link
  447.     public static function remove_comment_author_url() {
  448.         if ( !empty( $_POST['id'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
  449.             $comment_id = intval( $_POST['id'] );
  450.             $comment = get_comment( $comment_id, ARRAY_A );
  451.             if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
  452.                 $comment['comment_author_url'] = '';
  453.                 do_action( 'comment_remove_author_url' );
  454.                 print( wp_update_comment( $comment ) );
  455.                 die();
  456.             }
  457.         }
  458.     }
  459.  
  460.     public static function add_comment_author_url() {
  461.         if ( !empty( $_POST['id'] ) && !empty( $_POST['url'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
  462.             $comment_id = intval( $_POST['id'] );
  463.             $comment = get_comment( $comment_id, ARRAY_A );
  464.             if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
  465.                 $comment['comment_author_url'] = esc_url( $_POST['url'] );
  466.                 do_action( 'comment_add_author_url' );
  467.                 print( wp_update_comment( $comment ) );
  468.                 die();
  469.             }
  470.         }
  471.     }
  472.  
  473.     public static function comment_row_action( $a, $comment ) {
  474.         $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
  475.         $akismet_error  = get_comment_meta( $comment->comment_ID, 'akismet_error', true );
  476.         $user_result    = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
  477.         $comment_status = wp_get_comment_status( $comment->comment_ID );
  478.         $desc = null;
  479.         if ( $akismet_error ) {
  480.             $desc = __( 'Awaiting spam check' , 'akismet');
  481.         } elseif ( !$user_result || $user_result == $akismet_result ) {
  482.             // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
  483.             if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
  484.                 $desc = __( 'Flagged as spam by Akismet' , 'akismet');
  485.             elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
  486.                 $desc = __( 'Cleared by Akismet' , 'akismet');
  487.         } else {
  488.             $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
  489.             if ( $user_result == 'true' )
  490.                 $desc = sprintf( __('Flagged as spam by %s', 'akismet'), $who );
  491.             else
  492.                 $desc = sprintf( __('Un-spammed by %s', 'akismet'), $who );
  493.         }
  494.  
  495.         // add a History item to the hover links, just after Edit
  496.         if ( $akismet_result ) {
  497.             $b = array();
  498.             foreach ( $a as $k => $item ) {
  499.                 $b[ $k ] = $item;
  500.                 if (
  501.                     $k == 'edit'
  502.                     || $k == 'unspam'
  503.                 ) {
  504.                     $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>';
  505.                 }
  506.             }
  507.  
  508.             $a = $b;
  509.         }
  510.  
  511.         if ( $desc )
  512.             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>';
  513.  
  514.         $show_user_comments_option = get_option( 'akismet_show_user_comments_approved' );
  515.         
  516.         if ( $show_user_comments_option === false ) {
  517.             // Default to active if the user hasn't made a decision.
  518.             $show_user_comments_option = '1';
  519.         }
  520.         
  521.         $show_user_comments = apply_filters( 'akismet_show_user_comments_approved', $show_user_comments_option );
  522.         $show_user_comments = $show_user_comments === 'false' ? false : $show_user_comments; //option used to be saved as 'false' / 'true'
  523.         
  524.         if ( $show_user_comments ) {
  525.             $comment_count = Akismet::get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
  526.             $comment_count = intval( $comment_count );
  527.             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>';
  528.         }
  529.  
  530.         return $a;
  531.     }
  532.  
  533.     public static function comment_status_meta_box( $comment ) {
  534.         $history = Akismet::get_comment_history( $comment->comment_ID );
  535.  
  536.         if ( $history ) {
  537.             echo '<div class="akismet-history" style="margin: 13px;">';
  538.  
  539.             foreach ( $history as $row ) {
  540.                 $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
  541.                 
  542.                 $message = '';
  543.                 
  544.                 if ( ! empty( $row['message'] ) ) {
  545.                     // Old versions of Akismet stored the message as a literal string in the commentmeta.
  546.                     // New versions don't do that for two reasons:
  547.                     // 1) Save space.
  548.                     // 2) The message can be translated into the current language of the blog, not stuck 
  549.                     //    in the language of the blog when the comment was made.
  550.                     $message = $row['message'];
  551.                 }
  552.                 
  553.                 // If possible, use a current translation.
  554.                 switch ( $row['event'] ) {
  555.                     case 'recheck-spam';
  556.                         $message = __( 'Akismet re-checked and caught this comment as spam.', 'akismet' );
  557.                     break;
  558.                     case 'check-spam':
  559.                         $message = __( 'Akismet caught this comment as spam.', 'akismet' );
  560.                     break;
  561.                     case 'recheck-ham':
  562.                         $message = __( 'Akismet re-checked and cleared this comment.', 'akismet' );
  563.                     break;
  564.                     case 'check-ham':
  565.                         $message = __( 'Akismet cleared this comment.', 'akismet' );
  566.                     break;
  567.                     case 'wp-blacklisted':
  568.                         $message = __( 'Comment was caught by wp_blacklist_check.', 'akismet' );
  569.                     break;
  570.                     case 'report-spam':
  571.                         if ( isset( $row['user'] ) ) {
  572.                             $message = sprintf( __( '%s reported this comment as spam.', 'akismet' ), $row['user'] );
  573.                         }
  574.                         else if ( ! $message ) {
  575.                             $message = __( 'This comment was reported as spam.', 'akismet' );
  576.                         }
  577.                     break;
  578.                     case 'report-ham':
  579.                         if ( isset( $row['user'] ) ) {
  580.                             $message = sprintf( __( '%s reported this comment as not spam.', 'akismet' ), $row['user'] );
  581.                         }
  582.                         else if ( ! $message ) {
  583.                             $message = __( 'This comment was reported as not spam.', 'akismet' );
  584.                         }
  585.                     break;
  586.                     case 'cron-retry-spam':
  587.                         $message = __( 'Akismet caught this comment as spam during an automatic retry.' , 'akismet');
  588.                     break;
  589.                     case 'cron-retry-ham':
  590.                         $message = __( 'Akismet cleared this comment during an automatic retry.', 'akismet');
  591.                     break;
  592.                     case 'check-error':
  593.                         if ( isset( $row['meta'], $row['meta']['response'] ) ) {
  594.                             $message = sprintf( __( 'Akismet was unable to check this comment (response: %s) but will automatically retry later.', 'akismet'), $row['meta']['response'] );
  595.                         }
  596.                     break;
  597.                     case 'recheck-error':
  598.                         if ( isset( $row['meta'], $row['meta']['response'] ) ) {
  599.                             $message = sprintf( __( 'Akismet was unable to recheck this comment (response: %s).', 'akismet'), $row['meta']['response'] );
  600.                         }
  601.                     break;
  602.                     default:
  603.                         if ( preg_match( '/^status-changed/', $row['event'] ) ) {
  604.                             // Half of these used to be saved without the dash after 'status-changed'.
  605.                             // See https://plugins.trac.wordpress.org/changeset/1150658/akismet/trunk
  606.                             $new_status = preg_replace( '/^status-changed-?/', '', $row['event'] );
  607.                             $message = sprintf( __( 'Comment status was changed to %s', 'akismet' ), $new_status );
  608.                         }
  609.                         else if ( preg_match( '/^status-/', $row['event'] ) ) {
  610.                             $new_status = preg_replace( '/^status-/', '', $row['event'] );
  611.  
  612.                             if ( isset( $row['user'] ) ) {
  613.                                 $message = sprintf( __( '%1$s changed the comment status to %2$s.', 'akismet' ), $row['user'], $new_status );
  614.                             }
  615.                         }
  616.                     break;
  617.                     
  618.                 }
  619.  
  620.                 echo '<div style="margin-bottom: 13px;">';
  621.                     echo '<span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( esc_html__('%s ago', 'akismet'), human_time_diff( $row['time'] ) ) . '</span>';
  622.                     echo ' - ';
  623.                     echo esc_html( $message );
  624.                 echo '</div>';
  625.             }
  626.  
  627.             echo '</div>';
  628.         }
  629.     }
  630.  
  631.     public static function plugin_action_links( $links, $file ) {
  632.         if ( $file == plugin_basename( plugin_dir_url( __FILE__ ) . '/akismet.php' ) ) {
  633.             $links[] = '<a href="' . esc_url( self::get_page_url() ) . '">'.esc_html__( 'Settings' , 'akismet').'</a>';
  634.         }
  635.  
  636.         return $links;
  637.     }
  638.  
  639.     // Total spam in queue
  640.     // get_option( 'akismet_spam_count' ) is the total caught ever
  641.     public static function get_spam_count( $type = false ) {
  642.         global $wpdb;
  643.  
  644.         if ( !$type ) { // total
  645.             $count = wp_cache_get( 'akismet_spam_count', 'widget' );
  646.             if ( false === $count ) {
  647.                 $count = wp_count_comments();
  648.                 $count = $count->spam;
  649.                 wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
  650.             }
  651.             return $count;
  652.         } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
  653.             $type = '';
  654.         }
  655.  
  656.         return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type = %s", $type ) );
  657.     }
  658.  
  659.     // Check connectivity between the WordPress blog and Akismet's servers.
  660.     // 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).
  661.     public static function check_server_ip_connectivity() {
  662.         
  663.         $servers = $ips = array();
  664.  
  665.         // Some web hosts may disable this function
  666.         if ( function_exists('gethostbynamel') ) {    
  667.             
  668.             $ips = gethostbynamel( 'rest.akismet.com' );
  669.             if ( $ips && is_array($ips) && count($ips) ) {
  670.                 $api_key = Akismet::get_api_key();
  671.                 
  672.                 foreach ( $ips as $ip ) {
  673.                     $response = Akismet::verify_key( $api_key, $ip );
  674.                     // even if the key is invalid, at least we know we have connectivity
  675.                     if ( $response == 'valid' || $response == 'invalid' )
  676.                         $servers[$ip] = 'connected';
  677.                     else
  678.                         $servers[$ip] = $response ? $response : 'unable to connect';
  679.                 }
  680.             }
  681.         }
  682.         
  683.         return $servers;
  684.     }
  685.     
  686.     // Simpler connectivity check
  687.     public static function check_server_connectivity($cache_timeout = 86400) {
  688.         
  689.         $debug = array();
  690.         $debug[ 'PHP_VERSION' ]         = PHP_VERSION;
  691.         $debug[ 'WORDPRESS_VERSION' ]   = $GLOBALS['wp_version'];
  692.         $debug[ 'AKISMET_VERSION' ]     = AKISMET_VERSION;
  693.         $debug[ 'AKISMET__PLUGIN_DIR' ] = AKISMET__PLUGIN_DIR;
  694.         $debug[ 'SITE_URL' ]            = site_url();
  695.         $debug[ 'HOME_URL' ]            = home_url();
  696.         
  697.         $servers = get_option('akismet_available_servers');
  698.         if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false ) {
  699.             $servers = self::check_server_ip_connectivity();
  700.             update_option('akismet_available_servers', $servers);
  701.             update_option('akismet_connectivity_time', time());
  702.         }
  703.  
  704.         if ( wp_http_supports( array( 'ssl' ) ) ) {
  705.             $response = wp_remote_get( 'https://rest.akismet.com/1.1/test' );
  706.         }
  707.         else {
  708.             $response = wp_remote_get( 'http://rest.akismet.com/1.1/test' );
  709.         }
  710.  
  711.         $debug[ 'gethostbynamel' ]  = function_exists('gethostbynamel') ? 'exists' : 'not here';
  712.         $debug[ 'Servers' ]         = $servers;
  713.         $debug[ 'Test Connection' ] = $response;
  714.         
  715.         Akismet::log( $debug );
  716.         
  717.         if ( $response && 'connected' == wp_remote_retrieve_body( $response ) )
  718.             return true;
  719.         
  720.         return false;
  721.     }
  722.  
  723.     // Check the server connectivity and store the available servers in an option. 
  724.     public static function get_server_connectivity($cache_timeout = 86400) {
  725.         return self::check_server_connectivity( $cache_timeout );
  726.     }
  727.  
  728.     /**
  729.      * Find out whether any comments in the Pending queue have not yet been checked by Akismet.
  730.      *
  731.      * @return bool
  732.      */
  733.     public static function are_any_comments_waiting_to_be_checked() {
  734.         return !! get_comments( array(
  735.             // Exclude comments that are not pending. This would happen if someone manually approved or spammed a comment
  736.             // that was waiting to be checked. The akismet_error meta entry will eventually be removed by the cron recheck job.
  737.             'status' => 'hold',
  738.             
  739.             // This is the commentmeta that is saved when a comment couldn't be checked.
  740.             'meta_key' => 'akismet_error',
  741.             
  742.             // We only need to know whether at least one comment is waiting for a check.
  743.             'number' => 1,
  744.         ) );
  745.     }
  746.  
  747.     public static function get_page_url( $page = 'config' ) {
  748.  
  749.         $args = array( 'page' => 'akismet-key-config' );
  750.  
  751.         if ( $page == 'stats' )
  752.             $args = array( 'page' => 'akismet-key-config', 'view' => 'stats' );
  753.         elseif ( $page == 'delete_key' )
  754.             $args = array( 'page' => 'akismet-key-config', 'view' => 'start', 'action' => 'delete-key', '_wpnonce' => wp_create_nonce( self::NONCE ) );
  755.  
  756.         $url = add_query_arg( $args, class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'options-general.php' ) );
  757.  
  758.         return $url;
  759.     }
  760.     
  761.     public static function get_akismet_user( $api_key ) {
  762.         $akismet_user = false;
  763.  
  764.         $subscription_verification = Akismet::http_post( Akismet::build_query( array( 'key' => $api_key, 'blog' => get_option( 'home' ) ) ), 'get-subscription' );
  765.  
  766.         if ( ! empty( $subscription_verification[1] ) ) {
  767.             if ( 'invalid' !== $subscription_verification[1] ) {
  768.                 $akismet_user = json_decode( $subscription_verification[1] );
  769.             }
  770.         }
  771.  
  772.         return $akismet_user;
  773.     }
  774.     
  775.     public static function get_stats( $api_key ) {
  776.         $stat_totals = array();
  777.  
  778.         foreach( array( '6-months', 'all' ) as $interval ) {
  779.             $response = Akismet::http_post( Akismet::build_query( array( 'blog' => get_option( 'home' ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
  780.  
  781.             if ( ! empty( $response[1] ) ) {
  782.                 $stat_totals[$interval] = json_decode( $response[1] );
  783.             }
  784.         }
  785.  
  786.         return $stat_totals;
  787.     }
  788.     
  789.     public static function verify_wpcom_key( $api_key, $user_id, $extra = array() ) {
  790.         $akismet_account = Akismet::http_post( Akismet::build_query( array_merge( array(
  791.             'user_id'          => $user_id,
  792.             'api_key'          => $api_key,
  793.             'get_account_type' => 'true'
  794.         ), $extra ) ), 'verify-wpcom-key' );
  795.  
  796.         if ( ! empty( $akismet_account[1] ) )
  797.             $akismet_account = json_decode( $akismet_account[1] );
  798.  
  799.         Akismet::log( compact( 'akismet_account' ) );
  800.         
  801.         return $akismet_account;
  802.     }
  803.     
  804.     public static function connect_jetpack_user() {
  805.     
  806.         if ( $jetpack_user = self::get_jetpack_user() ) {
  807.             if ( isset( $jetpack_user['user_id'] ) && isset(  $jetpack_user['api_key'] ) ) {
  808.                 $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'], array( 'action' => 'connect_jetpack_user' ) );
  809.                             
  810.                 if ( is_object( $akismet_user ) ) {
  811.                     self::save_key( $akismet_user->api_key );
  812.                     return in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) );
  813.                 }
  814.             }
  815.         }
  816.         
  817.         return false;
  818.     }
  819.  
  820.     public static function display_alert() {
  821.         Akismet::view( 'notice', array(
  822.             'type' => 'alert',
  823.             'code' => (int) get_option( 'akismet_alert_code' ),
  824.             'msg'  => get_option( 'akismet_alert_msg' )
  825.         ) );
  826.     }
  827.  
  828.     public static function display_spam_check_warning() {
  829.         Akismet::fix_scheduled_recheck();
  830.  
  831.         if ( wp_next_scheduled('akismet_schedule_cron_recheck') > time() && self::are_any_comments_waiting_to_be_checked() ) {
  832.             $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() ) ) );
  833.             Akismet::view( 'notice', array( 'type' => 'spam-check', 'link_text' => $link_text ) );
  834.         }
  835.     }
  836.  
  837.     public static function display_api_key_warning() {
  838.         Akismet::view( 'notice', array( 'type' => 'plugin' ) );
  839.     }
  840.  
  841.     public static function display_page() {
  842.         if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) )
  843.             self::display_start_page();
  844.         elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' )
  845.             self::display_stats_page();
  846.         else
  847.             self::display_configuration_page();
  848.     }
  849.  
  850.     public static function display_start_page() {
  851.         if ( isset( $_GET['action'] ) ) {
  852.             if ( $_GET['action'] == 'delete-key' ) {
  853.                 if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], self::NONCE ) )
  854.                     delete_option( 'wordpress_api_key' );
  855.             }
  856.         }
  857.  
  858.         if ( $api_key = Akismet::get_api_key() && ( empty( self::$notices['status'] ) || 'existing-key-invalid' != self::$notices['status'] ) ) {
  859.             self::display_configuration_page();
  860.             return;
  861.         }
  862.         
  863.         //the user can choose to auto connect their API key by clicking a button on the akismet done page
  864.         //if jetpack, get verified api key by using connected wpcom user id
  865.         //if no jetpack, get verified api key by using an akismet token    
  866.         
  867.         $akismet_user = false;
  868.         
  869.         if ( isset( $_GET['token'] ) && preg_match('/^(\d+)-[0-9a-f]{20}$/', $_GET['token'] ) )
  870.             $akismet_user = self::verify_wpcom_key( '', '', array( 'token' => $_GET['token'] ) );
  871.         elseif ( $jetpack_user = self::get_jetpack_user() )
  872.             $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'] );
  873.             
  874.         if ( isset( $_GET['action'] ) ) {
  875.             if ( $_GET['action'] == 'save-key' ) {
  876.                 if ( is_object( $akismet_user ) ) {
  877.                     self::save_key( $akismet_user->api_key );
  878.                     self::display_configuration_page();
  879.                     return;
  880.                 }
  881.             }
  882.         }
  883.  
  884.         Akismet::view( 'start', compact( 'akismet_user' ) );
  885.  
  886.         /*
  887.         // To see all variants when testing.
  888.         $akismet_user->status = 'no-sub';
  889.         Akismet::view( 'start', compact( 'akismet_user' ) );
  890.         $akismet_user->status = 'cancelled';
  891.         Akismet::view( 'start', compact( 'akismet_user' ) );
  892.         $akismet_user->status = 'suspended';
  893.         Akismet::view( 'start', compact( 'akismet_user' ) );
  894.         $akismet_user->status = 'other';
  895.         Akismet::view( 'start', compact( 'akismet_user' ) );
  896.         $akismet_user = false;
  897.         */
  898.     }
  899.  
  900.     public static function display_stats_page() {
  901.         Akismet::view( 'stats' );
  902.     }
  903.  
  904.     public static function display_configuration_page() {
  905.         $api_key      = Akismet::get_api_key();
  906.         $akismet_user = self::get_akismet_user( $api_key );
  907.         
  908.         if ( ! $akismet_user ) {
  909.             // This could happen if the user's key became invalid after it was previously valid and successfully set up.
  910.             self::$notices['status'] = 'existing-key-invalid';
  911.             self::display_start_page();
  912.             return;
  913.         }
  914.  
  915.         $stat_totals  = self::get_stats( $api_key );
  916.  
  917.         // If unset, create the new strictness option using the old discard option to determine its default.
  918.         // If the old option wasn't set, default to discarding the blatant spam.
  919.         if ( get_option( 'akismet_strictness' ) === false ) {
  920.             add_option( 'akismet_strictness', ( get_option( 'akismet_discard_month' ) === 'false' ? '0' : '1' ) );
  921.         }
  922.         
  923.         // Sync the local "Total spam blocked" count with the authoritative count from the server.
  924.         if ( isset( $stat_totals['all'], $stat_totals['all']->spam ) ) {
  925.             update_option( 'akismet_spam_count', $stat_totals['all']->spam );
  926.         }
  927.  
  928.         $notices = array();
  929.  
  930.         if ( empty( self::$notices ) ) {
  931.             if ( ! empty( $stat_totals['all'] ) && isset( $stat_totals['all']->time_saved ) && $akismet_user->status == 'active' && $akismet_user->account_type == 'free-api-key' ) {
  932.  
  933.                 $time_saved = false;
  934.  
  935.                 if ( $stat_totals['all']->time_saved > 1800 ) {
  936.                     $total_in_minutes = round( $stat_totals['all']->time_saved / 60 );
  937.                     $total_in_hours   = round( $total_in_minutes / 60 );
  938.                     $total_in_days    = round( $total_in_hours / 8 );
  939.                     $cleaning_up      = __( 'Cleaning up spam takes time.' , 'akismet');
  940.  
  941.                     if ( $total_in_days > 1 )
  942.                         $time_saved = $cleaning_up . ' ' . sprintf( _n( 'Akismet has saved you %s day!', 'Akismet has saved you %s days!', $total_in_days, 'akismet' ), number_format_i18n( $total_in_days ) );
  943.                     elseif ( $total_in_hours > 1 )
  944.                         $time_saved = $cleaning_up . ' ' . sprintf( _n( 'Akismet has saved you %d hour!', 'Akismet has saved you %d hours!', $total_in_hours, 'akismet' ), $total_in_hours );
  945.                     elseif ( $total_in_minutes >= 30 )
  946.                         $time_saved = $cleaning_up . ' ' . sprintf( _n( 'Akismet has saved you %d minute!', 'Akismet has saved you %d minutes!', $total_in_minutes, 'akismet' ), $total_in_minutes );
  947.                 }
  948.                 
  949.                 $notices[] =  array( 'type' => 'active-notice', 'time_saved' => $time_saved );
  950.             }
  951.             
  952.             if ( !empty( $akismet_user->limit_reached ) && in_array( $akismet_user->limit_reached, array( 'yellow', 'red' ) ) ) {
  953.                 $notices[] = array( 'type' => 'limit-reached', 'level' => $akismet_user->limit_reached );
  954.             }
  955.         }
  956.         
  957.         if ( !isset( self::$notices['status'] ) && in_array( $akismet_user->status, array( 'cancelled', 'suspended', 'missing', 'no-sub' ) ) ) {
  958.             $notices[] = array( 'type' => $akismet_user->status );
  959.         }
  960.  
  961.         /*
  962.         // To see all variants when testing.
  963.         $notices[] = array( 'type' => 'active-notice', 'time_saved' => 'Cleaning up spam takes time. Akismet has saved you 1 minute!' );
  964.         $notices[] = array( 'type' => 'plugin' );
  965.         $notices[] = array( 'type' => 'spam-check', 'link_text' => 'Link text.' );
  966.         $notices[] = array( 'type' => 'notice', 'notice_header' => 'This is the notice header.', 'notice_text' => 'This is the notice text.' );
  967.         $notices[] = array( 'type' => 'missing-functions' );
  968.         $notices[] = array( 'type' => 'servers-be-down' );
  969.         $notices[] = array( 'type' => 'active-dunning' );
  970.         $notices[] = array( 'type' => 'cancelled' );
  971.         $notices[] = array( 'type' => 'suspended' );
  972.         $notices[] = array( 'type' => 'missing' );
  973.         $notices[] = array( 'type' => 'no-sub' );
  974.         $notices[] = array( 'type' => 'new-key-valid' );
  975.         $notices[] = array( 'type' => 'new-key-invalid' );
  976.         $notices[] = array( 'type' => 'existing-key-invalid' );
  977.         $notices[] = array( 'type' => 'new-key-failed' );
  978.         $notices[] = array( 'type' => 'limit-reached', 'level' => 'yellow' );
  979.         $notices[] = array( 'type' => 'limit-reached', 'level' => 'red' );
  980.         */
  981.         
  982.         Akismet::log( compact( 'stat_totals', 'akismet_user' ) );
  983.         Akismet::view( 'config', compact( 'api_key', 'akismet_user', 'stat_totals', 'notices' ) );
  984.     }
  985.  
  986.     public static function display_notice() {
  987.         global $hook_suffix;
  988.  
  989.         if ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config' ) ) ) {
  990.             // This page manages the notices and puts them inline where they make sense.
  991.             return;
  992.         }
  993.  
  994.         if ( in_array( $hook_suffix, array( 'edit-comments.php' ) ) && (int) get_option( 'akismet_alert_code' ) > 0 ) {
  995.             Akismet::verify_key( Akismet::get_api_key() ); //verify that the key is still in alert state
  996.             
  997.             if ( get_option( 'akismet_alert_code' ) > 0 )
  998.                 self::display_alert();
  999.         }
  1000.         elseif ( $hook_suffix == 'plugins.php' && !Akismet::get_api_key() ) {
  1001.             self::display_api_key_warning();
  1002.         }
  1003.         elseif ( $hook_suffix == 'edit-comments.php' && wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
  1004.             self::display_spam_check_warning();
  1005.         }
  1006.         else if ( isset( $_GET['akismet_recheck_complete'] ) ) {
  1007.             $recheck_count = (int) $_GET['recheck_count'];
  1008.             $spam_count = (int) $_GET['spam_count'];
  1009.             
  1010.             if ( $recheck_count === 0 ) {
  1011.                 $message = __( 'There were no comments to check. Akismet will only check comments in the Pending queue.', 'akismet' );
  1012.             }
  1013.             else {
  1014.                 $message = sprintf( _n( 'Akismet checked %s comment.', 'Akismet checked %s comments.', $recheck_count, 'akismet' ), number_format( $recheck_count ) );
  1015.                 $message .= ' ';
  1016.             
  1017.                 if ( $spam_count === 0 ) {
  1018.                     $message .= __( 'No comments were caught as spam.' );
  1019.                 }
  1020.                 else {
  1021.                     $message .= sprintf( _n( '%s comment was caught as spam.', '%s comments were caught as spam.', $spam_count, 'akismet' ), number_format( $spam_count ) );
  1022.                 }
  1023.             }
  1024.             
  1025.             echo '<div class="notice notice-success"><p>' . esc_html( $message ) . '</p></div>';
  1026.         }
  1027.     }
  1028.  
  1029.     public static function display_status() {
  1030.         if ( ! self::get_server_connectivity() ) {
  1031.             Akismet::view( 'notice', array( 'type' => 'servers-be-down' ) );
  1032.         }
  1033.         else if ( ! empty( self::$notices ) ) {
  1034.             foreach ( self::$notices as $index => $type ) {
  1035.                 if ( is_object( $type ) ) {
  1036.                     $notice_header = $notice_text = '';
  1037.                     
  1038.                     if ( property_exists( $type, 'notice_header' ) ) {
  1039.                         $notice_header = wp_kses( $type->notice_header, self::$allowed );
  1040.                     }
  1041.                 
  1042.                     if ( property_exists( $type, 'notice_text' ) ) {
  1043.                         $notice_text = wp_kses( $type->notice_text, self::$allowed );
  1044.                     }
  1045.                     
  1046.                     if ( property_exists( $type, 'status' ) ) {
  1047.                         $type = wp_kses( $type->status, self::$allowed );
  1048.                         Akismet::view( 'notice', compact( 'type', 'notice_header', 'notice_text' ) );
  1049.                         
  1050.                         unset( self::$notices[ $index ] );
  1051.                     }
  1052.                 }
  1053.                 else {
  1054.                     Akismet::view( 'notice', compact( 'type' ) );
  1055.                     
  1056.                     unset( self::$notices[ $index ] );
  1057.                 }
  1058.             }
  1059.         }
  1060.     }
  1061.  
  1062.     private static function get_jetpack_user() {
  1063.         if ( !class_exists('Jetpack') )
  1064.             return false;
  1065.  
  1066.         Jetpack::load_xml_rpc_client();
  1067.         $xml = new Jetpack_IXR_ClientMulticall( array( 'user_id' => get_current_user_id() ) );
  1068.  
  1069.         $xml->addCall( 'wpcom.getUserID' );
  1070.         $xml->addCall( 'akismet.getAPIKey' );
  1071.         $xml->query();
  1072.  
  1073.         Akismet::log( compact( 'xml' ) );
  1074.  
  1075.         if ( !$xml->isError() ) {
  1076.             $responses = $xml->getResponse();
  1077.             if ( count( $responses ) > 1 ) {
  1078.                 // Due to a quirk in how Jetpack does multi-calls, the response order
  1079.                 // can't be trusted to match the call order. It's a good thing our
  1080.                 // return values can be mostly differentiated from each other.
  1081.                 $first_response_value = array_shift( $responses[0] );
  1082.                 $second_response_value = array_shift( $responses[1] );
  1083.                 
  1084.                 // If WPCOM ever reaches 100 billion users, this will fail. :-)
  1085.                 if ( preg_match( '/^[a-f0-9]{12}$/i', $first_response_value ) ) {
  1086.                     $api_key = $first_response_value;
  1087.                     $user_id = (int) $second_response_value;
  1088.                 }
  1089.                 else {
  1090.                     $api_key = $second_response_value;
  1091.                     $user_id = (int) $first_response_value;
  1092.                 }
  1093.                 
  1094.                 return compact( 'api_key', 'user_id' );
  1095.             }
  1096.         }
  1097.         return false;
  1098.     }
  1099.     
  1100.     /**
  1101.      * Some commentmeta isn't useful in an export file. Suppress it (when supported).
  1102.      *
  1103.      * @param bool $exclude
  1104.      * @param string $key The meta key
  1105.      * @param object $meta The meta object
  1106.      * @return bool Whether to exclude this meta entry from the export.
  1107.      */
  1108.     public static function exclude_commentmeta_from_export( $exclude, $key, $meta ) {
  1109.         if ( in_array( $key, array( 'akismet_as_submitted', 'akismet_rechecking', 'akismet_delayed_moderation_email' ) ) ) {
  1110.             return true;
  1111.         }
  1112.         
  1113.         return $exclude;
  1114.     }
  1115.     
  1116.     /**
  1117.      * When Akismet is active, remove the "Activate Akismet" step from the plugin description.
  1118.      */
  1119.     public static function modify_plugin_description( $all_plugins ) {
  1120.         if ( isset( $all_plugins['akismet/akismet.php'] ) ) {
  1121.             if ( Akismet::get_api_key() ) {
  1122.                 $all_plugins['akismet/akismet.php']['Description'] = __( 'Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from spam</strong>. Your site is fully configured and being protected, even while you sleep.', 'akismet' );
  1123.             }
  1124.             else {
  1125.                 $all_plugins['akismet/akismet.php']['Description'] = __( 'Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from spam</strong>. It keeps your site protected even while you sleep. To get started, just go to <a href="admin.php?page=akismet-key-config">your Akismet Settings page</a> to set up your API key.', 'akismet' );
  1126.             }
  1127.         }
  1128.         
  1129.         return $all_plugins;
  1130.     }
  1131. }
  1132.