WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/26/2017 01:46:54 PM (2 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-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' );
Note: See TracChangeset for help on using the changeset viewer.