Make WordPress Core

Changeset 39348


Ignore:
Timestamp:
11/23/2016 03:32:25 PM (10 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.

Location:
trunk/src/wp-includes/rest-api/endpoints
Files:
5 edited

Legend:

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

    r39342 r39348  
    156156                $attachment = get_post( $id );
    157157
     158                /**
     159                 * Fires after a single attachment is created or updated via the REST API.
     160                 *
     161                 * @since 4.7.0
     162                 *
     163                 * @param WP_Post         $attachment Inserted or updated attachment
     164                 *                                    object.
     165                 * @param WP_REST_Request $request    The request sent to the API.
     166                 * @param bool            $creating   True when creating an attachment, false when updating.
     167                 */
     168                do_action( 'rest_insert_attachment', $attachment, $request, true );
     169
    158170                // Include admin functions to get access to wp_generate_attachment_metadata().
    159171                require_once ABSPATH . 'wp-admin/includes/admin.php';
    … …  
    177189                $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
    178190
    179                 /**
    180                  * Fires after a single attachment is created or updated via the REST API.
    181                  *
    182                  * @since 4.7.0
    183                  *
    184                  * @param object          $attachment Inserted attachment.
    185                  * @param WP_REST_Request $request    The request sent to the API.
    186                  * @param bool            $creating   True when creating an attachment, false when updating.
    187                  */
    188                 do_action( 'rest_insert_attachment', $attachment, $request, true );
    189 
    190191                return $response;
    191192        }
    … …  
    220221                $attachment = get_post( $request['id'] );
    221222
     223                /* This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
     224                do_action( 'rest_insert_attachment', $data, $request, false );
     225
    222226                $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
    223227
    … …  
    229233                $response = $this->prepare_item_for_response( $attachment, $request );
    230234                $response = rest_ensure_response( $response );
    231 
    232                 /* This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
    233                 do_action( 'rest_insert_attachment', $data, $request, false );
    234235
    235236                return $response;
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39342 r39348  
    568568
    569569                if ( isset( $request['status'] ) ) {
    570                         $comment = get_comment( $comment_id );
    571 
    572                         $this->handle_status_param( $request['status'], $comment );
    573                 }
     570                        $this->handle_status_param( $request['status'], $comment_id );
     571                }
     572
     573                $comment = get_comment( $comment_id );
     574
     575                /**
     576                 * Fires after a comment is created or updated via the REST API.
     577                 *
     578                 * @since 4.7.0
     579                 *
     580                 * @param WP_Comment      $comment  Inserted or updated comment object.
     581                 * @param WP_REST_Request $request  Request object.
     582                 * @param bool            $creating True when creating a comment, false
     583                 *                                  when updating.
     584                 */
     585                do_action( 'rest_insert_comment', $comment, $request, true );
    574586
    575587                $schema = $this->get_item_schema();
    … …  
    583595                }
    584596
    585                 $comment = get_comment( $comment_id );
    586 
    587597                $fields_update = $this->update_additional_fields_for_object( $comment, $request );
    588598
    … …  
    601611                $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) );
    602612
    603                 /**
    604                  * Fires after a comment is created or updated via the REST API.
    605                  *
    606                  * @since 4.7.0
    607                  *
    608                  * @param array           $comment  Comment as it exists in the database.
    609                  * @param WP_REST_Request $request  The request sent to the API.
    610                  * @param bool            $creating True when creating a comment, false when updating.
    611                  */
    612                 do_action( 'rest_insert_comment', $comment, $request, true );
    613613
    614614                return $response;
    … …  
    667667                if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
    668668                        // Only the comment status is being changed.
    669                         $change = $this->handle_status_param( $request['status'], $comment );
     669                        $change = $this->handle_status_param( $request['status'], $id );
    670670
    671671                        if ( ! $change ) {
    … …  
    696696
    697697                        if ( isset( $request['status'] ) ) {
    698                                 $this->handle_status_param( $request['status'], $comment );
    699                         }
    700                 }
     698                                $this->handle_status_param( $request['status'], $id );
     699                        }
     700                }
     701
     702                $comment = get_comment( $id );
     703
     704                /* This action is documented in lib/endpoints/class-wp-rest-comments-controller.php */
     705                do_action( 'rest_insert_comment', $comment, $request, false );
    701706
    702707                $schema = $this->get_item_schema();
    … …  
    710715                }
    711716
    712                 $comment = get_comment( $id );
    713 
    714717                $fields_update = $this->update_additional_fields_for_object( $comment, $request );
    715718
    … …  
    721724
    722725                $response = $this->prepare_item_for_response( $comment, $request );
    723 
    724                 /* This action is documented in lib/endpoints/class-wp-rest-comments-controller.php */
    725                 do_action( 'rest_insert_comment', $comment, $request, false );
    726726
    727727                return rest_ensure_response( $response );
    … …  
    14341434         *
    14351435         * @param string|int $new_status New comment status.
    1436          * @param WP_Comment $comment    Comment data.
     1436         * @param int        $comment_id Comment ID.
    14371437         * @return bool Whether the status was changed.
    14381438         */
    1439         protected function handle_status_param( $new_status, $comment ) {
    1440                 $old_status = wp_get_comment_status( $comment->comment_ID );
     1439        protected function handle_status_param( $new_status, $comment_id ) {
     1440                $old_status = wp_get_comment_status( $comment_id );
    14411441
    14421442                if ( $new_status === $old_status ) {
    … …  
    14481448                        case 'approve':
    14491449                        case '1':
    1450                                 $changed = wp_set_comment_status( $comment->comment_ID, 'approve' );
     1450                                $changed = wp_set_comment_status( $comment_id, 'approve' );
    14511451                                break;
    14521452                        case 'hold':
    14531453                        case '0':
    1454                                 $changed = wp_set_comment_status( $comment->comment_ID, 'hold' );
     1454                                $changed = wp_set_comment_status( $comment_id, 'hold' );
    14551455                                break;
    14561456                        case 'spam' :
    1457                                 $changed = wp_spam_comment( $comment->comment_ID );
     1457                                $changed = wp_spam_comment( $comment_id );
    14581458                                break;
    14591459                        case 'unspam' :
    1460                                 $changed = wp_unspam_comment( $comment->comment_ID );
     1460                                $changed = wp_unspam_comment( $comment_id );
    14611461                                break;
    14621462                        case 'trash' :
    1463                                 $changed = wp_trash_comment( $comment->comment_ID );
     1463                                $changed = wp_trash_comment( $comment_id );
    14641464                                break;
    14651465                        case 'untrash' :
    1466                                 $changed = wp_untrash_comment( $comment->comment_ID );
     1466                                $changed = wp_untrash_comment( $comment_id );
    14671467                                break;
    14681468                        default :
  • 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' );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r39342 r39348  
    404404                 * @since 4.7.0
    405405                 *
    406                  * @param WP_Term         $term     Inserted Term object.
     406                 * @param WP_Term         $term     Inserted or updated term object.
    407407                 * @param WP_REST_Request $request  Request object.
    408                  * @param bool            $creating True when creating term, false when updating.
     408                 * @param bool            $creating True when creating a term, false when updating.
    409409                 */
    410410                do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r39342 r39348  
    462462                $user = get_user_by( 'id', $user_id );
    463463
    464                 if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
    465                         array_map( array( $user, 'add_role' ), $request['roles'] );
    466                 }
    467 
    468                 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    469                         $meta_update = $this->meta->update_value( $request['meta'], $user_id );
    470 
    471                         if ( is_wp_error( $meta_update ) ) {
    472                                 return $meta_update;
    473                         }
    474                 }
    475 
    476                 $fields_update = $this->update_additional_fields_for_object( $user, $request );
    477 
    478                 if ( is_wp_error( $fields_update ) ) {
    479                         return $fields_update;
    480                 }
    481 
    482464                /**
    483465                 * Fires immediately after a user is created or updated via the REST API.
    … …  
    485467                 * @since 4.7.0
    486468                 *
    487                  * @param WP_User         $user     Data used to create the user.
     469                 * @param WP_User         $user     Inserted or updated user object.
    488470                 * @param WP_REST_Request $request  Request object.
    489                  * @param bool            $creating True when creating user, false when updating user.
     471                 * @param bool            $creating True when creating a user, false when updating.
    490472                 */
    491473                do_action( 'rest_insert_user', $user, $request, true );
     474
     475                if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
     476                        array_map( array( $user, 'add_role' ), $request['roles'] );
     477                }
     478
     479                if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
     480                        $meta_update = $this->meta->update_value( $request['meta'], $user_id );
     481
     482                        if ( is_wp_error( $meta_update ) ) {
     483                                return $meta_update;
     484                        }
     485                }
     486
     487                $user = get_user_by( 'id', $user_id );
     488                $fields_update = $this->update_additional_fields_for_object( $user, $request );
     489
     490                if ( is_wp_error( $fields_update ) ) {
     491                        return $fields_update;
     492                }
    492493
    493494                $request->set_param( 'context', 'edit' );
    … …  
    574575                }
    575576
    576                 $user = get_user_by( 'id', $id );
     577                $user = get_user_by( 'id', $user_id );
     578
     579                /* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
     580                do_action( 'rest_insert_user', $user, $request, false );
    577581
    578582                if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
    … …  
    594598                }
    595599
     600                $user = get_user_by( 'id', $user_id );
    596601                $fields_update = $this->update_additional_fields_for_object( $user, $request );
    597602
    … …  
    599604                        return $fields_update;
    600605                }
    601 
    602                 /* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
    603                 do_action( 'rest_insert_user', $user, $request, false );
    604606
    605607                $request->set_param( 'context', 'edit' );
Note: See TracChangeset for help on using the changeset viewer.