Make WordPress Core


Ignore:
Timestamp:
01/26/2017 01:46:54 PM (10 years ago)
Author:
ocean90
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.

Merge of [39954] to the 4.7 branch.

See #38792.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39631 r39957  
    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
    … …  
    625649         */
    626650        public function update_item_permissions_check( $request ) {
    627 
    628                 $id = (int) $request['id'];
    629 
    630                 $comment = get_comment( $id );
    631 
    632                 if ( $comment && ! $this->check_edit_permission( $comment ) ) {
     651                $comment = $this->get_comment( $request['id'] );
     652                if ( is_wp_error( $comment ) ) {
     653                        return $comment;
     654                }
     655
     656                if ( ! $this->check_edit_permission( $comment ) ) {
    633657                        return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this comment.' ), array( 'status' => rest_authorization_required_code() ) );
    634658                }
    … …  
    647671         */
    648672        public function update_item( $request ) {
    649                 $id = (int) $request['id'];
    650 
    651                 $comment = get_comment( $id );
    652 
    653                 if ( empty( $comment ) ) {
    654                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
    655                 }
     673                $comment = $this->get_comment( $request['id'] );
     674                if ( is_wp_error( $comment ) ) {
     675                        return $comment;
     676                }
     677
     678                $id = $comment->comment_ID;
    656679
    657680                if ( isset( $request['type'] ) && get_comment_type( $id ) !== $request['type'] ) {
    … …  
    745768         */
    746769        public function delete_item_permissions_check( $request ) {
    747                 $id      = (int) $request['id'];
    748                 $comment = get_comment( $id );
    749 
    750                 if ( ! $comment ) {
    751                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
     770                $comment = $this->get_comment( $request['id'] );
     771                if ( is_wp_error( $comment ) ) {
     772                        return $comment;
    752773                }
    753774
    … …  
    768789         */
    769790        public function delete_item( $request ) {
    770                 $id    = (int) $request['id'];
     791                $comment = $this->get_comment( $request['id'] );
     792                if ( is_wp_error( $comment ) ) {
     793                        return $comment;
     794                }
     795
    771796                $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
    772 
    773                 $comment = get_comment( $id );
    774 
    775                 if ( empty( $comment ) ) {
    776                         return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );
    777                 }
    778797
    779798                /**
Note: See TracChangeset for help on using the changeset viewer.