Editor: Add emoji reactions as a comment type for Notes#10930
Editor: Add emoji reactions as a comment type for Notes#10930adamsilverstein wants to merge 2 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| } | ||
| } | ||
| }, | ||
| "authentication": [], |
There was a problem hiding this comment.
Why did this changed? Stale from a previous commit?
There was a problem hiding this comment.
yes, I ran the test_build_wp_api_client_fixtures test locally to regenerate fixtures and this was the result. I wrote that test so I'm confident this is correct, but happy to add in a separate PR since its unrelated to the current changes. ideally we should have a check similar to package lock that checks if running the regeneration changes the fixture and reject the merge if so.
| "properties": [], | ||
| "properties": { | ||
| "wp_pattern_sync_status": { | ||
| "type": "string", | ||
| "title": "", | ||
| "description": "", | ||
| "default": "", | ||
| "enum": [ | ||
| "partial", | ||
| "unsynced" | ||
| ] | ||
| } | ||
| }, |
There was a problem hiding this comment.
I guess this fixture wasn't updated correctly for a previous commit that introduced this?
There was a problem hiding this comment.
correct. you can verify by running the test_build_wp_api_client_fixtures test locally to regenerate fixtures. the file should match what you see in this PR. (I used WP_TESTS_DIR=/Users/adamsilverstein/repositories/wordpress-develop/tests/phpunit/ DB_PASSWORD=**** php ./vendor/bin/phpunit --verbose -c ./phpunit.xml.dist --filter=test_build_wp_api_client_fixture)
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Introduce the `reaction` comment type to support emoji reactions on collaborative Notes, replacing the previous `_wp_note_reactions` meta approach. Changes include: - Add `reaction` to avatar comment types. - Exclude reactions from admin comment lists and comment counts. - Extend the REST API Comments Controller to handle reactions: permissions checks, validation (valid emoji slugs, parent must be a note, one emoji per user per note), auto-approval, and content allowed checks. - Add PHPUnit tests for reaction creation, validation, and counting. - Regenerate API fixtures. Props flavor flavor. See #63191. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
e8246a7 to
fe19243
Compare
|
I have updated this backport PR to apply the custom comment type approach for storage used in WordPress/gutenberg#75549 which replaces the meta based approach in WordPress/gutenberg#75144. |
| $comment_type = ''; | ||
|
|
||
| if ( ! empty( $_REQUEST['comment_type'] ) && 'note' !== $_REQUEST['comment_type'] ) { | ||
| if ( ! empty( $_REQUEST['comment_type'] ) && ! in_array( $_REQUEST['comment_type'], array( 'note', 'reaction' ), true ) ) { |
There was a problem hiding this comment.
It seems like perhaps there should be some centralized place for this list of non-comment comment types which can be re-used. This would avoid having to manually and tediously update all these instances whenever there is a new such type introduced. Maybe like a wp_get_internal_comment_types().
Then this could be:
| if ( ! empty( $_REQUEST['comment_type'] ) && ! in_array( $_REQUEST['comment_type'], array( 'note', 'reaction' ), true ) ) { | |
| if ( ! empty( $_REQUEST['comment_type'] ) && ! in_array( $_REQUEST['comment_type'], wp_get_internal_comment_types(), true ) ) { |
| 'post_id' => $post_id, | ||
| 'type' => $comment_type, | ||
| 'type__not_in' => array( 'note' ), | ||
| 'type__not_in' => array( 'note', 'reaction' ), |
There was a problem hiding this comment.
With the above suggestion:
| 'type__not_in' => array( 'note', 'reaction' ), | |
| 'type__not_in' => wp_get_internal_comment_types(), |
| $post_id_in = "'" . implode( "', '", $post_id_array ) . "'"; | ||
|
|
||
| $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' AND comment_type != 'note' GROUP BY comment_post_ID", ARRAY_A ); | ||
| $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' AND comment_type != 'note' AND comment_type != 'reaction' GROUP BY comment_post_ID", ARRAY_A ); |
There was a problem hiding this comment.
| $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' AND comment_type != 'note' AND comment_type != 'reaction' GROUP BY comment_post_ID", ARRAY_A ); | |
| $comment_type_not = implode( | |
| '', | |
| array_map( | |
| fn( $comment_type ) => sprintf( ' AND comment_type != "%s"' ), // TODO: Prepare!??? | |
| wp_get_internal_comment_types() | |
| ) | |
| ); | |
| $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' $comment_type_not GROUP BY comment_post_ID", ARRAY_A ); |
| */ | ||
| public function get_items_permissions_check( $request ) { | ||
| $is_note = 'note' === $request['type']; | ||
| $is_note = in_array( $request['type'], array( 'note', 'reaction' ), true ); |
There was a problem hiding this comment.
| $is_note = in_array( $request['type'], array( 'note', 'reaction' ), true ); | |
| $is_note = in_array( $request['type'], wp_get_internal_comment_types(), true ); |
| // Re-map edit context capabilities when requesting `note` type. | ||
| $edit_cap = 'note' === $comment->comment_type ? array( 'edit_comment', $comment->comment_ID ) : array( 'moderate_comments' ); | ||
| // Re-map edit context capabilities when requesting `note` or `reaction` type. | ||
| $edit_cap = in_array( $comment->comment_type, array( 'note', 'reaction' ), true ) ? array( 'edit_comment', $comment->comment_ID ) : array( 'moderate_comments' ); |
There was a problem hiding this comment.
| $edit_cap = in_array( $comment->comment_type, array( 'note', 'reaction' ), true ) ? array( 'edit_comment', $comment->comment_ID ) : array( 'moderate_comments' ); | |
| $edit_cap = in_array( $comment->comment_type, wp_get_internal_comment_types(), true ) ? array( 'edit_comment', $comment->comment_ID ) : array( 'moderate_comments' ); |
| // Don't check for duplicates or flooding for notes or reactions. | ||
| $prepared_comment['comment_approved'] = | ||
| 'note' === $prepared_comment['comment_type'] ? | ||
| in_array( $prepared_comment['comment_type'], array( 'note', 'reaction' ), true ) ? |
There was a problem hiding this comment.
| in_array( $prepared_comment['comment_type'], array( 'note', 'reaction' ), true ) ? | |
| in_array( $prepared_comment['comment_type'], wp_get_internal_comment_types(), true ) ? |
|
|
||
| // Embedding children for notes requires `type` and `status` inheritance. | ||
| if ( isset( $links['children'] ) && 'note' === $comment->comment_type ) { | ||
| if ( isset( $links['children'] ) && in_array( $comment->comment_type, array( 'note', 'reaction' ), true ) ) { |
There was a problem hiding this comment.
| if ( isset( $links['children'] ) && in_array( $comment->comment_type, array( 'note', 'reaction' ), true ) ) { | |
| if ( isset( $links['children'] ) && in_array( $comment->comment_type, wp_get_internal_comment_types(), true ) ) { |
| */ | ||
| protected function check_read_permission( $comment, $request ) { | ||
| if ( 'note' !== $comment->comment_type && ! empty( $comment->comment_post_ID ) ) { | ||
| if ( ! in_array( $comment->comment_type, array( 'note', 'reaction' ), true ) && ! empty( $comment->comment_post_ID ) ) { |
There was a problem hiding this comment.
| if ( ! in_array( $comment->comment_type, array( 'note', 'reaction' ), true ) && ! empty( $comment->comment_post_ID ) ) { | |
| if ( ! in_array( $comment->comment_type, wp_get_internal_comment_types(), true ) && ! empty( $comment->comment_post_ID ) ) { |
|
|
||
| if ( is_null( $new ) ) { | ||
| $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note'", $post_id ) ); | ||
| $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note' AND comment_type != 'reaction'", $post_id ) ); |
There was a problem hiding this comment.
Ditto above with using something like wp_get_internal_comment_types().
| * @param array $types An array of content types. Default contains 'comment', 'note', and 'reaction'. | ||
| */ | ||
| $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment', 'note' ) ); | ||
| $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment', 'note', 'reaction' ) ); |
There was a problem hiding this comment.
| $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment', 'note', 'reaction' ) ); | |
| $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array_merge( array( 'comment' ), wp_get_internal_comment_types() ) ); |
Summary
Introduces the
reactioncomment type to support emoji reactions on collaborative Notes, replacing the previous_wp_note_reactionsmeta approach from Gutenberg PR #75148 (now closed in favor of #75549).Each reaction is stored as a separate comment with
comment_type = 'reaction'rather than as serialized meta data on the note.Changes
reactionto the allowed avatar comment typesWP_REST_Comments_Controllerto handlereactiontype:edit_comment/edit_postinstead ofmoderate_comments)heart,celebration,smile,eyes,rocket), one emoji per user per notewp-api-generated.jsTrac ticket
See https://core.trac.wordpress.org/ticket/63191
Test plan
heartreaction to the note — should succeed (201)rocket) to the same note — should succeedphpunit --filter=test_create_reaction— all passphpunit tests/phpunit/tests/comment/wpUpdateCommentCountNow.php— all pass🤖 Generated with Claude Code