Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.
Merged
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
4 changes: 4 additions & 0 deletions lib/endpoints/class-wp-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ public function get_item_permissions_check( $request ) {
*/
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() ) );
Expand Down
11 changes: 11 additions & 0 deletions tests/test-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ public function test_create_comment_closed() {
$this->assertEquals( 403, $response->get_status() );
}

public function test_create_comment_require_login() {
wp_set_current_user( 0 );
update_option( 'comment_registration', 1 );
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->set_param( 'post', $this->post_id );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'rest_comment_login_required', $data['code'] );
}

public function test_create_comment_two_times() {

$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
Expand Down