Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,13 @@ public function create_item( $request ) {
$raw_content = $request['content']['raw'];
}

$emoji_key = trim( wp_strip_all_tags( $raw_content ) );
$emoji_key = trim( $raw_content );

/*
* An exact match against the fixed list is the only validation. If the
* set widens (any code point, grapheme clusters, slugs), parse and
* validate the key here.
*/
if ( ! in_array( $emoji_key, self::get_note_reaction_keys(), true ) ) {
return new WP_Error(
'rest_comment_invalid_reaction',
Expand Down Expand Up @@ -863,7 +868,7 @@ public function create_item( $request ) {
);

foreach ( $existing as $existing_reaction ) {
if ( wp_strip_all_tags( $existing_reaction->comment_content ) === $emoji_key ) {
if ( $existing_reaction->comment_content === $emoji_key ) {
return new WP_Error(
'rest_comment_duplicate_reaction',
__( 'You have already reacted with this emoji.' ),
Expand All @@ -885,7 +890,7 @@ public function create_item( $request ) {
/*
* Persist the validated, canonical reaction key rather than the raw
* request content, so stored values stay consistent for grouping and
* counting (e.g. "<b>2764</b>" is stored as "2764").
* counting (e.g. " 2764" is stored as "2764").
*/
if ( null !== $reaction_key ) {
$prepared_comment['comment_content'] = $reaction_key;
Expand Down Expand Up @@ -1056,7 +1061,7 @@ public function create_item( $request ) {
);
$duplicates = array();
foreach ( $matching as $candidate ) {
if ( wp_strip_all_tags( $candidate->comment_content ) === $reaction_key ) {
if ( $candidate->comment_content === $reaction_key ) {
$duplicates[] = (int) $candidate->comment_ID;
}
}
Expand Down Expand Up @@ -2446,7 +2451,7 @@ protected function prefetch_reaction_summaries( $note_ids ) {

if ( $user_rows ) {
foreach ( $user_rows as $row ) {
$key = (int) $row->comment_parent . ':' . wp_strip_all_tags( $row->comment_content );
$key = (int) $row->comment_parent . ':' . $row->comment_content;
$my_reactions[ $key ] = (int) $row->comment_ID;
}
}
Expand All @@ -2465,23 +2470,22 @@ protected function prefetch_reaction_summaries( $note_ids ) {

foreach ( $counts as $row ) {
$note_id = (int) $row->comment_parent;
$emoji_key = wp_strip_all_tags( $row->comment_content );
$emoji_key = $row->comment_content;
$key = $note_id . ':' . $emoji_key;

// Only curated emoji keys are summarized.
/*
* Only curated emoji keys are summarized. The exact match is also the
* only check on stored content, so a wider set needs the key parsed
* and validated here.
*/
if ( ! in_array( $emoji_key, $allowed_keys, true ) ) {
continue;
}

// Rows are grouped on the raw content, so several groups can strip to one key.
if ( ! isset( $this->reaction_summaries[ $note_id ][ $emoji_key ] ) ) {
$this->reaction_summaries[ $note_id ][ $emoji_key ] = array(
'count' => 0,
'current_user_reaction' => $my_reactions[ $key ] ?? 0,
);
}

$this->reaction_summaries[ $note_id ][ $emoji_key ]['count'] += (int) $row->reaction_count;
$this->reaction_summaries[ $note_id ][ $emoji_key ] = array(
'count' => (int) $row->reaction_count,
'current_user_reaction' => $my_reactions[ $key ] ?? 0,
);
}
}

Expand Down
23 changes: 10 additions & 13 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4997,13 +4997,13 @@ public static function data_uncurated_reaction_keys() {
}

/**
* The stored reaction content is the validated, canonical key - markup
* around the key must not reach the database, or `reaction_summary`
* grouping would split visually identical reactions.
* Markup around a key is not a key, so it is rejected and never reaches
* the database.
*
* @ticket 64638
* @ticket 66695
*/
public function test_create_reaction_stores_canonical_key() {
public function test_create_reaction_rejects_markup_around_key(): void {
wp_set_current_user( self::$editor_id );

$post_id = self::factory()->post->create();
Expand Down Expand Up @@ -5032,10 +5032,7 @@ public function test_create_reaction_stores_canonical_key() {
);

$response = rest_get_server()->dispatch( $request );
$this->assertSame( 201, $response->get_status() );

$new_comment = get_comment( $response->get_data()['id'] );
$this->assertSame( '2764', $new_comment->comment_content );
$this->assertErrorResponse( 'rest_comment_invalid_reaction', $response, 400 );
}

/**
Expand Down Expand Up @@ -6731,12 +6728,12 @@ public function test_reaction_summary_ignores_uncurated_keys(): void {
}

/**
* Rows that strip to the same key are counted together rather than the
* last one overwriting the others.
* A row holding markup around a curated key is not that key, so it is
* left out and does not add to the real key's count.
*
* @ticket 66635
* @ticket 66695
*/
public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_key(): void {
public function test_reaction_summary_ignores_markup_around_a_curated_key(): void {
list( $post_id, $note_id ) = $this->create_editor_note();

$heart_id = self::factory()->comment->create(
Expand Down Expand Up @@ -6765,7 +6762,7 @@ public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_ke

$this->assertCount( 1, $summary );
$this->assertArrayHasKey( self::HEART_REACTION_KEY, $summary );
$this->assertSame( 2, $summary[ self::HEART_REACTION_KEY ]['count'] );
$this->assertSame( 1, $summary[ self::HEART_REACTION_KEY ]['count'] );
$this->assertSame( $heart_id, $summary[ self::HEART_REACTION_KEY ]['current_user_reaction'] );
}

Expand Down
Loading