REST API: Fire actions after items are completely updated/inserted.

The existing `rest_insert_*` actions are fired before meta and additional fields are updated. These new `rest_after_*` actions fire after all write operations have completed.

Props timothyblynjacobs.
Fixes #42864.

Built from https://develop.svn.wordpress.org/branches/5.0@43737


git-svn-id: http://core.svn.wordpress.org/branches/5.0@43566 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
danielbachhuber 2018-10-17 11:20:26 +00:00
parent 12b674fb3a
commit fbd004bbbd
6 changed files with 78 additions and 2 deletions

View File

@ -185,6 +185,18 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
} }
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/**
* Fires after a single attachment is completely created or updated via the REST API.
*
* @since 5.0.0
*
* @param WP_Post $attachment Inserted or updated attachment object.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating an attachment, false when updating.
*/
do_action( 'rest_after_insert_attachment', $attachment, $request, true );
$response = $this->prepare_item_for_response( $attachment, $request ); $response = $this->prepare_item_for_response( $attachment, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
$response->set_status( 201 ); $response->set_status( 201 );
@ -231,6 +243,10 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
} }
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
do_action( 'rest_after_insert_attachment', $attachment, $request, false );
$response = $this->prepare_item_for_response( $attachment, $request ); $response = $this->prepare_item_for_response( $attachment, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );

View File

@ -622,9 +622,20 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
} }
$context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view'; $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view';
$request->set_param( 'context', $context ); $request->set_param( 'context', $context );
/**
* Fires completely after a comment is created or updated via the REST API.
*
* @since 5.0.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_after_insert_comment', $comment, $request, true );
$response = $this->prepare_item_for_response( $comment, $request ); $response = $this->prepare_item_for_response( $comment, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
@ -747,6 +758,9 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
do_action( 'rest_after_insert_comment', $comment, $request, false );
$response = $this->prepare_item_for_response( $comment, $request ); $response = $this->prepare_item_for_response( $comment, $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );

View File

@ -600,6 +600,19 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/**
* Fires after a single post is completely created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 5.0.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_after_insert_{$this->post_type}", $post, $request, true );
$response = $this->prepare_item_for_response( $post, $request ); $response = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
@ -726,6 +739,9 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
$response = $this->prepare_item_for_response( $post, $request ); $response = $this->prepare_item_for_response( $post, $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );

View File

@ -456,6 +456,19 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'view' ); $request->set_param( 'context', 'view' );
/**
* Fires after a single term is completely created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
*
* @since 5.0.0
*
* @param WP_Term $term Inserted or updated term object.
* @param WP_REST_Request $request Request object.
* @param bool $creating True when creating a term, false when updating.
*/
do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, true );
$response = $this->prepare_item_for_response( $term, $request ); $response = $this->prepare_item_for_response( $term, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
@ -545,6 +558,9 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'view' ); $request->set_param( 'context', 'view' );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false );
$response = $this->prepare_item_for_response( $term, $request ); $response = $this->prepare_item_for_response( $term, $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );

View File

@ -555,6 +555,17 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/**
* Fires after a user is completely created or updated via the REST API.
*
* @since 5.0.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_after_insert_user', $user, $request, true );
$response = $this->prepare_item_for_response( $user, $request ); $response = $this->prepare_item_for_response( $user, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
@ -678,6 +689,9 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php */
do_action( 'rest_after_insert_user', $user, $request, false );
$response = $this->prepare_item_for_response( $user, $request ); $response = $this->prepare_item_for_response( $user, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.0-alpha-43736'; $wp_version = '5.0-alpha-43737';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.