diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index a90d32293b..f0055ee4b3 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -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; } diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index a552df78dd..9747130912 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -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; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 162c56bf84..6a7e843409 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -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 ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index c758d60241..f48ab0620a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -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 ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index c61228ed57..7ede596c56 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index a9d45de20e..47872ca08b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.