WordPress.org

Make WordPress Core

Ticket #36901: 36901.4.diff

File 36901.4.diff, 10.7 KB (added by rachelbaker, 3 years ago)

Removes the check_comment action from check_comment_flood_db and includes minor docs and alignment tweaks.

  • src/wp-includes/comment.php

     
    581581 * Validates whether this comment is allowed to be made.
    582582 *
    583583 * @since 2.0.0
     584 * @since 4.7.0 The `$avoid_die` parameter was added.
    584585 *
    585586 * @global wpdb $wpdb WordPress database abstraction object.
    586587 *
    587  * @param array $commentdata Contains information on the comment
    588  * @return int|string Signifies the approval status (0|1|'spam')
     588 * @param array   $commentdata Contains information on the comment.
     589 * @param boolean $avoid_die   Should errors be returned as WP_Error objects
     590 *                             instead of executing wp_die()? Default false.
     591 * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam').
     592 *                             If $avoid_die is true, unallowed comments return a WP_Error.
    589593 */
    590 function wp_allow_comment( $commentdata ) {
     594function wp_allow_comment( $commentdata, $avoid_die = false ) {
    591595        global $wpdb;
    592596
    593597        // Simple duplicate check
     
    632636                 * @param array $commentdata Comment data.
    633637                 */
    634638                do_action( 'comment_duplicate_trigger', $commentdata );
    635                 if ( wp_doing_ajax() ) {
    636                         die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
     639                if ( true === $avoid_die ) {
     640                        return new WP_Error( 'comment_duplicate', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
     641                } else {
     642                        if ( wp_doing_ajax() ) {
     643                                die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
     644                        }
     645
     646                        wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
    637647                }
    638                 wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
    639648        }
    640649
    641650        /**
     
    656665                $commentdata['comment_date_gmt']
    657666        );
    658667
     668        /**
     669         * Filters wheter a comment is part of a comment flood or not.
     670         *
     671         * @since 4.7.0
     672         *
     673         * @param boolean $is_flood                            Whether it is a flood or not. Default
     674         *                                                     is false.
     675         * @param string  $commentdata['comment_author_IP']    Comment author's IP address.
     676         * @param string  $commentdata['comment_author_email'] Comment author's email.
     677         * @param string  $commentdata['comment_date_gmt']     GMT date the comment was posted.
     678         * @param boolean $avoid_die                           Prevent executing wp_die() or die()
     679         *                                                     if a comment flood is occuring.
     680         **/
     681        $is_flood = apply_filters(
     682                'wp_is_comment_flood',
     683                false,
     684                $commentdata['comment_author_IP'],
     685                $commentdata['comment_author_email'],
     686                $commentdata['comment_date_gmt'],
     687                $avoid_die
     688        );
     689
     690        if ( $is_flood ) {
     691                return new WP_Error( 'comment_flood', __( 'You are posting comments too quickly. Slow down.' ), 429 );
     692        }
     693
    659694        if ( ! empty( $commentdata['user_id'] ) ) {
    660695                $user = get_userdata( $commentdata['user_id'] );
    661696                $post_author = $wpdb->get_var( $wpdb->prepare(
     
    708743}
    709744
    710745/**
     746 * Hooks WP's native database-based comment-flood check.
     747 *
     748 * @since 2.3.0
     749 * @since 4.7.0 Converted to be an add_filter() wrapper.
     750 */
     751function check_comment_flood_db() {
     752        add_filter( 'wp_is_comment_flood', 'wp_check_comment_flood', 10, 5 );
     753}
     754
     755/**
    711756 * Check whether comment flooding is occurring.
    712757 *
    713758 * Won't run, if current user can manage options, so to not block
    714759 * administrators.
    715760 *
    716  * @since 2.3.0
     761 * @since 4.7.0
    717762 *
    718763 * @global wpdb $wpdb WordPress database abstraction object.
    719764 *
    720  * @param string $ip Comment IP.
    721  * @param string $email Comment author email address.
    722  * @param string $date MySQL time string.
     765 * @param boolean $is_flood  Is a comment flooding occurring?
     766 * @param string  $ip        Comment IP.
     767 * @param string  $email     Comment author email address.
     768 * @param string  $date      MySQL time string.
     769 * @param boolean $avoid_die Prevent executing wp_die() or die() if a
     770 *                           comment flood is occuring. Default is false.
     771 * @return boolean|WP_Error True or a WP_Error if a comment flood is occuring,
     772 *                          otherwise false.
    723773 */
    724 function check_comment_flood_db( $ip, $email, $date ) {
     774function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) {
     775
    725776        global $wpdb;
     777
     778        if ( true === $is_flood ) {
     779                return $is_flood;
     780        }
     781
    726782        // don't throttle admins or moderators
    727783        if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
    728                 return;
     784                return false;
    729785        }
    730786        $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
    731787
     
    767823                         * @param int $time_newcomment  Timestamp of when the new comment was posted.
    768824                         */
    769825                        do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
     826                        if ( true === $avoid_die ) {
     827                                return true;
     828                        } else {
     829                                if ( wp_doing_ajax() ) {
     830                                        die( __('You are posting comments too quickly. Slow down.') );
     831                                }
    770832
    771                         if ( wp_doing_ajax() )
    772                                 die( __('You are posting comments too quickly. Slow down.') );
    773 
    774                         wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
     833                                wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
     834                        }
    775835                }
    776836        }
     837
     838        return false;
    777839}
    778840
    779841/**
     
    17171779 *
    17181780 * @since 1.5.0
    17191781 * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
     1782 * @since 4.7.0 The `$avoid_die` parameter was added.
    17201783 *
    17211784 * @see wp_insert_comment()
    17221785 * @global wpdb $wpdb WordPress database abstraction object.
     
    17401803 *     @type string $comment_author_IP    Comment author IP address in IPv4 format. Default is the value of
    17411804 *                                        'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request.
    17421805 * }
    1743  * @return int|false The ID of the comment on success, false on failure.
     1806 * @param boolean $avoid_die Should errors be returned as WP_Error objects instead of
     1807 *                           executing wp_die()? Default false.
     1808 * @return int|false|WP_Error The ID of the comment on success, false or WP_Error on failure.
    17441809 */
    1745 function wp_new_comment( $commentdata ) {
     1810function wp_new_comment( $commentdata, $avoid_die = false ) {
    17461811        global $wpdb;
    17471812
    17481813        if ( isset( $commentdata['user_ID'] ) ) {
     
    17911856
    17921857        $commentdata = wp_filter_comment($commentdata);
    17931858
    1794         $commentdata['comment_approved'] = wp_allow_comment($commentdata);
     1859        $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die );
     1860        if ( is_wp_error( $commentdata['comment_approved'] ) ) {
     1861                return $commentdata['comment_approved'];
     1862        }
    17951863
    17961864        $comment_ID = wp_insert_comment($commentdata);
    17971865        if ( ! $comment_ID ) {
     
    18051873
    18061874                $commentdata = wp_filter_comment( $commentdata );
    18071875
    1808                 $commentdata['comment_approved'] = wp_allow_comment( $commentdata );
     1876                $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die );
     1877                if ( is_wp_error( $commentdata['comment_approved'] ) ) {
     1878                        return $commentdata['comment_approved'];
     1879                }
    18091880
    18101881                $comment_ID = wp_insert_comment( $commentdata );
    18111882                if ( ! $comment_ID ) {
     
    29272998                'user_ID'
    29282999        );
    29293000
    2930         $comment_id = wp_new_comment( wp_slash( $commentdata ) );
     3001        $comment_id = wp_new_comment( wp_slash( $commentdata ), true );
     3002        if ( is_wp_error( $comment_id ) ) {
     3003                return $comment_id;
     3004        }
     3005
    29313006        if ( ! $comment_id ) {
    29323007                return new WP_Error( 'comment_save_error', __( '<strong>ERROR</strong>: The comment could not be saved. Please try again later.' ), 500 );
    29333008        }
    29343009
    29353010        return get_comment( $comment_id );
    2936 
    29373011}
  • src/wp-includes/default-filters.php

     
    197197add_filter( 'teeny_mce_before_init',    '_mce_set_direction'                  );
    198198add_filter( 'pre_kses',                 'wp_pre_kses_less_than'               );
    199199add_filter( 'sanitize_title',           'sanitize_title_with_dashes',   10, 3 );
    200 add_action( 'check_comment_flood',      'check_comment_flood_db',       10, 3 );
    201200add_filter( 'comment_flood_filter',     'wp_throttle_comment_flood',    10, 3 );
    202201add_filter( 'pre_comment_content',      'wp_rel_nofollow',              15    );
    203202add_filter( 'comment_email',            'antispambot'                         );
  • tests/phpunit/tests/comment-submission.php

     
    714714                return $commentdata;
    715715        }
    716716
     717        /**
     718         * @ticket 36901
     719         */
     720        public function test_submitting_duplicate_comments() {
     721                $post = self::factory()->post->create_and_get( array(
     722                        'post_status' => 'publish',
     723                ) );
     724                $data = array(
     725                        'comment_post_ID' => $post->ID,
     726                        'comment'         => 'Did I say that?',
     727                        'author'          => 'Repeat myself',
     728                        'email'           => 'mail@example.com',
     729                );
     730                $first_comment = wp_handle_comment_submission( $data );
     731                $second_comment = wp_handle_comment_submission( $data );
     732                $this->assertWPError( $second_comment );
     733                $this->assertSame( 'comment_duplicate', $second_comment->get_error_code() );
     734        }
     735
     736        /**
     737         * @ticket 36901
     738         */
     739        public function test_comments_flood() {
     740                $post = self::factory()->post->create_and_get( array(
     741                        'post_status' => 'publish',
     742                ) );
     743                $data = array(
     744                        'comment_post_ID' => $post->ID,
     745                        'comment'         => 'Did I say that?',
     746                        'author'          => 'Repeat myself',
     747                        'email'           => 'mail@example.com',
     748                );
     749                $first_comment = wp_handle_comment_submission( $data );
     750
     751                $data['comment'] = 'Wow! I am quick!';
     752                $second_comment = wp_handle_comment_submission( $data );
     753
     754                $this->assertWPError( $second_comment );
     755                $this->assertSame( 'comment_flood', $second_comment->get_error_code() );
     756        }
     757
     758        /**
     759         * @ticket 36901
     760         */
     761        public function test_comments_flood_user_is_admin() {
     762                $user = self::factory()->user->create_and_get( array(
     763                        'role' => 'administrator',
     764                ) );
     765                wp_set_current_user( $user->ID );
     766
     767                $post = self::factory()->post->create_and_get( array(
     768                        'post_status' => 'publish',
     769                ) );
     770                $data = array(
     771                        'comment_post_ID' => $post->ID,
     772                        'comment'         => 'Did I say that?',
     773                        'author'          => 'Repeat myself',
     774                        'email'           => 'mail@example.com',
     775                );
     776                $first_comment = wp_handle_comment_submission( $data );
     777
     778                $data['comment'] = 'Wow! I am quick!';
     779                $second_comment = wp_handle_comment_submission( $data );
     780
     781                $this->assertNotWPError( $second_comment );
     782                $this->assertEquals( $post->ID, $second_comment->comment_post_ID );
     783        }
    717784}