WordPress 4.3
[autoinstalls/wordpress.git] / wp-content / plugins / akismet / class.akismet-admin.php
index 7a62ccbd6453cba7350954deb1cee08b20cf6c79..960739f8f81d5517cbf49654dc2ff30eabc9ab88 100644 (file)
@@ -37,6 +37,7 @@ class Akismet_Admin {
                add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
                add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
                add_action( 'wp_ajax_comment_author_reurl', array( 'Akismet_Admin', 'add_comment_author_url' ) );
+               add_action( 'jetpack_auto_activate_akismet', array( 'Akismet_Admin', 'connect_jetpack_user' ) );
 
                add_filter( 'plugin_action_links', array( 'Akismet_Admin', 'plugin_action_links' ), 10, 2 );
                add_filter( 'comment_row_actions', array( 'Akismet_Admin', 'comment_row_action' ), 10, 2 );
@@ -231,7 +232,7 @@ class Akismet_Admin {
                if ( defined( 'WPCOM_API_KEY' ) )
                        return false; //shouldn't have option to save key if already defined
 
-               $new_key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
+               $new_key = preg_replace( '/[^a-f0-9]/i', '', $_POST['key'] );
                $old_key = Akismet::get_api_key();
 
                if ( empty( $new_key ) ) {
@@ -289,15 +290,6 @@ class Akismet_Admin {
 
        // WP 2.5+
        public static function rightnow_stats() {
-               global $submenu, $wp_db_version;
-
-               if ( 8645 < $wp_db_version  ) // 2.7
-                       $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
-               elseif ( isset( $submenu['edit-comments.php'] ) )
-                       $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit-comments.php' ) );
-               else
-                       $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit.php' ) );
-
                if ( $count = get_option('akismet_spam_count') ) {
                        $intro = sprintf( _n(
                                '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
@@ -308,15 +300,16 @@ class Akismet_Admin {
                        $intro = sprintf( __('<a href="%s">Akismet</a> blocks spam from getting to your blog. ', 'akismet'), 'https://akismet.com/wordpress/' );
                }
 
-               $link = function_exists( 'esc_url' ) ? esc_url( $link ) : clean_url( $link );
+               $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
+
                if ( $queue_count = self::get_spam_count() ) {
                        $queue_text = sprintf( _n(
                                'There&#8217;s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
                                'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
                                $queue_count
-                       , 'akismet'), number_format_i18n( $queue_count ), $link );
+                       , 'akismet'), number_format_i18n( $queue_count ), esc_url( $link ) );
                } else {
-                       $queue_text = sprintf( __( "There&#8217;s nothing in your <a href='%s'>spam queue</a> at the moment." , 'akismet'), $link );
+                       $queue_text = sprintf( __( "There&#8217;s nothing in your <a href='%s'>spam queue</a> at the moment." , 'akismet'), esc_url( $link ) );
                }
 
                $text = $intro . '<br />' . $queue_text;
@@ -324,8 +317,12 @@ class Akismet_Admin {
        }
 
        public static function check_for_spam_button( $comment_status ) {
-               if ( 'approved' == $comment_status )
+               // The "Check for Spam" button should only appear when the page might be showing
+               // a comment with comment_approved=0, which means an un-trashed, un-spammed,
+               // not-yet-moderated comment.
+               if ( 'all' != $comment_status && 'moderated' != $comment_status ) {
                        return;
+               }
 
                if ( function_exists('plugins_url') )
                        $link = add_query_arg( array( 'action' => 'akismet_recheck_queue' ), admin_url( 'admin.php' ) );
@@ -367,23 +364,29 @@ class Akismet_Admin {
 
                        add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
 
-                       $response = Akismet::http_post( build_query( $c ), 'comment-check' );
+                       $response = Akismet::http_post( Akismet::build_query( $c ), 'comment-check' );
+                       
                        if ( 'true' == $response[1] ) {
                                wp_set_comment_status( $c['comment_ID'], 'spam' );
                                update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
                                delete_comment_meta( $c['comment_ID'], 'akismet_error' );
                                delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
-                               Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam', 'akismet'), 'check-spam' );
+                               Akismet::update_comment_history( $c['comment_ID'], '', 'recheck-spam' );
 
                        } elseif ( 'false' == $response[1] ) {
                                update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
                                delete_comment_meta( $c['comment_ID'], 'akismet_error' );
                                delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
-                               Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment', 'akismet'), 'check-ham' );
+                               Akismet::update_comment_history( $c['comment_ID'], '', 'recheck-ham' );
                        // abnormal result: error
                        } else {
                                update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
-                               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' );
+                               Akismet::update_comment_history(
+                                       $c['comment_ID'],
+                                       '',
+                                       'recheck-error',
+                                       array( 'response' => substr( $response[1], 0, 50 ) )
+                               );
                        }
 
                        delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' );
@@ -488,11 +491,95 @@ class Akismet_Admin {
 
                if ( $history ) {
                        echo '<div class="akismet-history" style="margin: 13px;">';
+
                        foreach ( $history as $row ) {
                                $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
-                               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> - ';
-                               echo esc_html( $row['message'] ) . '</div>';
+                               
+                               $message = '';
+                               
+                               if ( ! empty( $row['message'] ) ) {
+                                       // Old versions of Akismet stored the message as a literal string in the commentmeta.
+                                       // New versions don't do that for two reasons:
+                                       // 1) Save space.
+                                       // 2) The message can be translated into the current language of the blog, not stuck 
+                                       //    in the language of the blog when the comment was made.
+                                       $message = $row['message'];
+                               }
+                               
+                               // If possible, use a current translation.
+                               switch ( $row['event'] ) {
+                                       case 'recheck-spam';
+                                               $message = __( 'Akismet re-checked and caught this comment as spam.', 'akismet' );
+                                       break;
+                                       case 'check-spam':
+                                               $message = __( 'Akismet caught this comment as spam.', 'akismet' );
+                                       break;
+                                       case 'recheck-ham':
+                                               $message = __( 'Akismet re-checked and cleared this comment.', 'akismet' );
+                                       break;
+                                       case 'check-ham':
+                                               $message = __( 'Akismet cleared this comment.', 'akismet' );
+                                       break;
+                                       case 'wp-blacklisted':
+                                               $message = __( 'Comment was caught by wp_blacklist_check.', 'akismet' );
+                                       break;
+                                       case 'report-spam':
+                                               if ( isset( $row['user'] ) ) {
+                                                       $message = sprintf( __( '%s reported this comment as spam.', 'akismet' ), $row['user'] );
+                                               }
+                                               else if ( ! $message ) {
+                                                       $message = __( 'This comment was reported as spam.', 'akismet' );
+                                               }
+                                       break;
+                                       case 'report-ham':
+                                               if ( isset( $row['user'] ) ) {
+                                                       $message = sprintf( __( '%s reported this comment as not spam.', 'akismet' ), $row['user'] );
+                                               }
+                                               else if ( ! $message ) {
+                                                       $message = __( 'This comment was reported as not spam.', 'akismet' );
+                                               }
+                                       break;
+                                       case 'cron-retry-spam':
+                                               $message = __( 'Akismet caught this comment as spam during an automatic retry.' , 'akismet');
+                                       break;
+                                       case 'cron-retry-ham':
+                                               $message = __( 'Akismet cleared this comment during an automatic retry.', 'akismet');
+                                       break;
+                                       case 'check-error':
+                                               if ( isset( $row['meta'], $row['meta']['response'] ) ) {
+                                                       $message = sprintf( __( 'Akismet was unable to check this comment (response: %s) but will automatically retry later.', 'akismet'), $row['meta']['response'] );
+                                               }
+                                       break;
+                                       case 'recheck-error':
+                                               if ( isset( $row['meta'], $row['meta']['response'] ) ) {
+                                                       $message = sprintf( __( 'Akismet was unable to recheck this comment (response: %s).', 'akismet'), $row['meta']['response'] );
+                                               }
+                                       break;
+                                       default:
+                                               if ( preg_match( '/^status-changed/', $row['event'] ) ) {
+                                                       // Half of these used to be saved without the dash after 'status-changed'.
+                                                       // See https://plugins.trac.wordpress.org/changeset/1150658/akismet/trunk
+                                                       $new_status = preg_replace( '/^status-changed-?/', '', $row['event'] );
+                                                       $message = sprintf( __( 'Comment status was changed to %s', 'akismet' ), $new_status );
+                                               }
+                                               else if ( preg_match( '/^status-/', $row['event'] ) ) {
+                                                       $new_status = preg_replace( '/^status-/', '', $row['event'] );
+
+                                                       if ( isset( $row['user'] ) ) {
+                                                               $message = sprintf( __( '%1$s changed the comment status to %2$s.', 'akismet' ), $row['user'], $new_status );
+                                                       }
+                                               }
+                                       break;
+                                       
+                               }
+
+                               echo '<div style="margin-bottom: 13px;">';
+                                       echo '<span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( esc_html__('%s ago', 'akismet'), human_time_diff( $row['time'] ) ) . '</span>';
+                                       echo ' - ';
+                                       echo esc_html( $message );
+                               echo '</div>';
                        }
+
                        echo '</div>';
                }
        }
@@ -625,13 +712,16 @@ class Akismet_Admin {
        }
        
        public static function get_akismet_user( $api_key ) {
-               $akismet_user = Akismet::http_post( build_query( array( 'key' => $api_key ) ), 'get-subscription' );
+               $akismet_user = false;
+
+               $subscription_verification = Akismet::http_post( Akismet::build_query( array( 'key' => $api_key, 'blog' => get_bloginfo( 'url' ) ) ), 'get-subscription' );
+
+               if ( ! empty( $subscription_verification[1] ) ) {
+                       if ( 'invalid' !== $subscription_verification[1] ) {
+                               $akismet_user = json_decode( $subscription_verification[1] );
+                       }
+               }
 
-               if ( ! empty( $akismet_user[1] ) )
-                       $akismet_user = json_decode( $akismet_user[1] );
-               else
-                       $akismet_user = false;
-                       
                return $akismet_user;
        }
        
@@ -639,22 +729,22 @@ class Akismet_Admin {
                $stat_totals = array();
 
                foreach( array( '6-months', 'all' ) as $interval ) {
-                       $response = Akismet::http_post( build_query( array( 'blog' => urlencode( get_bloginfo('url') ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
+                       $response = Akismet::http_post( Akismet::build_query( array( 'blog' => get_bloginfo( 'url' ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
 
                        if ( ! empty( $response[1] ) ) {
                                $stat_totals[$interval] = json_decode( $response[1] );
                        }
                }
+
                return $stat_totals;
        }
        
-       public static function verify_wpcom_key( $api_key, $user_id, $token = '' ) {
-               $akismet_account = Akismet::http_post( build_query( array(
+       public static function verify_wpcom_key( $api_key, $user_id, $extra = array() ) {
+               $akismet_account = Akismet::http_post( Akismet::build_query( array_merge( array(
                        'user_id'          => $user_id,
                        'api_key'          => $api_key,
-                       'token'            => $token,
                        'get_account_type' => 'true'
-               ) ), 'verify-wpcom-key' );
+               ), $extra ) ), 'verify-wpcom-key' );
 
                if ( ! empty( $akismet_account[1] ) )
                        $akismet_account = json_decode( $akismet_account[1] );
@@ -663,6 +753,22 @@ class Akismet_Admin {
                
                return $akismet_account;
        }
+       
+       public static function connect_jetpack_user() {
+       
+               if ( $jetpack_user = self::get_jetpack_user() ) {
+                       if ( isset( $jetpack_user['user_id'] ) && isset(  $jetpack_user['api_key'] ) ) {
+                               $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'], array( 'action' => 'connect_jetpack_user' ) );
+                                                       
+                               if ( is_object( $akismet_user ) ) {
+                                       self::save_key( $akismet_user->api_key );
+                                       return in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) );
+                               }
+                       }
+               }
+               
+               return false;
+       }
 
        public static function display_alert() {
                Akismet::view( 'notice', array(
@@ -674,11 +780,11 @@ class Akismet_Admin {
 
        public static function display_spam_check_warning() {
                Akismet::fix_scheduled_recheck();
-               
-               $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() ) ) );
 
-               if ( self::get_number_spam_waiting() > 0 && wp_next_scheduled('akismet_schedule_cron_recheck') > time() )
+               if ( wp_next_scheduled('akismet_schedule_cron_recheck') > time() && self::get_number_spam_waiting() > 0 ) {
+                       $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() ) ) );
                        Akismet::view( 'notice', array( 'type' => 'spam-check', 'link_text' => $link_text ) );
+               }
        }
 
        public static function display_invalid_version() {
@@ -706,7 +812,7 @@ class Akismet_Admin {
                        }
                }
 
-               if ( $api_key = Akismet::get_api_key() ) {
+               if ( $api_key = Akismet::get_api_key() && ( empty( self::$notices['status'] ) || 'existing-key-invalid' != self::$notices['status'] ) ) {
                        self::display_configuration_page();
                        return;
                }
@@ -718,7 +824,7 @@ class Akismet_Admin {
                $akismet_user = false;
                
                if ( isset( $_GET['token'] ) && preg_match('/^(\d+)-[0-9a-f]{20}$/', $_GET['token'] ) )
-                       $akismet_user = self::verify_wpcom_key( '', '', $_GET['token'] );
+                       $akismet_user = self::verify_wpcom_key( '', '', array( 'token' => $_GET['token'] ) );
                elseif ( $jetpack_user = self::get_jetpack_user() )
                        $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'] );
                        
@@ -747,8 +853,16 @@ class Akismet_Admin {
        public static function display_configuration_page() {
                $api_key      = Akismet::get_api_key();
                $akismet_user = self::get_akismet_user( $api_key );
-               $stat_totals  = self::get_stats( $api_key );
                
+               if ( ! $akismet_user ) {
+                       // This could happen if the user's key became invalid after it was previously valid and successfully set up.
+                       self::$notices['status'] = 'existing-key-invalid';
+                       self::display_start_page();
+                       return;
+               }
+
+               $stat_totals  = self::get_stats( $api_key );
+
                // If unset, create the new strictness option using the old discard option to determine its default
                if ( get_option( 'akismet_strictness' ) === false )
                add_option( 'akismet_strictness', (get_option('akismet_discard_month') === 'true' ? '1' : '0') );
@@ -792,7 +906,10 @@ class Akismet_Admin {
                global $hook_suffix;
 
                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 ) {
-                       self::display_alert();
+                       Akismet::verify_key( Akismet::get_api_key() ); //verify that the key is still in alert state
+                       
+                       if ( get_option( 'akismet_alert_code' ) > 0 )
+                               self::display_alert();
                }
                elseif ( $hook_suffix == 'plugins.php' && !Akismet::get_api_key() ) {
                        self::display_api_key_warning();