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.
Built from https://develop.svn.wordpress.org/trunk@39348


git-svn-id: http://core.svn.wordpress.org/trunk@39288 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Rachel Baker 2016-11-23 15:33:31 +00:00
parent 98e8bf7ba2
commit a985a4d126
6 changed files with 97 additions and 93 deletions

View File

@ -155,6 +155,18 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
$attachment = get_post( $id );
/**
* Fires after a single attachment is created or updated via the REST API.
*
* @since 4.7.0
*
* @param WP_Post $attachment Inserted or updated attachment
* object.
* @param WP_REST_Request $request The request sent to the API.
* @param bool $creating True when creating an attachment, false when updating.
*/
do_action( 'rest_insert_attachment', $attachment, $request, true );
// Include admin functions to get access to wp_generate_attachment_metadata().
require_once ABSPATH . 'wp-admin/includes/admin.php';
@ -176,17 +188,6 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
/**
* Fires after a single attachment is created or updated via the REST API.
*
* @since 4.7.0
*
* @param object $attachment Inserted attachment.
* @param WP_REST_Request $request The request sent to the API.
* @param bool $creating True when creating an attachment, false when updating.
*/
do_action( 'rest_insert_attachment', $attachment, $request, true );
return $response;
}
@ -219,6 +220,9 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
$attachment = get_post( $request['id'] );
/* This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
do_action( 'rest_insert_attachment', $data, $request, false );
$fields_update = $this->update_additional_fields_for_object( $attachment, $request );
if ( is_wp_error( $fields_update ) ) {
@ -229,9 +233,6 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
$response = $this->prepare_item_for_response( $attachment, $request );
$response = rest_ensure_response( $response );
/* This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
do_action( 'rest_insert_attachment', $data, $request, false );
return $response;
}

View File

@ -567,11 +567,23 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
if ( isset( $request['status'] ) ) {
$comment = get_comment( $comment_id );
$this->handle_status_param( $request['status'], $comment );
$this->handle_status_param( $request['status'], $comment_id );
}
$comment = get_comment( $comment_id );
/**
* Fires after a comment is created or updated via the REST API.
*
* @since 4.7.0
*
* @param WP_Comment $comment Inserted or updated comment object.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating a comment, false
* when updating.
*/
do_action( 'rest_insert_comment', $comment, $request, true );
$schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
@ -582,8 +594,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
}
$comment = get_comment( $comment_id );
$fields_update = $this->update_additional_fields_for_object( $comment, $request );
if ( is_wp_error( $fields_update ) ) {
@ -600,16 +610,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) );
/**
* Fires after a comment is created or updated via the REST API.
*
* @since 4.7.0
*
* @param array $comment Comment as it exists in the database.
* @param WP_REST_Request $request The request sent to the API.
* @param bool $creating True when creating a comment, false when updating.
*/
do_action( 'rest_insert_comment', $comment, $request, true );
return $response;
}
@ -666,7 +666,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
// Only the comment status is being changed.
$change = $this->handle_status_param( $request['status'], $comment );
$change = $this->handle_status_param( $request['status'], $id );
if ( ! $change ) {
return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment status failed.' ), array( 'status' => 500 ) );
@ -695,10 +695,15 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
if ( isset( $request['status'] ) ) {
$this->handle_status_param( $request['status'], $comment );
$this->handle_status_param( $request['status'], $id );
}
}
$comment = get_comment( $id );
/* This action is documented in lib/endpoints/class-wp-rest-comments-controller.php */
do_action( 'rest_insert_comment', $comment, $request, false );
$schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
@ -709,8 +714,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
}
$comment = get_comment( $id );
$fields_update = $this->update_additional_fields_for_object( $comment, $request );
if ( is_wp_error( $fields_update ) ) {
@ -721,9 +724,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
$response = $this->prepare_item_for_response( $comment, $request );
/* This action is documented in lib/endpoints/class-wp-rest-comments-controller.php */
do_action( 'rest_insert_comment', $comment, $request, false );
return rest_ensure_response( $response );
}
@ -1433,11 +1433,11 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
* @access protected
*
* @param string|int $new_status New comment status.
* @param WP_Comment $comment Comment data.
* @param int $comment_id Comment ID.
* @return bool Whether the status was changed.
*/
protected function handle_status_param( $new_status, $comment ) {
$old_status = wp_get_comment_status( $comment->comment_ID );
protected function handle_status_param( $new_status, $comment_id ) {
$old_status = wp_get_comment_status( $comment_id );
if ( $new_status === $old_status ) {
return false;
@ -1447,23 +1447,23 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
case 'approved' :
case 'approve':
case '1':
$changed = wp_set_comment_status( $comment->comment_ID, 'approve' );
$changed = wp_set_comment_status( $comment_id, 'approve' );
break;
case 'hold':
case '0':
$changed = wp_set_comment_status( $comment->comment_ID, 'hold' );
$changed = wp_set_comment_status( $comment_id, 'hold' );
break;
case 'spam' :
$changed = wp_spam_comment( $comment->comment_ID );
$changed = wp_spam_comment( $comment_id );
break;
case 'unspam' :
$changed = wp_unspam_comment( $comment->comment_ID );
$changed = wp_unspam_comment( $comment_id );
break;
case 'trash' :
$changed = wp_trash_comment( $comment->comment_ID );
$changed = wp_trash_comment( $comment_id );
break;
case 'untrash' :
$changed = wp_untrash_comment( $comment->comment_ID );
$changed = wp_untrash_comment( $comment_id );
break;
default :
$changed = false;

View File

@ -482,14 +482,15 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
}
$post = $this->prepare_item_for_database( $request );
$prepared_post = $this->prepare_item_for_database( $request );
if ( is_wp_error( $post ) ) {
return $post;
if ( is_wp_error( $prepared_post ) ) {
return $prepared_post;
}
$post->post_type = $this->post_type;
$post_id = wp_insert_post( wp_slash( (array) $post ), true );
$prepared_post->post_type = $this->post_type;
$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
if ( is_wp_error( $post_id ) ) {
@ -502,7 +503,20 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return $post_id;
}
$post->ID = $post_id;
$post = get_post( $post_id );
/**
* Fires after a single post is created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 4.7.0
*
* @param WP_Post $post Inserted or updated post object.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating a post, false when updating.
*/
do_action( "rest_insert_{$this->post_type}", $post, $request, true );
$schema = $this->get_item_schema();
@ -515,7 +529,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
$this->handle_featured_media( $request['featured_media'], $post->ID );
$this->handle_featured_media( $request['featured_media'], $post_id );
}
if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
@ -523,44 +537,30 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
$this->handle_template( $request['template'], $post->ID );
$this->handle_template( $request['template'], $post_id );
}
$terms_update = $this->handle_terms( $post->ID, $request );
$terms_update = $this->handle_terms( $post_id, $request );
if ( is_wp_error( $terms_update ) ) {
return $terms_update;
}
$post = get_post( $post_id );
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
$meta_update = $this->meta->update_value( $request['meta'], $post_id );
if ( is_wp_error( $meta_update ) ) {
return $meta_update;
}
}
$post = get_post( $post_id );
$fields_update = $this->update_additional_fields_for_object( $post, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
/**
* Fires after a single post is created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 4.7.0
*
* @param object $post Inserted Post object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating post, false when updating.
*/
do_action( "rest_insert_{$this->post_type}", $post, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
@ -640,6 +640,11 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return $post_id;
}
$post = get_post( $post_id );
/* This action is documented in lib/endpoints/class-wp-rest-controller.php */
do_action( "rest_insert_{$this->post_type}", $post, $request, false );
$schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
@ -668,8 +673,6 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return $terms_update;
}
$post = get_post( $post_id );
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], $post->ID );
@ -678,15 +681,13 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
}
$post = get_post( $post_id );
$fields_update = $this->update_additional_fields_for_object( $post, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
/* This action is documented in lib/endpoints/class-wp-rest-controller.php */
do_action( "rest_insert_{$this->post_type}", $post, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );

View File

@ -403,9 +403,9 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
*
* @since 4.7.0
*
* @param WP_Term $term Inserted Term object.
* @param WP_Term $term Inserted or updated term object.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating term, false when updating.
* @param bool $creating True when creating a term, false when updating.
*/
do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );

View File

@ -461,6 +461,17 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
$user = get_user_by( 'id', $user_id );
/**
* Fires immediately after a user is created or updated via the REST API.
*
* @since 4.7.0
*
* @param WP_User $user Inserted or updated user object.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating a user, false when updating.
*/
do_action( 'rest_insert_user', $user, $request, true );
if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
array_map( array( $user, 'add_role' ), $request['roles'] );
}
@ -473,23 +484,13 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
}
}
$user = get_user_by( 'id', $user_id );
$fields_update = $this->update_additional_fields_for_object( $user, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
/**
* Fires immediately after a user is created or updated via the REST API.
*
* @since 4.7.0
*
* @param WP_User $user Data used to create the user.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating user, false when updating user.
*/
do_action( 'rest_insert_user', $user, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $user, $request );
@ -573,7 +574,10 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
return $user_id;
}
$user = get_user_by( 'id', $id );
$user = get_user_by( 'id', $user_id );
/* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
do_action( 'rest_insert_user', $user, $request, false );
if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
add_user_to_blog( get_current_blog_id(), $id, '' );
@ -593,15 +597,13 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
}
}
$user = get_user_by( 'id', $user_id );
$fields_update = $this->update_additional_fields_for_object( $user, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
/* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
do_action( 'rest_insert_user', $user, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $user, $request );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-beta4-39347';
$wp_version = '4.7-beta4-39348';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.