From 81bebc023ad494a5d6ccf7fad07c502bedbdc23d Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 16 Nov 2026 10:43:09 -0700 Subject: [PATCH 1/8] Forms: Store response notes as held and limit reading them to response managers --- .../changelog/update-forms-notes-hold-status | 4 ++ .../class-contact-form-endpoint.php | 22 ++++++++ .../class-contact-form-plugin.php | 43 +++++++++++++++ .../components/feedback-comments/index.tsx | 12 ++--- .../Contact_Form_Endpoint_Test.php | 52 +++++++++++++++++++ .../contact-form/Contact_Form_Plugin_Test.php | 37 +++++++++++++ 6 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 projects/packages/forms/changelog/update-forms-notes-hold-status diff --git a/projects/packages/forms/changelog/update-forms-notes-hold-status b/projects/packages/forms/changelog/update-forms-notes-hold-status new file mode 100644 index 000000000000..229566e4a2cf --- /dev/null +++ b/projects/packages/forms/changelog/update-forms-notes-hold-status @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Notes: Keep notes on responses visible only to people who can manage responses. diff --git a/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php b/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php index d632b6a24c74..7a9acc480d3a 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php @@ -1698,6 +1698,28 @@ public function get_item_permissions_check( $request ) { //phpcs:ignore Variable return true; } + /** + * Check whether the current user can read a feedback post. + * + * Core's comments controller also calls this to decide who may read a response's notes. + * + * @since $$next-version$$ + * + * @param \WP_Post $post Post object. + * @return bool + */ + public function check_read_permission( $post ) { + if ( 'feedback' !== $post->post_type ) { + return parent::check_read_permission( $post ); + } + + if ( is_super_admin() ) { + return true; + } + + return current_user_can( 'edit_pages' ) && is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ); + } + /** * Get static metadata for an integration (without status checks). * diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index 9fcc49328851..f6dafd46cf49 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -246,6 +246,8 @@ protected function __construct() { // Restrict feedback comments to logged-in users only add_filter( 'comments_open', array( $this, 'restrict_feedback_comments_to_logged_in' ), 10, 2 ); + add_filter( 'pre_comment_approved', array( $this, 'hold_feedback_notes' ), 10, 2 ); + add_filter( 'notify_moderator', array( $this, 'skip_moderation_email_for_feedback_notes' ), 10, 2 ); // custom post type we'll use to keep copies of the feedback items register_post_type( @@ -3952,6 +3954,47 @@ public function restrict_feedback_comments_to_logged_in( $open, $post_id ) { return current_user_can( 'edit_pages' ); } + /** + * Store notes on feedback posts unapproved, so they stay out of public comment listings. + * + * @since $$next-version$$ + * + * @param int|string|\WP_Error $approved The approval status. + * @param array $commentdata Comment data. + * @return int|string|\WP_Error + */ + public function hold_feedback_notes( $approved, $commentdata ) { + if ( is_wp_error( $approved ) || empty( $commentdata['comment_post_ID'] ) ) { + return $approved; + } + + if ( 'feedback' !== get_post_type( (int) $commentdata['comment_post_ID'] ) ) { + return $approved; + } + + // Keep spam and trash verdicts. + return 1 === (int) $approved ? 0 : $approved; + } + + /** + * Don't email moderators about notes, which are held by design rather than awaiting review. + * + * @since $$next-version$$ + * + * @param bool $maybe_notify Whether to notify the moderators. + * @param int $comment_id Comment ID. + * @return bool + */ + public function skip_moderation_email_for_feedback_notes( $maybe_notify, $comment_id ) { + $comment = get_comment( $comment_id ); + + if ( $comment && 'feedback' === get_post_type( (int) $comment->comment_post_ID ) ) { + return false; + } + + return $maybe_notify; + } + /** * Kludge method: reverses the output of a standard print_r( $array ). * Sort of what unserialize does to a serialized object. diff --git a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx index 48d171a4ec8e..6f35a0ac3c05 100644 --- a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx +++ b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx @@ -51,19 +51,15 @@ const FeedbackComments = ( { postId }: FeedbackCommentsProps ): JSX.Element => { isLoadingComments, } = useSelect( select => { + // Notes are stored unapproved so they stay private; core only returns approved ones by default. + const query = { per_page: perPage, page, post: postId, status: 'hold' }; const commentsData = select( coreStore ).getEntityRecords( 'root', 'comment', { - per_page: perPage, - page, + ...query, orderby: 'date', order: 'asc', - post: postId, } ) as FeedbackComment[] | null | undefined; - const total = select( coreStore ).getEntityRecordsTotalItems( 'root', 'comment', { - per_page: perPage, - page, - post: postId, - } ); + const total = select( coreStore ).getEntityRecordsTotalItems( 'root', 'comment', query ); return { comments: commentsData, diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php index ec9e40eeed3d..d74911d2011c 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php @@ -4,6 +4,7 @@ require_once __DIR__ . '/class-utility.php'; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\PreserveGlobalState; use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\TestCase; @@ -1922,4 +1923,55 @@ public function test_get_crm_integration_follows_a_symlinked_renamed_plugin_fold $this->assertSame( 'HereTogetherCRM/HereTogetherCRM', $data['pluginFile'] ); $this->assertTrue( $data['isActive'] ); } + + /** + * Only users who can manage responses may read a response's notes. + * + * @dataProvider provide_note_readers + * + * @param string|null $role Role of the requesting user, or null when logged out. + * @param int $expected_status Expected HTTP status. + */ + #[DataProvider( 'provide_note_readers' )] + public function test_feedback_notes_are_readable_only_by_response_managers( $role, $expected_status ) { + $feedback_id = wp_insert_post( + array( + 'post_type' => 'feedback', + 'post_status' => 'publish', + ) + ); + + $user_id = 0; + if ( null !== $role ) { + $user_id = wp_insert_user( + array( + 'user_login' => 'note_reader_' . $role, + 'user_pass' => 'password', + 'role' => $role, + ) + ); + } + wp_set_current_user( $user_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'post', $feedback_id ); + $response = $this->server->dispatch( $request ); + + $this->assertSame( $expected_status, $response->get_status() ); + } + + /** + * Roles and the status each gets when listing a response's notes. + * + * @return array + */ + public static function provide_note_readers() { + return array( + 'logged out' => array( null, 401 ), + 'subscriber' => array( 'subscriber', 403 ), + 'author' => array( 'author', 403 ), + 'editor' => array( 'editor', 200 ), + 'administrator' => array( 'administrator', 200 ), + ); + } } diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index 4aa8f0ea0b12..aacec27079cb 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -1893,6 +1893,43 @@ public function test_comment_filter_only_affects_feedback_posts() { $this->assertTrue( $comments_open, 'Comment filter should not affect non-feedback posts' ); } + /** + * Notes on responses are never stored approved. + * + * @dataProvider provide_note_approvals + * + * @param string $post_type Post type the comment is left on. + * @param int|string $approved Approval status core computed. + * @param int|string $expected Approval status that should be stored. + */ + #[DataProvider( 'provide_note_approvals' )] + public function test_feedback_notes_are_held( $post_type, $approved, $expected ) { + Contact_Form_Plugin::init(); + $post_id = wp_insert_post( + array( + 'post_type' => $post_type, + 'post_status' => 'publish', + ) + ); + + $result = apply_filters( 'pre_comment_approved', $approved, array( 'comment_post_ID' => $post_id ) ); + + $this->assertSame( $expected, $result ); + } + + /** + * Post type, computed approval and the approval that should be stored. + * + * @return array + */ + public static function provide_note_approvals() { + return array( + 'approved note is held' => array( 'feedback', 1, 0 ), + 'spam note stays spam' => array( 'feedback', 'spam', 'spam' ), + 'regular post comment untouched' => array( 'post', 1, 1 ), + ); + } + /** * Test track_feedback_status_change sets spam meta when transitioning to spam */ From 38a4fdfd3bf3e5f462f233802e5bf1588b535370 Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 16 Nov 2026 11:18:48 -0700 Subject: [PATCH 2/8] Forms: Give response notes their own comment type and keep them out of wp-admin Comments --- .../class-contact-form-plugin.php | 41 ++++++++ .../forms/src/contact-form/class-feedback.php | 9 ++ .../components/feedback-comments/index.tsx | 10 +- .../contact-form/Contact_Form_Plugin_Test.php | 93 +++++++++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index f6dafd46cf49..40f8dbcff1ec 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -247,6 +247,9 @@ protected function __construct() { // Restrict feedback comments to logged-in users only add_filter( 'comments_open', array( $this, 'restrict_feedback_comments_to_logged_in' ), 10, 2 ); add_filter( 'pre_comment_approved', array( $this, 'hold_feedback_notes' ), 10, 2 ); + add_filter( 'preprocess_comment', array( $this, 'set_feedback_note_type' ) ); + add_filter( 'rest_pre_insert_comment', array( $this, 'set_feedback_note_type' ) ); + add_action( 'pre_get_comments', array( $this, 'exclude_feedback_notes_from_comment_queries' ) ); add_filter( 'notify_moderator', array( $this, 'skip_moderation_email_for_feedback_notes' ), 10, 2 ); // custom post type we'll use to keep copies of the feedback items @@ -3976,6 +3979,44 @@ public function hold_feedback_notes( $approved, $commentdata ) { return 1 === (int) $approved ? 0 : $approved; } + /** + * Give comments on feedback posts the note comment type. + * + * Core's REST API only accepts core types on create, so the type is set here instead. + * + * @since $$next-version$$ + * + * @param array|\WP_Error $commentdata Comment data. + * @return array|\WP_Error + */ + public function set_feedback_note_type( $commentdata ) { + if ( is_array( $commentdata ) && ! empty( $commentdata['comment_post_ID'] ) && 'feedback' === get_post_type( (int) $commentdata['comment_post_ID'] ) ) { + $commentdata['comment_type'] = Feedback::NOTE_COMMENT_TYPE; + } + + return $commentdata; + } + + /** + * Leave notes out of comment queries that don't ask for them, as core does for its `note` type. + * + * This keeps them out of the Comments screen, its counts, and the Activity dashboard widget. + * + * @since $$next-version$$ + * + * @param \WP_Comment_Query $query Comment query. + */ + public function exclude_feedback_notes_from_comment_queries( $query ) { + $requested = array_merge( (array) $query->query_vars['type'], (array) $query->query_vars['type__in'] ); + + if ( in_array( 'all', $requested, true ) || in_array( Feedback::NOTE_COMMENT_TYPE, $requested, true ) ) { + return; + } + + $query->query_vars['type__not_in'] = (array) $query->query_vars['type__not_in']; + $query->query_vars['type__not_in'][] = Feedback::NOTE_COMMENT_TYPE; + } + /** * Don't email moderators about notes, which are held by design rather than awaiting review. * diff --git a/projects/packages/forms/src/contact-form/class-feedback.php b/projects/packages/forms/src/contact-form/class-feedback.php index 36a3b2577eba..f80adf524d4d 100644 --- a/projects/packages/forms/src/contact-form/class-feedback.php +++ b/projects/packages/forms/src/contact-form/class-feedback.php @@ -40,6 +40,15 @@ class Feedback { */ public const STATUS_READ = 'closed'; + /** + * Comment type for notes left on a response. + * + * @since $$next-version$$ + * + * @var string + */ + public const NOTE_COMMENT_TYPE = 'jetpack_form_note'; + /** * Meta key used to store the source post ID on feedback posts. * diff --git a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx index 6f35a0ac3c05..d6a550eb4b66 100644 --- a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx +++ b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx @@ -51,8 +51,14 @@ const FeedbackComments = ( { postId }: FeedbackCommentsProps ): JSX.Element => { isLoadingComments, } = useSelect( select => { - // Notes are stored unapproved so they stay private; core only returns approved ones by default. - const query = { per_page: perPage, page, post: postId, status: 'hold' }; + // Notes are stored held and with their own type, and core returns neither unless asked. + const query = { + per_page: perPage, + page, + post: postId, + status: 'hold', + type: 'jetpack_form_note', + }; const commentsData = select( coreStore ).getEntityRecords( 'root', 'comment', { ...query, orderby: 'date', diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index aacec27079cb..dc26ac391d82 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -1930,6 +1930,99 @@ public static function provide_note_approvals() { ); } + /** + * Comments on responses get the note type on both the REST and the classic insert path. + * + * @dataProvider provide_note_type_cases + * + * @param string $hook Filter the comment data passes through. + * @param string $post_type Post type the comment is left on. + * @param string $expected Comment type that should be stored. + */ + #[DataProvider( 'provide_note_type_cases' )] + public function test_feedback_notes_get_note_type( $hook, $post_type, $expected ) { + Contact_Form_Plugin::init(); + $post_id = wp_insert_post( + array( + 'post_type' => $post_type, + 'post_status' => 'publish', + ) + ); + + $data = apply_filters( + $hook, + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'comment', + ), + null + ); + + $this->assertSame( $expected, $data['comment_type'] ); + } + + /** + * Hook, post type and the comment type that should be stored. + * + * @return array + */ + public static function provide_note_type_cases() { + return array( + 'REST note' => array( 'rest_pre_insert_comment', 'feedback', Feedback::NOTE_COMMENT_TYPE ), + 'classic note' => array( 'preprocess_comment', 'feedback', Feedback::NOTE_COMMENT_TYPE ), + 'REST post comment' => array( 'rest_pre_insert_comment', 'post', 'comment' ), + 'classic post reply' => array( 'preprocess_comment', 'post', 'comment' ), + ); + } + + /** + * Core stores `comment_type` in a varchar(20) column, and MySQL rejects the whole insert when it's longer. + */ + public function test_note_comment_type_fits_core_column() { + $this->assertLessThanOrEqual( 20, strlen( Feedback::NOTE_COMMENT_TYPE ) ); + } + + /** + * Comment queries leave notes out unless they ask for them, so wp-admin's Comments screens never list or count them. + * + * @dataProvider provide_note_query_cases + * + * @param array $args Comment query arguments. + * @param bool $excluded Whether notes should be excluded. + */ + #[DataProvider( 'provide_note_query_cases' )] + public function test_comment_queries_exclude_feedback_notes( $args, $excluded ) { + Contact_Form_Plugin::init(); + $query = new \WP_Comment_Query(); + $query->parse_query( $args ); + + do_action_ref_array( 'pre_get_comments', array( &$query ) ); + + $this->assertSame( $excluded, in_array( Feedback::NOTE_COMMENT_TYPE, (array) $query->query_vars['type__not_in'], true ) ); + } + + /** + * Query arguments and whether notes should be excluded. + * + * @return array + */ + public static function provide_note_query_cases() { + return array( + 'default query' => array( array(), true ), + 'pending comments count' => array( + array( + 'status' => 'hold', + 'count' => true, + ), + true, + ), + 'other excluded types' => array( array( 'type__not_in' => array( 'pings' ) ), true ), + 'notes requested by type' => array( array( 'type' => Feedback::NOTE_COMMENT_TYPE ), false ), + 'notes in type__in' => array( array( 'type__in' => array( Feedback::NOTE_COMMENT_TYPE ) ), false ), + 'all types requested' => array( array( 'type' => 'all' ), false ), + ); + } + /** * Test track_feedback_status_change sets spam meta when transitioning to spam */ From 66c1ebe0a0dd948c1a502dddc4c37733778617ae Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 16 Nov 2026 11:31:06 -0700 Subject: [PATCH 3/8] Forms: Reuse the response permission check and share the note type with the dashboard --- .../class-contact-form-endpoint.php | 12 ++------ .../class-contact-form-plugin.php | 29 ++++++++++--------- .../components/feedback-comments/index.tsx | 11 +++---- .../packages/forms/src/dashboard/constants.ts | 5 ++++ 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php b/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php index 7a9acc480d3a..71cbcd44bfbc 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php @@ -1708,16 +1708,8 @@ public function get_item_permissions_check( $request ) { //phpcs:ignore Variable * @param \WP_Post $post Post object. * @return bool */ - public function check_read_permission( $post ) { - if ( 'feedback' !== $post->post_type ) { - return parent::check_read_permission( $post ); - } - - if ( is_super_admin() ) { - return true; - } - - return current_user_can( 'edit_pages' ) && is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ); + public function check_read_permission( $post ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + return true === $this->get_items_permissions_check( null ); } /** diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index 40f8dbcff1ec..64574a59be9b 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -3958,7 +3958,17 @@ public function restrict_feedback_comments_to_logged_in( $open, $post_id ) { } /** - * Store notes on feedback posts unapproved, so they stay out of public comment listings. + * Whether a post ID belongs to a feedback post. + * + * @param mixed $post_id Post ID. + * @return bool + */ + private static function is_feedback_post( $post_id ) { + return ! empty( $post_id ) && Feedback::POST_TYPE === get_post_type( (int) $post_id ); + } + + /** + * Store notes on feedback posts unapproved; reads are gated by the endpoint, not by this status. * * @since $$next-version$$ * @@ -3967,11 +3977,7 @@ public function restrict_feedback_comments_to_logged_in( $open, $post_id ) { * @return int|string|\WP_Error */ public function hold_feedback_notes( $approved, $commentdata ) { - if ( is_wp_error( $approved ) || empty( $commentdata['comment_post_ID'] ) ) { - return $approved; - } - - if ( 'feedback' !== get_post_type( (int) $commentdata['comment_post_ID'] ) ) { + if ( is_wp_error( $approved ) || ! self::is_feedback_post( $commentdata['comment_post_ID'] ?? 0 ) ) { return $approved; } @@ -3990,7 +3996,7 @@ public function hold_feedback_notes( $approved, $commentdata ) { * @return array|\WP_Error */ public function set_feedback_note_type( $commentdata ) { - if ( is_array( $commentdata ) && ! empty( $commentdata['comment_post_ID'] ) && 'feedback' === get_post_type( (int) $commentdata['comment_post_ID'] ) ) { + if ( is_array( $commentdata ) && self::is_feedback_post( $commentdata['comment_post_ID'] ?? 0 ) ) { $commentdata['comment_type'] = Feedback::NOTE_COMMENT_TYPE; } @@ -4013,8 +4019,7 @@ public function exclude_feedback_notes_from_comment_queries( $query ) { return; } - $query->query_vars['type__not_in'] = (array) $query->query_vars['type__not_in']; - $query->query_vars['type__not_in'][] = Feedback::NOTE_COMMENT_TYPE; + $query->query_vars['type__not_in'] = array_merge( (array) $query->query_vars['type__not_in'], array( Feedback::NOTE_COMMENT_TYPE ) ); } /** @@ -4029,11 +4034,7 @@ public function exclude_feedback_notes_from_comment_queries( $query ) { public function skip_moderation_email_for_feedback_notes( $maybe_notify, $comment_id ) { $comment = get_comment( $comment_id ); - if ( $comment && 'feedback' === get_post_type( (int) $comment->comment_post_ID ) ) { - return false; - } - - return $maybe_notify; + return $comment && Feedback::NOTE_COMMENT_TYPE === $comment->comment_type ? false : $maybe_notify; } /** diff --git a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx index d6a550eb4b66..0c84938449f8 100644 --- a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx +++ b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx @@ -10,6 +10,7 @@ import { store as noticesStore } from '@wordpress/notices'; /** * Internal dependencies */ +import { NOTE_COMMENT_TYPE } from '../../constants'; import CommentItem from './comment-item'; import type { FeedbackComment } from '../../../types'; import type { JSX } from 'react'; @@ -52,18 +53,18 @@ const FeedbackComments = ( { postId }: FeedbackCommentsProps ): JSX.Element => { } = useSelect( select => { // Notes are stored held and with their own type, and core returns neither unless asked. + // One shared query so the total comes from the same request as the records. const query = { per_page: perPage, page, post: postId, status: 'hold', - type: 'jetpack_form_note', - }; - const commentsData = select( coreStore ).getEntityRecords( 'root', 'comment', { - ...query, + type: NOTE_COMMENT_TYPE, orderby: 'date', order: 'asc', - } ) as FeedbackComment[] | null | undefined; + }; + const commentsData = select( coreStore ).getEntityRecords( 'root', 'comment', query ) as + FeedbackComment[] | null | undefined; const total = select( coreStore ).getEntityRecordsTotalItems( 'root', 'comment', query ); diff --git a/projects/packages/forms/src/dashboard/constants.ts b/projects/packages/forms/src/dashboard/constants.ts index fdbb0bc0c84f..29a42b6561bd 100644 --- a/projects/packages/forms/src/dashboard/constants.ts +++ b/projects/packages/forms/src/dashboard/constants.ts @@ -7,6 +7,11 @@ import { __ } from '@wordpress/i18n'; */ import type { QueryParams } from './inbox/stage/types.tsx'; +/** + * Comment type of response notes; matches `Feedback::NOTE_COMMENT_TYPE` in PHP. + */ +export const NOTE_COMMENT_TYPE = 'jetpack_form_note'; + /** * All form status filter values, including the virtual "all" filter. */ From a82526456680a226dc90f22118e2be225d454afe Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 16 Nov 2026 11:56:01 -0700 Subject: [PATCH 4/8] Forms: Keep response notes held through Akismet and include them in privacy exports --- .../class-contact-form-plugin.php | 105 +++++++++++-- .../components/feedback-comments/index.tsx | 4 +- .../Contact_Form_Endpoint_Test.php | 10 +- .../contact-form/Contact_Form_Plugin_Test.php | 143 ++++++++++++++++-- 4 files changed, 232 insertions(+), 30 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index 64574a59be9b..e94ddfa017c2 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -246,9 +246,12 @@ protected function __construct() { // Restrict feedback comments to logged-in users only add_filter( 'comments_open', array( $this, 'restrict_feedback_comments_to_logged_in' ), 10, 2 ); - add_filter( 'pre_comment_approved', array( $this, 'hold_feedback_notes' ), 10, 2 ); - add_filter( 'preprocess_comment', array( $this, 'set_feedback_note_type' ) ); - add_filter( 'rest_pre_insert_comment', array( $this, 'set_feedback_note_type' ) ); + // Notes come in through REST and through wp_new_comment() (e.g. the WordPress.com v1.1 replies endpoint). + // Priority 20 so these run after Akismet's verdict at 1 and 10. + add_filter( 'pre_comment_approved', array( $this, 'hold_feedback_notes' ), 20, 2 ); + add_filter( 'preprocess_comment', array( $this, 'prepare_feedback_note' ), 20 ); + add_filter( 'rest_pre_insert_comment', array( $this, 'prepare_feedback_note' ), 20 ); + add_action( 'transition_comment_status', array( $this, 'keep_feedback_notes_out_of_spam' ), 20, 3 ); add_action( 'pre_get_comments', array( $this, 'exclude_feedback_notes_from_comment_queries' ) ); add_filter( 'notify_moderator', array( $this, 'skip_moderation_email_for_feedback_notes' ), 10, 2 ); @@ -2752,6 +2755,11 @@ public function register_personal_data_exporter( $exporters ) { 'callback' => array( $this, 'personal_data_exporter' ), ); + $exporters['jetpack-feedback-notes'] = array( + 'exporter_friendly_name' => __( 'Form response notes', 'jetpack-forms' ), + 'callback' => array( $this, 'personal_data_notes_exporter' ), + ); + return $exporters; } @@ -2770,9 +2778,61 @@ public function register_personal_data_eraser( $erasers ) { 'callback' => array( $this, 'personal_data_eraser' ), ); + $erasers['jetpack-feedback-notes'] = array( + 'eraser_friendly_name' => __( 'Form response notes', 'jetpack-forms' ), + 'callback' => array( $this, 'personal_data_notes_eraser' ), + ); + return $erasers; } + /** + * Exports notes left on responses, which core's comment exporter skips because of their comment type. + * + * @since $$next-version$$ + * + * @param string $email Email address. + * @param int $page Page to export. + * @return array Associative array with keys expected by core. + */ + public function personal_data_notes_exporter( $email, $page = 1 ) { + return self::run_for_feedback_notes_only( 'wp_comments_personal_data_exporter', $email, $page ); + } + + /** + * Anonymizes notes left on responses, which core's comment eraser skips because of their comment type. + * + * @since $$next-version$$ + * + * @param string $email Email address. + * @param int $page Page to erase. + * @return array Associative array with keys expected by core. + */ + public function personal_data_notes_eraser( $email, $page = 1 ) { + return self::run_for_feedback_notes_only( 'wp_comments_personal_data_eraser', $email, $page ); + } + + /** + * Run a core comment privacy callback with its comment queries limited to response notes. + * + * @param callable $callback Core exporter or eraser. + * @param string $email Email address. + * @param int $page Page number. + * @return array + */ + private static function run_for_feedback_notes_only( $callback, $email, $page ) { + $only_notes = static function ( $query ) { + $query->query_vars['type'] = Feedback::NOTE_COMMENT_TYPE; + }; + + add_action( 'pre_get_comments', $only_notes, 5 ); + try { + return $callback( $email, $page ); + } finally { + remove_action( 'pre_get_comments', $only_notes, 5 ); + } + } + /** * Exports personal data. * @@ -3968,7 +4028,9 @@ private static function is_feedback_post( $post_id ) { } /** - * Store notes on feedback posts unapproved; reads are gated by the endpoint, not by this status. + * Store notes on feedback posts held; reads are gated by the endpoint, not by this status. + * + * Only people who can manage responses can leave notes, so spam and trash verdicts don't apply. * * @since $$next-version$$ * @@ -3981,28 +4043,49 @@ public function hold_feedback_notes( $approved, $commentdata ) { return $approved; } - // Keep spam and trash verdicts. - return 1 === (int) $approved ? 0 : $approved; + return 0; } /** - * Give comments on feedback posts the note comment type. + * Give comments on feedback posts the note comment type, and keep them held. * - * Core's REST API only accepts core types on create, so the type is set here instead. + * Core's REST API only accepts core types on create, and Akismet sets its REST verdict after the approval check. * * @since $$next-version$$ * * @param array|\WP_Error $commentdata Comment data. * @return array|\WP_Error */ - public function set_feedback_note_type( $commentdata ) { - if ( is_array( $commentdata ) && self::is_feedback_post( $commentdata['comment_post_ID'] ?? 0 ) ) { - $commentdata['comment_type'] = Feedback::NOTE_COMMENT_TYPE; + public function prepare_feedback_note( $commentdata ) { + if ( ! is_array( $commentdata ) || ! self::is_feedback_post( $commentdata['comment_post_ID'] ?? 0 ) ) { + return $commentdata; + } + + $commentdata['comment_type'] = Feedback::NOTE_COMMENT_TYPE; + if ( isset( $commentdata['comment_approved'] ) ) { + $commentdata['comment_approved'] = 0; } return $commentdata; } + /** + * Put a note back on hold when it's marked as spam, such as by Akismet's bulk recheck of held comments. + * + * Notes don't appear in wp-admin's Spam view, so Akismet would otherwise delete them unseen. + * + * @since $$next-version$$ + * + * @param string $new_status New comment status. + * @param string $old_status Previous comment status. + * @param \WP_Comment $comment Comment object. + */ + public function keep_feedback_notes_out_of_spam( $new_status, $old_status, $comment ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + if ( 'spam' === $new_status && Feedback::NOTE_COMMENT_TYPE === $comment->comment_type ) { + wp_set_comment_status( $comment->comment_ID, 'hold' ); + } + } + /** * Leave notes out of comment queries that don't ask for them, as core does for its `note` type. * diff --git a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx index 0c84938449f8..49070d38b31f 100644 --- a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx +++ b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx @@ -52,13 +52,13 @@ const FeedbackComments = ( { postId }: FeedbackCommentsProps ): JSX.Element => { isLoadingComments, } = useSelect( select => { - // Notes are stored held and with their own type, and core returns neither unless asked. + // Notes have their own type, which core only returns when asked. `all` also keeps a note a moderator approved. // One shared query so the total comes from the same request as the records. const query = { per_page: perPage, page, post: postId, - status: 'hold', + status: 'all', type: NOTE_COMMENT_TYPE, orderby: 'date', order: 'asc', diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php index d74911d2011c..dcc0a483a165 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php @@ -1925,7 +1925,7 @@ public function test_get_crm_integration_follows_a_symlinked_renamed_plugin_fold } /** - * Only users who can manage responses may read a response's notes. + * Only users who can manage responses may read a response's notes, using the dashboard's query. * * @dataProvider provide_note_readers * @@ -1954,7 +1954,13 @@ public function test_feedback_notes_are_readable_only_by_response_managers( $rol wp_set_current_user( $user_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_param( 'post', $feedback_id ); + $request->set_query_params( + array( + 'post' => $feedback_id, + 'status' => 'all', + 'type' => Feedback::NOTE_COMMENT_TYPE, + ) + ); $response = $this->server->dispatch( $request ); $this->assertSame( $expected_status, $response->get_status() ); diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index dc26ac391d82..58f31cac9304 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -1894,7 +1894,7 @@ public function test_comment_filter_only_affects_feedback_posts() { } /** - * Notes on responses are never stored approved. + * Notes on responses are always stored held, whatever core or Akismet decided. * * @dataProvider provide_note_approvals * @@ -1925,22 +1925,24 @@ public function test_feedback_notes_are_held( $post_type, $approved, $expected ) public static function provide_note_approvals() { return array( 'approved note is held' => array( 'feedback', 1, 0 ), - 'spam note stays spam' => array( 'feedback', 'spam', 'spam' ), + 'Akismet spam verdict is held' => array( 'feedback', 'spam', 0 ), 'regular post comment untouched' => array( 'post', 1, 1 ), + 'regular post spam untouched' => array( 'post', 'spam', 'spam' ), ); } /** - * Comments on responses get the note type on both the REST and the classic insert path. + * Comments on responses get the note type, and stay held after Akismet's REST verdict. * * @dataProvider provide_note_type_cases * - * @param string $hook Filter the comment data passes through. - * @param string $post_type Post type the comment is left on. - * @param string $expected Comment type that should be stored. + * @param string $hook Filter the comment data passes through. + * @param string $post_type Post type the comment is left on. + * @param string $expected_type Comment type that should be stored. + * @param int|string $expected_approved Approval status that should be stored. */ #[DataProvider( 'provide_note_type_cases' )] - public function test_feedback_notes_get_note_type( $hook, $post_type, $expected ) { + public function test_feedback_notes_get_note_type( $hook, $post_type, $expected_type, $expected_approved ) { Contact_Form_Plugin::init(); $post_id = wp_insert_post( array( @@ -1952,26 +1954,27 @@ public function test_feedback_notes_get_note_type( $hook, $post_type, $expected $data = apply_filters( $hook, array( - 'comment_post_ID' => $post_id, - 'comment_type' => 'comment', + 'comment_post_ID' => $post_id, + 'comment_type' => 'comment', + 'comment_approved' => 'spam', ), null ); - $this->assertSame( $expected, $data['comment_type'] ); + $this->assertSame( array( $expected_type, $expected_approved ), array( $data['comment_type'], $data['comment_approved'] ) ); } /** - * Hook, post type and the comment type that should be stored. + * Hook, post type, and the comment type and approval that should be stored. * * @return array */ public static function provide_note_type_cases() { return array( - 'REST note' => array( 'rest_pre_insert_comment', 'feedback', Feedback::NOTE_COMMENT_TYPE ), - 'classic note' => array( 'preprocess_comment', 'feedback', Feedback::NOTE_COMMENT_TYPE ), - 'REST post comment' => array( 'rest_pre_insert_comment', 'post', 'comment' ), - 'classic post reply' => array( 'preprocess_comment', 'post', 'comment' ), + 'REST note' => array( 'rest_pre_insert_comment', 'feedback', Feedback::NOTE_COMMENT_TYPE, 0 ), + 'classic note' => array( 'preprocess_comment', 'feedback', Feedback::NOTE_COMMENT_TYPE, 0 ), + 'REST post comment' => array( 'rest_pre_insert_comment', 'post', 'comment', 'spam' ), + 'classic post reply' => array( 'preprocess_comment', 'post', 'comment', 'spam' ), ); } @@ -2023,6 +2026,116 @@ public static function provide_note_query_cases() { ); } + /** + * Cache a comment so get_comment() finds it without a database. + * + * @param int $id Comment ID. + * @param string $type Comment type. + * @return \WP_Comment + */ + private function cache_comment( $id, $type ) { + $comment = new \WP_Comment( + (object) array( + 'comment_ID' => $id, + 'comment_post_ID' => 1, + 'comment_type' => $type, + 'comment_approved' => 'spam', + ) + ); + wp_cache_set( $id, $comment, 'comment' ); + + return $comment; + } + + /** + * A note marked as spam, e.g. by Akismet's bulk recheck, goes back on hold; other comments stay spam. + * + * @dataProvider provide_note_and_regular_comment + * + * @param string $type Comment type. + * @param bool $is_note Whether the comment is a note. + */ + #[DataProvider( 'provide_note_and_regular_comment' )] + public function test_feedback_notes_are_kept_out_of_spam( $type, $is_note ) { + Contact_Form_Plugin::init(); + $comment = $this->cache_comment( 9001, $type ); + $statuses = array(); + // Report the write as failed so core stops before re-reading the comment from a database we don't have. + $spy = function ( $result, $table, $data ) use ( &$statuses ) { + $statuses[] = $data['comment_approved'] ?? null; + return false; + }; + add_filter( 'wordbless_wpdb_update', $spy, 10, 3 ); + + do_action( 'transition_comment_status', 'spam', 'unapproved', $comment ); + remove_filter( 'wordbless_wpdb_update', $spy, 10 ); + + $this->assertSame( $is_note ? array( '0' ) : array(), $statuses ); + } + + /** + * A note and a regular comment. + * + * @return array + */ + public static function provide_note_and_regular_comment() { + return array( + 'note' => array( Feedback::NOTE_COMMENT_TYPE, true ), + 'regular comment' => array( 'comment', false ), + ); + } + + /** + * Moderators aren't emailed about held notes, but still are about other held comments. + * + * @dataProvider provide_note_and_regular_comment + * + * @param string $type Comment type. + * @param bool $is_note Whether the comment is a note. + */ + #[DataProvider( 'provide_note_and_regular_comment' )] + public function test_moderators_are_not_emailed_about_feedback_notes( $type, $is_note ) { + Contact_Form_Plugin::init(); + $this->cache_comment( 9002, $type ); + + $this->assertSame( ! $is_note, apply_filters( 'notify_moderator', true, 9002 ) ); + } + + /** + * The notes exporter and eraser run core's comment callbacks against notes only, and leave other queries alone after. + * + * @dataProvider provide_notes_privacy_callbacks + * + * @param string $method Contact_Form_Plugin method. + */ + #[DataProvider( 'provide_notes_privacy_callbacks' )] + public function test_notes_privacy_callbacks_query_notes_only( $method ) { + $plugin = Contact_Form_Plugin::init(); + $types = array(); + $probe = function ( $query ) use ( &$types ) { + $types[] = $query->query_vars['type']; + }; + add_action( 'pre_get_comments', $probe, 6 ); + + $plugin->$method( 'someone@example.com' ); + get_comments( array( 'number' => 1 ) ); + remove_action( 'pre_get_comments', $probe, 6 ); + + $this->assertSame( array( Feedback::NOTE_COMMENT_TYPE, '' ), $types ); + } + + /** + * Privacy callbacks for notes. + * + * @return array + */ + public static function provide_notes_privacy_callbacks() { + return array( + 'exporter' => array( 'personal_data_notes_exporter' ), + 'eraser' => array( 'personal_data_notes_eraser' ), + ); + } + /** * Test track_feedback_status_change sets spam meta when transitioning to spam */ From 9052ec103010425b9d5fae0e6589fec41ade7bb0 Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 16 Nov 2026 13:53:39 -0700 Subject: [PATCH 5/8] Forms: Pass a request to the permissions check and test the notes privacy registration --- .../class-contact-form-endpoint.php | 2 +- .../contact-form/Contact_Form_Plugin_Test.php | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php b/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php index 71cbcd44bfbc..0624a73e9826 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-endpoint.php @@ -1709,7 +1709,7 @@ public function get_item_permissions_check( $request ) { //phpcs:ignore Variable * @return bool */ public function check_read_permission( $post ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - return true === $this->get_items_permissions_check( null ); + return true === $this->get_items_permissions_check( new WP_REST_Request() ); } /** diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index 58f31cac9304..eed9d91a4d41 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -2102,22 +2102,23 @@ public function test_moderators_are_not_emailed_about_feedback_notes( $type, $is } /** - * The notes exporter and eraser run core's comment callbacks against notes only, and leave other queries alone after. + * The registered notes exporter and eraser run core's comment callbacks against notes only, and leave other queries alone after. * * @dataProvider provide_notes_privacy_callbacks * - * @param string $method Contact_Form_Plugin method. + * @param string $register Contact_Form_Plugin method that registers the callback. */ #[DataProvider( 'provide_notes_privacy_callbacks' )] - public function test_notes_privacy_callbacks_query_notes_only( $method ) { - $plugin = Contact_Form_Plugin::init(); - $types = array(); - $probe = function ( $query ) use ( &$types ) { + public function test_notes_privacy_callbacks_query_notes_only( $register ) { + $plugin = Contact_Form_Plugin::init(); + $registered = $plugin->$register( array() ); + $types = array(); + $probe = function ( $query ) use ( &$types ) { $types[] = $query->query_vars['type']; }; add_action( 'pre_get_comments', $probe, 6 ); - $plugin->$method( 'someone@example.com' ); + call_user_func( $registered['jetpack-feedback-notes']['callback'], 'someone@example.com', 1 ); get_comments( array( 'number' => 1 ) ); remove_action( 'pre_get_comments', $probe, 6 ); @@ -2131,8 +2132,8 @@ public function test_notes_privacy_callbacks_query_notes_only( $method ) { */ public static function provide_notes_privacy_callbacks() { return array( - 'exporter' => array( 'personal_data_notes_exporter' ), - 'eraser' => array( 'personal_data_notes_eraser' ), + 'exporter' => array( 'register_personal_data_exporter' ), + 'eraser' => array( 'register_personal_data_eraser' ), ); } From 3bb5d1c6b9379319fc2f5abcc09794fd18d1087e Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Fri, 27 Nov 2026 15:31:06 -0700 Subject: [PATCH 6/8] Forms: Delete response notes outright instead of moving them to the Trash --- .../src/dashboard/components/feedback-comments/index.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx index 49070d38b31f..c5a933960b0c 100644 --- a/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx +++ b/projects/packages/forms/src/dashboard/components/feedback-comments/index.tsx @@ -203,7 +203,14 @@ const FeedbackComments = ( { postId }: FeedbackCommentsProps ): JSX.Element => { return next; } ); try { - await deleteEntityRecord( 'root', 'comment', commentId, {}, { throwOnError: true } ); + // Notes have no Trash view to restore them from, so delete them outright. + await deleteEntityRecord( + 'root', + 'comment', + commentId, + { force: true }, + { throwOnError: true } + ); createSuccessNotice( __( 'Note deleted.', 'jetpack-forms' ) ); // Remove deleted comment from local lists so UI updates immediately. setLoadedComments( prev => prev.filter( c => c.id !== commentId ) ); From bc698e85297eb642b114026dcec10684f86aaecd Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Fri, 27 Nov 2026 16:15:50 -0700 Subject: [PATCH 7/8] Forms: Limit reading responses to people who manage them --- .../class-contact-form-plugin.php | 2 + .../Contact_Form_Endpoint_Test.php | 26 ++++++------ .../contact-form/Contact_Form_Plugin_Test.php | 41 +++++++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index e94ddfa017c2..35d39ccd493d 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -290,6 +290,8 @@ protected function __construct() { 'edit_post' => 'edit_page', 'delete_post' => 'delete_page', 'read_post' => 'read_page', + // Only people who manage responses may read them, or the notes on them. + 'read' => 'edit_pages', ), 'map_meta_cap' => true, ) diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php index dcc0a483a165..970151eea165 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Endpoint_Test.php @@ -1925,15 +1925,16 @@ public function test_get_crm_integration_follows_a_symlinked_renamed_plugin_fold } /** - * Only users who can manage responses may read a response's notes, using the dashboard's query. + * Only users who can manage responses may read a response's notes. * * @dataProvider provide_note_readers * * @param string|null $role Role of the requesting user, or null when logged out. + * @param bool $panel_query Whether to send the dashboard's `status` and `type` params, which core reserves for `edit_posts`. * @param int $expected_status Expected HTTP status. */ #[DataProvider( 'provide_note_readers' )] - public function test_feedback_notes_are_readable_only_by_response_managers( $role, $expected_status ) { + public function test_feedback_notes_are_readable_only_by_response_managers( $role, $panel_query, $expected_status ) { $feedback_id = wp_insert_post( array( 'post_type' => 'feedback', @@ -1954,13 +1955,14 @@ public function test_feedback_notes_are_readable_only_by_response_managers( $rol wp_set_current_user( $user_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_query_params( - array( - 'post' => $feedback_id, + $params = array( 'post' => $feedback_id ); + if ( $panel_query ) { + $params += array( 'status' => 'all', 'type' => Feedback::NOTE_COMMENT_TYPE, - ) - ); + ); + } + $request->set_query_params( $params ); $response = $this->server->dispatch( $request ); $this->assertSame( $expected_status, $response->get_status() ); @@ -1973,11 +1975,11 @@ public function test_feedback_notes_are_readable_only_by_response_managers( $rol */ public static function provide_note_readers() { return array( - 'logged out' => array( null, 401 ), - 'subscriber' => array( 'subscriber', 403 ), - 'author' => array( 'author', 403 ), - 'editor' => array( 'editor', 200 ), - 'administrator' => array( 'administrator', 200 ), + 'logged out' => array( null, false, 401 ), + 'subscriber' => array( 'subscriber', false, 403 ), + 'author' => array( 'author', true, 403 ), + 'editor' => array( 'editor', true, 200 ), + 'administrator' => array( 'administrator', true, 200 ), ); } } diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index eed9d91a4d41..a64ad75bcbc7 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -2101,6 +2101,47 @@ public function test_moderators_are_not_emailed_about_feedback_notes( $type, $is $this->assertSame( ! $is_note, apply_filters( 'notify_moderator', true, 9002 ) ); } + /** + * Only people who manage responses can read one; wp-admin's Comments screen shows a note to anyone who can. + * + * @dataProvider provide_response_readers + * + * @param string $role Role of the user. + * @param bool $can_read Whether the user can read a published response. + */ + #[DataProvider( 'provide_response_readers' )] + public function test_only_response_managers_can_read_responses( $role, $can_read ) { + Contact_Form_Plugin::init(); + $feedback_id = wp_insert_post( + array( + 'post_type' => 'feedback', + 'post_status' => 'publish', + ) + ); + $user_id = wp_insert_user( + array( + 'user_login' => 'response_reader_' . $role, + 'user_pass' => 'password', + 'role' => $role, + ) + ); + + $this->assertSame( $can_read, user_can( $user_id, 'read_post', $feedback_id ) ); + } + + /** + * Roles and whether each can read a response. + * + * @return array + */ + public static function provide_response_readers() { + return array( + 'contributor' => array( 'contributor', false ), + 'author' => array( 'author', false ), + 'editor' => array( 'editor', true ), + ); + } + /** * The registered notes exporter and eraser run core's comment callbacks against notes only, and leave other queries alone after. * From 2015fbfa55e97e834b133ee586cc772865bf3fef Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 30 Nov 2026 16:04:01 -0700 Subject: [PATCH 8/8] Forms: Keep response notes off the Comments screen when its URL asks for them --- .../class-contact-form-plugin.php | 15 +++++++++++ .../contact-form/Contact_Form_Plugin_Test.php | 25 +++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index 35d39ccd493d..5180076822bf 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -253,6 +253,7 @@ protected function __construct() { add_filter( 'rest_pre_insert_comment', array( $this, 'prepare_feedback_note' ), 20 ); add_action( 'transition_comment_status', array( $this, 'keep_feedback_notes_out_of_spam' ), 20, 3 ); add_action( 'pre_get_comments', array( $this, 'exclude_feedback_notes_from_comment_queries' ) ); + add_filter( 'comments_list_table_query_args', array( $this, 'exclude_feedback_notes_from_comments_screen' ) ); add_filter( 'notify_moderator', array( $this, 'skip_moderation_email_for_feedback_notes' ), 10, 2 ); // custom post type we'll use to keep copies of the feedback items @@ -4107,6 +4108,20 @@ public function exclude_feedback_notes_from_comment_queries( $query ) { $query->query_vars['type__not_in'] = array_merge( (array) $query->query_vars['type__not_in'], array( Feedback::NOTE_COMMENT_TYPE ) ); } + /** + * Keep notes off the Comments screen even when its URL asks for their type, as core does for its `note` type. + * + * @since $$next-version$$ + * + * @param array $args Comments list table query arguments. + * @return array + */ + public function exclude_feedback_notes_from_comments_screen( $args ) { + $args['type__not_in'] = array_merge( (array) ( $args['type__not_in'] ?? array() ), array( Feedback::NOTE_COMMENT_TYPE ) ); + + return $args; + } + /** * Don't email moderators about notes, which are held by design rather than awaiting review. * diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index a64ad75bcbc7..a8489df6f423 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -1986,16 +1986,20 @@ public function test_note_comment_type_fits_core_column() { } /** - * Comment queries leave notes out unless they ask for them, so wp-admin's Comments screens never list or count them. + * Comment queries leave notes out unless they ask for them, and the Comments screen never lists them. * * @dataProvider provide_note_query_cases * - * @param array $args Comment query arguments. - * @param bool $excluded Whether notes should be excluded. + * @param array $args Comment query arguments. + * @param bool $excluded Whether notes should be excluded. + * @param bool $comments_screen Whether the query comes from wp-admin's Comments screen. */ #[DataProvider( 'provide_note_query_cases' )] - public function test_comment_queries_exclude_feedback_notes( $args, $excluded ) { + public function test_comment_queries_exclude_feedback_notes( $args, $excluded, $comments_screen = false ) { Contact_Form_Plugin::init(); + if ( $comments_screen ) { + $args = apply_filters( 'comments_list_table_query_args', $args ); + } $query = new \WP_Comment_Query(); $query->parse_query( $args ); @@ -2011,18 +2015,19 @@ public function test_comment_queries_exclude_feedback_notes( $args, $excluded ) */ public static function provide_note_query_cases() { return array( - 'default query' => array( array(), true ), - 'pending comments count' => array( + 'default query' => array( array(), true ), + 'pending comments count' => array( array( 'status' => 'hold', 'count' => true, ), true, ), - 'other excluded types' => array( array( 'type__not_in' => array( 'pings' ) ), true ), - 'notes requested by type' => array( array( 'type' => Feedback::NOTE_COMMENT_TYPE ), false ), - 'notes in type__in' => array( array( 'type__in' => array( Feedback::NOTE_COMMENT_TYPE ) ), false ), - 'all types requested' => array( array( 'type' => 'all' ), false ), + 'other excluded types' => array( array( 'type__not_in' => array( 'pings' ) ), true ), + 'notes requested by type' => array( array( 'type' => Feedback::NOTE_COMMENT_TYPE ), false ), + 'notes in type__in' => array( array( 'type__in' => array( Feedback::NOTE_COMMENT_TYPE ) ), false ), + 'all types requested' => array( array( 'type' => 'all' ), false ), + 'Comments screen URL asking for notes' => array( array( 'type' => Feedback::NOTE_COMMENT_TYPE ), true, true ), ); }