Skip to content

Commit 86d0d8a

Browse files
committed
Comments: Check permissions before moving a comment to another post in the admin.
Props jorbin, ramonopoly, westonruter, adamsilverstein, jeremyfelt, rfaile313. git-svn-id: https://develop.svn.wordpress.org/trunk@65578 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ba9a93c commit 86d0d8a

1 file changed

Lines changed: 107 additions & 4 deletions

File tree

‎src/wp-admin/includes/comment.php‎

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
4747
* @since 2.0.0
4848
* @since 5.5.0 A return value was added.
4949
* @since 7.1.0 The comment parent can be updated via `$_POST['comment_parent']`.
50+
* @since 7.1.5 Moving a comment to another post is checked against the user's capabilities,
51+
* and the comment type can no longer be changed.
5052
*
5153
* @return int|WP_Error The value 1 if the comment was updated, 0 if not updated.
5254
* A WP_Error object on failure.
@@ -75,14 +77,50 @@ function edit_comment() {
7577
$_POST['comment_ID'] = (int) $_POST['comment_ID'];
7678
}
7779

80+
$comment_id = (int) $_POST['comment_ID'];
81+
$comment = get_comment( $comment_id );
82+
83+
/*
84+
* The comment type is not editable from the Edit Comment screen. Changing it
85+
* would turn a private note into a public comment or vice versa, and would
86+
* break the rule that replies share their parent's type.
87+
*/
88+
if ( $comment && isset( $_POST['comment_type'] ) && $_POST['comment_type'] !== $comment->comment_type ) {
89+
return new WP_Error( 'comment_type_invalid', __( 'The comment type cannot be changed.' ) );
90+
}
91+
92+
/*
93+
* The capability check above only establishes that the comment may be edited
94+
* where it currently sits, because 'edit_comment' maps to 'edit_post' on the
95+
* comment's current post. Moving the comment to another post has to be
96+
* authorized separately, otherwise a user who can edit a comment on their own
97+
* post could attach it to any post, including posts they can neither read nor
98+
* edit.
99+
*/
100+
101+
$comment_post_id = $comment ? (int) $comment->comment_post_ID : 0;
102+
103+
if ( $comment && isset( $_POST['comment_post_ID'] ) ) {
104+
$target_post_id = (int) $_POST['comment_post_ID'];
105+
106+
$_POST['comment_post_ID'] = $target_post_id;
107+
108+
if ( $target_post_id !== $comment_post_id ) {
109+
$target_post_check = _wp_check_comment_target_post( $comment, $target_post_id );
110+
111+
if ( is_wp_error( $target_post_check ) ) {
112+
return $target_post_check;
113+
}
114+
115+
$comment_post_id = $target_post_id;
116+
}
117+
}
118+
78119
if ( isset( $_POST['comment_parent'] ) ) {
79-
$comment_id = (int) $_POST['comment_ID'];
80120
$comment_parent = (int) $_POST['comment_parent'];
81121

82122
$_POST['comment_parent'] = $comment_parent;
83123

84-
$comment = get_comment( $comment_id );
85-
86124
if ( $comment && $comment_parent && $comment_parent !== (int) $comment->comment_parent ) {
87125
if ( ! get_option( 'thread_comments' ) ) {
88126
return new WP_Error( 'comment_parent_invalid', __( 'The comment parent cannot be changed because threaded comments are disabled.' ) );
@@ -100,7 +138,7 @@ function edit_comment() {
100138
// The parent must be a comment of the same type, on the same post, and publicly visible if the comment is approved.
101139
if (
102140
! $parent
103-
|| (int) $parent->comment_post_ID !== (int) $comment->comment_post_ID
141+
|| (int) $parent->comment_post_ID !== $comment_post_id
104142
|| $parent->comment_type !== $comment->comment_type
105143
|| in_array( $parent_status, array( 'spam', 'trash' ), true )
106144
|| ( $comment_is_approved && 'approved' !== $parent_status )
@@ -183,6 +221,71 @@ function edit_comment() {
183221
return wp_update_comment( $_POST, true );
184222
}
185223

224+
/**
225+
* Checks that the current user may move a comment to another post.
226+
*
227+
* Mirrors the checks applied by the REST API comments controller when the
228+
* parent post of a comment or note is changed.
229+
*
230+
* @since 7.1.5
231+
* @access private
232+
*
233+
* @param WP_Comment $comment The comment being moved.
234+
* @param int $post_id ID of the post the comment is being moved to.
235+
* @return true|WP_Error True if the comment may be moved to the post, WP_Error otherwise.
236+
*/
237+
function _wp_check_comment_target_post( WP_Comment $comment, int $post_id ) {
238+
$post = $post_id ? get_post( $post_id ) : null;
239+
240+
if ( ! $post ) {
241+
return new WP_Error( 'comment_post_invalid', __( 'Invalid post ID.' ) );
242+
}
243+
244+
/*
245+
* Notes are editorial content, so they may only be attached to a post the
246+
* user can edit. Any other comment needs either comment moderation rights
247+
* or edit access to the post, matching what 'edit_comment' requires on the
248+
* post the comment is moving away from.
249+
*/
250+
$is_note = 'note' === strtolower( $comment->comment_type );
251+
252+
if ( $is_note ) {
253+
$can_target_post = current_user_can( 'edit_post', $post->ID );
254+
} else {
255+
$can_target_post = current_user_can( 'moderate_comments' ) || current_user_can( 'edit_post', $post->ID );
256+
}
257+
258+
if ( ! $can_target_post ) {
259+
return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
260+
}
261+
262+
if ( ! $is_note ) {
263+
return true;
264+
}
265+
266+
$supports_notes = false;
267+
$supports = get_all_post_type_supports( $post->post_type );
268+
269+
if ( isset( $supports['editor'] ) && is_array( $supports['editor'] ) ) {
270+
$supports_notes = array_any(
271+
$supports['editor'],
272+
static function ( $item ) {
273+
return is_array( $item ) && ! empty( $item['notes'] );
274+
}
275+
);
276+
}
277+
278+
if ( ! $supports_notes ) {
279+
return new WP_Error( 'comment_post_invalid', __( 'Sorry, this post type does not support notes.' ) );
280+
}
281+
282+
if ( 'trash' === $post->post_status || ! current_user_can( 'read_post', $post->ID ) ) {
283+
return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
284+
}
285+
286+
return true;
287+
}
288+
186289
/**
187290
* Returns a WP_Comment object based on comment ID.
188291
*

0 commit comments

Comments
 (0)