Make WordPress Core

Ticket #38701: 38701.diff

File 38701.diff, 15.6 KB (added by rmccue, 10 years ago)

Remove rest_get_post, and use accessor functions for filterability

  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

     
    7272
    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
    7878                        if ( ! current_user_can( $post_parent_type->cap->edit_post, $request['post'] ) ) {
     
    153153                        return $id;
    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().
    159159                require_once ABSPATH . 'wp-admin/includes/admin.php';
     
    217217                        update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] );
    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 );
    223223
     
    275275        public function prepare_item_for_response( $post, $request ) {
    276276                $response = parent::prepare_item_for_response( $post, $request );
    277277                $data = $response->get_data();
     278                $parent = (int) wp_get_post_parent_id( $post );
    278279
    279280                $data['alt_text']      = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
    280281                $data['caption']       = $post->post_excerpt;
     
    282283                $data['media_type']    = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
    283284                $data['mime_type']     = $post->post_mime_type;
    284285                $data['media_details'] = wp_get_attachment_metadata( $post->ID );
    285                 $data['post']          = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
     286                $data['post']          = ! empty( $parent ) ? $parent : null;
    286287                $data['source_url']    = wp_get_attachment_url( $post->ID );
    287288
    288289                // Ensure empty details is an empty object.
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

     
    106106
    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 ) ) {
    112112                                        return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
     
    314314                        return new WP_Error( 'rest_cannot_read', __( 'Sorry, you cannot read this comment.' ), array( 'status' => rest_authorization_required_code() ) );
    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 ) ) {
    320320                        return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
     
    345345                }
    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 ) );
    351351                        }
     
    389389                        return new WP_Error( 'rest_comment_invalid_post_id', __( 'Sorry, you cannot create this comment without a post.' ), array( 'status' => rest_authorization_required_code() ) );
    390390                }
    391391
    392                 if ( ! empty( $request['post'] ) && $post = $this->get_post( (int) $request['post'] ) ) {
    393                         if ( 'draft' === $post->post_status ) {
     392                if ( ! empty( $request['post'] ) && $post = get_post( (int) $request['post'] ) ) {
     393                        $post_status = get_post_status( $post );
     394
     395                        if ( 'draft' === $post_status ) {
    394396                                return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
    395397                        }
    396398
    397                         if ( 'trash' === $post->post_status ) {
     399                        if ( 'trash' === $post_status ) {
    398400                                return new WP_Error( 'rest_comment_trash_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
    399401                        }
    400402
     
    871873                }
    872874
    873875                if ( 0 !== (int) $comment->comment_post_ID ) {
    874                         $post = $this->get_post( $comment->comment_post_ID );
     876                        $post = get_post( $comment->comment_post_ID );
    875877
    876878                        if ( ! empty( $post->ID ) ) {
    877879                                $obj = get_post_type_object( $post->post_type );
  • src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

     
    581581        }
    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         *
    621586         * @since 4.7.0
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    350350         */
    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 ) ) {
    356356                        return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
     
    419419         */
    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 ) {
    425425                        return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) );
     
    531531                        return $terms_update;
    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'] ) ) {
    537537                        $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
     
    582582         */
    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
    588588                if ( $post && ! $this->check_update_permission( $post ) ) {
     
    615615         */
    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 ) {
    621621                        return new WP_Error( 'rest_post_invalid_id', __( 'Post id is invalid.' ), array( 'status' => 404 ) );
     
    667667                        return $terms_update;
    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'] ) ) {
    673673                        $meta_update = $this->meta->update_value( $request['meta'], $post->ID );
     
    704704         */
    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 ) ) {
    710710                        return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete posts.' ), array( 'status' => rest_authorization_required_code() ) );
     
    726726                $id    = (int) $request['id'];
    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 ) {
    732732                        return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) );
     
    772772                        }
    773773
    774774                        // Otherwise, only trash if we haven't already.
    775                         if ( 'trash' === $post->post_status ) {
     775                        if ( 'trash' === get_post_status( $post ) ) {
    776776                                return new WP_Error( 'rest_already_trashed', __( 'The post has already been deleted.' ), array( 'status' => 410 ) );
    777777                        }
    778778
    779779                        // (Note that internally this falls through to `wp_delete_post` if
    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                }
    785785
     
    10711071
    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 ) ) {
    10771077                                return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent id.' ), array( 'status' => 400 ) );
     
    11831183         * @param integer $post_id  Post ID.
    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 {
    11891189                        update_post_meta( $post_id, '_wp_page_template', '' );
     
    12891289                }
    12901290
    12911291                // Is the post readable?
    1292                 if ( 'publish' === $post->post_status || current_user_can( $post_type->cap->read_post, $post->ID ) ) {
     1292                $status = get_post_status( $post );
     1293                if ( 'publish' === $status || current_user_can( $post_type->cap->read_post, $post->ID ) ) {
    12931294                        return true;
    12941295                }
    12951296
    1296                 $post_status_obj = get_post_status_object( $post->post_status );
     1297                $post_status_obj = get_post_status_object( $status );
    12971298                if ( $post_status_obj && $post_status_obj->public ) {
    12981299                        return true;
    12991300                }
    13001301
    13011302                // Can we read the parent if we're inheriting?
    1302                 if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
    1303                         $parent = $this->get_post( $post->post_parent );
     1303                $parent_id = (int) wp_get_post_parent_id( $post );
     1304                if ( 'inherit' === $status && $parent_id > 0 ) {
     1305                        $parent = get_post( $parent_id );
    13041306                        return $this->check_read_permission( $parent );
    13051307                }
    13061308
     
    13081310                 * If there isn't a parent, but the status is set to inherit, assume
    13091311                 * it's published (as per get_post_status()).
    13101312                 */
    1311                 if ( 'inherit' === $post->post_status ) {
     1313                if ( 'inherit' === $status ) {
    13121314                        return true;
    13131315                }
    13141316
     
    14291431                }
    14301432
    14311433                if ( ! empty( $schema['properties']['status'] ) ) {
    1432                         $data['status'] = $post->post_status;
     1434                        $data['status'] = get_post_status( $post );
    14331435                }
    14341436
    14351437                if ( ! empty( $schema['properties']['type'] ) ) {
     
    14931495                }
    14941496
    14951497                if ( ! empty( $schema['properties']['parent'] ) ) {
    1496                         $data['parent'] = (int) $post->post_parent;
     1498                        $data['parent'] = (int) wp_get_post_parent_id( $post );
    14971499                }
    14981500
    14991501                if ( ! empty( $schema['properties']['menu_order'] ) ) {
     
    16311633
    16321634                $post_type_obj = get_post_type_object( $post->post_type );
    16331635
    1634                 if ( $post_type_obj->hierarchical && ! empty( $post->post_parent ) ) {
     1636                $parent_id = (int) wp_get_post_parent_id( $post );
     1637                if ( $post_type_obj->hierarchical && $parent_id > 0 ) {
    16351638                        $links['up'] = array(
    1636                                 'href'       => rest_url( trailingslashit( $base ) . (int) $post->post_parent ),
     1639                                'href'       => rest_url( trailingslashit( $base ) . $parent_id ),
    16371640                                'embeddable' => true,
    16381641                        );
    16391642                }
  • src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

     
    117117         */
    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;
    123123                }
     
    139139         * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
    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 ) );
    146145                }
     
    178177         * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
    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 ) );
    190188                }
     
    209207                        return $response;
    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 ) );
    215213                }
     
    234232                        return new WP_Error( 'rest_trash_not_supported', __( 'Revisions do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    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
    240238                $result = wp_delete_post( $request['id'], true );
     
    301299                }
    302300
    303301                if ( ! empty( $schema['properties']['parent'] ) ) {
    304                         $data['parent'] = (int) $post->post_parent;
     302                        $data['parent'] = (int) wp_get_post_parent_id( $post );
    305303                }
    306304
    307305                if ( ! empty( $schema['properties']['slug'] ) ) {