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