Repository navigation
Comments: Tighten note and reaction rules in all handlers. #15853
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
Changes from all commits
fab4a1d
1d53146
738b1ab
98e9c0f
281923b
4b266f8
4b873e1
946c74d
144ec65
83c8c40
96e7e1f
b7ac133
ba24806
22c761e
3544bc9
f0fbb5b
4db6f95
5383e31
9c697bb
2e27bfd
e2bdfcb
eb12398
d3254fd
290f638
789bc4a
290628f
2cb2d8a
ed7aaa4
7ec614d
3fb4fae
3d11f72
61f82d7
572928d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -731,6 +731,14 @@ public function create_item( $request ) { | |||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if ( 'note' === $request['type'] && $request['parent'] ) { | ||||||||||
| $parent_check = $this->check_note_parent( absint( $request['parent'] ), absint( $request['post'] ) ); | ||||||||||
|
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. The |
||||||||||
|
|
||||||||||
| if ( is_wp_error( $parent_check ) ) { | ||||||||||
| return $parent_check; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* | ||||||||||
| * The canonical reaction key, populated once validated below so the | ||||||||||
| * stored content matches what was validated (not the raw input). | ||||||||||
|
|
@@ -841,10 +849,12 @@ public function create_item( $request ) { | |||||||||
| * | ||||||||||
| * Scope to active (approved) reactions only — trashed reactions | ||||||||||
| * are invisible to the user and must not block re-adding the | ||||||||||
| * same emoji. | ||||||||||
| * same emoji. The same goes for a reaction stored on another | ||||||||||
| * post, which the reaction summary leaves out. | ||||||||||
| */ | ||||||||||
| $existing = get_comments( | ||||||||||
| array( | ||||||||||
| 'post_id' => absint( $request['post'] ), | ||||||||||
| 'parent' => $request['parent'], | ||||||||||
| 'user_id' => get_current_user_id(), | ||||||||||
| 'type' => 'reaction', | ||||||||||
|
|
@@ -1035,6 +1045,7 @@ public function create_item( $request ) { | |||||||||
| if ( null !== $reaction_key ) { | ||||||||||
| $matching = get_comments( | ||||||||||
| array( | ||||||||||
| 'post_id' => absint( $request['post'] ), | ||||||||||
| 'parent' => $request['parent'], | ||||||||||
| 'user_id' => get_current_user_id(), | ||||||||||
| 'type' => 'reaction', | ||||||||||
|
|
@@ -1233,6 +1244,40 @@ public function update_item( $request ) { | |||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if ( 'note' === $comment->comment_type ) { | ||||||||||
| $post_id = isset( $request['post'] ) ? absint( $request['post'] ) : (int) $comment->comment_post_ID; | ||||||||||
| $parent_id = isset( $request['parent'] ) ? absint( $request['parent'] ) : (int) $comment->comment_parent; | ||||||||||
|
Comment on lines
+1248
to
+1249
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. I believe the
Suggested change
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. i might have missed this before committing. if so i'll try to add it in a follow up commit.
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. No, I added it as a post-commit comment. |
||||||||||
| $is_moving = $post_id !== (int) $comment->comment_post_ID; | ||||||||||
|
|
||||||||||
| if ( $parent_id && ( $is_moving || $parent_id !== (int) $comment->comment_parent ) ) { | ||||||||||
| $parent_check = $this->check_note_parent( $parent_id, $post_id ); | ||||||||||
|
|
||||||||||
| if ( is_wp_error( $parent_check ) ) { | ||||||||||
| return $parent_check; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Replies and reactions stay on the note's post, so a move would separate them. | ||||||||||
| if ( $is_moving ) { | ||||||||||
| $children = get_comments( | ||||||||||
| array( | ||||||||||
| 'parent' => $id, | ||||||||||
| 'type' => array( 'note', 'reaction' ), | ||||||||||
| 'status' => 'any', | ||||||||||
| 'count' => true, | ||||||||||
| ) | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| if ( $children ) { | ||||||||||
| return new WP_Error( | ||||||||||
| 'rest_comment_has_children', | ||||||||||
| __( 'A note with replies or reactions cannot be moved to another post.' ), | ||||||||||
| array( 'status' => 400 ) | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* | ||||||||||
| * Re-sending the current author is a no-op. Otherwise the author's details | ||||||||||
| * would be refreshed from their user account, exposing any change to their | ||||||||||
|
|
@@ -2010,12 +2055,14 @@ public function get_item_schema() { | |||||||||
| 'parent' => array( | ||||||||||
| 'description' => __( 'The ID for the parent of the comment.' ), | ||||||||||
| 'type' => 'integer', | ||||||||||
| 'minimum' => 0, | ||||||||||
| 'context' => array( 'view', 'edit', 'embed' ), | ||||||||||
| 'default' => 0, | ||||||||||
| ), | ||||||||||
| 'post' => array( | ||||||||||
| 'description' => __( 'The ID of the associated post object.' ), | ||||||||||
| 'type' => 'integer', | ||||||||||
| 'minimum' => 0, | ||||||||||
| 'context' => array( 'view', 'edit' ), | ||||||||||
| 'default' => 0, | ||||||||||
| ), | ||||||||||
|
|
@@ -2189,7 +2236,8 @@ public function get_collection_params() { | |||||||||
| 'description' => __( 'Limit result set to comments of specific parent IDs.' ), | ||||||||||
| 'type' => 'array', | ||||||||||
| 'items' => array( | ||||||||||
| 'type' => 'integer', | ||||||||||
| 'type' => 'integer', | ||||||||||
| 'minimum' => 0, | ||||||||||
| ), | ||||||||||
| ); | ||||||||||
|
|
||||||||||
|
|
@@ -2374,26 +2422,27 @@ protected function prefetch_reaction_summaries( $note_ids ) { | |||||||||
|
|
||||||||||
| $current_user_id = get_current_user_id(); | ||||||||||
|
|
||||||||||
| $parent_in = $wpdb->prepare( | ||||||||||
| 'comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')', | ||||||||||
| // Only reactions stored on their note's post count, which is what create_item() writes. | ||||||||||
| $from = "FROM $wpdb->comments AS r INNER JOIN $wpdb->comments AS n"; | ||||||||||
| $from .= " ON n.comment_ID = r.comment_parent AND n.comment_post_ID = r.comment_post_ID AND n.comment_type = 'note'"; | ||||||||||
|
adamsilverstein marked this conversation as resolved.
Resolved
|
||||||||||
| $where = "WHERE r.comment_type = 'reaction' AND r.comment_approved = '1' AND "; | ||||||||||
| $where .= $wpdb->prepare( | ||||||||||
| 'r.comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')', | ||||||||||
| ...$note_ids | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Query 1: Get aggregated counts per emoji per note. | ||||||||||
| $query = "SELECT comment_parent, comment_content, COUNT(*) AS reaction_count FROM $wpdb->comments WHERE comment_type = 'reaction' AND comment_approved = '1' AND "; | ||||||||||
| $query .= $parent_in; | ||||||||||
| $query .= ' GROUP BY comment_parent, comment_content'; | ||||||||||
| // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | ||||||||||
| $counts = $wpdb->get_results( $query ); | ||||||||||
| $query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content"; | ||||||||||
| /** @var list<object{ comment_parent: decimal-int-string, comment_content: string, reaction_count: decimal-int-string }&stdClass>|null $counts */ | ||||||||||
| $counts = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above. | ||||||||||
|
|
||||||||||
| // Query 2: Get the current user's own reaction IDs (if logged in). | ||||||||||
| $my_reactions = array(); | ||||||||||
| if ( $current_user_id ) { | ||||||||||
| $query = "SELECT comment_ID, comment_parent, comment_content FROM $wpdb->comments WHERE comment_type = 'reaction' AND comment_approved = '1' AND "; | ||||||||||
| $query .= $parent_in; | ||||||||||
| $query .= $wpdb->prepare( ' AND user_id = %d', $current_user_id ); | ||||||||||
| // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | ||||||||||
| $user_rows = $wpdb->get_results( $query ); | ||||||||||
| $query = "SELECT r.comment_ID, r.comment_parent, r.comment_content $from $where"; | ||||||||||
| $query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id ); | ||||||||||
| /** @var list<object{ comment_ID: decimal-int-string, comment_parent: decimal-int-string, comment_content: string }&stdClass>|null $user_rows */ | ||||||||||
| $user_rows = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above. | ||||||||||
|
|
||||||||||
| if ( $user_rows ) { | ||||||||||
| foreach ( $user_rows as $row ) { | ||||||||||
|
|
@@ -2412,15 +2461,27 @@ protected function prefetch_reaction_summaries( $note_ids ) { | |||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $allowed_keys = self::get_note_reaction_keys(); | ||||||||||
|
|
||||||||||
| foreach ( $counts as $row ) { | ||||||||||
| $note_id = (int) $row->comment_parent; | ||||||||||
| $emoji_key = wp_strip_all_tags( $row->comment_content ); | ||||||||||
| $key = $note_id . ':' . $emoji_key; | ||||||||||
|
|
||||||||||
| $this->reaction_summaries[ $note_id ][ $emoji_key ] = array( | ||||||||||
| 'count' => (int) $row->reaction_count, | ||||||||||
| 'current_user_reaction' => $my_reactions[ $key ] ?? 0, | ||||||||||
| ); | ||||||||||
| // Only curated emoji keys are summarized. | ||||||||||
| 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; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -2699,6 +2760,33 @@ protected function check_target_post_permission( int $post_id, WP_REST_Request $ | |||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Checks that a note's parent is a note on the same post. | ||||||||||
| * | ||||||||||
| * @since 7.2.0 | ||||||||||
| * | ||||||||||
| * @param int $parent_id ID of the parent comment. | ||||||||||
| * @param int $post_id ID of the post the note is, or will be, on. | ||||||||||
| * @return true|WP_Error True if the parent is valid, error object otherwise. | ||||||||||
| */ | ||||||||||
| private function check_note_parent( int $parent_id, int $post_id ) { | ||||||||||
| $parent = get_comment( $parent_id ); | ||||||||||
|
|
||||||||||
| if ( | ||||||||||
| ! $parent || | ||||||||||
| 'note' !== $parent->comment_type || | ||||||||||
| ( $post_id && $post_id !== (int) $parent->comment_post_ID ) | ||||||||||
| ) { | ||||||||||
| return new WP_Error( | ||||||||||
| 'rest_comment_invalid_parent', | ||||||||||
| __( 'A note reply must be attached to a note on the same post.' ), | ||||||||||
| array( 'status' => 400 ) | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Checks whether a note belongs to a resolved thread. | ||||||||||
| * | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.