REST API: Remove the `karma` property and query parameter from the Comments endpoints.

WordPress has not used the `karma` property internally for the past 8 years. There is no need to expose it in the REST API endpoints. Sites that use `karma` can include it using the `register_rest_field()` function.

Props dd32, danielbachhuber.
Fixes #38821.

git-svn-id: https://develop.svn.wordpress.org/trunk@39292 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Rachel Baker 2016-11-18 18:52:19 +00:00
parent ebe736af3f
commit 7552367e7a
2 changed files with 4 additions and 73 deletions

View File

@ -121,7 +121,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
if ( ! current_user_can( 'edit_posts' ) ) {
$protected_params = array( 'author', 'author_exclude', 'karma', 'author_email', 'type', 'status' );
$protected_params = array( 'author', 'author_exclude', 'author_email', 'type', 'status' );
$forbidden_params = array();
foreach ( $protected_params as $param ) {
@ -172,7 +172,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
'author_exclude' => 'author__not_in',
'exclude' => 'comment__not_in',
'include' => 'comment__in',
'karma' => 'karma',
'offset' => 'offset',
'order' => 'order',
'parent' => 'parent__in',
@ -197,7 +196,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
// Ensure certain parameter values default to empty strings.
foreach ( array( 'author_email', 'karma', 'search' ) as $param ) {
foreach ( array( 'author_email', 'search' ) as $param ) {
if ( ! isset( $prepared_args[ $param ] ) ) {
$prepared_args[ $param ] = '';
}
@ -372,15 +371,11 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
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.
// Limit who can set comment `author` 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 are not allowed to 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 are not allowed to set status for comments.' ), array( 'status' => rest_authorization_required_code() ) );
}
@ -817,7 +812,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
'raw' => $comment->comment_content,
),
'karma' => (int) $comment->comment_karma,
'link' => get_comment_link( $comment ),
'status' => $this->prepare_status_response( $comment->comment_approved ),
'type' => get_comment_type( $comment->comment_ID ),
@ -1060,10 +1054,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
$prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type'];
}
if ( isset( $request['karma'] ) ) {
$prepared_comment['comment_karma'] = $request['karma'] ;
}
if ( ! empty( $request['date'] ) ) {
$date_data = rest_get_date_with_gmt( $request['date'] );
@ -1184,11 +1174,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'karma' => array(
'description' => __( 'Karma for the object.' ),
'type' => 'integer',
'context' => array( 'edit' ),
),
'link' => array(
'description' => __( 'URL to the object.' ),
'type' => 'string',
@ -1322,12 +1307,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
'default' => array(),
);
$query_params['karma'] = array(
'default' => null,
'description' => __( 'Limit result set to that of a particular comment karma. Requires authorization.' ),
'type' => 'integer',
);
$query_params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of comments.' ),
'type' => 'integer',

View File

@ -135,7 +135,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
'context',
'exclude',
'include',
'karma',
'offset',
'order',
'orderby',
@ -1161,27 +1160,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 );
}
public function test_create_comment_karma_without_permission() {
wp_set_current_user( self::$subscriber_id );
$params = array(
'post' => self::$post_id,
'author_name' => 'Homer Jay Simpson',
'author_email' => 'chunkylover53@aol.com',
'author_url' => 'http://compuglobalhypermeganet.com',
'content' => 'Here\s to alcohol: the cause of, and solution to, all of life\s problems.',
'author' => self::$subscriber_id,
'karma' => 100,
);
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );
}
public function test_create_comment_invalid_post() {
wp_set_current_user( self::$subscriber_id );
@ -1202,27 +1180,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_create_comment_karma_invalid_value() {
wp_set_current_user( self::$subscriber_id );
$params = array(
'post' => self::$post_id,
'author_name' => 'Homer Jay Simpson',
'author_email' => 'chunkylover53@aol.com',
'author_url' => 'http://compuglobalhypermeganet.com',
'content' => 'Here\s to alcohol: the cause of, and solution to, all of life\s problems.',
'author' => self::$subscriber_id,
'karma' => 'themostkarmaever',
);
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_create_comment_status_without_permission() {
wp_set_current_user( self::$subscriber_id );
@ -1649,7 +1606,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
'author_ip' => '4.4.4.4',
'content' => 'Testing.',
'date' => '2014-11-07T10:14:25',
'karma' => 100,
'post' => $post_id,
);
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
@ -1668,7 +1624,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertEquals( $params['author_email'], $comment['author_email'] );
$this->assertEquals( $params['author_ip'], $comment['author_ip'] );
$this->assertEquals( $params['post'], $comment['post'] );
$this->assertEquals( $params['karma'], $comment['karma'] );
$this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
$this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
@ -2223,7 +2178,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 18, count( $properties ) );
$this->assertEquals( 17, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'author_avatar_urls', $properties );
@ -2235,7 +2190,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertArrayHasKey( 'content', $properties );
$this->assertArrayHasKey( 'date', $properties );
$this->assertArrayHasKey( 'date_gmt', $properties );
$this->assertArrayHasKey( 'karma', $properties );
$this->assertArrayHasKey( 'link', $properties );
$this->assertArrayHasKey( 'meta', $properties );
$this->assertArrayHasKey( 'parent', $properties );
@ -2382,7 +2336,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertEquals( $comment->comment_author_IP, $data['author_ip'] );
$this->assertEquals( $comment->comment_agent, $data['author_user_agent'] );
$this->assertEquals( $comment->comment_content, $data['content']['raw'] );
$this->assertEquals( $comment->comment_karma, $data['karma'] );
}
if ( 'edit' !== $context ) {
@ -2390,7 +2343,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertArrayNotHasKey( 'author_ip', $data );
$this->assertArrayNotHasKey( 'author_user_agent', $data );
$this->assertArrayNotHasKey( 'raw', $data['content'] );
$this->assertArrayNotHasKey( 'karma', $data );
}
}
}