Make WordPress Core

Changeset 49731


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

Location:
trunk
Files:
1 added
5 edited

Legend:

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

    r49614 r49731  
    688688            ),
    689689            false,
    690             true
     690            false
    691691        );
    692692        $post    = get_post( $post_id );
     
    694694            set_post_format( $post, get_option( 'default_post_format' ) );
    695695        }
    696         wp_after_insert_post( $post, false );
     696        wp_after_insert_post( $post, false, null );
    697697
    698698        // Schedule auto-draft cleanup.
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r49193 r49731  
    31063106        do_action( 'wp_insert_post', $post->ID, $post, true );
    31073107
    3108         wp_after_insert_post( $post, true );
     3108        wp_after_insert_post( get_post( $post_id ), true, $post );
    31093109
    31103110        wp_trash_post_comments( $post_id );
  • 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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r49172 r49731  
    192192        do_action( 'rest_after_insert_attachment', $attachment, $request, true );
    193193
    194         wp_after_insert_post( $attachment, false );
     194        wp_after_insert_post( $attachment, false, null );
    195195
    196196        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     
    322322        }
    323323
    324         $response = parent::update_item( $request );
     324        $attachment_before = get_post( $request['id'] );
     325        $response          = parent::update_item( $request );
    325326
    326327        if ( is_wp_error( $response ) ) {
     
    348349        do_action( 'rest_after_insert_attachment', $attachment, $request, false );
    349350
    350         wp_after_insert_post( $attachment, true );
     351        wp_after_insert_post( $attachment, true, $attachment_before );
    351352
    352353        $response = $this->prepare_item_for_response( $attachment, $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r49302 r49731  
    678678        do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
    679679
    680         wp_after_insert_post( $post, false );
     680        wp_after_insert_post( $post, false, null );
    681681
    682682        $response = $this->prepare_item_for_response( $post, $request );
     
    754754        }
    755755
    756         $post = $this->prepare_item_for_database( $request );
     756        $post_before = get_post( $request['id'] );
     757        $post        = $this->prepare_item_for_database( $request );
    757758
    758759        if ( is_wp_error( $post ) ) {
     
    831832        do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
    832833
    833         wp_after_insert_post( $post, true );
     834        wp_after_insert_post( $post, true, $post_before );
    834835
    835836        $response = $this->prepare_item_for_response( $post, $request );
Note: See TracChangeset for help on using the changeset viewer.