Make WordPress Core

Ticket #16979: comment.diff

File comment.diff, 9.9 KB (added by sgrant, 11 years ago)

Updated patch from @dd32 and added unit tests.

  • src/wp-comments-post.php

     
    126126        }
    127127}
    128128
    129 if ( '' == $comment_content ) {
    130         wp_die( __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
    131 }
    132 
    133129$comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;
    134130
    135131$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
  • src/wp-includes/comment.php

     
    13501350function wp_allow_comment( $commentdata ) {
    13511351        global $wpdb;
    13521352
    1353         // Simple duplicate check
    1354         // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
    1355         $dupe = $wpdb->prepare(
    1356                 "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
    1357                 wp_unslash( $commentdata['comment_post_ID'] ),
    1358                 wp_unslash( $commentdata['comment_parent'] ),
    1359                 wp_unslash( $commentdata['comment_author'] )
    1360         );
    1361         if ( $commentdata['comment_author_email'] ) {
    1362                 $dupe .= $wpdb->prepare(
    1363                         "OR comment_author_email = %s ",
    1364                         wp_unslash( $commentdata['comment_author_email'] )
    1365                 );
    1366         }
    1367         $dupe .= $wpdb->prepare(
    1368                 ") AND comment_content = %s LIMIT 1",
    1369                 wp_unslash( $commentdata['comment_content'] )
    1370         );
    1371         if ( $wpdb->get_var( $dupe ) ) {
    1372                 /**
    1373                  * Fires immediately after a duplicate comment is detected.
    1374                  *
    1375                  * @since 3.0.0
    1376                  *
    1377                  * @param array $commentdata Comment data.
    1378                  */
    1379                 do_action( 'comment_duplicate_trigger', $commentdata );
    1380                 if ( defined( 'DOING_AJAX' ) ) {
    1381                         die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
    1382                 }
    1383                 wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
    1384         }
    1385 
    13861353        /**
    13871354         * Fires immediately before a comment is marked approved.
    13881355         *
     
    13981365                'check_comment_flood',
    13991366                $commentdata['comment_author_IP'],
    14001367                $commentdata['comment_author_email'],
    1401                 $commentdata['comment_date_gmt']
     1368                $commentdata['comment_date_gmt'],
     1369                $commentdata
    14021370        );
    14031371
    14041372        if ( ! empty( $commentdata['user_id'] ) ) {
     
    14531421}
    14541422
    14551423/**
     1424 * Check whether the comment has been posted already
     1425 *
     1426 *
     1427 * @since 4.3.0
     1428 * @uses $wpdb
     1429 * @uses do_action() Calls 'comment_duplicate_trigger' action with the comment data
     1430 *
     1431 * @param string $ip Comment IP.
     1432 * @param string $email Comment author email address.
     1433 * @param string $date MySQL time string.
     1434 * @param array  $commentdata The pre-processed comment data
     1435 */
     1436function check_comment_flood_duplicate( $ip, $email, $date, $commentdata ) {
     1437        global $wpdb;
     1438
     1439        // Simple duplicate check
     1440        // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
     1441        $dupe = $wpdb->prepare(
     1442                "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
     1443                wp_unslash( $commentdata['comment_post_ID'] ),
     1444                wp_unslash( $commentdata['comment_parent'] ),
     1445                wp_unslash( $commentdata['comment_author'] )
     1446        );
     1447        if ( $commentdata['comment_author_email'] ) {
     1448                $dupe .= $wpdb->prepare(
     1449                        "OR comment_author_email = %s ",
     1450                        wp_unslash( $commentdata['comment_author_email'] )
     1451                );
     1452        }
     1453        $dupe .= $wpdb->prepare(
     1454                ") AND comment_content = %s LIMIT 1",
     1455                wp_unslash( $commentdata['comment_content'] )
     1456        );
     1457        if ( $wpdb->get_var( $dupe ) ) {
     1458                /**
     1459                 * Fires immediately after a duplicate comment is detected.
     1460                 *
     1461                 * @since 3.0.0
     1462                 *
     1463                 * @param array $commentdata Comment data.
     1464                 */
     1465                do_action( 'comment_duplicate_trigger', $commentdata );
     1466                if ( defined( 'DOING_AJAX' ) ) {
     1467                        die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
     1468                }
     1469                wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
     1470        }
     1471}
     1472
     1473/**
    14561474 * Check whether comment flooding is occurring.
    14571475 *
    14581476 * Won't run, if current user can manage options, so to not block
     
    15031521        }
    15041522}
    15051523
     1524/**
     1525 * Blocks empty comments by wp_die()'ing on empty comments
     1526 *
     1527 * This function is hooked as such:
     1528 * <code>add_filter( 'preprocess_comment', 'disallow_empty_comments', 5 );</code>
     1529 *
     1530 * @since 4.3.0
     1531 *
     1532 * @param array $commentdata the comment data
     1533 * @return array the comment data
     1534 */
     1535function disallow_empty_comments( $commentdata ) {
     1536        if ( '' == $commentdata[ 'comment_content' ] ) {
     1537                wp_die( __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
     1538        }
     1539
     1540        return $commentdata;
     1541}
     1542
    15061543/**
    15071544 * Separates an array of comments into an array keyed by comment_type.
    15081545 *
     
    21932230        $user_id  = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
    21942231
    21952232        $compacted = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id' );
     2233
     2234        $compacted = apply_filters( 'wp_insert_comment_data', $compacted );
     2235
    21962236        if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
    21972237                return false;
    21982238        }
     
    23752415
    23762416        $commentdata['comment_approved'] = wp_allow_comment($commentdata);
    23772417
     2418        $commentdata = apply_filters( 'process_comment', $commentdata );
     2419
    23782420        $comment_ID = wp_insert_comment($commentdata);
    23792421        if ( ! $comment_ID ) {
    23802422                $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
  • src/wp-includes/default-filters.php

     
    185185add_filter( 'teeny_mce_before_init',    '_mce_set_direction'                  );
    186186add_filter( 'pre_kses',                 'wp_pre_kses_less_than'               );
    187187add_filter( 'sanitize_title',           'sanitize_title_with_dashes',   10, 3 );
     188add_action( 'check_comment_flood',      'check_comment_flood_duplicate', 10, 4);
    188189add_action( 'check_comment_flood',      'check_comment_flood_db',       10, 3 );
    189190add_filter( 'comment_flood_filter',     'wp_throttle_comment_flood',    10, 3 );
    190191add_filter( 'pre_comment_content',      'wp_rel_nofollow',              15    );
     
    197198add_filter( 'editable_slug',            'urldecode'                           );
    198199add_filter( 'editable_slug',            'esc_textarea'                        );
    199200add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object'        );
     201add_filter( 'preprocess_comment',       'disallow_empty_comments',          5 );
    200202add_filter( 'pingback_ping_source_uri', 'pingback_ping_source_uri'            );
    201203add_filter( 'xmlrpc_pingback_error',    'xmlrpc_pingback_error'               );
    202204add_filter( 'title_save_pre',           'trim'                                );
  • tests/phpunit/tests/comment/duplicateContent.php

     
     1<?php
     2/**
     3 * @group comment
     4 */
     5class Tests_Comment_DuplicateContent extends WP_UnitTestCase {
     6
     7        function test_comment_allow_duplicate_content_after_filter_removed() {
     8                $user = $this->factory->user->create();
     9                $post_id = $this->factory->post->create();
     10
     11                $content = 'test_content';
     12
     13                $data = array(
     14                        'comment_post_ID'      => $post_id,
     15                        'comment_author'       => $user,
     16                        'comment_author_IP'    => '192.168.1.1',
     17                        'comment_author_url'   => '',
     18                        'comment_author_email' => '',
     19                        'comment_type'         => '',
     20                        'comment_date_gmt' => '2015-01-01 10:00:00',
     21                        'comment_content'      => $content,
     22                );
     23
     24                $id_first = wp_new_comment( $data );
     25                $comment_first = get_comment( $id_first );
     26
     27                remove_action( 'check_comment_flood', 'check_comment_flood_duplicate', 10 );
     28
     29                $data[ 'comment_date_gmt' ] = '2015-02-02 10:00:00';
     30
     31                $id_next = wp_new_comment( $data );
     32                $comment_next = get_comment( $id_next );
     33
     34                add_action( 'check_comment_flood', 'check_comment_flood_duplicate', 10, 4 );
     35
     36                $this->assertEquals( $data[ 'comment_author' ], $comment_first->comment_author );
     37                $this->assertEquals( $data[ 'comment_author' ], $comment_next->comment_author );
     38        }
     39
     40}
     41 No newline at end of file
  • tests/phpunit/tests/comment/emptyContent.php

     
     1<?php
     2/**
     3 * @group comment
     4 */
     5class Tests_Comment_EmptyContent extends WP_UnitTestCase {
     6
     7        function test_comment_allow_empty_content_after_filter_removed() {
     8                $post_id = $this->factory->post->create();
     9
     10                $data = array(
     11                        'comment_post_ID'      => $post_id,
     12                        'comment_author'       => 'test_author',
     13                        'comment_author_IP'    => '192.168.1.1',
     14                        'comment_author_url'   => '',
     15                        'comment_author_email' => '',
     16                        'comment_type'         => '',
     17                        'comment_content'      => '',
     18                );
     19
     20                remove_filter( 'preprocess_comment', 'disallow_empty_comments', 5 );
     21
     22                $id = wp_new_comment( $data );
     23               
     24                add_filter( 'preprocess_comment', 'disallow_empty_comments', 5 );
     25
     26                $comment = get_comment( $id );
     27
     28                $this->assertEquals( $data[ 'comment_content' ], $comment->comment_content );
     29                $this->assertEquals( $data[ 'comment_post_ID' ], $comment->comment_post_ID );
     30                $this->assertEquals( $data[ 'comment_author' ], $comment->comment_author );
     31                $this->assertEquals( $data[ 'comment_author_IP' ], $comment->comment_author_IP );
     32        }
     33
     34}