Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Notes: Keep notes on responses visible only to people who can manage responses.
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,20 @@ 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 ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return true === $this->get_items_permissions_check( new WP_REST_Request() );
}

/**
* Get static metadata for an integration (without status checks).
*
Expand Down
170 changes: 170 additions & 0 deletions projects/packages/forms/src/contact-form/class-contact-form-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ 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 );
// 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 );

// custom post type we'll use to keep copies of the feedback items
register_post_type(
Expand Down Expand Up @@ -282,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,
)
Expand Down Expand Up @@ -2747,6 +2757,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;
}

Expand All @@ -2765,9 +2780,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.
*
Expand Down Expand Up @@ -3952,6 +4019,109 @@ public function restrict_feedback_comments_to_logged_in( $open, $post_id ) {
return current_user_can( 'edit_pages' );
}

/**
* 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 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$$
*
* @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 ) || ! self::is_feedback_post( $commentdata['comment_post_ID'] ?? 0 ) ) {
return $approved;
}

return 0;
}

/**
* Give comments on feedback posts the note comment type, and keep them held.
*
* 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 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.
*
* 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_merge( (array) $query->query_vars['type__not_in'], array( Feedback::NOTE_COMMENT_TYPE ) );
}

/**
* 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 );

return $comment && Feedback::NOTE_COMMENT_TYPE === $comment->comment_type ? false : $maybe_notify;
}

/**
* Kludge method: reverses the output of a standard print_r( $array ).
* Sort of what unserialize does to a serialized object.
Expand Down
9 changes: 9 additions & 0 deletions projects/packages/forms/src/contact-form/class-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -51,19 +52,21 @@ const FeedbackComments = ( { postId }: FeedbackCommentsProps ): JSX.Element => {
isLoadingComments,
} = useSelect(
select => {
const commentsData = select( coreStore ).getEntityRecords( 'root', 'comment', {
// 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: 'all',
type: NOTE_COMMENT_TYPE,
orderby: 'date',
order: 'asc',
post: postId,
} ) as FeedbackComment[] | null | undefined;
};
const commentsData = select( coreStore ).getEntityRecords( 'root', 'comment', query ) 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,
Expand Down Expand Up @@ -200,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 ) );
Expand Down
5 changes: 5 additions & 0 deletions projects/packages/forms/src/dashboard/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1922,4 +1923,63 @@ 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 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, $panel_query, $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' );
$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() );
}

/**
* 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, false, 401 ),
'subscriber' => array( 'subscriber', false, 403 ),
'author' => array( 'author', true, 403 ),
'editor' => array( 'editor', true, 200 ),
'administrator' => array( 'administrator', true, 200 ),
);
}
}
Loading
Loading