~canonical-sysadmins/wordpress/4.9.7

« back to all changes in this revision

Viewing changes to wp-content/plugins/akismet/class.akismet.php

  • Committer: Barry Price
  • Date: 2017-11-02 04:15:48 UTC
  • mfrom: (1.1.29 upstream)
  • Revision ID: barry.price@canonical.com-20171102041548-n4kg8l9f98xajt7m
Merge WP4.8.3 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
        private static $prevent_moderation_email_for_these_comments = array();
11
11
        private static $last_comment_result = null;
12
12
        private static $comment_as_submitted_allowed_keys = array( 'blog' => '', 'blog_charset' => '', 'blog_lang' => '', 'blog_ua' => '', 'comment_agent' => '', 'comment_author' => '', 'comment_author_IP' => '', 'comment_author_email' => '', 'comment_author_url' => '', 'comment_content' => '', 'comment_date_gmt' => '', 'comment_tags' => '', 'comment_type' => '', 'guid' => '', 'is_test' => '', 'permalink' => '', 'reporter' => '', 'site_domain' => '', 'submit_referer' => '', 'submit_uri' => '', 'user_ID' => '', 'user_agent' => '', 'user_id' => '', 'user_ip' => '' );
13
 
 
 
13
        private static $is_rest_api_call = false;
 
14
        
14
15
        public static function init() {
15
16
                if ( ! self::$initiated ) {
16
17
                        self::init_hooks();
25
26
 
26
27
                add_action( 'wp_insert_comment', array( 'Akismet', 'auto_check_update_meta' ), 10, 2 );
27
28
                add_filter( 'preprocess_comment', array( 'Akismet', 'auto_check_comment' ), 1 );
 
29
                add_filter( 'rest_pre_insert_comment', array( 'Akismet', 'rest_auto_check_comment' ), 1 );
 
30
 
28
31
                add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments' ) );
29
32
                add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments_meta' ) );
30
33
                add_action( 'akismet_schedule_cron_recheck', array( 'Akismet', 'cron_recheck' ) );
104
107
                        self::verify_key( $value );
105
108
                }
106
109
        }
 
110
        
 
111
        public static function rest_auto_check_comment( $commentdata ) {
 
112
                self::$is_rest_api_call = true;
 
113
                
 
114
                return self::auto_check_comment( $commentdata );
 
115
        }
107
116
 
108
117
        public static function auto_check_comment( $commentdata ) {
109
118
                self::$last_comment_result = null;
190
199
                        do_action( 'akismet_spam_caught', $discard );
191
200
 
192
201
                        if ( $discard ) {
 
202
                                // The spam is obvious, so we're bailing out early. 
193
203
                                // akismet_result_spam() won't be called so bump the counter here
194
 
                                if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
195
 
                                        update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
196
 
                                // The spam is obvious, so we're bailing out early. Redirect back to the previous page,
197
 
                                // or failing that, the post permalink, or failing that, the homepage of the blog.
198
 
                                $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ( $post ? get_permalink( $post ) : home_url() );
199
 
                                wp_safe_redirect( esc_url_raw( $redirect_to ) );
200
 
                                die();
 
204
                                if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) {
 
205
                                        update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr );
 
206
                                }
 
207
 
 
208
                                if ( self::$is_rest_api_call ) {
 
209
                                        return new WP_Error( 'akismet_rest_comment_discarded', __( 'Comment discarded.', 'akismet' ) );
 
210
                                }
 
211
                                else {
 
212
                                        // Redirect back to the previous page, or failing that, the post permalink, or failing that, the homepage of the blog.
 
213
                                        $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ( $post ? get_permalink( $post ) : home_url() );
 
214
                                        wp_safe_redirect( esc_url_raw( $redirect_to ) );
 
215
                                        die();
 
216
                                }
 
217
                        }
 
218
                        else if ( self::$is_rest_api_call ) {
 
219
                                // The way the REST API structures its calls, we can set the comment_approved value right away.
 
220
                                $commentdata['comment_approved'] = 'spam';
201
221
                        }
202
222
                }
203
223
                
207
227
                                // Comment status should be moderated
208
228
                                self::$last_comment_result = '0';
209
229
                        }
210
 
                        if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_single_event') ) {
211
 
                                if ( !wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
212
 
                                        wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
213
 
                                        do_action( 'akismet_scheduled_recheck', 'invalid-response-' . $response[1] );
214
 
                                }
 
230
 
 
231
                        if ( ! wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
 
232
                                wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
 
233
                                do_action( 'akismet_scheduled_recheck', 'invalid-response-' . $response[1] );
215
234
                        }
216
235
 
217
236
                        self::$prevent_moderation_email_for_these_comments[] = $commentdata;
218
237
                }
219
238
 
220
 
                if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_event') ) {
221
 
                        // WP 2.1+: delete old comments daily
222
 
                        if ( !wp_next_scheduled( 'akismet_scheduled_delete' ) )
223
 
                                wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' );
224
 
                }
225
 
                elseif ( (mt_rand(1, 10) == 3) ) {
226
 
                        // WP 2.0: run this one time in ten
227
 
                        self::delete_old_comments();
228
 
                }
229
 
                
 
239
                // Delete old comments daily
 
240
                if ( ! wp_next_scheduled( 'akismet_scheduled_delete' ) ) {
 
241
                        wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' );
 
242
                }
 
243
 
230
244
                self::set_last_comment( $commentdata );
231
245
                self::fix_scheduled_recheck();
232
246
 
256
270
        // this fires on wp_insert_comment.  we can't update comment_meta when auto_check_comment() runs
257
271
        // because we don't know the comment ID at that point.
258
272
        public static function auto_check_update_meta( $id, $comment ) {
259
 
 
260
 
                // failsafe for old WP versions
261
 
                if ( !function_exists('add_comment_meta') )
262
 
                        return false;
263
 
 
264
273
                // wp_insert_comment() might be called in other contexts, so make sure this is the same comment
265
274
                // as was checked by auto_check_comment
266
275
                if ( is_object( $comment ) && !empty( self::$last_comment ) && is_array( self::$last_comment ) ) {
398
407
 
399
408
        // get the full comment history for a given comment, as an array in reverse chronological order
400
409
        public static function get_comment_history( $comment_id ) {
401
 
 
402
 
                // failsafe for old WP versions
403
 
                if ( !function_exists('add_comment_meta') )
404
 
                        return false;
405
 
 
406
410
                $history = get_comment_meta( $comment_id, 'akismet_history', false );
407
411
                usort( $history, array( 'Akismet', '_cmp_time' ) );
408
412
                return $history;
419
423
        public static function update_comment_history( $comment_id, $message, $event=null, $meta=null ) {
420
424
                global $current_user;
421
425
 
422
 
                // failsafe for old WP versions
423
 
                if ( !function_exists('add_comment_meta') )
424
 
                        return false;
425
 
 
426
426
                $user = '';
427
427
 
428
428
                $event = array(
1024
1024
                        do_action( 'akismet_ssl_disabled' );
1025
1025
                }
1026
1026
 
1027
 
                if ( ! $ssl_disabled && function_exists( 'wp_http_supports') && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) {
 
1027
                if ( ! $ssl_disabled && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) {
1028
1028
                        $akismet_url = set_url_scheme( $akismet_url, 'https' );
1029
1029
 
1030
1030
                        do_action( 'akismet_https_request_pre' );