Skip to content

Commit 5f6c43a

Browse files
committed
Comments: Fix apostrophe in author name breaking comment_whitelist check
1 parent bf4c174 commit 5f6c43a

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/wp-includes/comment.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent,
141141
)
142142
);
143143
} else {
144-
// expected_slashed ($author, $email)
145144
$ok_to_comment = $wpdb->get_var(
146145
$wpdb->prepare(
147146
"SELECT comment_approved
@@ -150,8 +149,8 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent,
150149
AND comment_author_email = %s
151150
AND comment_approved = '1'
152151
LIMIT 1",
153-
$author,
154-
$email
152+
wp_unslash( $author ),
153+
wp_unslash( $email )
155154
)
156155
);
157156
}

tests/phpunit/tests/comment/wpAllowComment.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,55 @@ public function test_allow_comment_if_comment_author_emails_differ() {
5252
$this->assertSame( 1, $result );
5353
}
5454

55+
/**
56+
* @ticket 40319
57+
* @covers ::check_comment
58+
*/
59+
public function test_allow_comment_if_previously_approved_author_name_and_email_contain_apostrophe() {
60+
update_option( 'comment_previously_approved', 1 );
61+
add_filter( 'comment_flood_filter', '__return_false' );
62+
63+
$now = time();
64+
65+
// Insert an already-approved comment with apostrophe in name and email.
66+
$approved_comment_id = wp_insert_comment(
67+
array(
68+
'comment_post_ID' => self::$post_id,
69+
'comment_approved' => '1',
70+
'comment_author' => "O'Brien",
71+
'comment_author_email' => "o'brien@example.com",
72+
'comment_author_url' => 'http://example.com',
73+
'comment_content' => 'Test comment.',
74+
'comment_parent' => 0,
75+
'comment_author_IP' => '192.168.0.1',
76+
'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 60 ),
77+
'comment_agent' => 'TestAgent/1.0',
78+
'comment_type' => '',
79+
)
80+
);
81+
82+
$new_comment = array(
83+
'comment_post_ID' => self::$post_id,
84+
'comment_author' => wp_slash( "O'Brien" ),
85+
'comment_author_email' => wp_slash( "o'brien@example.com" ),
86+
'comment_author_url' => 'http://example.com',
87+
'comment_content' => 'A new comment.',
88+
'comment_parent' => 0,
89+
'comment_author_IP' => '192.168.0.1',
90+
'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
91+
'comment_agent' => 'TestAgent/1.0',
92+
'comment_type' => '',
93+
);
94+
95+
$result = wp_allow_comment( $new_comment );
96+
97+
wp_delete_comment( $approved_comment_id, true );
98+
update_option( 'comment_previously_approved', 0 );
99+
remove_filter( 'comment_flood_filter', '__return_false' );
100+
101+
$this->assertSame( 1, $result, 'Comment from previously-approved author with apostrophe in name/email should be auto-approved.' );
102+
}
103+
55104
public function test_die_as_duplicate_if_comment_author_name_and_emails_match() {
56105
$this->expectException( 'WPDieException' );
57106

0 commit comments

Comments
 (0)