Make WordPress Core


Ignore:
Timestamp:
01/26/2017 01:38:27 PM (8 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-terms-controller.php

    r39671 r39954  
    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 does not exist.' ), array( 'status' => 404 ) );
    325         }
    326 
     354        $term = $this->get_term( $request['id'] );
    327355        if ( is_wp_error( $term ) ) {
    328356            return $term;
     
    446474     */
    447475    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 does not exist.' ), array( 'status' => 404 ) );
     476        $term = $this->get_term( $request['id'] );
     477        if ( is_wp_error( $term ) ) {
     478            return $term;
    457479        }
    458480
     
    474496     */
    475497    public function update_item( $request ) {
     498        $term = $this->get_term( $request['id'] );
     499        if ( is_wp_error( $term ) ) {
     500            return $term;
     501        }
     502
    476503        if ( isset( $request['parent'] ) ) {
    477504            if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
     
    488515        $prepared_term = $this->prepare_item_for_database( $request );
    489516
    490         $term = get_term( (int) $request['id'], $this->taxonomy );
    491 
    492517        // Only update the term if we haz something to update.
    493518        if ( ! empty( $prepared_term ) ) {
     
    499524        }
    500525
    501         $term = get_term( (int) $request['id'], $this->taxonomy );
     526        $term = get_term( $term->term_id, $this->taxonomy );
    502527
    503528        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
     
    506531        $schema = $this->get_item_schema();
    507532        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    508             $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
     533            $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
    509534
    510535            if ( is_wp_error( $meta_update ) ) {
     
    536561     */
    537562    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 does not exist.' ), array( 'status' => 404 ) );
     563        $term = $this->get_term( $request['id'] );
     564        if ( is_wp_error( $term ) ) {
     565            return $term;
    546566        }
    547567
     
    563583     */
    564584    public function delete_item( $request ) {
     585        $term = $this->get_term( $request['id'] );
     586        if ( is_wp_error( $term ) ) {
     587            return $term;
     588        }
    565589
    566590        $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
     
    570594            return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    571595        }
    572 
    573         $term = get_term( (int) $request['id'], $this->taxonomy );
    574596
    575597        $request->set_param( 'context', 'view' );
Note: See TracChangeset for help on using the changeset viewer.