Make WordPress Core


Ignore:
Timestamp:
11/23/2016 03:32:25 PM (9 years ago)
Author:
rachelbaker
Message:

REST API: Always fire the rest_insert_* actions after the related object is updated or inserted.

Brings consistency to the rest_insert_* actions. Also includes some shuffling and clean-up as well including:

  • Ensure we are passing the most current $post and $user objects to the update_additional_fields_for_object() callbacks.
  • Changes the function signature of handle_status_param() in the Comments controller to accept just the comment_id as the 2nd parameter, instead of a full WP_Comment object. Only the comment_id is needed in the method, this avoids having to include another get_comment() call.
  • Renames a variable in the create_item() method of the Posts controller from $post -> $prepared_post to be more explicit.
  • Minor fixes/clarifications to the rest_insert_* hook docs

Props rachelbaker, joehoyle
Fixes #38905.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39343 r39348  
    483483        }
    484484
    485         $post = $this->prepare_item_for_database( $request );
    486 
    487         if ( is_wp_error( $post ) ) {
    488             return $post;
    489         }
    490 
    491         $post->post_type = $this->post_type;
    492         $post_id         = wp_insert_post( wp_slash( (array) $post ), true );
     485        $prepared_post = $this->prepare_item_for_database( $request );
     486
     487        if ( is_wp_error( $prepared_post ) ) {
     488            return $prepared_post;
     489        }
     490
     491        $prepared_post->post_type = $this->post_type;
     492
     493        $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
    493494
    494495        if ( is_wp_error( $post_id ) ) {
     
    503504        }
    504505
    505         $post->ID = $post_id;
     506        $post = get_post( $post_id );
     507
     508        /**
     509         * Fires after a single post is created or updated via the REST API.
     510         *
     511         * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
     512         *
     513         * @since 4.7.0
     514         *
     515         * @param WP_Post         $post     Inserted or updated post object.
     516         * @param WP_REST_Request $request  Request object.
     517         * @param bool            $creating True when creating a post, false when updating.
     518         */
     519        do_action( "rest_insert_{$this->post_type}", $post, $request, true );
    506520
    507521        $schema = $this->get_item_schema();
     
    516530
    517531        if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
    518             $this->handle_featured_media( $request['featured_media'], $post->ID );
     532            $this->handle_featured_media( $request['featured_media'], $post_id );
    519533        }
    520534
     
    524538
    525539        if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
    526             $this->handle_template( $request['template'], $post->ID );
    527         }
    528 
    529         $terms_update = $this->handle_terms( $post->ID, $request );
     540            $this->handle_template( $request['template'], $post_id );
     541        }
     542
     543        $terms_update = $this->handle_terms( $post_id, $request );
    530544
    531545        if ( is_wp_error( $terms_update ) ) {
     
    533547        }
    534548
    535         $post = get_post( $post_id );
    536 
    537549        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    538             $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
     550            $meta_update = $this->meta->update_value( $request['meta'], $post_id );
    539551
    540552            if ( is_wp_error( $meta_update ) ) {
     
    543555        }
    544556
     557        $post = get_post( $post_id );
    545558        $fields_update = $this->update_additional_fields_for_object( $post, $request );
    546559
     
    548561            return $fields_update;
    549562        }
    550 
    551         /**
    552          * Fires after a single post is created or updated via the REST API.
    553          *
    554          * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
    555          *
    556          * @since 4.7.0
    557          *
    558          * @param object          $post     Inserted Post object (not a WP_Post object).
    559          * @param WP_REST_Request $request  Request object.
    560          * @param bool            $creating True when creating post, false when updating.
    561          */
    562         do_action( "rest_insert_{$this->post_type}", $post, $request, true );
    563563
    564564        $request->set_param( 'context', 'edit' );
     
    641641        }
    642642
     643        $post = get_post( $post_id );
     644
     645        /* This action is documented in lib/endpoints/class-wp-rest-controller.php */
     646        do_action( "rest_insert_{$this->post_type}", $post, $request, false );
     647
    643648        $schema = $this->get_item_schema();
    644649
     
    669674        }
    670675
    671         $post = get_post( $post_id );
    672 
    673676        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    674677            $meta_update = $this->meta->update_value( $request['meta'], $post->ID );
     
    679682        }
    680683
     684        $post = get_post( $post_id );
    681685        $fields_update = $this->update_additional_fields_for_object( $post, $request );
    682686
     
    684688            return $fields_update;
    685689        }
    686 
    687         /* This action is documented in lib/endpoints/class-wp-rest-controller.php */
    688         do_action( "rest_insert_{$this->post_type}", $post, $request, false );
    689690
    690691        $request->set_param( 'context', 'edit' );
Note: See TracChangeset for help on using the changeset viewer.