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 ccd172f..32d9db3 100644
|
a
|
b
|
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 124 | | $protected_params = array( 'author', 'author_exclude', 'karma', 'author_email', 'type', 'status' ); |
| | 124 | $protected_params = array( 'author', 'author_exclude', 'author_email', 'type', 'status' ); |
| 125 | 125 | $forbidden_params = array(); |
| 126 | 126 | |
| 127 | 127 | foreach ( $protected_params as $param ) { |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 172 | 172 | 'author_exclude' => 'author__not_in', |
| 173 | 173 | 'exclude' => 'comment__not_in', |
| 174 | 174 | 'include' => 'comment__in', |
| 175 | | 'karma' => 'karma', |
| 176 | 175 | 'offset' => 'offset', |
| 177 | 176 | 'order' => 'order', |
| 178 | 177 | 'parent' => 'parent__in', |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 197 | 196 | } |
| 198 | 197 | |
| 199 | 198 | // Ensure certain parameter values default to empty strings. |
| 200 | | foreach ( array( 'author_email', 'karma', 'search' ) as $param ) { |
| | 199 | foreach ( array( 'author_email', 'search' ) as $param ) { |
| 201 | 200 | if ( ! isset( $prepared_args[ $param ] ) ) { |
| 202 | 201 | $prepared_args[ $param ] = ''; |
| 203 | 202 | } |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 377 | 376 | return new WP_Error( 'rest_comment_invalid_author', __( 'Comment author invalid.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 378 | 377 | } |
| 379 | 378 | |
| 380 | | if ( isset( $request['karma'] ) && $request['karma'] > 0 && ! current_user_can( 'moderate_comments' ) ) { |
| 381 | | return new WP_Error( 'rest_comment_invalid_karma', __( 'Sorry, you are not allowed to set karma for comments.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 382 | | } |
| 383 | | |
| 384 | 379 | if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { |
| 385 | 380 | return new WP_Error( 'rest_comment_invalid_status', __( 'Sorry, you are not allowed to set status for comments.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 386 | 381 | } |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 812 | 807 | 'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ), |
| 813 | 808 | 'raw' => $comment->comment_content, |
| 814 | 809 | ), |
| 815 | | 'karma' => (int) $comment->comment_karma, |
| 816 | 810 | 'link' => get_comment_link( $comment ), |
| 817 | 811 | 'status' => $this->prepare_status_response( $comment->comment_approved ), |
| 818 | 812 | 'type' => get_comment_type( $comment->comment_ID ), |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 1055 | 1049 | $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type']; |
| 1056 | 1050 | } |
| 1057 | 1051 | |
| 1058 | | if ( isset( $request['karma'] ) ) { |
| 1059 | | $prepared_comment['comment_karma'] = $request['karma'] ; |
| 1060 | | } |
| 1061 | | |
| 1062 | 1052 | if ( ! empty( $request['date'] ) ) { |
| 1063 | 1053 | $date_data = rest_get_date_with_gmt( $request['date'] ); |
| 1064 | 1054 | |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 1179 | 1169 | 'format' => 'date-time', |
| 1180 | 1170 | 'context' => array( 'view', 'edit' ), |
| 1181 | 1171 | ), |
| 1182 | | 'karma' => array( |
| 1183 | | 'description' => __( 'Karma for the object.' ), |
| 1184 | | 'type' => 'integer', |
| 1185 | | 'context' => array( 'edit' ), |
| 1186 | | ), |
| 1187 | 1172 | 'link' => array( |
| 1188 | 1173 | 'description' => __( 'URL to the object.' ), |
| 1189 | 1174 | 'type' => 'string', |
| … |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 1317 | 1302 | 'default' => array(), |
| 1318 | 1303 | ); |
| 1319 | 1304 | |
| 1320 | | $query_params['karma'] = array( |
| 1321 | | 'default' => null, |
| 1322 | | 'description' => __( 'Limit result set to that of a particular comment karma. Requires authorization.' ), |
| 1323 | | 'type' => 'integer', |
| 1324 | | ); |
| 1325 | | |
| 1326 | 1305 | $query_params['offset'] = array( |
| 1327 | 1306 | 'description' => __( 'Offset the result set by a specific number of comments.' ), |
| 1328 | 1307 | 'type' => 'integer', |
diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
index 33ac37d..df88734 100644
|
a
|
b
|
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 135 | 135 | 'context', |
| 136 | 136 | 'exclude', |
| 137 | 137 | 'include', |
| 138 | | 'karma', |
| 139 | 138 | 'offset', |
| 140 | 139 | 'order', |
| 141 | 140 | 'orderby', |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 1135 | 1134 | $this->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 ); |
| 1136 | 1135 | } |
| 1137 | 1136 | |
| 1138 | | public function test_create_comment_karma_without_permission() { |
| 1139 | | wp_set_current_user( self::$subscriber_id ); |
| 1140 | | |
| 1141 | | $params = array( |
| 1142 | | 'post' => self::$post_id, |
| 1143 | | 'author_name' => 'Homer Jay Simpson', |
| 1144 | | 'author_email' => 'chunkylover53@aol.com', |
| 1145 | | 'author_url' => 'http://compuglobalhypermeganet.com', |
| 1146 | | 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.', |
| 1147 | | 'author' => self::$subscriber_id, |
| 1148 | | 'karma' => 100, |
| 1149 | | ); |
| 1150 | | |
| 1151 | | $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); |
| 1152 | | $request->add_header( 'content-type', 'application/json' ); |
| 1153 | | $request->set_body( wp_json_encode( $params ) ); |
| 1154 | | $response = $this->server->dispatch( $request ); |
| 1155 | | |
| 1156 | | $this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 ); |
| 1157 | | } |
| 1158 | | |
| 1159 | 1137 | public function test_create_comment_invalid_post() { |
| 1160 | 1138 | wp_set_current_user( self::$subscriber_id ); |
| 1161 | 1139 | |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 1176 | 1154 | $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); |
| 1177 | 1155 | } |
| 1178 | 1156 | |
| 1179 | | public function test_create_comment_karma_invalid_value() { |
| 1180 | | wp_set_current_user( self::$subscriber_id ); |
| 1181 | | |
| 1182 | | $params = array( |
| 1183 | | 'post' => self::$post_id, |
| 1184 | | 'author_name' => 'Homer Jay Simpson', |
| 1185 | | 'author_email' => 'chunkylover53@aol.com', |
| 1186 | | 'author_url' => 'http://compuglobalhypermeganet.com', |
| 1187 | | 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.', |
| 1188 | | 'author' => self::$subscriber_id, |
| 1189 | | 'karma' => 'themostkarmaever', |
| 1190 | | ); |
| 1191 | | |
| 1192 | | $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); |
| 1193 | | $request->add_header( 'content-type', 'application/json' ); |
| 1194 | | $request->set_body( wp_json_encode( $params ) ); |
| 1195 | | $response = $this->server->dispatch( $request ); |
| 1196 | | |
| 1197 | | $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); |
| 1198 | | } |
| 1199 | | |
| 1200 | 1157 | public function test_create_comment_status_without_permission() { |
| 1201 | 1158 | wp_set_current_user( self::$subscriber_id ); |
| 1202 | 1159 | |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 1623 | 1580 | 'author_ip' => '4.4.4.4', |
| 1624 | 1581 | 'content' => 'Testing.', |
| 1625 | 1582 | 'date' => '2014-11-07T10:14:25', |
| 1626 | | 'karma' => 100, |
| 1627 | 1583 | 'post' => $post_id, |
| 1628 | 1584 | ); |
| 1629 | 1585 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 1642 | 1598 | $this->assertEquals( $params['author_email'], $comment['author_email'] ); |
| 1643 | 1599 | $this->assertEquals( $params['author_ip'], $comment['author_ip'] ); |
| 1644 | 1600 | $this->assertEquals( $params['post'], $comment['post'] ); |
| 1645 | | $this->assertEquals( $params['karma'], $comment['karma'] ); |
| 1646 | 1601 | |
| 1647 | 1602 | $this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] ); |
| 1648 | 1603 | $this->assertEquals( '2014-11-07T10:14:25', $comment['date'] ); |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 2197 | 2152 | $response = $this->server->dispatch( $request ); |
| 2198 | 2153 | $data = $response->get_data(); |
| 2199 | 2154 | $properties = $data['schema']['properties']; |
| 2200 | | $this->assertEquals( 18, count( $properties ) ); |
| | 2155 | $this->assertEquals( 17, count( $properties ) ); |
| 2201 | 2156 | $this->assertArrayHasKey( 'id', $properties ); |
| 2202 | 2157 | $this->assertArrayHasKey( 'author', $properties ); |
| 2203 | 2158 | $this->assertArrayHasKey( 'author_avatar_urls', $properties ); |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 2209 | 2164 | $this->assertArrayHasKey( 'content', $properties ); |
| 2210 | 2165 | $this->assertArrayHasKey( 'date', $properties ); |
| 2211 | 2166 | $this->assertArrayHasKey( 'date_gmt', $properties ); |
| 2212 | | $this->assertArrayHasKey( 'karma', $properties ); |
| 2213 | 2167 | $this->assertArrayHasKey( 'link', $properties ); |
| 2214 | 2168 | $this->assertArrayHasKey( 'meta', $properties ); |
| 2215 | 2169 | $this->assertArrayHasKey( 'parent', $properties ); |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 2353 | 2307 | $this->assertEquals( $comment->comment_author_IP, $data['author_ip'] ); |
| 2354 | 2308 | $this->assertEquals( $comment->comment_agent, $data['author_user_agent'] ); |
| 2355 | 2309 | $this->assertEquals( $comment->comment_content, $data['content']['raw'] ); |
| 2356 | | $this->assertEquals( $comment->comment_karma, $data['karma'] ); |
| 2357 | 2310 | } |
| 2358 | 2311 | |
| 2359 | 2312 | if ( 'edit' !== $context ) { |
| … |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 2361 | 2314 | $this->assertArrayNotHasKey( 'author_ip', $data ); |
| 2362 | 2315 | $this->assertArrayNotHasKey( 'author_user_agent', $data ); |
| 2363 | 2316 | $this->assertArrayNotHasKey( 'raw', $data['content'] ); |
| 2364 | | $this->assertArrayNotHasKey( 'karma', $data ); |
| 2365 | 2317 | } |
| 2366 | 2318 | } |
| 2367 | 2319 | } |