Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#66276 new enhancement

Notes: Add a filter for the emoji a note can be reacted with

Reported by: adamsilverstein Owned by:
Priority: normal Milestone: Awaiting Review
Component: Comments Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Follow up to ​https://github.com/WordPress/wordpress-develop/pull/10930 - emoji reactions on Notes ship with a fixed set of five emoji. This adds a way for sites to change that set.

Claude wrote the rest of this ticket; I checked it against the code:

Reactions accept a fixed set of five emoji (heart, celebration, smile, eyes, rocket). Right now the list is hardcoded in a private WP_REST_Comments_Controller::get_note_reaction_keys() method, so a site can't add an emoji its team uses, remove one that doesn't fit, or turn reactions off.

The proposed change:

  • Add a wp_note_reaction_emojis filter. Each entry has a hexKey (the emoji's code points in hex, eg. 1f984 for the unicorn) and a translatable label. Returning an empty array turns off adding reactions. Existing reactions still show and their authors can still remove them.
  • Add wp_get_note_reaction_emojis() and wp_get_note_reaction_keys(), plus wp_normalize_note_reaction_key(), which normalizes filtered keys the same way reactions are stored: lowercase, padded to four digits, U+FE0F dropped. Malformed keys, surrogates, values past U+10FFFF, empty labels and duplicate keys are dropped.
  • The REST API validates reactions against the filtered list instead of the hardcoded one.
  • The list is passed to the editor as the noteReactionEmojis block editor setting, so the reaction menu offers exactly the emoji the server accepts.

Example:

<?php
add_filter( 'wp_note_reaction_emojis', function ( $emojis ) {
        $emojis[] = array(
                'hexKey' => '1f984',
                'label'  => __( 'unicorn', 'my-plugin' ),
        );
        return $emojis;
} );

The filter docs note that the editor renders emoji as text, so newer emoji and flags may not display on every system. Choosing emoji that work for a site's users is left to the site for now.

Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/84200

The patch is stacked on the backport (​https://github.com/WordPress/wordpress-develop/pull/10930) and needs that to land first.

Change History (2)

This ticket was mentioned in ​PR #14132 on ​WordPress/wordpress-develop by ​@adamsilverstein.


4 weeks ago
#1

  • Keywords has-patch has-unit-tests added

Backport of the reaction emoji filter from ​https://github.com/WordPress/gutenberg/pull/84200 - this one is stacked on #10930 and should be reviewed after it, the first commits here are from that PR.

_Claude did the port from the Gutenberg side; here is the rundown:_

Adds a wp_note_reaction_emojis filter so a site can add or remove the emoji a note can be reacted with, or turn adding reactions off with an empty list.

  • Adds wp_get_note_reaction_emojis(), which runs the filter and drops malformed entries, empty labels and duplicate keys, and wp_get_note_reaction_keys().
  • Adds wp_normalize_note_reaction_key(), which normalizes filtered keys to the stored form (lowercase, padded to four digits, U+FE0F dropped) and rejects surrogates and values past U+10FFFF.
  • The comments controller validates reactions against wp_get_note_reaction_keys(), replacing its private hardcoded list.
  • get_block_editor_settings() passes the list to the editor as noteReactionEmojis, so the reaction menu offers the same emoji the REST API accepts.

Existing reactions whose emoji is filtered out stay visible and can still be removed by their authors.

Only the commits after backport-note-reactions-meta-70 belong to this change: ​https://github.com/adamsilverstein/wordpress-develop/compare/backport-note-reactions-meta-70...backport-note-reaction-emoji-filter

Test plan

  • Add a filter that drops the heart and adds a unicorn:
    add_filter( 'wp_note_reaction_emojis', function ( $emojis ) {
            $emojis   = array_slice( $emojis, 1 );
            $emojis[] = array( 'hexKey' => '1f984', 'label' => 'unicorn' );
            return $emojis;
    } );
    
  • POST a 1f984 reaction to a note: 201. POST 2764: 400 rest_comment_invalid_reaction.
  • Return an empty array from the filter, then DELETE an existing reaction of your own: 200.
  • Check that noteReactionEmojis in the editor settings matches the filtered list.
  • phpunit --group comment,notes,restapi and phpunit --filter Tests_Blocks_Editor

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Porting the code and tests from the Gutenberg PR and writing this description. I will review and test.

🤖 Generated with ​Claude Code

​https://claude.ai/code/session_017FGp5zYv14MKoTuh3RFuWi

#2 @ekamran
4 weeks ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested

Environment

  • WordPress: 7.2-alpha-64238 with the PR applied. The PR is based on r64244, and none of its files changed between the two.
  • PHP: 8.4.24
  • Server: PHP built-in server (WordPress Studio)
  • Database: SQLite (WordPress Studio)
  • Browser: Chrome 152
  • OS: macOS 27.0.1
  • Theme: Twenty Twenty-Five 1.5
  • MU-Plugins: a test plugin that switches the wp_note_reaction_emojis filter between the cases below
  • Plugins: None activated
  • Unit tests: PHPUnit 9.6.38 on PHP 8.3.35 and MariaDB 12.3.3, on the PR branch

Steps

  1. As an editor, create a draft post and a note on it with status hold, so the thread is open.
  2. For each filter case, read wp_get_note_reaction_emojis() and the noteReactionEmojis block editor setting, then add reactions through POST /wp/v2/comments with type set to reaction.
  3. Run the unit tests in the 11 test files the PR touches.

Actual Results

Reactions added through the REST API (✅ 201, ❌ 400 rest_comment_invalid_reaction):

Filter returns 2764 heart 1f440 eyes 1f984 unicorn 0023-20e3 keycap #
no change (defaults) ✅ ✅ ❌ ❌
defaults plus unicorn, keycap #, rainbow flag, heart on fire and the flag of India ✅ ✅ ✅ ✅
defaults without eyes ✅ ❌ ❌ ❌
an empty array ❌ ❌ ❌ ❌
a string ✅ ✅ ❌ ❌
  • The noteReactionEmojis editor setting matched wp_get_note_reaction_emojis() in every case.
  • With the empty list, the editor who reacted with eyes earlier could still remove that reaction (200), and the note's reaction summary still listed it until then.
  • A duplicate (2764-FE0F), a non-hex key, a key past U+10FFFF, a surrogate, an empty label and an entry that is not an array were all dropped. Uppercase keys came back lowercase.

Unit tests, all passing (626 tests):

Test file Result
admin/includes/comment/CommentExists_Test.php OK (7 tests, 9 assertions)
admin/wpCommentsListTable.php OK (11 tests, 28 assertions)
blocks/editor.php OK (32 tests, 119 assertions)
comment.php OK (103 tests, 205 assertions)
comment/getLastCommentModified.php OK (13 tests, 19 assertions)
comment/isAvatarCommentType.php OK (7 tests, 9 assertions)
comment/query.php OK (162 tests, 482 assertions)
comment/wpGetNoteReactionEmojis.php OK (16 tests, 16 assertions)
comment/wpUpdateCommentCountNow.php OK (4 tests, 13 assertions)
query/commentFeed.php OK (9 tests, 36 assertions)
rest-api/rest-comments-controller.php OK (262 tests, 1258 assertions)

Additional Notes

  • ✅ The filter works as described on the server side, including the empty list and the fallback for a non-array value.
  • ❌ Some emoji sequences that need U+FE0F can display wrong in the editor. wp_normalize_note_reaction_key() drops every U+FE0F, and hexKeyToEmoji() in ​https://github.com/WordPress/gutenberg/pull/84200 adds it back only for a single code point. A keycap added through the filter as 0023-FE0F-20E3 (#️⃣) is stored as 0023-20e3, and hexKeyToEmoji() returns # followed by U+20E3, which Chrome on macOS shows as a # next to an empty box. The rainbow flag (1F3F3-FE0F-200D-1F308) and heart on fire (2764-FE0F-200D-1F525) lose their U+FE0F the same way. They still look right on macOS, but Unicode's emoji-test.txt lists the forms without U+FE0F as unqualified, so other systems may not show them as emoji.

One way to fix it is to drop U+FE0F only when it follows a single code point, and keep it inside sequences. I tried this on the PR branch:

  • src/wp-includes/comment.php

    diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php
    index 8d67158a23..9bdf26bb43 100644
    a b function wp_normalize_note_reaction_key( $hex_key ): string {  
    409409                if ( $value > 0x10FFFF || ( $value >= 0xD800 && $value <= 0xDFFF ) ) {
    410410                        return '';
    411411                }
    412                 if ( 0xFE0F === $value ) {
    413                         continue;
    414                 }
    415412                $code_points[] = str_pad( dechex( $value ), 4, '0', STR_PAD_LEFT );
    416413        }
    417414
     415        /*
     416         * U+FE0F after a single code point only asks for emoji presentation, which
     417         * the editor adds back. Inside a sequence it is part of the emoji, as in
     418         * #️⃣ or 🏳️‍🌈, so it stays.
     419         */
     420        if ( 'fe0f' === end( $code_points ) && count( $code_points ) <= 2 ) {
     421                array_pop( $code_points );
     422        }
     423
    418424        return implode( '-', $code_points );
    419425}
    420426

With it, 0023-FE0F-20E3 is stored as 0023-fe0f-20e3, and the same hexKeyToEmoji() returns the fully-qualified #️⃣, 🏳️‍🌈 and ❤️‍🔥. 2764-FE0F still becomes 2764 and a lone fe0f is still dropped. wpGetNoteReactionEmojis.php (16 tests), blocks/editor.php (32) and rest-comments-controller.php (262) still pass. gutenberg_normalize_note_reaction_key() in the Gutenberg PR would need the same change. The editor does not build keys from typed emoji on either Gutenberg branch (​https://github.com/WordPress/gutenberg/pull/76767 and the PR above); it sends the keys from this list, so nothing else has to match.

  • Minor: keys that are not emoji pass the format check, for example 0000, fffe and a lone 200d. They can only come from the filter, so the risk is low, but the filter docs say malformed keys are dropped.

AI assistance: Yes, Claude Code, for setting up the test site and the unit test environment, running the checks and drafting this report. I reviewed the results.

Note: See TracTickets for help on using tickets.