Make WordPress Core

Ticket #38821: 38821.diff

File 38821.diff, 11.4 KB (added by dd32, 7 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    class WP_REST_Comments_Controller extend 
    109109                                $post = get_post( $post_id );
    110110
    111111                                if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) {
    112112                                        return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you are not allowed to read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
    113113                                } elseif ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) {
    114114                                        return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to read comments without a post.' ), array( 'status' => rest_authorization_required_code() ) );
    115115                                }
    116116                        }
    117117                }
    118118
    119119                if ( ! empty( $request['context'] ) && 'edit' === $request['context'] && ! current_user_can( 'moderate_comments' ) ) {
    120120                        return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view comments with edit context.' ), array( 'status' => rest_authorization_required_code() ) );
    121121                }
    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
    127127                        foreach ( $protected_params as $param ) {
    128128                                if ( 'status' === $param ) {
    129129                                        if ( 'approve' !== $request[ $param ] ) {
    130130                                                $forbidden_params[] = $param;
    131131                                        }
    132132                                } elseif ( 'type' === $param ) {
    133133                                        if ( 'comment' !== $request[ $param ] ) {
    134134                                                $forbidden_params[] = $param;
    135135                                        }
    136136                                } elseif ( ! empty( $request[ $param ] ) ) {
    137137                                        $forbidden_params[] = $param;
    138138                                }
    139139                        }
    class WP_REST_Comments_Controller extend 
    160160                // Retrieve the list of registered collection query parameters.
    161161                $registered = $this->get_collection_params();
    162162
    163163                /*
    164164                 * This array defines mappings between public API query parameters whose
    165165                 * values are accepted as-passed, and their internal WP_Query parameter
    166166                 * name equivalents (some are the same). Only values which are also
    167167                 * present in $registered will be set.
    168168                 */
    169169                $parameter_mappings = array(
    170170                        'author'         => 'author__in',
    171171                        'author_email'   => 'author_email',
    172172                        'author_exclude' => 'author__not_in',
    173173                        'exclude'        => 'comment__not_in',
    174174                        'include'        => 'comment__in',
    175                         'karma'          => 'karma',
    176175                        'offset'         => 'offset',
    177176                        'order'          => 'order',
    178177                        'parent'         => 'parent__in',
    179178                        'parent_exclude' => 'parent__not_in',
    180179                        'per_page'       => 'number',
    181180                        'post'           => 'post__in',
    182181                        'search'         => 'search',
    183182                        'status'         => 'status',
    184183                        'type'           => 'type',
    185184                );
    186185
    187186                $prepared_args = array();
    188187
    189188                /*
    190189                 * For each known parameter which is both registered and present in the request,
    191190                 * set the parameter's value on the query $prepared_args.
    192191                 */
    193192                foreach ( $parameter_mappings as $api_param => $wp_param ) {
    194193                        if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
    195194                                $prepared_args[ $wp_param ] = $request[ $api_param ];
    196195                        }
    197196                }
    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 ] = '';
    203202                        }
    204203                }
    205204
    206205                if ( isset( $registered['orderby'] ) ) {
    207206                        $prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] );
    208207                }
    209208
    210209                $prepared_args['no_found_rows'] = false;
    211210
    212211                $prepared_args['date_query'] = array();
    213212
    214213                // Set before into date query. Date query must be specified as an array of an array.
    215214                if ( isset( $registered['before'], $request['before'] ) ) {
    class WP_REST_Comments_Controller extend 
    360359        /**
    361360         * Checks if a given request has access to create a comment.
    362361         *
    363362         * @since 4.7.0
    364363         * @access public
    365364         *
    366365         * @param WP_REST_Request $request Full details about the request.
    367366         * @return WP_Error|bool True if the request has access to create items, error object otherwise.
    368367         */
    369368        public function create_item_permissions_check( $request ) {
    370369
    371370                if ( ! is_user_logged_in() && get_option( 'comment_registration' ) ) {
    372371                        return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) );
    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() ) );
    378377                }
    379378
    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 
    384379                if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) {
    385380                        return new WP_Error( 'rest_comment_invalid_status', __( 'Sorry, you are not allowed to set status for comments.' ), array( 'status' => rest_authorization_required_code() ) );
    386381                }
    387382
    388383                if ( empty( $request['post'] ) && ! current_user_can( 'moderate_comments' ) ) {
    389384                        return new WP_Error( 'rest_comment_invalid_post_id', __( 'Sorry, you are not allowed to create this comment without a post.' ), array( 'status' => rest_authorization_required_code() ) );
    390385                }
    391386
    392387                if ( ! empty( $request['post'] ) && $post = get_post( (int) $request['post'] ) ) {
    393388                        if ( 'draft' === $post->post_status ) {
    394389                                return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you are not allowed to create a comment on this post.' ), array( 'status' => 403 ) );
    395390                        }
    396391
    397392                        if ( 'trash' === $post->post_status ) {
    398393                                return new WP_Error( 'rest_comment_trash_post', __( 'Sorry, you are not allowed to create a comment on this post.' ), array( 'status' => 403 ) );
    class WP_REST_Comments_Controller extend 
    797792                        'post'               => (int) $comment->comment_post_ID,
    798793                        'parent'             => (int) $comment->comment_parent,
    799794                        'author'             => (int) $comment->user_id,
    800795                        'author_name'        => $comment->comment_author,
    801796                        'author_email'       => $comment->comment_author_email,
    802797                        'author_url'         => $comment->comment_author_url,
    803798                        'author_ip'          => $comment->comment_author_IP,
    804799                        'author_user_agent'  => $comment->comment_agent,
    805800                        'date'               => mysql_to_rfc3339( $comment->comment_date ),
    806801                        'date_gmt'           => mysql_to_rfc3339( $comment->comment_date_gmt ),
    807802                        'content'            => array(
    808803                                /** This filter is documented in wp-includes/comment-template.php */
    809804                                'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
    810805                                'raw'      => $comment->comment_content,
    811806                        ),
    812                         'karma'              => (int) $comment->comment_karma,
    813807                        'link'               => get_comment_link( $comment ),
    814808                        'status'             => $this->prepare_status_response( $comment->comment_approved ),
    815809                        'type'               => get_comment_type( $comment->comment_ID ),
    816810                );
    817811
    818812                $schema = $this->get_item_schema();
    819813
    820814                if ( ! empty( $schema['properties']['author_avatar_urls'] ) ) {
    821815                        $data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email );
    822816                }
    823817
    824818                if ( ! empty( $schema['properties']['meta'] ) ) {
    825819                        $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
    826820                }
    827821
    class WP_REST_Comments_Controller extend 
    10381032                }
    10391033
    10401034                if ( isset( $request['author_ip'] ) ) {
    10411035                        $prepared_comment['comment_author_IP'] = $request['author_ip'];
    10421036                }
    10431037
    10441038                if ( isset( $request['author_user_agent'] ) ) {
    10451039                        $prepared_comment['comment_agent'] = $request['author_user_agent'];
    10461040                }
    10471041
    10481042                if ( isset( $request['type'] ) ) {
    10491043                        // Comment type "comment" needs to be created as an empty string.
    10501044                        $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type'];
    10511045                }
    10521046
    1053                 if ( isset( $request['karma'] ) ) {
    1054                         $prepared_comment['comment_karma'] = $request['karma'] ;
    1055                 }
    1056 
    10571047                if ( ! empty( $request['date'] ) ) {
    10581048                        $date_data = rest_get_date_with_gmt( $request['date'] );
    10591049
    10601050                        if ( ! empty( $date_data ) ) {
    10611051                                list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data;
    10621052                        }
    10631053                } elseif ( ! empty( $request['date_gmt'] ) ) {
    10641054                        $date_data = rest_get_date_with_gmt( $request['date_gmt'], true );
    10651055
    10661056                        if ( ! empty( $date_data ) ) {
    10671057                                list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data;
    10681058                        }
    10691059                }
    10701060
    10711061                /**
    class WP_REST_Comments_Controller extend 
    11621152                                                ),
    11631153                                        ),
    11641154                                ),
    11651155                                'date'             => array(
    11661156                                        'description'  => __( 'The date the object was published.' ),
    11671157                                        'type'         => 'string',
    11681158                                        'format'       => 'date-time',
    11691159                                        'context'      => array( 'view', 'edit', 'embed' ),
    11701160                                ),
    11711161                                'date_gmt'         => array(
    11721162                                        'description'  => __( 'The date the object was published as GMT.' ),
    11731163                                        'type'         => 'string',
    11741164                                        'format'       => 'date-time',
    11751165                                        'context'      => array( 'view', 'edit' ),
    11761166                                ),
    1177                                 'karma'             => array(
    1178                                         'description'  => __( 'Karma for the object.' ),
    1179                                         'type'         => 'integer',
    1180                                         'context'      => array( 'edit' ),
    1181                                 ),
    11821167                                'link'             => array(
    11831168                                        'description'  => __( 'URL to the object.' ),
    11841169                                        'type'         => 'string',
    11851170                                        'format'       => 'uri',
    11861171                                        'context'      => array( 'view', 'edit', 'embed' ),
    11871172                                        'readonly'     => true,
    11881173                                ),
    11891174                                'parent'           => array(
    11901175                                        'description'  => __( 'The id for the parent of the object.' ),
    11911176                                        'type'         => 'integer',
    11921177                                        'context'      => array( 'view', 'edit', 'embed' ),
    11931178                                        'default'      => 0,
    11941179                                ),
    11951180                                'post'             => array(
    11961181                                        'description'  => __( 'The id of the associated post object.' ),
    class WP_REST_Comments_Controller extend 
    13001285                        'items'              => array(
    13011286                                'type'           => 'integer',
    13021287                        ),
    13031288                        'default'            => array(),
    13041289                );
    13051290
    13061291                $query_params['include'] = array(
    13071292                        'description'        => __( 'Limit result set to specific ids.' ),
    13081293                        'type'               => 'array',
    13091294                        'items'              => array(
    13101295                                'type'           => 'integer',
    13111296                        ),
    13121297                        'default'            => array(),
    13131298                );
    13141299
    1315                 $query_params['karma'] = array(
    1316                         'default'           => null,
    1317                         'description'       => __( 'Limit result set to that of a particular comment karma. Requires authorization.' ),
    1318                         'type'              => 'integer',
    1319                 );
    1320 
    13211300                $query_params['offset'] = array(
    13221301                        'description'        => __( 'Offset the result set by a specific number of comments.' ),
    13231302                        'type'               => 'integer',
    13241303                );
    13251304
    13261305                $query_params['order']      = array(
    13271306                        'description'           => __( 'Order sort attribute ascending or descending.' ),
    13281307                        'type'                  => 'string',
    13291308                        'default'               => 'desc',
    13301309                        'enum'                  => array(
    13311310                                'asc',
    13321311                                'desc',
    13331312                        ),
    13341313                );
    13351314