diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index e0ca944072..e5a9541a0e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -419,7 +419,10 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { $prepared_comment['comment_author_url'] = ''; } - $prepared_comment['comment_agent'] = ''; + if ( ! isset( $prepared_comment['comment_agent'] ) ) { + $prepared_comment['comment_agent'] = ''; + } + $prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment, true ); if ( is_wp_error( $prepared_comment['comment_approved'] ) ) { @@ -888,6 +891,10 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { $prepared_comment['comment_author_IP'] = $request['author_ip']; } + if ( isset( $request['author_user_agent'] ) ) { + $prepared_comment['comment_agent'] = $request['author_user_agent']; + } + if ( isset( $request['type'] ) ) { // Comment type "comment" needs to be created as an empty string. $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type']; @@ -975,7 +982,9 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { 'description' => __( 'User agent for the object author.' ), 'type' => 'string', 'context' => array( 'edit' ), - 'readonly' => true, + 'arg_options' => array( + 'sanitize_callback' => 'sanitize_text_field', + ), ), 'content' => array( 'description' => __( 'The content for the object.' ), diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index b3feb27c01..df7f3fa583 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -1064,7 +1064,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertErrorResponse( 'rest_comment_invalid_status', $response, 403 ); } - public function test_create_comment_with_status_and_IP() { + public function test_create_comment_with_status_IP_and_user_agent() { $post_id = $this->factory->post->create(); wp_set_current_user( $this->admin_id ); @@ -1074,6 +1074,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'author_email' => 'cbg@androidsdungeon.com', 'author_ip' => '139.130.4.5', 'author_url' => 'http://androidsdungeon.com', + 'author_user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', 'content' => 'Worst Comment Ever!', 'status' => 'approved', ); @@ -1088,6 +1089,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $data = $response->get_data(); $this->assertEquals( 'approved', $data['status'] ); $this->assertEquals( '139.130.4.5', $data['author_ip'] ); + $this->assertEquals( 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', $data['author_user_agent'] ); } public function test_create_comment_invalid_author_IP() {