_wp_check_comment_target_post()
Checks that the current user may move a comment to another post.
Mirrors the checks applied by the REST API comments controller when the parent post of a comment or note is changed.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
true|WP_Error. True if the comment may be moved to the post, WP_Error otherwise.
Usage
_wp_check_comment_target_post( $comment, $post_id );
- $comment(WP_Comment) (required)
- The comment being moved.
- $post_id(int) (required)
- ID of the post the comment is being moved to.
Changelog
| Since 7.1.5 | Introduced. |
_wp_check_comment_target_post() wp check comment target post code WP 7.1.5
function _wp_check_comment_target_post( WP_Comment $comment, int $post_id ) {
$post = $post_id ? get_post( $post_id ) : null;
if ( ! $post ) {
return new WP_Error( 'comment_post_invalid', __( 'Invalid post ID.' ) );
}
/*
* Notes are editorial content, so they may only be attached to a post the
* user can edit. Any other comment needs either comment moderation rights
* or edit access to the post, matching what 'edit_comment' requires on the
* post the comment is moving away from.
*/
$is_note = 'note' === strtolower( $comment->comment_type );
if ( $is_note ) {
$can_target_post = current_user_can( 'edit_post', $post->ID );
} else {
$can_target_post = current_user_can( 'moderate_comments' ) || current_user_can( 'edit_post', $post->ID );
}
if ( ! $can_target_post ) {
return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
}
if ( ! $is_note ) {
return true;
}
$supports_notes = false;
$supports = get_all_post_type_supports( $post->post_type );
if ( isset( $supports['editor'] ) && is_array( $supports['editor'] ) ) {
$supports_notes = array_any(
$supports['editor'],
static function ( $item ) {
return is_array( $item ) && ! empty( $item['notes'] );
}
);
}
if ( ! $supports_notes ) {
return new WP_Error( 'comment_post_invalid', __( 'Sorry, this post type does not support notes.' ) );
}
if ( 'trash' === $post->post_status || ! current_user_can( 'read_post', $post->ID ) ) {
return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
}
return true;
}