WordPress.org

Make WordPress Core

Ticket #36901: 36901.diff

File 36901.diff, 9.5 KB (added by rachelbaker, 3 years ago)

Use $avoid_die parameter and attempt to simplify the logic by using check_comment_flood_db() BUT unhooking it's action

  • src/wp-includes/comment.php

     
    581586 * Validates whether this comment is allowed to be made.
    582587 *
    583588 * @since 2.0.0
     589 * @since 4.7.0 The `$avoid_die` parameter was added.
    584590 *
    585591 * @global wpdb $wpdb WordPress database abstraction object.
    586592 *
    587  * @param array $commentdata Contains information on the comment
    588  * @return int|string Signifies the approval status (0|1|'spam')
     593 * @param array   $commentdata Contains information on the comment.
     594 * @param boolean $avoid_die   Should errors be returned as WP_Error objects
     595 *                             instead of executing wp_die()? Default false.
     596 * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam').
     597 *                             If $avoid_die is true, unallowed comments return a WP_Error.
    589598 */
    590 function wp_allow_comment( $commentdata ) {
     599function wp_allow_comment( $commentdata, $avoid_die = false ) {
    591600        global $wpdb;
    592601
    593602        // Simple duplicate check
     
    632641                 * @param array $commentdata Comment data.
    633642                 */
    634643                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!') );
     644                if ( true === $avoid_die ) {
     645                        return new WP_Error( 'comment_duplicate', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
     646                } else {
     647                        if ( wp_doing_ajax() ) {
     648                                die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
     649                        }
     650
     651                        wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
    637652                }
    638                 wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
    639653        }
    640654
    641655        /**
     
    656670                $commentdata['comment_date_gmt']
    657671        );
    658672
     673        $is_flood = check_comment_flood_db( $commentdata['comment_author_IP'], $commentdata['comment_author_email'], $commentdata['comment_date_gmt'], $avoid_die );
     674        if ( $is_flood ) {
     675                return new WP_Error( 'comment_flood', __( 'You are posting comments too quickly. Slow down.' ), 429 );
     676        }
     677
    659678        if ( ! empty( $commentdata['user_id'] ) ) {
    660679                $user = get_userdata( $commentdata['user_id'] );
    661680                $post_author = $wpdb->get_var( $wpdb->prepare(
     
    714733 * administrators.
    715734 *
    716735 * @since 2.3.0
     736 * @since 4.7.0 The `$avoid_die` parameter was added.
    717737 *
    718738 * @global wpdb $wpdb WordPress database abstraction object.
    719739 *
    720  * @param string $ip Comment IP.
    721  * @param string $email Comment author email address.
    722  * @param string $date MySQL time string.
     740 * @param string  $ip        Comment IP.
     741 * @param string  $email     Comment author email address.
     742 * @param string  $date      MySQL time string.
     743 * @param boolean $avoid_die Prevent executing wp_die() or die() if a
     744 *                           comment flood is occuring. Default is false.
     745 * @return boolean|WP_Error True or a WP_Error if a comment flood is occuring,
     746 *                          otherwise false.
    723747 */
    724 function check_comment_flood_db( $ip, $email, $date ) {
     748function check_comment_flood_db( $ip, $email, $date, $avoid_die = false ) {
     749
    725750        global $wpdb;
    726751        // don't throttle admins or moderators
    727752        if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
     
    767792                         * @param int $time_newcomment  Timestamp of when the new comment was posted.
    768793                         */
    769794                        do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
     795                        if ( true === $avoid_die ) {
     796                                return true;
     797                        } else {
     798                                if ( wp_doing_ajax() ) {
     799                                        die( __('You are posting comments too quickly. Slow down.') );
     800                                }
    770801
    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 );
     802                                wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
     803                        }
    775804                }
    776805        }
     806
     807        return false;
    777808}
    778809
    779810/**
     
    17171748 *
    17181749 * @since 1.5.0
    17191750 * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
     1751 * @since 4.7.0 The `$avoid_die` parameter was added.
    17201752 *
    17211753 * @see wp_insert_comment()
    17221754 * @global wpdb $wpdb WordPress database abstraction object.
     
    17401772 *     @type string $comment_author_IP    Comment author IP address in IPv4 format. Default is the value of
    17411773 *                                        'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request.
    17421774 * }
    1743  * @return int|false The ID of the comment on success, false on failure.
     1775 * @param boolean $avoid_die Should errors be returned as WP_Error objects instead of
     1776 *                           executing wp_die()? Default false.
     1777 * @return int|false|WP_Error The ID of the comment on success, false or WP_Error on failure.
    17441778 */
    1745 function wp_new_comment( $commentdata ) {
     1779function wp_new_comment( $commentdata, $avoid_die = false ) {
    17461780        global $wpdb;
    17471781
    17481782        if ( isset( $commentdata['user_ID'] ) ) {
     
    17911825
    17921826        $commentdata = wp_filter_comment($commentdata);
    17931827
    1794         $commentdata['comment_approved'] = wp_allow_comment($commentdata);
     1828        $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die );
     1829        if ( is_wp_error( $commentdata['comment_approved'] ) ) {
     1830                return $commentdata['comment_approved'];
     1831        }
    17951832
    17961833        $comment_ID = wp_insert_comment($commentdata);
    17971834        if ( ! $comment_ID ) {
     
    18051842
    18061843                $commentdata = wp_filter_comment( $commentdata );
    18071844
    1808                 $commentdata['comment_approved'] = wp_allow_comment( $commentdata );
     1845                $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die );
     1846                if ( is_wp_error( $commentdata['comment_approved'] ) ) {
     1847                        return $commentdata['comment_approved'];
     1848                }
    18091849
    18101850                $comment_ID = wp_insert_comment( $commentdata );
    18111851                if ( ! $comment_ID ) {
     
    29132953                'user_ID'
    29142954        );
    29152955
    2916         $comment_id = wp_new_comment( wp_slash( $commentdata ) );
     2956        $comment_id = wp_new_comment( wp_slash( $commentdata ), true );
     2957        if ( is_wp_error( $comment_id ) ) {
     2958                return $comment_id;
     2959        }
     2960
    29172961        if ( ! $comment_id ) {
    29182962                return new WP_Error( 'comment_save_error', __( '<strong>ERROR</strong>: The comment could not be saved. Please try again later.' ), 500 );
    29192963        }
    29202964
    29212965        return get_comment( $comment_id );
    2922 
    29232966}
  • 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        }
     784
    717785}