diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 076549b218b82..ba3b9bc2851a1 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1310,12 +1310,33 @@ function wp_ajax_replyto_comment( $action ) { $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment'; + /* + * Notes and reactions are only created through the REST API. The lowercase + * ASCII limit also catches spellings the case- and accent-insensitive + * collation would match. + */ + if ( + ! preg_match( '/^[a-z0-9_-]+\z/', $comment_type ) || + in_array( $comment_type, array( 'note', 'reaction' ), true ) + ) { + wp_die( -1 ); + } + $comment_parent = 0; if ( isset( $_POST['comment_ID'] ) ) { $comment_parent = absint( $_POST['comment_ID'] ); } + // A comment on another post cannot be the parent. + if ( $comment_parent ) { + $parent = get_comment( $comment_parent ); + + if ( $parent && (int) $parent->comment_post_ID !== $comment_post_id ) { + $comment_parent = 0; + } + } + $comment_auto_approved = false; $commentdata = array( diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 5f46be6a5cdfa..49c2b3d32c421 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -80,6 +80,11 @@ function edit_comment() { $comment_id = (int) $_POST['comment_ID']; $comment = get_comment( $comment_id ); + // Reactions are only validated on creation. Removing one is a delete. + if ( $comment && 'reaction' === $comment->comment_type ) { + wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), 403 ); + } + /* * The comment type is not editable from the Edit Comment screen. Changing it * would turn a private note into a public comment or vice versa, and would @@ -135,7 +140,7 @@ function edit_comment() { $comment_status = $_POST['comment_approved'] ?? $comment->comment_approved; $comment_is_approved = in_array( $comment_status, array( 1, '1', 'approve' ), true ); - // The parent must be a comment of the same type, on the same post, and publicly visible if the comment is approved. + // The parent must be a comment of the same type, on the post the comment will be on, and publicly visible if the comment is approved. if ( ! $parent || (int) $parent->comment_post_ID !== $comment_post_id @@ -196,6 +201,19 @@ function edit_comment() { } } + // The checks above only run when the parent changes. A kept parent must be on the destination post too. + if ( $comment && $comment_post_id !== (int) $comment->comment_post_ID ) { + $kept_parent = (int) ( isset( $_POST['comment_parent'] ) && is_numeric( $_POST['comment_parent'] ) ? $_POST['comment_parent'] : $comment->comment_parent ); + + if ( $kept_parent ) { + $parent = get_comment( $kept_parent ); + + if ( ! $parent || (int) $parent->comment_post_ID !== $comment_post_id ) { + return new WP_Error( 'comment_parent_invalid', __( 'Invalid parent comment.' ) ); + } + } + } + foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) { if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] !== $_POST[ $timeunit ] ) { $_POST['edit_date'] = '1'; @@ -283,6 +301,20 @@ static function ( $item ) { return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) ); } + // Replies and reactions stay on the note's post, so a move would separate them. + $children = get_comments( + array( + 'parent' => $comment->comment_ID, + 'type' => array( 'note', 'reaction' ), + 'status' => 'any', + 'count' => true, + ) + ); + + if ( $children ) { + return new WP_Error( 'comment_post_invalid', __( 'A note with replies or reactions cannot be moved to another post.' ) ); + } + return true; } diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index 515a2458f9e38..56448aa6e2520 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -3971,7 +3971,9 @@ public function wp_editComment( $args ) { return $this->error; } - if ( ! get_comment( $comment_id ) ) { + $existing_comment = get_comment( $comment_id ); + + if ( ! $existing_comment ) { return new IXR_Error( 404, __( 'Invalid comment ID.' ) ); } @@ -3979,6 +3981,11 @@ public function wp_editComment( $args ) { return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) ); } + // Reactions are only validated on creation. Removing one is a delete. + if ( 'reaction' === $existing_comment->comment_type ) { + return new IXR_Error( 403, __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) ); + } + /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */ do_action( 'xmlrpc_call', 'wp.editComment', $args, $this ); $comment = array( diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 490bd7659ea59..3bb44059052eb 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -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'] ) ); + + 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; + $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'"; + $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|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|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. * diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index fb39bd4dae41b..155a7ccc278db 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -242,4 +242,521 @@ public function test_wp_update_comment_data_is_wp_error() { public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) { return new WP_Error( 'comment_wrong', 'wp_update_comment_data filter fails for this comment.', 500 ); } + + /** + * The curated reaction key for the heart emoji, as stored in a reaction's content. + */ + private const HEART_REACTION_KEY = '2764'; + + /** + * Creates an administrator's open note and approved reaction on a post. + * + * @param int $post_id Post to attach the note to. + * @param int $admin_id User who owns the note and reaction. + * @return positive-int The reaction ID. + */ + private function create_admin_reaction( int $post_id, int $admin_id ): int { + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + + return self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => $admin_id, + 'comment_content' => self::HEART_REACTION_KEY, + ) + ); + } + + /** + * Reactions are immutable. The REST API refuses to update them, and the + * admin-ajax writer has to refuse too, or anyone who can edit the post + * rewrites and reattributes other users' reactions. + * + * @ticket 66635 + */ + public function test_cannot_edit_reaction(): void { + $this->_setRole( 'contributor' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + $reaction_id = $this->create_admin_reaction( $draft_id, $admin_id ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reaction_id, + 'content' => 'rewritten', + 'newcomment_author' => 'Forged', + 'newcomment_author_email' => 'forged@example.org', + 'user_id' => (string) get_current_user_id(), + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to edit a reaction.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( self::HEART_REACTION_KEY, $reaction->comment_content ); + $this->assertSame( (string) $admin_id, $reaction->user_id ); + $this->assertSame( '', $reaction->comment_author_email ); + } + + /** + * Moving a comment requires edit access to the destination post, not only + * to the post it is on. + * + * @ticket 66635 + */ + public function test_cannot_move_comment_to_post_user_cannot_edit(): void { + $this->_setRole( 'contributor' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + $admin_post_id = self::factory()->post->create( array( 'post_author' => $admin_id ) ); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $draft_id ) ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $comment_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $admin_post_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to move the comment.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $draft_id, $comment->comment_post_ID ); + } + + /** + * A user who can edit both posts can still move a comment between them. + * + * @ticket 66635 + */ + public function test_can_move_comment_to_post_user_can_edit(): void { + $this->_setRole( 'editor' ); + + $source_post_id = self::factory()->post->create(); + $target_post_id = self::factory()->post->create(); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $source_post_id ) ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $comment_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $target_post_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $target_post_id, $comment->comment_post_ID ); + } + + /** + * An author who can reply beneath a note on their own post must not be + * able to turn that reply into a reaction attributed to someone else. + * + * @ticket 66635 + */ + public function test_cannot_convert_comment_into_reaction(): void { + $this->_setRole( 'author' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $post_id = self::factory()->post->create( array( 'post_author' => get_current_user_id() ) ); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + + // Step 1: reply beneath the note with an ordinary comment. + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_post_ID' => (string) $post_id, + 'comment_ID' => (string) $note_id, + 'content' => 'Ordinary reply', + ) + ); + + try { + $this->_handleAjax( 'replyto-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $replies = get_comments( + array( + 'post_id' => $post_id, + 'parent' => $note_id, + 'type' => 'comment', + 'status' => 'all', + 'fields' => 'ids', + ) + ); + $this->assertCount( 1, $replies ); + $reply_id = (int) $replies[0]; + + // Step 2: edit the reply into a reaction owned by the administrator. + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reply_id, + 'content' => self::HEART_REACTION_KEY, + 'comment_type' => 'reaction', + 'user_id' => (string) $admin_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( 'comment', $reply->comment_type ); + $this->assertSame( 'Ordinary reply', $reply->comment_content ); + $this->assertSame( (string) get_current_user_id(), $reply->user_id ); + } + + /** + * A post ID of zero skips the destination check and would orphan the + * comment, which drops its capability mapping to the post. + * + * @ticket 66635 + */ + public function test_cannot_detach_comment_from_its_post(): void { + $this->_setRole( 'contributor' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $draft_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Editorial note', + 'comment_post_ID' => '0', + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $draft_id, $note->comment_post_ID ); + } + + /** + * The new parent has to be on the post the comment is moving to, not the + * one it is leaving. + * + * @ticket 66635 + */ + public function test_cannot_move_comment_under_parent_on_the_post_it_leaves(): void { + $this->_setRole( 'editor' ); + update_option( 'thread_comments', 1 ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $comment_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + 'comment_parent' => (string) $parent_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $post_a_id, $comment->comment_post_ID ); + $this->assertSame( '0', $comment->comment_parent ); + } + + /** + * A reply cannot move to another post while still threaded under a + * comment on the post it leaves. + * + * @ticket 66635 + */ + public function test_cannot_move_reply_away_from_its_parent(): void { + $this->_setRole( 'editor' ); + update_option( 'thread_comments', 1 ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + $reply_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_parent' => $parent_id, + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reply_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( (string) $post_a_id, $reply->comment_post_ID ); + } + + /** + * A reply can move to another post when the same request detaches it from + * its parent, since the submitted parent is the one that will be kept. + * + * @ticket 66635 + */ + public function test_can_move_reply_to_another_post_when_detached_from_its_parent(): void { + $this->_setRole( 'editor' ); + update_option( 'thread_comments', 1 ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + $reply_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_parent' => $parent_id, + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reply_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + 'comment_parent' => '0', + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( (string) $post_b_id, $reply->comment_post_ID ); + $this->assertSame( '0', $reply->comment_parent ); + } + + /** + * A note's reactions stay on its post, so moving the note would separate + * them and empty its reaction summary. + * + * @ticket 66635 + */ + public function test_cannot_move_note_that_has_reactions(): void { + $this->_setRole( 'editor' ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $reaction_id = $this->create_admin_reaction( $post_a_id, get_current_user_id() ); + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $note_id = (int) $reaction->comment_parent; + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_a_id, $note->comment_post_ID ); + } + + /** + * A note with no replies or reactions can still move. + * + * @ticket 66635 + */ + public function test_can_move_note_without_children(): void { + $this->_setRole( 'editor' ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_b_id, $note->comment_post_ID ); + } + + /** + * A note cannot stop being one, including by a change of case. + * + * @ticket 66635 + * + * @dataProvider data_types_a_note_cannot_become + * + * @param non-falsy-string $type The requested comment type. + */ + public function test_cannot_convert_note_into_another_type( string $type ): void { + $this->_setRole( 'editor' ); + + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Converted', + 'comment_type' => $type, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to change the note type.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( 'note', $note->comment_type ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_types_a_note_cannot_become(): array { + return array( + 'comment' => array( 'comment' ), + 'uppercase note' => array( 'NOTE' ), + ); + } } diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index b67a7ba828439..5c6d335aa7e68 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -270,4 +270,103 @@ public function test_pre_comments_approved() { public function _pre_comment_approved_filter( $approved, $commentdata ) { return new WP_Error( 'comment_wrong', 'pre_comment_approved filter fails for new comment.', 403 ); } + + /** + * Notes and reactions are created through the REST API, which enforces + * their invariants. The reply handler must not mint them. + * + * @ticket 66635 + * + * @dataProvider data_restricted_comment_types + * + * @param non-falsy-string $type The comment type to request. + */ + public function test_cannot_reply_with_restricted_comment_type( string $type ): void { + $this->_setRole( 'administrator' ); + + $post_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $parent_id, + 'content' => '2764', + 'comment_post_ID' => (string) $post_id, + 'comment_type' => $type, + ) + ); + + try { + $this->_handleAjax( 'replyto-comment' ); + $this->fail( 'The handler should have refused the comment type.' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage() ); + } + + $this->assertSame( + array(), + get_comments( + array( + 'type' => $type, + 'status' => 'any', + 'fields' => 'ids', + ) + ), + 'No comment of the restricted type should have been created.' + ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_restricted_comment_types(): array { + return array( + 'reaction' => array( 'reaction' ), + 'reaction, capitalized' => array( 'Reaction' ), + 'note' => array( 'note' ), + 'note, capitalized' => array( 'Note' ), + 'reaction, accented' => array( 'reactión' ), + 'uppercase custom type' => array( 'Custom' ), + ); + } + + /** + * A reply whose parent is on a different post is stored unthreaded. + * + * @ticket 66635 + */ + public function test_reply_to_comment_on_another_post_is_not_threaded(): void { + $this->_setRole( 'administrator' ); + + $post_id = self::factory()->post->create(); + $other_post = self::factory()->post->create(); + $other_comment = self::factory()->comment->create( array( 'comment_post_ID' => $other_post ) ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $other_comment, + 'content' => 'Reply across posts', + 'comment_post_ID' => (string) $post_id, + ) + ); + + try { + $this->_handleAjax( 'replyto-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); + $this->assertInstanceOf( SimpleXMLElement::class, $xml ); + + $comment = get_comment( (int) $xml->response[0]->comment['id'] ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + + $this->assertSame( (string) $post_id, $comment->comment_post_ID ); + $this->assertSame( '0', $comment->comment_parent, 'A parent on another post should be dropped.' ); + } } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c5c40b9b34da6..d66dc69308f62 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2654,4 +2654,44 @@ public function test_meta_query_invalidation_preserves_absent_callback() { add_action( 'added_comment_meta', 'wp_cache_set_comments_last_changed' ); } } + + /** + * The rules for notes and reactions belong to the request handlers. + * wp_update_comment() stays unrestricted, like other low-level writers. + * + * @ticket 66635 + */ + public function test_wp_update_comment_does_not_restrict_reactions(): void { + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'comment_content' => '2764', + ) + ); + + $result = wp_update_comment( + array( + 'comment_ID' => $reaction_id, + 'comment_content' => '1f680', + ), + true + ); + + $this->assertSame( 1, $result ); + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( '1f680', $reaction->comment_content ); + } } diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 7cb9f647063d9..513bdbfd51220 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6635,4 +6635,345 @@ public function test_reaction_summary_excludes_trashed_reactions() { $this->assertSame( array(), $response->get_data()['reaction_summary'] ); } + + /** + * The curated reaction key for the heart emoji, as stored in a reaction's content. + */ + private const HEART_REACTION_KEY = '2764'; + + /** + * Creates an open note by the editor on a new post. + * + * @return array{int, int} The post ID and the note ID. + */ + private function create_editor_note(): array { + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => self::$editor_id, + 'comment_content' => 'Test note', + ) + ); + + return array( $post_id, $note_id ); + } + + /** + * Reads a note's reaction summary as the editor. + * + * @param int $note_id The note ID. + * @return array + */ + private function get_reaction_summary( int $note_id ): array { + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'context', 'edit' ); + + $data = rest_get_server()->dispatch( $request )->get_data(); + $this->assertIsArray( $data ); + $this->assertArrayHasKey( 'reaction_summary', $data ); + $this->assertIsArray( $data['reaction_summary'] ); + + /** @var array $summary */ + $summary = $data['reaction_summary']; + + return $summary; + } + + /** + * A reaction stored against another post is not a reaction to this note, + * whatever its parent says. + * + * @ticket 66635 + */ + public function test_reaction_summary_ignores_reactions_stored_on_another_post(): void { + list( , $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $other_post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$subscriber_id, + 'comment_content' => self::HEART_REACTION_KEY, + ) + ); + + $this->assertSame( array(), $this->get_reaction_summary( $note_id ) ); + } + + /** + * Only the curated emoji keys are summarized. + * + * @ticket 66635 + */ + public function test_reaction_summary_ignores_uncurated_keys(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$subscriber_id, + 'comment_content' => 'not-an-emoji-key', + ) + ); + + $this->assertSame( array(), $this->get_reaction_summary( $note_id ) ); + } + + /** + * Rows that strip to the same key are counted together rather than the + * last one overwriting the others. + * + * @ticket 66635 + */ + public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_key(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + + $heart_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$editor_id, + 'comment_content' => self::HEART_REACTION_KEY, + ) + ); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$subscriber_id, + 'comment_content' => '' . self::HEART_REACTION_KEY . '', + ) + ); + + $summary = $this->get_reaction_summary( $note_id ); + + $this->assertCount( 1, $summary ); + $this->assertArrayHasKey( self::HEART_REACTION_KEY, $summary ); + $this->assertSame( 2, $summary[ self::HEART_REACTION_KEY ]['count'] ); + $this->assertSame( $heart_id, $summary[ self::HEART_REACTION_KEY ]['current_user_reaction'] ); + } + + /** + * A note reply has to sit on the same post as the note it answers, or a + * user could thread their own note under one on a post they cannot read. + * + * @ticket 66635 + */ + public function test_cannot_create_note_reply_under_note_on_another_post(): void { + list( , $note_id ) = $this->create_editor_note(); + $post_b_id = self::factory()->post->create(); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + $request->set_param( 'type', 'note' ); + $request->set_param( 'post', $post_b_id ); + $request->set_param( 'parent', $note_id ); + $request->set_param( 'content', 'Reply from another post' ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + } + + /** + * The same-post rule for note replies holds on update too. + * + * @ticket 66635 + */ + public function test_cannot_update_note_parent_to_note_on_another_post(): void { + list( , $note_a_id ) = $this->create_editor_note(); + list( , $note_b_id ) = $this->create_editor_note(); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_b_id ); + $request->set_param( 'parent', $note_a_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + + $note_b = get_comment( $note_b_id ); + $this->assertInstanceOf( WP_Comment::class, $note_b ); + $this->assertSame( '0', $note_b->comment_parent ); + } + + /** + * A note cannot be threaded under a regular comment by an update. + * + * @ticket 66635 + */ + public function test_cannot_update_note_parent_to_a_comment(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'parent', $comment_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + } + + /** + * Moving a note reply to another post would leave it under a note on the + * post it left. + * + * @ticket 66635 + */ + public function test_cannot_move_note_reply_away_from_its_parent(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + $reply_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_parent' => $note_id, + 'comment_approved' => 0, + 'user_id' => self::$editor_id, + ) + ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $reply_id ); + $request->set_param( 'post', $other_post_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( (string) $post_id, $reply->comment_post_ID ); + } + + /** + * A note's replies and reactions stay on its post, so moving the note + * would separate them and empty its reaction summary. + * + * @ticket 66635 + * + * @dataProvider data_note_child_types + * + * @param string $child_type Comment type of the note's child. + */ + public function test_cannot_move_note_that_has_children( string $child_type ): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => $child_type, + 'comment_parent' => $note_id, + 'comment_approved' => 'reaction' === $child_type ? 1 : 0, + 'user_id' => self::$subscriber_id, + 'comment_content' => 'reaction' === $child_type ? self::HEART_REACTION_KEY : 'Reply', + ) + ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'post', $other_post_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_has_children', $response, 400 ); + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_id, $note->comment_post_ID ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_note_child_types(): array { + return array( + 'a reaction' => array( 'reaction' ), + 'a reply' => array( 'note' ), + ); + } + + /** + * A note with no replies or reactions can still move. + * + * @ticket 66635 + */ + public function test_can_move_note_without_children(): void { + list( , $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'post', $other_post_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $other_post_id, $note->comment_post_ID ); + } + + /** + * The reaction summary ignores a reaction stored on another post, so the + * uniqueness check has to as well, or the user is refused a reaction they + * cannot see. + * + * @ticket 66635 + */ + public function test_reaction_stored_on_another_post_does_not_block_a_new_reaction(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $other_post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$editor_id, + 'comment_content' => self::HEART_REACTION_KEY, + ) + ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + $request->set_param( 'type', 'reaction' ); + $request->set_param( 'post', $post_id ); + $request->set_param( 'parent', $note_id ); + $request->set_param( 'content', self::HEART_REACTION_KEY ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 201, $response->get_status() ); + $this->assertArrayHasKey( self::HEART_REACTION_KEY, $this->get_reaction_summary( $note_id ) ); + } } diff --git a/tests/phpunit/tests/xmlrpc/wp/editComment.php b/tests/phpunit/tests/xmlrpc/wp/editComment.php index 0f998df96707a..860591eab1f85 100644 --- a/tests/phpunit/tests/xmlrpc/wp/editComment.php +++ b/tests/phpunit/tests/xmlrpc/wp/editComment.php @@ -93,4 +93,60 @@ public function test_trash_comment() { $this->assertSame( 'trash', get_comment( $comment_id )->comment_approved ); } + + /** + * Reactions are immutable, whoever asks: XML-RPC needs no nonce, so it + * must not be the one writer that lets a reaction be rewritten. + * + * @ticket 66635 + */ + public function test_cannot_edit_reaction(): void { + $contributor_id = $this->make_user_by_role( 'contributor' ); + $admin_id = $this->make_user_by_role( 'administrator' ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => $contributor_id, + 'post_status' => 'draft', + ) + ); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $draft_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $draft_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => $admin_id, + 'comment_content' => '2764', + ) + ); + + $result = $this->myxmlrpcserver->wp_editComment( + array( + 1, + 'contributor', + 'contributor', + $reaction_id, + array( + 'content' => 'rewritten', + 'author' => 'Forged', + ), + ) + ); + + $this->assertIXRError( $result ); + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( '2764', $reaction->comment_content ); + $this->assertSame( $admin_id, (int) $reaction->user_id ); + } } diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index bb83cd91b8728..ba63e6425b7fd 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -10598,7 +10598,8 @@ mockedApiResponse.Schema = { "description": "Limit result set to comments of specific parent IDs.", "type": "array", "items": { - "type": "integer" + "type": "integer", + "minimum": 0 }, "required": false }, @@ -10722,12 +10723,14 @@ mockedApiResponse.Schema = { "default": 0, "description": "The ID for the parent of the comment.", "type": "integer", + "minimum": 0, "required": false }, "post": { "default": 0, "description": "The ID of the associated post object.", "type": "integer", + "minimum": 0, "required": false }, "status": { @@ -10870,11 +10873,13 @@ mockedApiResponse.Schema = { "parent": { "description": "The ID for the parent of the comment.", "type": "integer", + "minimum": 0, "required": false }, "post": { "description": "The ID of the associated post object.", "type": "integer", + "minimum": 0, "required": false }, "status": {