wpseek.com
Bazujące na WordPress narzędzie wyszukiwania dla deweloperów i twórców motywów.



_wp_check_comment_target_post › WordPress Function

Od7.1.5
Przestarzałyn/a
› _wp_check_comment_target_post ( $comment, $post_id )
Dostęp:
  • private
Parametry: (2)
  • (WP_Comment) $comment The comment being moved.
    Wymagane: Tak
  • (int) $post_id ID of the post the comment is being moved to.
    Wymagane: Tak
Powrót:
  • (true|WP_Error) True if the comment may be moved to the post, WP_Error otherwise.
Zdefiniowane na:
Codex:

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.


Źródło

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;
}