REST API: Pass the previous state of the post as a parameter to the wp_after_insert_post hook.

This enables, for example, the previous post status to be used by this hook without the need to first capture it on an earlier hook.

This also fixes the value of the `$fire_after_hooks` parameter in `get_default_post_to_edit()` so the `wp_after_insert_post` action correctly fires just once on the new post screen.

Props Collizo4sky, peterwilsoncc, hellofromTonya, TimothyBlynJacobs, SergeyBiryukov

Fixes #45114


git-svn-id: https://develop.svn.wordpress.org/trunk@49731 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn
2020-12-01 20:45:43 +00:00
parent 17be19d0c4
commit f1e610531a
6 changed files with 248 additions and 18 deletions
+16 -9
View File
@@ -3716,6 +3716,7 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
$previous_status = get_post_field( 'post_status', $post_ID );
} else {
$previous_status = 'new';
$post_before = null;
}
$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
@@ -4318,7 +4319,7 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
do_action( 'wp_insert_post', $post_ID, $post, $update );
if ( $fire_after_hooks ) {
wp_after_insert_post( $post, $update );
wp_after_insert_post( $post, $update, $post_before );
}
return $post_ID;
@@ -4431,6 +4432,8 @@ function wp_publish_post( $post ) {
return;
}
$post_before = get_post( $post->ID );
// Ensure at least one term is applied for taxonomies with a default term.
foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
// Skip taxonomy if no default term is set.
@@ -4481,7 +4484,7 @@ function wp_publish_post( $post ) {
/** This action is documented in wp-includes/post.php */
do_action( 'wp_insert_post', $post->ID, $post, true );
wp_after_insert_post( $post, true );
wp_after_insert_post( $post, true, $post_before );
}
/**
@@ -4936,10 +4939,12 @@ function wp_transition_post_status( $new_status, $old_status, $post ) {
*
* @since 5.6.0
*
* @param int|WP_Post $post The post ID or object that has been saved.
* @param bool $update Whether this is an existing post being updated.
* @param int|WP_Post $post The post ID or object that has been saved.
* @param bool $update Whether this is an existing post being updated.
* @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
* to the update for updated posts.
*/
function wp_after_insert_post( $post, $update ) {
function wp_after_insert_post( $post, $update, $post_before ) {
$post = get_post( $post );
if ( ! $post ) {
return;
@@ -4952,11 +4957,13 @@ function wp_after_insert_post( $post, $update ) {
*
* @since 5.6.0
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
* @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
* to the update for updated posts.
*/
do_action( 'wp_after_insert_post', $post_id, $post, $update );
do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before );
}
//