public function create_item_permissions_check( $request ) {

		if ( ! is_user_logged_in() && get_option( 'comment_registration' ) ) {
			return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) );
		}

		// Limit who can set comment `author`, `karma` or `status` to anything other than the default.
		if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_author', __( 'Comment author invalid.' ), array( 'status' => rest_authorization_required_code() ) );
		}
		if ( isset( $request['karma'] ) && $request['karma'] > 0 && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_karma', __( 'Sorry, you cannot set karma for comments.' ), array( 'status' => rest_authorization_required_code() ) );
		}
		if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_status', __( 'Sorry, you cannot set status for comments.' ), array( 'status' => rest_authorization_required_code() ) );
		}

		if ( empty( $request['post'] ) && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_post_id', __( 'Sorry, you cannot create this comment without a post.' ), array( 'status' => rest_authorization_required_code() ) );
		}

		if ( ! empty( $request['post'] ) && $post = $this->get_post( (int) $request['post'] ) ) {
			if ( 'draft' === $post->post_status ) {
				return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
			}

			if ( 'trash' === $post->post_status ) {
				return new WP_Error( 'rest_comment_trash_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
			}

			if ( ! $this->check_read_post_permission( $post ) ) {
				return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
			}

			if ( ! comments_open( $post->ID ) ) {
				return new WP_Error( 'rest_comment_closed', __( 'Sorry, comments are closed on this post.' ), array( 'status' => 403 ) );
			}
		}

		return true;
	}
public function create_item_permissions_check( $request ) {

		if ( ! is_user_logged_in() && get_option( 'comment_registration' ) ) {
			return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) );
		}

		// Limit who can set comment `author`, `karma` or `status` to anything other than the default.
		if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_author', __( 'Comment author invalid.' ), array( 'status' => rest_authorization_required_code() ) );
		}
		if ( isset( $request['karma'] ) && $request['karma'] > 0 && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_karma', __( 'Sorry, you cannot set karma for comments.' ), array( 'status' => rest_authorization_required_code() ) );
		}
		if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_status', __( 'Sorry, you cannot set status for comments.' ), array( 'status' => rest_authorization_required_code() ) );
		}

		if ( empty( $request['post'] ) && ! current_user_can( 'moderate_comments' ) ) {
			return new WP_Error( 'rest_comment_invalid_post_id', __( 'Sorry, you cannot create this comment without a post.' ), array( 'status' => rest_authorization_required_code() ) );
		}

		if ( ! empty( $request['post'] ) && $post = $this->get_post( (int) $request['post'] ) ) {
			if ( 'draft' === $post->post_status ) {
				return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
			}

			if ( 'trash' === $post->post_status ) {
				return new WP_Error( 'rest_comment_trash_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
			}

			if ( ! $this->check_read_post_permission( $post ) ) {
				return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
			}

			if ( ! comments_open( $post->ID ) ) {
				return new WP_Error( 'rest_comment_closed', __( 'Sorry, comments are closed on this post.' ), array( 'status' => 403 ) );
			}
		}

		return true;
	}
public function create_item_permissions_check ( $ request ) { if ( ! is_user_logged_in ( ) && get_option ( 'comment_registration' ) ) { return new WP_Error ( 'rest_comment_login_required' , __ ( 'Sorry, you must be logged in to comment.' ) , array ( 'status' => 401 ) ) ; } // Limit who can set comment `author`, `karma` or `status` to anything other than the default. if ( isset ( $ request [ 'author' ] ) && get_current_user_id ( ) !== $ request [ 'author' ] && ! current_user_can ( 'moderate_comments' ) ) { return new WP_Error ( 'rest_comment_invalid_author' , __ ( 'Comment author invalid.' ) , array ( 'status' => rest_authorization_required_code ( ) ) ) ; } if ( isset ( $ request [ 'karma' ] ) && $ request [ 'karma' ] > 0 && ! current_user_can ( 'moderate_comments' ) ) { return new WP_Error ( 'rest_comment_invalid_karma' , __ ( 'Sorry, you cannot set karma for comments.' ) , array ( 'status' => rest_authorization_required_code ( ) ) ) ; } if ( isset ( $ request [ 'status' ] ) && ! current_user_can ( 'moderate_comments' ) ) { return new WP_Error ( 'rest_comment_invalid_status' , __ ( 'Sorry, you cannot set status for comments.' ) , array ( 'status' => rest_authorization_required_code ( ) ) ) ; } if ( empty ( $ request [ 'post' ] ) && ! current_user_can ( 'moderate_comments' ) ) { return new WP_Error ( 'rest_comment_invalid_post_id' , __ ( 'Sorry, you cannot create this comment without a post.' ) , array ( 'status' => rest_authorization_required_code ( ) ) ) ; } if ( ! empty ( $ request [ 'post' ] ) && $ post = $ this -> get_post ( ( int ) $ request [ 'post' ] ) ) { if ( 'draft' === $ post -> post_status ) { return new WP_Error ( 'rest_comment_draft_post' , __ ( 'Sorry, you cannot create a comment on this post.' ) , array ( 'status' => 403 ) ) ; } if ( 'trash' === $ post -> post_status ) { return new WP_Error ( 'rest_comment_trash_post' , __ ( 'Sorry, you cannot create a comment on this post.' ) , array ( 'status' => 403 ) ) ; } if ( ! $ this -> check_read_post_permission ( $ post ) ) { return new WP_Error ( 'rest_cannot_read_post' , __ ( 'Sorry, you cannot read the post for this comment.' ) , array ( 'status' => rest_authorization_required_code ( ) ) ) ; } if ( ! comments_open ( $ post -> ID ) ) { return new WP_Error ( 'rest_comment_closed' , __ ( 'Sorry, comments are closed on this post.' ) , array ( 'status' => 403 ) ) ; } } return true ; }
Check if a given request has access to create a comment

@param  WP_REST_Request $request Full details about the request.
@return WP_Error|boolean
Check if a given request has access to create a comment