Make WordPress Core


Ignore:
Timestamp:
12/01/2020 08:45:43 PM (4 years ago)
Author:
johnbillion
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r49693 r49731  
    37173717    } else {
    37183718        $previous_status = 'new';
     3719        $post_before     = null;
    37193720    }
    37203721
     
    43194320
    43204321    if ( $fire_after_hooks ) {
    4321         wp_after_insert_post( $post, $update );
     4322        wp_after_insert_post( $post, $update, $post_before );
    43224323    }
    43234324
     
    44324433    }
    44334434
     4435    $post_before = get_post( $post->ID );
     4436
    44344437    // Ensure at least one term is applied for taxonomies with a default term.
    44354438    foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
     
    44824485    do_action( 'wp_insert_post', $post->ID, $post, true );
    44834486
    4484     wp_after_insert_post( $post, true );
     4487    wp_after_insert_post( $post, true, $post_before );
    44854488}
    44864489
     
    49374940 * @since 5.6.0
    49384941 *
    4939  * @param int|WP_Post $post   The post ID or object that has been saved.
    4940  * @param bool        $update Whether this is an existing post being updated.
    4941  */
    4942 function wp_after_insert_post( $post, $update ) {
     4942 * @param int|WP_Post  $post        The post ID or object that has been saved.
     4943 * @param bool         $update      Whether this is an existing post being updated.
     4944 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
     4945 *                                  to the update for updated posts.
     4946 */
     4947function wp_after_insert_post( $post, $update, $post_before ) {
    49434948    $post = get_post( $post );
    49444949    if ( ! $post ) {
     
    49534958     * @since 5.6.0
    49544959     *
    4955      * @param int     $post_id Post ID.
    4956      * @param WP_Post $post    Post object.
    4957      * @param bool    $update  Whether this is an existing post being updated.
     4960     * @param int          $post_id     Post ID.
     4961     * @param WP_Post      $post        Post object.
     4962     * @param bool         $update      Whether this is an existing post being updated.
     4963     * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
     4964     *                                  to the update for updated posts.
    49584965     */
    4959     do_action( 'wp_after_insert_post', $post_id, $post, $update );
     4966    do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before );
    49604967}
    49614968
Note: See TracChangeset for help on using the changeset viewer.