WordPress.org

Make WordPress Core

Changeset 39292


Ignore:
Timestamp:
11/18/16 18:52:19 (5 months ago)
Author:
rachelbaker
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39290 r39292  
    122122 
    123123        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' ); 
    125125            $forbidden_params = array(); 
    126126 
     
    173173            'exclude'        => 'comment__not_in', 
    174174            'include'        => 'comment__in', 
    175             'karma'          => 'karma', 
    176175            'offset'         => 'offset', 
    177176            'order'          => 'order', 
     
    198197 
    199198        // 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 ) { 
    201200            if ( ! isset( $prepared_args[ $param ] ) ) { 
    202201                $prepared_args[ $param ] = ''; 
     
    373372        } 
    374373 
    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. 
    376375        if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { 
    377376            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() ) ); 
    382377        } 
    383378 
     
    818813                'raw'      => $comment->comment_content, 
    819814            ), 
    820             'karma'              => (int) $comment->comment_karma, 
    821815            'link'               => get_comment_link( $comment ), 
    822816            'status'             => $this->prepare_status_response( $comment->comment_approved ), 
     
    10591053            // Comment type "comment" needs to be created as an empty string. 
    10601054            $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type']; 
    1061         } 
    1062  
    1063         if ( isset( $request['karma'] ) ) { 
    1064             $prepared_comment['comment_karma'] = $request['karma'] ; 
    10651055        } 
    10661056 
     
    11851175                    'context'      => array( 'view', 'edit' ), 
    11861176                ), 
    1187                 'karma'             => array( 
    1188                     'description'  => __( 'Karma for the object.' ), 
    1189                     'type'         => 'integer', 
    1190                     'context'      => array( 'edit' ), 
    1191                 ), 
    11921177                'link'             => array( 
    11931178                    'description'  => __( 'URL to the object.' ), 
     
    13211306            ), 
    13221307            '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', 
    13291308        ); 
    13301309 
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39290 r39292  
    136136            'exclude', 
    137137            'include', 
    138             'karma', 
    139138            'offset', 
    140139            'order', 
     
    11621161    } 
    11631162 
    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  
    11851163    public function test_create_comment_invalid_post() { 
    11861164        wp_set_current_user( self::$subscriber_id ); 
     
    11931171            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.', 
    11941172            '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', 
    12161173        ); 
    12171174 
     
    16501607            'content'      => 'Testing.', 
    16511608            'date'         => '2014-11-07T10:14:25', 
    1652             'karma'        => 100, 
    16531609            'post'         => $post_id, 
    16541610        ); 
     
    16691625        $this->assertEquals( $params['author_ip'], $comment['author_ip'] ); 
    16701626        $this->assertEquals( $params['post'], $comment['post'] ); 
    1671         $this->assertEquals( $params['karma'], $comment['karma'] ); 
    16721627 
    16731628        $this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] ); 
     
    22242179        $data = $response->get_data(); 
    22252180        $properties = $data['schema']['properties']; 
    2226         $this->assertEquals( 18, count( $properties ) ); 
     2181        $this->assertEquals( 17, count( $properties ) ); 
    22272182        $this->assertArrayHasKey( 'id', $properties ); 
    22282183        $this->assertArrayHasKey( 'author', $properties ); 
     
    22362191        $this->assertArrayHasKey( 'date', $properties ); 
    22372192        $this->assertArrayHasKey( 'date_gmt', $properties ); 
    2238         $this->assertArrayHasKey( 'karma', $properties ); 
    22392193        $this->assertArrayHasKey( 'link', $properties ); 
    22402194        $this->assertArrayHasKey( 'meta', $properties ); 
     
    23832337            $this->assertEquals( $comment->comment_agent, $data['author_user_agent'] ); 
    23842338            $this->assertEquals( $comment->comment_content, $data['content']['raw'] ); 
    2385             $this->assertEquals( $comment->comment_karma, $data['karma'] ); 
    23862339        } 
    23872340 
     
    23912344            $this->assertArrayNotHasKey( 'author_user_agent', $data ); 
    23922345            $this->assertArrayNotHasKey( 'raw', $data['content'] ); 
    2393             $this->assertArrayNotHasKey( 'karma', $data ); 
    23942346        } 
    23952347    } 
Note: See TracChangeset for help on using the changeset viewer.