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-attachments-controller.php

    r39342 r39348  
    156156        $attachment = get_post( $id );
    157157
     158        /**
     159         * Fires after a single attachment is created or updated via the REST API.
     160         *
     161         * @since 4.7.0
     162         *
     163         * @param WP_Post         $attachment Inserted or updated attachment
     164         *                                    object.
     165         * @param WP_REST_Request $request    The request sent to the API.
     166         * @param bool            $creating   True when creating an attachment, false when updating.
     167         */
     168        do_action( 'rest_insert_attachment', $attachment, $request, true );
     169
    158170        // Include admin functions to get access to wp_generate_attachment_metadata().
    159171        require_once ABSPATH . 'wp-admin/includes/admin.php';
     
    177189        $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
    178190
    179         /**
    180          * Fires after a single attachment is created or updated via the REST API.
    181          *
    182          * @since 4.7.0
    183          *
    184          * @param object          $attachment Inserted attachment.
    185          * @param WP_REST_Request $request    The request sent to the API.
    186          * @param bool            $creating   True when creating an attachment, false when updating.
    187          */
    188         do_action( 'rest_insert_attachment', $attachment, $request, true );
    189 
    190191        return $response;
    191192    }
     
    220221        $attachment = get_post( $request['id'] );
    221222
     223        /* This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
     224        do_action( 'rest_insert_attachment', $data, $request, false );
     225
    222226        $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
    223227
     
    229233        $response = $this->prepare_item_for_response( $attachment, $request );
    230234        $response = rest_ensure_response( $response );
    231 
    232         /* This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
    233         do_action( 'rest_insert_attachment', $data, $request, false );
    234235
    235236        return $response;
Note: See TracChangeset for help on using the changeset viewer.