WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/16/2020 03:32:11 AM (12 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.