-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Editor: Add emoji reactions as a comment type for Notes #10930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamsilverstein
wants to merge
6
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:backport-note-reactions-meta-70
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe19243
Editor: Add emoji reactions as a comment type for Notes.
adamsilverstein 82a98f7
Merge branch 'trunk' into backport-note-reactions-meta-70
adamsilverstein b3b78f8
Tests: Regenerate wp-api-generated.js fixtures after trunk merge.
adamsilverstein 059480d
Merge branch 'trunk' into backport-note-reactions-meta-70
adamsilverstein ec10e92
Comments: Centralize internal comment types via wp_get_internal_comme…
adamsilverstein 7a83b58
Apply suggestions from code review
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,7 +123,7 @@ public function register_routes() { | |
| * @return true|WP_Error True if the request has read access, error object otherwise. | ||
| */ | ||
| public function get_items_permissions_check( $request ) { | ||
| $is_note = 'note' === $request['type']; | ||
| $is_note = in_array( $request['type'], wp_get_internal_comment_types(), true ); | ||
| $is_edit_context = 'edit' === $request['context']; | ||
| $protected_params = array( 'author', 'author_exclude', 'author_email', 'type', 'status' ); | ||
| $forbidden_params = array(); | ||
|
|
@@ -437,8 +437,8 @@ public function get_item_permissions_check( $request ) { | |
| return $comment; | ||
| } | ||
|
|
||
| // 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, wp_get_internal_comment_types(), true ) ? array( 'edit_comment', $comment->comment_ID ) : array( 'moderate_comments' ); | ||
| if ( ! empty( $request['context'] ) && 'edit' === $request['context'] && ! current_user_can( ...$edit_cap ) ) { | ||
| return new WP_Error( | ||
| 'rest_forbidden_context', | ||
|
|
@@ -497,7 +497,7 @@ public function get_item( $request ) { | |
| * @return true|WP_Error True if the request has access to create items, error object otherwise. | ||
| */ | ||
| public function create_item_permissions_check( $request ) { | ||
| $is_note = ! empty( $request['type'] ) && 'note' === $request['type']; | ||
| $is_note = ! empty( $request['type'] ) && in_array( $request['type'], wp_get_internal_comment_types(), true ); | ||
|
|
||
| if ( ! is_user_logged_in() && $is_note ) { | ||
| return new WP_Error( | ||
|
|
@@ -657,14 +657,65 @@ public function create_item( $request ) { | |
| } | ||
|
|
||
| // Do not allow comments to be created with a non-core type. | ||
| if ( ! empty( $request['type'] ) && ! in_array( $request['type'], array( 'comment', 'note' ), true ) ) { | ||
| if ( ! empty( $request['type'] ) && ! in_array( $request['type'], array_merge( array( 'comment' ), wp_get_internal_comment_types() ), true ) ) { | ||
| return new WP_Error( | ||
| 'rest_invalid_comment_type', | ||
| __( 'Cannot create a comment with that type.' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| // Validate reaction-specific constraints. | ||
| if ( ! empty( $request['type'] ) && 'reaction' === $request['type'] ) { | ||
| $valid_emojis = array( 'heart', 'celebration', 'smile', 'eyes', 'rocket' ); | ||
|
|
||
| // Reaction content must be a valid emoji slug. | ||
| if ( empty( $request['content'] ) || ! in_array( $request['content'], $valid_emojis, true ) ) { | ||
| return new WP_Error( | ||
| 'rest_reaction_invalid_emoji', | ||
| __( 'Reaction content must be a valid emoji slug.' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| // Reaction parent must exist and be a note. | ||
| if ( empty( $request['parent'] ) ) { | ||
| return new WP_Error( | ||
| 'rest_reaction_parent_required', | ||
| __( 'Reactions must have a parent note.' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| $parent_comment = get_comment( $request['parent'] ); | ||
| if ( ! $parent_comment || 'note' !== $parent_comment->comment_type ) { | ||
| return new WP_Error( | ||
| 'rest_reaction_invalid_parent', | ||
| __( 'Reactions can only be added to notes.' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| // Enforce uniqueness: one emoji per user per note. | ||
|
Comment on lines
+690
to
+699
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future, perhaps reactions could be enabled for non-note comments as well?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, or blocks could have reactions! |
||
| $existing = get_comments( | ||
| array( | ||
| 'comment_type' => 'reaction', | ||
| 'comment_parent' => $request['parent'], | ||
| 'user_id' => get_current_user_id(), | ||
| 'search' => $request['content'], | ||
| 'count' => true, | ||
| ) | ||
| ); | ||
|
|
||
| if ( $existing > 0 ) { | ||
| return new WP_Error( | ||
| 'rest_reaction_duplicate', | ||
| __( 'You have already added this reaction.' ), | ||
| array( 'status' => 409 ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| $prepared_comment = $this->prepare_item_for_database( $request ); | ||
| if ( is_wp_error( $prepared_comment ) ) { | ||
| return $prepared_comment; | ||
|
|
@@ -743,9 +794,9 @@ public function create_item( $request ) { | |
| ); | ||
| } | ||
|
|
||
| // Don't check for duplicates or flooding for notes. | ||
| // 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'], wp_get_internal_comment_types(), true ) ? | ||
| '1' : | ||
| wp_allow_comment( $prepared_comment, true ); | ||
|
|
||
|
|
@@ -1305,7 +1356,7 @@ protected function prepare_links( $comment ) { | |
| } | ||
|
|
||
| // 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, wp_get_internal_comment_types(), true ) ) { | ||
| $args = array( | ||
| 'parent' => $comment->comment_ID, | ||
| 'type' => $comment->comment_type, | ||
|
|
@@ -1919,7 +1970,7 @@ protected function check_read_post_permission( $post, $request ) { | |
| * @return bool Whether the comment can be read. | ||
| */ | ||
| protected function check_read_permission( $comment, $request ) { | ||
| if ( 'note' !== $comment->comment_type && ! empty( $comment->comment_post_ID ) ) { | ||
| if ( ! in_array( $comment->comment_type, wp_get_internal_comment_types(), true ) && ! empty( $comment->comment_post_ID ) ) { | ||
| $post = get_post( $comment->comment_post_ID ); | ||
| if ( $post ) { | ||
| if ( $this->check_read_post_permission( $post, $request ) && 1 === (int) $comment->comment_approved ) { | ||
|
|
@@ -2034,6 +2085,11 @@ protected function check_is_comment_content_allowed( $prepared_comment ) { | |
| return true; | ||
| } | ||
|
|
||
| // Reactions always have content (the emoji slug), so allow them. | ||
| if ( isset( $check['comment_type'] ) && 'reaction' === $check['comment_type'] ) { | ||
| return true; | ||
| } | ||
|
|
||
| /* | ||
| * Do not allow a comment to be created with missing or empty | ||
| * comment_content. See wp_handle_comment_submission(). | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No 💩? 😜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ideally the list will be filterable at some point. initially keeping the list short and simple to keep the UI simple.