Make WordPress Core

Changeset 39957 for branches/4.7


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:
14 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api.php

    r39401 r39957  
    4747        }
    4848
     49        if ( isset( $args['args'] ) ) {
     50                $common_args = $args['args'];
     51                unset( $args['args'] );
     52        } else {
     53                $common_args = array();
     54        }
     55
    4956        if ( isset( $args['callback'] ) ) {
    5057                // Upgrade a single set to multiple.
     
    5865        );
    5966        foreach ( $args as $key => &$arg_group ) {
    60                 if ( ! is_numeric( $arg_group ) ) {
     67                if ( ! is_numeric( $key ) ) {
    6168                        // Route option, skip here.
    6269                        continue;
     
    6471
    6572                $arg_group = array_merge( $defaults, $arg_group );
     73                $arg_group['args'] = array_merge( $common_args, $arg_group['args'] );
    6674        }
    6775
  • 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                /**
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php

    r39342 r39957  
    4949
    5050                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<status>[\w-]+)', array(
     51                        'args' => array(
     52                                'status' => array(
     53                                        'description' => __( 'An alphanumeric identifier for the status.' ),
     54                                        'type'        => 'string',
     55                                ),
     56                        ),
    5157                        array(
    5258                                'methods'             => WP_REST_Server::READABLE,
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r39342 r39957  
    4949
    5050                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<type>[\w-]+)', array(
     51                        'args' => array(
     52                                'type' => array(
     53                                        'description' => __( 'An alphanumeric identifier for the post type.' ),
     54                                        'type'        => 'string',
     55                                ),
     56                        ),
    5157                        array(
    5258                                'methods'  => WP_REST_Server::READABLE,
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39631 r39957  
    8989                }
    9090                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
     91                        'args' => array(
     92                                'id' => array(
     93                                        'description' => __( 'Unique identifier for the object.' ),
     94                                        'type'        => 'integer',
     95                                ),
     96                        ),
    9197                        array(
    9298                                'methods'             => WP_REST_Server::READABLE,
     
    351357
    352358        /**
     359         * Get the post, if the ID is valid.
     360         *
     361         * @since 4.7.2
     362         *
     363         * @param int $id Supplied ID.
     364         * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
     365         */
     366        protected function get_post( $id ) {
     367                $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
     368                if ( (int) $id <= 0 ) {
     369                        return $error;
     370                }
     371
     372                $post = get_post( (int) $id );
     373                if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
     374                        return $error;
     375                }
     376
     377                return $post;
     378        }
     379
     380        /**
    353381         * Checks if a given request has access to read a post.
    354382         *
     
    360388         */
    361389        public function get_item_permissions_check( $request ) {
    362 
    363                 $post = get_post( (int) $request['id'] );
     390                $post = $this->get_post( $request['id'] );
     391                if ( is_wp_error( $post ) ) {
     392                        return $post;
     393                }
    364394
    365395                if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
     
    429459         */
    430460        public function get_item( $request ) {
    431                 $id   = (int) $request['id'];
    432                 $post = get_post( $id );
    433 
    434                 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
    435                         return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
     461                $post = $this->get_post( $request['id'] );
     462                if ( is_wp_error( $post ) ) {
     463                        return $post;
    436464                }
    437465
     
    440468
    441469                if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) {
    442                         $response->link_header( 'alternate',  get_permalink( $id ), array( 'type' => 'text/html' ) );
     470                        $response->link_header( 'alternate',  get_permalink( $post->ID ), array( 'type' => 'text/html' ) );
    443471                }
    444472
     
    456484         */
    457485        public function create_item_permissions_check( $request ) {
     486                if ( ! empty( $request['id'] ) ) {
     487                        return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
     488                }
    458489
    459490                $post_type = get_post_type_object( $this->post_type );
     
    592623         */
    593624        public function update_item_permissions_check( $request ) {
    594 
    595                 $post = get_post( $request['id'] );
     625                $post = $this->get_post( $request['id'] );
     626                if ( is_wp_error( $post ) ) {
     627                        return $post;
     628                }
     629
    596630                $post_type = get_post_type_object( $this->post_type );
    597631
     
    625659         */
    626660        public function update_item( $request ) {
    627                 $id   = (int) $request['id'];
    628                 $post = get_post( $id );
    629 
    630                 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
    631                         return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
     661                $valid_check = $this->get_post( $request['id'] );
     662                if ( is_wp_error( $valid_check ) ) {
     663                        return $valid_check;
    632664                }
    633665
     
    715747         */
    716748        public function delete_item_permissions_check( $request ) {
    717 
    718                 $post = get_post( $request['id'] );
     749                $post = $this->get_post( $request['id'] );
     750                if ( is_wp_error( $post ) ) {
     751                        return $post;
     752                }
    719753
    720754                if ( $post && ! $this->check_delete_permission( $post ) ) {
     
    735769         */
    736770        public function delete_item( $request ) {
    737                 $id    = (int) $request['id'];
     771                $post = $this->get_post( $request['id'] );
     772                if ( is_wp_error( $post ) ) {
     773                        return $post;
     774                }
     775
     776                $id    = $post->ID;
    738777                $force = (bool) $request['force'];
    739 
    740                 $post = get_post( $id );
    741 
    742                 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
    743                         return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
    744                 }
    745778
    746779                $supports_trash = ( EMPTY_TRASH_DAYS > 0 );
     
    902935                // Post ID.
    903936                if ( isset( $request['id'] ) ) {
    904                         $prepared_post->ID = absint( $request['id'] );
     937                        $existing_post = $this->get_post( $request['id'] );
     938                        if ( is_wp_error( $existing_post ) ) {
     939                                return $existing_post;
     940                        }
     941
     942                        $prepared_post->ID = $existing_post->ID;
    905943                }
    906944
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r39489 r39957  
    7272
    7373                register_rest_route( $this->namespace, '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base, array(
     74                        'args' => array(
     75                                'parent' => array(
     76                                        'description' => __( 'The ID for the parent of the object.' ),
     77                                        'type'        => 'integer',
     78                                ),
     79                        ),
    7480                        array(
    7581                                'methods'             => WP_REST_Server::READABLE,
     
    8288
    8389                register_rest_route( $this->namespace, '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)', array(
     90                        'args' => array(
     91                                'parent' => array(
     92                                        'description' => __( 'The ID for the parent of the object.' ),
     93                                        'type'        => 'integer',
     94                                ),
     95                                'id' => array(
     96                                        'description' => __( 'Unique identifier for the object.' ),
     97                                        'type'        => 'integer',
     98                                ),
     99                        ),
    84100                        array(
    85101                                'methods'             => WP_REST_Server::READABLE,
     
    108124
    109125        /**
     126         * Get the parent post, if the ID is valid.
     127         *
     128         * @since 4.7.2
     129         *
     130         * @param int $id Supplied ID.
     131         * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
     132         */
     133        protected function get_parent( $parent ) {
     134                $error = new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent ID.' ), array( 'status' => 404 ) );
     135                if ( (int) $parent <= 0 ) {
     136                        return $error;
     137                }
     138
     139                $parent = get_post( (int) $parent );
     140                if ( empty( $parent ) || empty( $parent->ID ) || $this->parent_post_type !== $parent->post_type ) {
     141                        return $error;
     142                }
     143
     144                return $parent;
     145        }
     146
     147        /**
    110148         * Checks if a given request has access to get revisions.
    111149         *
     
    117155         */
    118156        public function get_items_permissions_check( $request ) {
    119 
    120                 $parent = get_post( $request['parent'] );
    121                 if ( ! $parent ) {
    122                         return true;
    123                 }
     157                $parent = $this->get_parent( $request['parent'] );
     158                if ( is_wp_error( $parent ) ) {
     159                        return $parent;
     160                }
     161
    124162                $parent_post_type_obj = get_post_type_object( $parent->post_type );
    125163                if ( ! current_user_can( $parent_post_type_obj->cap->edit_post, $parent->ID ) ) {
     
    131169
    132170        /**
     171         * Get the revision, if the ID is valid.
     172         *
     173         * @since 4.7.2
     174         *
     175         * @param int $id Supplied ID.
     176         * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise.
     177         */
     178        protected function get_revision( $id ) {
     179                $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision ID.' ), array( 'status' => 404 ) );
     180                if ( (int) $id <= 0 ) {
     181                        return $error;
     182                }
     183
     184                $revision = get_post( (int) $id );
     185                if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) {
     186                        return $error;
     187                }
     188
     189                return $revision;
     190        }
     191
     192        /**
    133193         * Gets a collection of revisions.
    134194         *
     
    140200         */
    141201        public function get_items( $request ) {
    142                 $parent = get_post( $request['parent'] );
    143                 if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) {
    144                         return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent ID.' ), array( 'status' => 404 ) );
     202                $parent = $this->get_parent( $request['parent'] );
     203                if ( is_wp_error( $parent ) ) {
     204                        return $parent;
    145205                }
    146206
     
    178238         */
    179239        public function get_item( $request ) {
    180                 $parent = get_post( $request['parent'] );
    181                 if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) {
    182                         return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent ID.' ), array( 'status' => 404 ) );
    183                 }
    184 
    185                 $revision = get_post( $request['id'] );
    186                 if ( ! $revision || 'revision' !== $revision->post_type ) {
    187                         return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision ID.' ), array( 'status' => 404 ) );
     240                $parent = $this->get_parent( $request['parent'] );
     241                if ( is_wp_error( $parent ) ) {
     242                        return $parent;
     243                }
     244
     245                $revision = $this->get_revision( $request['id'] );
     246                if ( is_wp_error( $revision ) ) {
     247                        return $revision;
    188248                }
    189249
     
    202262         */
    203263        public function delete_item_permissions_check( $request ) {
     264                $parent = $this->get_parent( $request['parent'] );
     265                if ( is_wp_error( $parent ) ) {
     266                        return $parent;
     267                }
     268
     269                $revision = $this->get_revision( $request['id'] );
     270                if ( is_wp_error( $revision ) ) {
     271                        return $revision;
     272                }
    204273
    205274                $response = $this->get_items_permissions_check( $request );
     
    208277                }
    209278
    210                 $post = get_post( $request['id'] );
    211                 if ( ! $post ) {
    212                         return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision ID.' ), array( 'status' => 404 ) );
    213                 }
    214279                $post_type = get_post_type_object( 'revision' );
    215                 return current_user_can( $post_type->cap->delete_post, $post->ID );
     280                return current_user_can( $post_type->cap->delete_post, $revision->ID );
    216281        }
    217282
     
    226291         */
    227292        public function delete_item( $request ) {
     293                $revision = $this->get_revision( $request['id'] );
     294                if ( is_wp_error( $revision ) ) {
     295                        return $revision;
     296                }
     297
    228298                $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
    229299
     
    233303                }
    234304
    235                 $revision = get_post( $request['id'] );
    236305                $previous = $this->prepare_item_for_response( $revision, $request );
    237306
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r39342 r39957  
    4949
    5050                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)', array(
     51                        'args' => array(
     52                                'taxonomy' => array(
     53                                        'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
     54                                        'type'         => 'string',
     55                                ),
     56                        ),
    5157                        array(
    5258                                'methods'         => WP_REST_Server::READABLE,
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r39631 r39957  
    9797
    9898                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
     99                        'args' => array(
     100                                'id' => array(
     101                                        'description' => __( 'Unique identifier for the term.' ),
     102                                        'type'        => 'integer',
     103                                ),
     104                        ),
    99105                        array(
    100106                                'methods'             => WP_REST_Server::READABLE,
     
    109115                                'callback'            => array( $this, 'update_item' ),
    110116                                'permission_callback' => array( $this, 'update_item_permissions_check' ),
    111                                 'args'                 => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
     117                                'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
    112118                        ),
    113119                        array(
     
    289295
    290296        /**
     297         * Get the term, if the ID is valid.
     298         *
     299         * @since 4.7.2
     300         *
     301         * @param int $id Supplied ID.
     302         * @return WP_Term|WP_Error Term object if ID is valid, WP_Error otherwise.
     303         */
     304        protected function get_term( $id ) {
     305                $error = new WP_Error( 'rest_term_invalid', __( 'Term does not exist.' ), array( 'status' => 404 ) );
     306
     307                if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
     308                        return $error;
     309                }
     310
     311                if ( (int) $id <= 0 ) {
     312                        return $error;
     313                }
     314
     315                $term = get_term( (int) $id, $this->taxonomy );
     316                if ( empty( $term ) || $term->taxonomy !== $this->taxonomy ) {
     317                        return $error;
     318                }
     319
     320                return $term;
     321        }
     322
     323        /**
    291324         * Checks if a request has access to read or edit the specified term.
    292325         *
     
    298331         */
    299332        public function get_item_permissions_check( $request ) {
    300                 $tax_obj = get_taxonomy( $this->taxonomy );
    301                 if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
    302                         return false;
    303                 }
    304                 if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', (int) $request['id'] ) ) {
     333                $term = $this->get_term( $request['id'] );
     334                if ( is_wp_error( $term ) ) {
     335                        return $term;
     336                }
     337
     338                if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', $term->term_id ) ) {
    305339                        return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this term.' ), array( 'status' => rest_authorization_required_code() ) );
    306340                }
     
    318352         */
    319353        public function get_item( $request ) {
    320 
    321                 $term = get_term( (int) $request['id'], $this->taxonomy );
    322 
    323                 if ( ! $term || $term->taxonomy !== $this->taxonomy ) {
    324                         return new WP_Error( 'rest_term_invalid', __( "Term doesn't exist." ), array( 'status' => 404 ) );
    325                 }
     354                $term = $this->get_term( $request['id'] );
    326355
    327356                if ( is_wp_error( $term ) ) {
     
    446475         */
    447476        public function update_item_permissions_check( $request ) {
    448 
    449                 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
    450                         return false;
    451                 }
    452 
    453                 $term = get_term( (int) $request['id'], $this->taxonomy );
    454 
    455                 if ( ! $term ) {
    456                         return new WP_Error( 'rest_term_invalid', __( "Term doesn't exist." ), array( 'status' => 404 ) );
     477                $term = $this->get_term( $request['id'] );
     478                if ( is_wp_error( $term ) ) {
     479                        return $term;
    457480                }
    458481
     
    474497         */
    475498        public function update_item( $request ) {
     499                $term = $this->get_term( $request['id'] );
     500                if ( is_wp_error( $term ) ) {
     501                        return $term;
     502                }
     503
    476504                if ( isset( $request['parent'] ) ) {
    477505                        if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
     
    488516                $prepared_term = $this->prepare_item_for_database( $request );
    489517
    490                 $term = get_term( (int) $request['id'], $this->taxonomy );
    491 
    492518                // Only update the term if we haz something to update.
    493519                if ( ! empty( $prepared_term ) ) {
     
    499525                }
    500526
    501                 $term = get_term( (int) $request['id'], $this->taxonomy );
     527                $term = get_term( $term->term_id, $this->taxonomy );
    502528
    503529                /* This action is documented in lib/endpoints/class-wp-rest-terms-controller.php */
     
    506532                $schema = $this->get_item_schema();
    507533                if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    508                         $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
     534                        $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
    509535
    510536                        if ( is_wp_error( $meta_update ) ) {
     
    536562         */
    537563        public function delete_item_permissions_check( $request ) {
    538                 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
    539                         return false;
    540                 }
    541 
    542                 $term = get_term( (int) $request['id'], $this->taxonomy );
    543 
    544                 if ( ! $term ) {
    545                         return new WP_Error( 'rest_term_invalid', __( "Term doesn't exist." ), array( 'status' => 404 ) );
     564                $term = $this->get_term( $request['id'] );
     565                if ( is_wp_error( $term ) ) {
     566                        return $term;
    546567                }
    547568
     
    563584         */
    564585        public function delete_item( $request ) {
     586                $term = $this->get_term( $request['id'] );
     587                if ( is_wp_error( $term ) ) {
     588                        return $term;
     589                }
    565590
    566591                $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
     
    570595                        return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    571596                }
    572 
    573                 $term = get_term( (int) $request['id'], $this->taxonomy );
    574597
    575598                $request->set_param( 'context', 'view' );
  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r39844 r39957  
    6666
    6767                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
     68                        'args' => array(
     69                                'id' => array(
     70                                        'description' => __( 'Unique identifier for the user.' ),
     71                                        'type'        => 'integer',
     72                                ),
     73                        ),
    6874                        array(
    6975                                'methods'             => WP_REST_Server::READABLE,
     
    327333
    328334        /**
     335         * Get the user, if the ID is valid.
     336         *
     337         * @since 4.7.2
     338         *
     339         * @param int $id Supplied ID.
     340         * @return WP_User|WP_Error True if ID is valid, WP_Error otherwise.
     341         */
     342        protected function get_user( $id ) {
     343                $error = new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
     344                if ( (int) $id <= 0 ) {
     345                        return $error;
     346                }
     347
     348                $user = get_userdata( (int) $id );
     349                if ( empty( $user ) || ! $user->exists() ) {
     350                        return $error;
     351                }
     352
     353                return $user;
     354        }
     355
     356        /**
    329357         * Checks if a given request has access to read a user.
    330358         *
     
    336364         */
    337365        public function get_item_permissions_check( $request ) {
    338 
    339                 $id = (int) $request['id'];
    340                 $user = get_userdata( $id );
     366                $user = $this->get_user( $request['id'] );
     367                if ( is_wp_error( $user ) ) {
     368                        return $user;
     369                }
     370
    341371                $types = get_post_types( array( 'show_in_rest' => true ), 'names' );
    342372
    343                 if ( empty( $id ) || empty( $user->ID ) ) {
    344                         return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
    345                 }
    346 
    347                 if ( get_current_user_id() === $id ) {
     373                if ( get_current_user_id() === $user->ID ) {
    348374                        return true;
    349375                }
     
    351377                if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
    352378                        return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
    353                 } elseif ( ! count_user_posts( $id, $types ) && ! current_user_can( 'edit_user', $id ) && ! current_user_can( 'list_users' ) ) {
     379                } elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) {
    354380                        return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
    355381                }
     
    368394         */
    369395        public function get_item( $request ) {
    370                 $id   = (int) $request['id'];
    371                 $user = get_userdata( $id );
    372 
    373                 if ( empty( $id ) || empty( $user->ID ) ) {
    374                         return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
     396                $user = $this->get_user( $request['id'] );
     397                if ( is_wp_error( $user ) ) {
     398                        return $user;
    375399                }
    376400
     
    542566         */
    543567        public function update_item_permissions_check( $request ) {
    544 
    545                 $id = (int) $request['id'];
    546 
    547                 if ( ! current_user_can( 'edit_user', $id ) ) {
     568                $user = $this->get_user( $request['id'] );
     569                if ( is_wp_error( $user ) ) {
     570                        return $user;
     571                }
     572
     573                if ( ! current_user_can( 'edit_user', $user->ID ) ) {
    548574                        return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) );
    549575                }
     
    566592         */
    567593        public function update_item( $request ) {
    568                 $id   = (int) $request['id'];
    569                 $user = get_userdata( $id );
     594                $user = $this->get_user( $request['id'] );
     595                if ( is_wp_error( $user ) ) {
     596                        return $user;
     597                }
     598
     599                $id = $user->ID;
    570600
    571601                if ( ! $user ) {
     
    682712         */
    683713        public function delete_item_permissions_check( $request ) {
    684 
    685                 $id = (int) $request['id'];
    686 
    687                 if ( ! current_user_can( 'delete_user', $id ) ) {
     714                $user = $this->get_user( $request['id'] );
     715                if ( is_wp_error( $user ) ) {
     716                        return $user;
     717                }
     718
     719                if ( ! current_user_can( 'delete_user', $user->ID ) ) {
    688720                        return new WP_Error( 'rest_user_cannot_delete', __( 'Sorry, you are not allowed to delete this user.' ), array( 'status' => rest_authorization_required_code() ) );
    689721                }
     
    706738                        return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 501 ) );
    707739                }
    708 
    709                 $id       = (int) $request['id'];
     740                $user = $this->get_user( $request['id'] );
     741                if ( is_wp_error( $user ) ) {
     742                        return $user;
     743                }
     744
     745                $id       = $user->ID;
    710746                $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] );
    711747                $force    = isset( $request['force'] ) ? (bool) $request['force'] : false;
     
    714750                if ( ! $force ) {
    715751                        return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    716                 }
    717 
    718                 $user = get_userdata( $id );
    719 
    720                 if ( ! $user ) {
    721                         return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
    722752                }
    723753
  • branches/4.7/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r39849 r39957  
    183183                $keys = array_keys( $data['endpoints'][0]['args'] );
    184184                sort( $keys );
    185                 $this->assertEquals( array( 'context' ), $keys );
     185                $this->assertEquals( array( 'context', 'id' ), $keys );
    186186        }
    187187
  • branches/4.7/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39628 r39957  
    836836
    837837                $response = $this->server->dispatch( $request );
    838                 $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
     838                $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
    839839        }
    840840
  • branches/4.7/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39630 r39957  
    128128                $keys = array_keys( $data['endpoints'][0]['args'] );
    129129                sort( $keys );
    130                 $this->assertEquals( array( 'context', 'password' ), $keys );
     130                $this->assertEquals( array( 'context', 'id', 'password' ), $keys );
    131131        }
    132132
  • branches/4.7/tests/phpunit/tests/rest-api/rest-users-controller.php

    r39844 r39957  
    18501850                $response = $this->server->dispatch( $request );
    18511851
    1852                 // Not implemented in multisite.
    1853                 if ( is_multisite() ) {
    1854                         $this->assertErrorResponse( 'rest_cannot_delete', $response, 501 );
    1855                         return;
    1856                 }
    1857 
    18581852                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
    18591853        }
Note: See TracChangeset for help on using the changeset viewer.