WordPress.org

Make WordPress Core

Changeset 49172


Ignore:
Timestamp:
10/16/2020 03:32:11 AM (7 weeks ago)
Author:
peterwilsoncc
Message:

REST API, Posts: Add a hook to fire once a post, its terms and meta update.

Introduces the action wp_after_insert_post inside a wrapper function of the same name. This hook allows plugin developers to access a posts full data (including its terms and meta data) regardless of the workflow used to save it.

A new parameter is introduced to wp_insert_post() to indicate whether the hook should be fired within the function call or will be fired afterward.

Props aristath, Collizo4sky, danielbachhuber, joyously, kadamwhite, kraftbj, markparnell, mikeschroder, noisysocks, peterwilsoncc, SergeyBiryukov, talldanwp, thewebprincess, TimothyBlynJacobs.
Fixes #45114.

Location:
trunk
Files:
6 edited

Legend:

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

    r49108 r49172  
    686686                'post_type'   => $post_type,
    687687                'post_status' => 'auto-draft',
    688             )
     688            ),
     689            false,
     690            true
    689691        );
    690692        $post    = get_post( $post_id );
     
    692694            set_post_format( $post, get_option( 'default_post_format' ) );
    693695        }
     696        wp_after_insert_post( $post, false );
    694697
    695698        // Schedule auto-draft cleanup.
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r49108 r49172  
    31053105        /** This action is documented in wp-includes/post.php */
    31063106        do_action( 'wp_insert_post', $post->ID, $post, true );
     3107
     3108        wp_after_insert_post( $post, true );
    31073109
    31083110        wp_trash_post_comments( $post_id );
  • trunk/src/wp-includes/post.php

    r49141 r49172  
    36463646 *     @type array  $meta_input            Array of post meta values keyed by their post meta key. Default empty.
    36473647 * }
    3648  * @param bool  $wp_error Optional. Whether to return a WP_Error on failure. Default false.
     3648 * @param bool  $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
     3649 * @param bool  $fire_after_hooks Whether to fire the after insert hooks. Default true.
    36493650 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
    36503651 */
    3651 function wp_insert_post( $postarr, $wp_error = false ) {
     3652function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) {
    36523653    global $wpdb;
    36533654
     
    43114312    do_action( 'wp_insert_post', $post_ID, $post, $update );
    43124313
     4314    if ( $fire_after_hooks ) {
     4315        wp_after_insert_post( $post, $update );
     4316    }
     4317
    43134318    return $post_ID;
    43144319}
     
    43224327 * @since 1.0.0
    43234328 *
    4324  * @param array|object $postarr  Optional. Post data. Arrays are expected to be escaped,
    4325  *                               objects are not. Default array.
    4326  * @param bool         $wp_error Optional. Allow return of WP_Error on failure. Default false.
     4329 * @param array|object $postarr          Optional. Post data. Arrays are expected to be escaped,
     4330 *                                       objects are not. Default array.
     4331 * @param bool         $wp_error         Optional. Allow return of WP_Error on failure. Default false.
     4332 * @param bool         $fire_after_hooks Whether to fire the after insert hooks. Default true.
    43274333 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
    43284334 */
    4329 function wp_update_post( $postarr = array(), $wp_error = false ) {
     4335function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) {
    43304336    if ( is_object( $postarr ) ) {
    43314337        // Non-escaped post was passed.
     
    43924398    }
    43934399
    4394     return wp_insert_post( $postarr, $wp_error );
     4400    return wp_insert_post( $postarr, $wp_error, $fire_after_hooks );
    43954401}
    43964402
     
    44684474    /** This action is documented in wp-includes/post.php */
    44694475    do_action( 'wp_insert_post', $post->ID, $post, true );
     4476
     4477    wp_after_insert_post( $post, true );
    44704478}
    44714479
     
    49154923     */
    49164924    do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
     4925}
     4926
     4927/**
     4928 * Fires actions after a post, its terms and meta data has been saved.
     4929 *
     4930 * @since 5.6.0
     4931 *
     4932 * @param int|WP_Post $post   The post ID or object that has been saved.
     4933 * @param bool        $update Whether this is an existing post being updated.
     4934 */
     4935function wp_after_insert_post( $post, $update ) {
     4936    $post = get_post( $post );
     4937    if ( ! $post ) {
     4938        return;
     4939    }
     4940
     4941    $post_id = $post->ID;
     4942
     4943    /**
     4944     * Fires once a post, its terms and meta data has been saved.
     4945     *
     4946     * @since 5.6.0
     4947     *
     4948     * @param int     $post_id Post ID.
     4949     * @param WP_Post $post    Post object.
     4950     * @param bool    $update  Whether this is an existing post being updated.
     4951     */
     4952    do_action( 'wp_after_insert_post', $post_id, $post, $update );
    49174953}
    49184954
     
    57905826 * @see wp_insert_post()
    57915827 *
    5792  * @param string|array $args     Arguments for inserting an attachment.
    5793  * @param string       $file     Optional. Filename.
    5794  * @param int          $parent   Optional. Parent post ID.
    5795  * @param bool         $wp_error Optional. Whether to return a WP_Error on failure. Default false.
     5828 * @param string|array $args             Arguments for inserting an attachment.
     5829 * @param string       $file             Optional. Filename.
     5830 * @param int          $parent           Optional. Parent post ID.
     5831 * @param bool         $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
     5832 * @param bool         $fire_after_hooks Whether to fire the after insert hooks. Default true.
    57965833 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
    57975834 */
    5798 function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) {
     5835function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) {
    57995836    $defaults = array(
    58005837        'file'        => $file,
     
    58105847    $data['post_type'] = 'attachment';
    58115848
    5812     return wp_insert_post( $data, $wp_error );
     5849    return wp_insert_post( $data, $wp_error, $fire_after_hooks );
    58135850}
    58145851
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r49108 r49172  
    192192        do_action( 'rest_after_insert_attachment', $attachment, $request, true );
    193193
     194        wp_after_insert_post( $attachment, false );
     195
    194196        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
    195197            // Set a custom header with the attachment_id.
     
    271273
    272274        // $post_parent is inherited from $attachment['post_parent'].
    273         $id = wp_insert_attachment( wp_slash( (array) $attachment ), $file, 0, true );
     275        $id = wp_insert_attachment( wp_slash( (array) $attachment ), $file, 0, true, false );
    274276
    275277        if ( is_wp_error( $id ) ) {
     
    345347        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
    346348        do_action( 'rest_after_insert_attachment', $attachment, $request, false );
     349
     350        wp_after_insert_post( $attachment, true );
    347351
    348352        $response = $this->prepare_item_for_response( $attachment, $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r49120 r49172  
    592592        $prepared_post->post_type = $this->post_type;
    593593
    594         $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
     594        $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true, false );
    595595
    596596        if ( is_wp_error( $post_id ) ) {
     
    678678        do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
    679679
     680        wp_after_insert_post( $post, false );
     681
    680682        $response = $this->prepare_item_for_response( $post, $request );
    681683        $response = rest_ensure_response( $response );
     
    759761
    760762        // Convert the post object to an array, otherwise wp_update_post() will expect non-escaped input.
    761         $post_id = wp_update_post( wp_slash( (array) $post ), true );
     763        $post_id = wp_update_post( wp_slash( (array) $post ), true, false );
    762764
    763765        if ( is_wp_error( $post_id ) ) {
     
    828830        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
    829831        do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
     832
     833        wp_after_insert_post( $post, true );
    830834
    831835        $response = $this->prepare_item_for_response( $post, $request );
  • trunk/tests/phpunit/tests/customize/manager.php

    r49117 r49172  
    11721172            'save_post'                     => 2,
    11731173            'wp_insert_post'                => 2,
     1174            'wp_after_insert_post'          => 2,
    11741175            'trashed_post'                  => 1,
    11751176        );
Note: See TracChangeset for help on using the changeset viewer.