Make WordPress Core


Ignore:
Timestamp:
01/26/2017 01:38:27 PM (10 years ago)
Author:
joehoyle
Message:

REST API: Unify object access handling for simplicity.

Rather than repeating ourselves, unifying the access into a single method keeps everything tidy. While we're at it, add in additional schema handling for common parameters.

See #38792.

File:
1 edited

Legend:

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

    r39922 r39954  
    6464
    6565                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
     66                        'args' => array(
     67                                'id' => array(
     68                                        'description' => __( 'Unique identifier for the object.' ),
     69                                        'type'        => 'integer',
     70                                ),
     71                        ),
    6672                        array(
    6773                                'methods'  => WP_REST_Server::READABLE,
     
    301307
    302308        /**
     309         * Get the comment, if the ID is valid.
     310         *
     311         * @since 4.7.2
     312         *
     313         * @param int $id Supplied ID.
     314         * @return WP_Comment|WP_Error Comment object if ID is valid, WP_Error otherwise.
     315         */
     316        protected function get_comment( $id ) {
     317                $error = new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
     318                if ( (int) $id <= 0 ) {
     319                        return $error;
     320                }
     321
     322                $id = (int) $id;
     323                $comment = get_comment( $id );
     324                if ( empty( $comment ) ) {
     325                        return $error;
     326                }
     327
     328                if ( ! empty( $comment->comment_post_ID ) ) {
     329                        $post = get_post( (int) $comment->comment_post_ID );
     330                        if ( empty( $post ) ) {
     331                                return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
     332                        }
     333                }
     334
     335                return $comment;
     336        }
     337
     338        /**
    303339         * Checks if a given request has access to read the comment.
    304340         *
     
    310346         */
    311347        public function get_item_permissions_check( $request ) {
    312                 $id = (int) $request['id'];
    313 
    314                 $comment = get_comment( $id );
    315 
    316                 if ( ! $comment ) {
    317                         return true;
     348                $comment = $this->get_comment( $request['id'] );
     349                if ( is_wp_error( $comment ) ) {
     350                        return $comment;
    318351                }
    319352
     
    345378         */
    346379        public function get_item( $request ) {
    347                 $id = (int) $request['id'];
    348 
    349                 $comment = get_comment( $id );
    350                 if ( empty( $comment ) ) {
    351                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
    352                 }
    353 
    354                 if ( ! empty( $comment->comment_post_ID ) ) {
    355                         $post = get_post( $comment->comment_post_ID );
    356                         if ( empty( $post ) ) {
    357                                 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
    358                         }
     380                $comment = $this->get_comment( $request['id'] );
     381                if ( is_wp_error( $comment ) ) {
     382                        return $comment;
    359383                }
    360384
     
    631655         */
    632656        public function update_item_permissions_check( $request ) {
    633 
    634                 $id = (int) $request['id'];
    635 
    636                 $comment = get_comment( $id );
    637 
    638                 if ( $comment && ! $this->check_edit_permission( $comment ) ) {
     657                $comment = $this->get_comment( $request['id'] );
     658                if ( is_wp_error( $comment ) ) {
     659                        return $comment;
     660                }
     661
     662                if ( ! $this->check_edit_permission( $comment ) ) {
    639663                        return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this comment.' ), array( 'status' => rest_authorization_required_code() ) );
    640664                }
     
    653677         */
    654678        public function update_item( $request ) {
    655                 $id = (int) $request['id'];
    656 
    657                 $comment = get_comment( $id );
    658 
    659                 if ( empty( $comment ) ) {
    660                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
    661                 }
     679                $comment = $this->get_comment( $request['id'] );
     680                if ( is_wp_error( $comment ) ) {
     681                        return $comment;
     682                }
     683
     684                $id = $comment->comment_ID;
    662685
    663686                if ( isset( $request['type'] ) && get_comment_type( $id ) !== $request['type'] ) {
     
    751774         */
    752775        public function delete_item_permissions_check( $request ) {
    753                 $id      = (int) $request['id'];
    754                 $comment = get_comment( $id );
    755 
    756                 if ( ! $comment ) {
    757                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
     776                $comment = $this->get_comment( $request['id'] );
     777                if ( is_wp_error( $comment ) ) {
     778                        return $comment;
    758779                }
    759780
     
    774795         */
    775796        public function delete_item( $request ) {
    776                 $id    = (int) $request['id'];
     797                $comment = $this->get_comment( $request['id'] );
     798                if ( is_wp_error( $comment ) ) {
     799                        return $comment;
     800                }
     801
    777802                $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
    778 
    779                 $comment = get_comment( $id );
    780 
    781                 if ( empty( $comment ) ) {
    782                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
    783                 }
    784803
    785804                /**
Note: See TracChangeset for help on using the changeset viewer.