Make WordPress Core


Ignore:
Timestamp:
11/23/2016 03:32:25 PM (8 years ago)
Author:
rachelbaker
Message:

REST API: Always fire the rest_insert_* actions after the related object is updated or inserted.

Brings consistency to the rest_insert_* actions. Also includes some shuffling and clean-up as well including:

  • Ensure we are passing the most current $post and $user objects to the update_additional_fields_for_object() callbacks.
  • Changes the function signature of handle_status_param() in the Comments controller to accept just the comment_id as the 2nd parameter, instead of a full WP_Comment object. Only the comment_id is needed in the method, this avoids having to include another get_comment() call.
  • Renames a variable in the create_item() method of the Posts controller from $post -> $prepared_post to be more explicit.
  • Minor fixes/clarifications to the rest_insert_* hook docs

Props rachelbaker, joehoyle
Fixes #38905.

File:
1 edited

Legend:

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

    r39342 r39348  
    462462        $user = get_user_by( 'id', $user_id );
    463463
    464         if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
    465             array_map( array( $user, 'add_role' ), $request['roles'] );
    466         }
    467 
    468         if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    469             $meta_update = $this->meta->update_value( $request['meta'], $user_id );
    470 
    471             if ( is_wp_error( $meta_update ) ) {
    472                 return $meta_update;
    473             }
    474         }
    475 
    476         $fields_update = $this->update_additional_fields_for_object( $user, $request );
    477 
    478         if ( is_wp_error( $fields_update ) ) {
    479             return $fields_update;
    480         }
    481 
    482464        /**
    483465         * Fires immediately after a user is created or updated via the REST API.
     
    485467         * @since 4.7.0
    486468         *
    487          * @param WP_User         $user     Data used to create the user.
     469         * @param WP_User         $user     Inserted or updated user object.
    488470         * @param WP_REST_Request $request  Request object.
    489          * @param bool            $creating True when creating user, false when updating user.
     471         * @param bool            $creating True when creating a user, false when updating.
    490472         */
    491473        do_action( 'rest_insert_user', $user, $request, true );
     474
     475        if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
     476            array_map( array( $user, 'add_role' ), $request['roles'] );
     477        }
     478
     479        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
     480            $meta_update = $this->meta->update_value( $request['meta'], $user_id );
     481
     482            if ( is_wp_error( $meta_update ) ) {
     483                return $meta_update;
     484            }
     485        }
     486
     487        $user = get_user_by( 'id', $user_id );
     488        $fields_update = $this->update_additional_fields_for_object( $user, $request );
     489
     490        if ( is_wp_error( $fields_update ) ) {
     491            return $fields_update;
     492        }
    492493
    493494        $request->set_param( 'context', 'edit' );
     
    574575        }
    575576
    576         $user = get_user_by( 'id', $id );
     577        $user = get_user_by( 'id', $user_id );
     578
     579        /* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
     580        do_action( 'rest_insert_user', $user, $request, false );
    577581
    578582        if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
     
    594598        }
    595599
     600        $user = get_user_by( 'id', $user_id );
    596601        $fields_update = $this->update_additional_fields_for_object( $user, $request );
    597602
     
    599604            return $fields_update;
    600605        }
    601 
    602         /* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
    603         do_action( 'rest_insert_user', $user, $request, false );
    604606
    605607        $request->set_param( 'context', 'edit' );
Note: See TracChangeset for help on using the changeset viewer.