Make WordPress Core

Changeset 39161


Ignore:
Timestamp:
11/08/2016 01:08:49 PM (9 years ago)
Author:
joehoyle
Message:

REST API: Remove rest_get_post filter and get_post abstraction.

This filter was originally introduced in https://github.com/WP-API/WP-API/pull/2535 to support Customizer Changesets (née Transactions). This is a super broad filter and doesn't really fit with the design of the API, nor is it (arguably) the right level to do this.

Props rmccue.
Fixes #38701.

Location:
trunk
Files:
6 edited

Legend:

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

    r39155 r39161  
    7373        // Attaching media to a post requires ability to edit said post.
    7474        if ( ! empty( $request['post'] ) ) {
    75             $parent = $this->get_post( (int) $request['post'] );
     75            $parent = get_post( (int) $request['post'] );
    7676            $post_parent_type = get_post_type_object( $parent->post_type );
    7777
     
    154154        }
    155155
    156         $attachment = $this->get_post( $id );
     156        $attachment = get_post( $id );
    157157
    158158        // Include admin functions to get access to wp_generate_attachment_metadata().
     
    218218        }
    219219
    220         $attachment = $this->get_post( $request['id'] );
     220        $attachment = get_post( $request['id'] );
    221221
    222222        $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39157 r39161  
    107107        if ( ! empty( $request['post'] ) ) {
    108108            foreach ( (array) $request['post'] as $post_id ) {
    109                 $post = $this->get_post( $post_id );
     109                $post = get_post( $post_id );
    110110
    111111                if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) {
     
    315315        }
    316316
    317         $post = $this->get_post( $comment->comment_post_ID );
     317        $post = get_post( $comment->comment_post_ID );
    318318
    319319        if ( $post && ! $this->check_read_post_permission( $post ) ) {
     
    346346
    347347        if ( ! empty( $comment->comment_post_ID ) ) {
    348             $post = $this->get_post( $comment->comment_post_ID );
     348            $post = get_post( $comment->comment_post_ID );
    349349            if ( empty( $post ) ) {
    350350                return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) );
     
    390390        }
    391391
    392         if ( ! empty( $request['post'] ) && $post = $this->get_post( (int) $request['post'] ) ) {
     392        if ( ! empty( $request['post'] ) && $post = get_post( (int) $request['post'] ) ) {
    393393            if ( 'draft' === $post->post_status ) {
    394394                return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
     
    872872
    873873        if ( 0 !== (int) $comment->comment_post_ID ) {
    874             $post = $this->get_post( $comment->comment_post_ID );
     874            $post = get_post( $comment->comment_post_ID );
    875875
    876876            if ( ! empty( $post->ID ) ) {
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r39046 r39161  
    582582
    583583    /**
    584      * Retrieves post data given a post ID or post object.
    585      *
    586      * This is a subset of the functionality of the `get_post()` function, with
    587      * the additional functionality of having `the_post` action done on the
    588      * resultant post object. This is done so that plugins may manipulate the
    589      * post that is used in the REST API.
    590      *
    591      * @since 4.7.0
    592      * @access public
    593      *
    594      * @see get_post()
    595      * @global WP_Query $wp_query
    596      *
    597      * @param int|WP_Post $post Post ID or object. Defaults to global `$post` object.
    598      * @return WP_Post|null A WP_Post object when successful, otherwise null.
    599      */
    600     public function get_post( $post ) {
    601         $post_obj = get_post( $post );
    602 
    603         /**
    604          * Filters the post in the context of a REST request.
    605          *
    606          * Allows plugins to filter the post object as returned by WP_REST_Controller::get_post().
    607          *
    608          * @since 4.7.0
    609          *
    610          * @param WP_Post|null $post_obj The post object as returned by get_post().
    611          * @param int|WP_Post  $post     The original value used to obtain the post object.
    612          */
    613         $post = apply_filters( 'rest_the_post', $post_obj, $post );
    614 
    615         return $post;
    616     }
    617 
    618     /**
    619584     * Sanitizes the slug value.
    620585     *
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39155 r39161  
    351351    public function get_item_permissions_check( $request ) {
    352352
    353         $post = $this->get_post( (int) $request['id'] );
     353        $post = get_post( (int) $request['id'] );
    354354
    355355        if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
     
    420420    public function get_item( $request ) {
    421421        $id   = (int) $request['id'];
    422         $post = $this->get_post( $id );
     422        $post = get_post( $id );
    423423
    424424        if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
     
    532532        }
    533533
    534         $post = $this->get_post( $post_id );
     534        $post = get_post( $post_id );
    535535
    536536        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
     
    583583    public function update_item_permissions_check( $request ) {
    584584
    585         $post = $this->get_post( $request['id'] );
     585        $post = get_post( $request['id'] );
    586586        $post_type = get_post_type_object( $this->post_type );
    587587
     
    616616    public function update_item( $request ) {
    617617        $id   = (int) $request['id'];
    618         $post = $this->get_post( $id );
     618        $post = get_post( $id );
    619619
    620620        if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
     
    668668        }
    669669
    670         $post = $this->get_post( $post_id );
     670        $post = get_post( $post_id );
    671671
    672672        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
     
    705705    public function delete_item_permissions_check( $request ) {
    706706
    707         $post = $this->get_post( $request['id'] );
     707        $post = get_post( $request['id'] );
    708708
    709709        if ( $post && ! $this->check_delete_permission( $post ) ) {
     
    727727        $force = (bool) $request['force'];
    728728
    729         $post = $this->get_post( $id );
     729        $post = get_post( $id );
    730730
    731731        if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
     
    780780            // the trash is disabled.)
    781781            $result = wp_trash_post( $id );
    782             $post = $this->get_post( $id );
     782            $post = get_post( $id );
    783783            $response = $this->prepare_item_for_response( $post, $request );
    784784        }
     
    10721072        // Parent.
    10731073        if ( ! empty( $schema['properties']['parent'] ) && ! empty( $request['parent'] ) ) {
    1074             $parent = $this->get_post( (int) $request['parent'] );
     1074            $parent = get_post( (int) $request['parent'] );
    10751075
    10761076            if ( empty( $parent ) ) {
     
    11841184     */
    11851185    public function handle_template( $template, $post_id ) {
    1186         if ( in_array( $template, array_keys( wp_get_theme()->get_page_templates( $this->get_post( $post_id ) ) ), true ) ) {
     1186        if ( in_array( $template, array_keys( wp_get_theme()->get_page_templates( get_post( $post_id ) ) ), true ) ) {
    11871187            update_post_meta( $post_id, '_wp_page_template', $template );
    11881188        } else {
     
    13011301        // Can we read the parent if we're inheriting?
    13021302        if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
    1303             $parent = $this->get_post( $post->post_parent );
     1303            $parent = get_post( $post->post_parent );
    13041304            return $this->check_read_permission( $parent );
    13051305        }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r39126 r39161  
    118118    public function get_items_permissions_check( $request ) {
    119119
    120         $parent = $this->get_post( $request['parent'] );
     120        $parent = get_post( $request['parent'] );
    121121        if ( ! $parent ) {
    122122            return true;
     
    140140     */
    141141    public function get_items( $request ) {
    142 
    143         $parent = $this->get_post( $request['parent'] );
     142        $parent = get_post( $request['parent'] );
    144143        if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) {
    145144            return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent id.' ), array( 'status' => 404 ) );
     
    179178     */
    180179    public function get_item( $request ) {
    181 
    182         $parent = $this->get_post( $request['parent'] );
     180        $parent = get_post( $request['parent'] );
    183181        if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) {
    184182            return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent id.' ), array( 'status' => 404 ) );
    185183        }
    186184
    187         $revision = $this->get_post( $request['id'] );
     185        $revision = get_post( $request['id'] );
    188186        if ( ! $revision || 'revision' !== $revision->post_type ) {
    189187            return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision id.' ), array( 'status' => 404 ) );
     
    210208        }
    211209
    212         $post = $this->get_post( $request['id'] );
     210        $post = get_post( $request['id'] );
    213211        if ( ! $post ) {
    214212            return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision id.' ), array( 'status' => 404 ) );
     
    235233        }
    236234
    237         $revision = $this->get_post( $request['id'] );
     235        $revision = get_post( $request['id'] );
    238236        $previous = $this->prepare_item_for_response( $revision, $request );
    239237
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r38832 r39161  
    191191        $this->assertEquals( 'a', $args['somedefault']['default'] );
    192192    }
    193 
    194     public $rest_the_post_filter_apply_count = 0;
    195 
    196     public function test_get_post() {
    197         $post_id = $this->factory()->post->create( array( 'post_title' => 'Original' ) );
    198         $controller = new WP_REST_Test_Controller();
    199 
    200         $post = $controller->get_post( $post_id );
    201         $this->assertEquals( 'Original', $post->post_title );
    202 
    203         $filter_apply_count = $this->rest_the_post_filter_apply_count;
    204         add_filter( 'rest_the_post', array( $this, 'filter_rest_the_post_for_test_get_post' ), 10, 2 );
    205         $post = $controller->get_post( $post_id );
    206         $this->assertEquals( 'Overridden', $post->post_title );
    207         $this->assertEquals( 1 + $filter_apply_count, $this->rest_the_post_filter_apply_count );
    208     }
    209 
    210     public function filter_rest_the_post_for_test_get_post( $post, $post_id ) {
    211         $this->assertInstanceOf( 'WP_Post', $post );
    212         $this->assertInternalType( 'int', $post_id );
    213         $post->post_title = 'Overridden';
    214         $this->rest_the_post_filter_apply_count += 1;
    215         return $post;
    216     }
    217193}
Note: See TracChangeset for help on using the changeset viewer.