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.

This merges [45114] into the 5.6 branch

See #45114

Built from https://develop.svn.wordpress.org/branches/5.6@49732


git-svn-id: http://core.svn.wordpress.org/branches/5.6@49455 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2020-12-01 20:57:05 +00:00
parent ea6021160a
commit 670319d84d
6 changed files with 28 additions and 19 deletions

View File

@ -687,13 +687,13 @@ function get_default_post_to_edit( $post_type = 'post', $create_in_db = false )
'post_status' => 'auto-draft', 'post_status' => 'auto-draft',
), ),
false, false,
true false
); );
$post = get_post( $post_id ); $post = get_post( $post_id );
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) { if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
set_post_format( $post, get_option( 'default_post_format' ) ); set_post_format( $post, get_option( 'default_post_format' ) );
} }
wp_after_insert_post( $post, false ); wp_after_insert_post( $post, false, null );
// Schedule auto-draft cleanup. // Schedule auto-draft cleanup.
if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) { if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {

View File

@ -3105,7 +3105,7 @@ final class WP_Customize_Manager {
/** This action is documented in wp-includes/post.php */ /** This action is documented in wp-includes/post.php */
do_action( 'wp_insert_post', $post->ID, $post, true ); do_action( 'wp_insert_post', $post->ID, $post, true );
wp_after_insert_post( $post, true ); wp_after_insert_post( get_post( $post_id ), true, $post );
wp_trash_post_comments( $post_id ); wp_trash_post_comments( $post_id );

View File

@ -3717,6 +3717,7 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
$previous_status = get_post_field( 'post_status', $post_ID ); $previous_status = get_post_field( 'post_status', $post_ID );
} else { } else {
$previous_status = 'new'; $previous_status = 'new';
$post_before = null;
} }
$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
@ -4319,7 +4320,7 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
do_action( 'wp_insert_post', $post_ID, $post, $update ); do_action( 'wp_insert_post', $post_ID, $post, $update );
if ( $fire_after_hooks ) { if ( $fire_after_hooks ) {
wp_after_insert_post( $post, $update ); wp_after_insert_post( $post, $update, $post_before );
} }
return $post_ID; return $post_ID;
@ -4432,6 +4433,8 @@ function wp_publish_post( $post ) {
return; return;
} }
$post_before = get_post( $post->ID );
// Ensure at least one term is applied for taxonomies with a default term. // 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 ) { foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
// Skip taxonomy if no default term is set. // Skip taxonomy if no default term is set.
@ -4482,7 +4485,7 @@ function wp_publish_post( $post ) {
/** This action is documented in wp-includes/post.php */ /** This action is documented in wp-includes/post.php */
do_action( 'wp_insert_post', $post->ID, $post, true ); do_action( 'wp_insert_post', $post->ID, $post, true );
wp_after_insert_post( $post, true ); wp_after_insert_post( $post, true, $post_before );
} }
/** /**
@ -4937,10 +4940,12 @@ function wp_transition_post_status( $new_status, $old_status, $post ) {
* *
* @since 5.6.0 * @since 5.6.0
* *
* @param int|WP_Post $post The post ID or object that has been saved. * @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 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 ); $post = get_post( $post );
if ( ! $post ) { if ( ! $post ) {
return; return;
@ -4953,11 +4958,13 @@ function wp_after_insert_post( $post, $update ) {
* *
* @since 5.6.0 * @since 5.6.0
* *
* @param int $post_id Post ID. * @param int $post_id Post ID.
* @param WP_Post $post Post object. * @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated. * @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 );
} }
// //

View File

@ -191,7 +191,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
*/ */
do_action( 'rest_after_insert_attachment', $attachment, $request, true ); do_action( 'rest_after_insert_attachment', $attachment, $request, true );
wp_after_insert_post( $attachment, false ); wp_after_insert_post( $attachment, false, null );
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
// Set a custom header with the attachment_id. // Set a custom header with the attachment_id.
@ -321,7 +321,8 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
); );
} }
$response = parent::update_item( $request ); $attachment_before = get_post( $request['id'] );
$response = parent::update_item( $request );
if ( is_wp_error( $response ) ) { if ( is_wp_error( $response ) ) {
return $response; return $response;
@ -347,7 +348,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */ /** 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 ); do_action( 'rest_after_insert_attachment', $attachment, $request, false );
wp_after_insert_post( $attachment, true ); wp_after_insert_post( $attachment, true, $attachment_before );
$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

@ -677,7 +677,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/ */
do_action( "rest_after_insert_{$this->post_type}", $post, $request, true ); do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
wp_after_insert_post( $post, false ); wp_after_insert_post( $post, false, null );
$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 );
@ -753,7 +753,8 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return $valid_check; return $valid_check;
} }
$post = $this->prepare_item_for_database( $request ); $post_before = get_post( $request['id'] );
$post = $this->prepare_item_for_database( $request );
if ( is_wp_error( $post ) ) { if ( is_wp_error( $post ) ) {
return $post; return $post;
@ -830,7 +831,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ /** 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 ); do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
wp_after_insert_post( $post, true ); wp_after_insert_post( $post, true, $post_before );
$response = $this->prepare_item_for_response( $post, $request ); $response = $this->prepare_item_for_response( $post, $request );

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.6-RC1-49730'; $wp_version = '5.6-RC1-49732';
/** /**
* 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.