Make WordPress Core

Changeset 52276


Ignore:
Timestamp:
11/30/2021 01:08:05 AM (3 years ago)
Author:
peterwilsoncc
Message:

REST API: Fire wp_after_insert_post later in new post object endpoints.

In the new menu items, global items and template controllers manually call the function wp_after_insert_post() to manually fire the action of the same name after all potential data is updated.

This accounts for the use case in which a theme or plugin developer has modified the endpoints use of either taxonomy or meta data.

The new parameter $fire_after_hooks is added to wp_update_nav_menu_item() to control whether the wp_after_insert_post action is fired when it in turn calls wp_insert_post().

Props spacedmonkey, peterwilsoncc, zieladam.
Fixes #54536.

Location:
trunk/src/wp-includes
Files:
4 edited

Legend:

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

    r52184 r52276  
    411411 *
    412412 * @since 3.0.0
     413 * @since 5.9.0 Added the `$fire_after_hooks` parameter.
    413414 *
    414415 * @param int   $menu_id         The ID of the menu. Required. If "0", makes the menu item a draft orphan.
    415416 * @param int   $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
    416417 * @param array $menu_item_data  The menu item's data.
     418 * @param bool  $fire_after_hooks Whether to fire the after insert hooks. Default true.
    417419 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
    418420 */
    419 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
     421function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array(), $fire_after_hooks = true ) {
    420422    $menu_id         = (int) $menu_id;
    421423    $menu_item_db_id = (int) $menu_item_db_id;
     
    527529        $post['ID']          = 0;
    528530        $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
    529         $menu_item_db_id     = wp_insert_post( $post, true );
     531        $menu_item_db_id     = wp_insert_post( $post, true, $fire_after_hooks );
    530532        if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
    531533            return $menu_item_db_id;
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r52275 r52276  
    147147     */
    148148    public function update_item( $request ) {
    149         $post = get_post( $request['id'] );
    150         if ( ! $post || 'wp_global_styles' !== $post->post_type ) {
     149        $post_before = get_post( $request['id'] );
     150        if ( ! $post_before || 'wp_global_styles' !== $post_before->post_type ) {
    151151            return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.' ), array( 'status' => 404 ) );
    152152        }
    153153
    154154        $changes = $this->prepare_item_for_database( $request );
    155         $result  = wp_update_post( wp_slash( (array) $changes ), true );
     155        $result  = wp_update_post( wp_slash( (array) $changes ), true, false );
    156156        if ( is_wp_error( $result ) ) {
    157157            return $result;
     
    164164        }
    165165
    166         return $this->prepare_item_for_response(
    167             get_post( $request['id'] ),
    168             $request
    169         );
     166        wp_after_insert_post( $post, true, $post_before );
     167
     168        $response = $this->prepare_item_for_response( $post, $request );
     169
     170        return rest_ensure_response( $response );
    170171    }
    171172
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php

    r52184 r52276  
    123123        $prepared_nav_item = (array) $prepared_nav_item;
    124124
    125         $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ) );
     125        $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );
    126126        if ( is_wp_error( $nav_menu_item_id ) ) {
    127127            if ( 'db_insert_error' === $nav_menu_item_id->get_error_code() ) {
     
    182182        do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, true );
    183183
    184         $response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );
     184        $post = get_post( $nav_menu_item_id );
     185        wp_after_insert_post( $post, false, null );
     186
     187        $response = $this->prepare_item_for_response( $post, $request );
    185188        $response = rest_ensure_response( $response );
    186189
     
    205208            return $valid_check;
    206209        }
    207 
     210        $post_before       = get_post( $request['id'] );
    208211        $prepared_nav_item = $this->prepare_item_for_database( $request );
    209212
     
    214217        $prepared_nav_item = (array) $prepared_nav_item;
    215218
    216         $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ) );
     219        $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );
    217220
    218221        if ( is_wp_error( $nav_menu_item_id ) ) {
     
    246249        }
    247250
     251        $post          = get_post( $nav_menu_item_id );
    248252        $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
    249253        $fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );
     
    257261        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
    258262        do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false );
     263
     264        wp_after_insert_post( $post, true, $post_before );
    259265
    260266        $response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r52275 r52276  
    267267        }
    268268
     269        $post_before = get_post( $template->wp_id );
     270
    269271        if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
    270272            wp_delete_post( $template->wp_id, true );
     
    284286
    285287        if ( 'custom' === $template->source ) {
    286             $result = wp_update_post( wp_slash( (array) $changes ), true );
     288            $update = true;
     289            $result = wp_update_post( wp_slash( (array) $changes ), false );
    287290        } else {
    288             $result = wp_insert_post( wp_slash( (array) $changes ), true );
     291            $update      = false;
     292            $post_before = null;
     293            $result      = wp_insert_post( wp_slash( (array) $changes ), false );
    289294        }
    290295
     
    309314        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
    310315        do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
     316
     317        wp_after_insert_post( $post, $update, $post_before );
    311318
    312319        $response = $this->prepare_item_for_response( $template, $request );
     
    367374        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
    368375        do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
     376
     377        wp_after_insert_post( $post, false, null );
    369378
    370379        $response = $this->prepare_item_for_response( $template, $request );
Note: See TracChangeset for help on using the changeset viewer.