Changeset 39292
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r39290 r39292 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 … … 173 173 'exclude' => 'comment__not_in', 174 174 'include' => 'comment__in', 175 'karma' => 'karma',176 175 'offset' => 'offset', 177 176 'order' => 'order', … … 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 ] = ''; … … 373 372 } 374 373 375 // Limit who can set comment `author` , `karma`or `status` to anything other than the default.374 // Limit who can set comment `author` or `status` to anything other than the default. 376 375 if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { 377 376 return new WP_Error( 'rest_comment_invalid_author', __( 'Comment author invalid.' ), array( 'status' => rest_authorization_required_code() ) ); 378 }379 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 377 } 383 378 … … 818 813 'raw' => $comment->comment_content, 819 814 ), 820 'karma' => (int) $comment->comment_karma,821 815 'link' => get_comment_link( $comment ), 822 816 'status' => $this->prepare_status_response( $comment->comment_approved ), … … 1059 1053 // Comment type "comment" needs to be created as an empty string. 1060 1054 $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type']; 1061 }1062 1063 if ( isset( $request['karma'] ) ) {1064 $prepared_comment['comment_karma'] = $request['karma'] ;1065 1055 } 1066 1056 … … 1185 1175 'context' => array( 'view', 'edit' ), 1186 1176 ), 1187 'karma' => array(1188 'description' => __( 'Karma for the object.' ),1189 'type' => 'integer',1190 'context' => array( 'edit' ),1191 ),1192 1177 'link' => array( 1193 1178 'description' => __( 'URL to the object.' ), … … 1321 1306 ), 1322 1307 'default' => array(), 1323 );1324 1325 $query_params['karma'] = array(1326 'default' => null,1327 'description' => __( 'Limit result set to that of a particular comment karma. Requires authorization.' ),1328 'type' => 'integer',1329 1308 ); 1330 1309 -
trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php
r39290 r39292 136 136 'exclude', 137 137 'include', 138 'karma',139 138 'offset', 140 139 'order', … … 1162 1161 } 1163 1162 1164 public function test_create_comment_karma_without_permission() {1165 wp_set_current_user( self::$subscriber_id );1166 1167 $params = array(1168 'post' => self::$post_id,1169 'author_name' => 'Homer Jay Simpson',1170 'author_email' => 'chunkylover53@aol.com',1171 'author_url' => 'http://compuglobalhypermeganet.com',1172 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',1173 'author' => self::$subscriber_id,1174 'karma' => 100,1175 );1176 1177 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );1178 $request->add_header( 'content-type', 'application/json' );1179 $request->set_body( wp_json_encode( $params ) );1180 $response = $this->server->dispatch( $request );1181 1182 $this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );1183 }1184 1185 1163 public function test_create_comment_invalid_post() { 1186 1164 wp_set_current_user( self::$subscriber_id ); … … 1193 1171 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.', 1194 1172 'author' => self::$subscriber_id, 1195 );1196 1197 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );1198 $request->add_header( 'content-type', 'application/json' );1199 $request->set_body( wp_json_encode( $params ) );1200 $response = $this->server->dispatch( $request );1201 1202 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );1203 }1204 1205 public function test_create_comment_karma_invalid_value() {1206 wp_set_current_user( self::$subscriber_id );1207 1208 $params = array(1209 'post' => self::$post_id,1210 'author_name' => 'Homer Jay Simpson',1211 'author_email' => 'chunkylover53@aol.com',1212 'author_url' => 'http://compuglobalhypermeganet.com',1213 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',1214 'author' => self::$subscriber_id,1215 'karma' => 'themostkarmaever',1216 1173 ); 1217 1174 … … 1650 1607 'content' => 'Testing.', 1651 1608 'date' => '2014-11-07T10:14:25', 1652 'karma' => 100,1653 1609 'post' => $post_id, 1654 1610 ); … … 1669 1625 $this->assertEquals( $params['author_ip'], $comment['author_ip'] ); 1670 1626 $this->assertEquals( $params['post'], $comment['post'] ); 1671 $this->assertEquals( $params['karma'], $comment['karma'] );1672 1627 1673 1628 $this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] ); … … 2224 2179 $data = $response->get_data(); 2225 2180 $properties = $data['schema']['properties']; 2226 $this->assertEquals( 1 8, count( $properties ) );2181 $this->assertEquals( 17, count( $properties ) ); 2227 2182 $this->assertArrayHasKey( 'id', $properties ); 2228 2183 $this->assertArrayHasKey( 'author', $properties ); … … 2236 2191 $this->assertArrayHasKey( 'date', $properties ); 2237 2192 $this->assertArrayHasKey( 'date_gmt', $properties ); 2238 $this->assertArrayHasKey( 'karma', $properties );2239 2193 $this->assertArrayHasKey( 'link', $properties ); 2240 2194 $this->assertArrayHasKey( 'meta', $properties ); … … 2383 2337 $this->assertEquals( $comment->comment_agent, $data['author_user_agent'] ); 2384 2338 $this->assertEquals( $comment->comment_content, $data['content']['raw'] ); 2385 $this->assertEquals( $comment->comment_karma, $data['karma'] );2386 2339 } 2387 2340 … … 2391 2344 $this->assertArrayNotHasKey( 'author_user_agent', $data ); 2392 2345 $this->assertArrayNotHasKey( 'raw', $data['content'] ); 2393 $this->assertArrayNotHasKey( 'karma', $data );2394 2346 } 2395 2347 }
Note: See TracChangeset
for help on using the changeset viewer.