Ticket #38701: 38701.3.diff
File 38701.3.diff, 13.4 KB (added by , 7 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
72 72 73 73 // Attaching media to a post requires ability to edit said post. 74 74 if ( ! empty( $request['post'] ) ) { 75 $parent = $this->get_post( (int) $request['post'] );75 $parent = get_post( (int) $request['post'] ); 76 76 $post_parent_type = get_post_type_object( $parent->post_type ); 77 77 78 78 if ( ! current_user_can( $post_parent_type->cap->edit_post, $request['post'] ) ) { … … 153 153 return $id; 154 154 } 155 155 156 $attachment = $this->get_post( $id );156 $attachment = get_post( $id ); 157 157 158 158 // Include admin functions to get access to wp_generate_attachment_metadata(). 159 159 require_once ABSPATH . 'wp-admin/includes/admin.php'; … … 217 217 update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] ); 218 218 } 219 219 220 $attachment = $this->get_post( $request['id'] );220 $attachment = get_post( $request['id'] ); 221 221 222 222 $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); 223 223 -
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
106 106 107 107 if ( ! empty( $request['post'] ) ) { 108 108 foreach ( (array) $request['post'] as $post_id ) { 109 $post = $this->get_post( $post_id );109 $post = get_post( $post_id ); 110 110 111 111 if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) { 112 112 return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); … … 314 314 return new WP_Error( 'rest_cannot_read', __( 'Sorry, you cannot read this comment.' ), array( 'status' => rest_authorization_required_code() ) ); 315 315 } 316 316 317 $post = $this->get_post( $comment->comment_post_ID );317 $post = get_post( $comment->comment_post_ID ); 318 318 319 319 if ( $post && ! $this->check_read_post_permission( $post ) ) { 320 320 return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); … … 345 345 } 346 346 347 347 if ( ! empty( $comment->comment_post_ID ) ) { 348 $post = $this->get_post( $comment->comment_post_ID );348 $post = get_post( $comment->comment_post_ID ); 349 349 if ( empty( $post ) ) { 350 350 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) ); 351 351 } … … 389 389 return new WP_Error( 'rest_comment_invalid_post_id', __( 'Sorry, you cannot create this comment without a post.' ), array( 'status' => rest_authorization_required_code() ) ); 390 390 } 391 391 392 if ( ! empty( $request['post'] ) && $post = $this->get_post( (int) $request['post'] ) ) {392 if ( ! empty( $request['post'] ) && $post = get_post( (int) $request['post'] ) ) { 393 393 if ( 'draft' === $post->post_status ) { 394 394 return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) ); 395 395 } … … 871 871 } 872 872 873 873 if ( 0 !== (int) $comment->comment_post_ID ) { 874 $post = $this->get_post( $comment->comment_post_ID );874 $post = get_post( $comment->comment_post_ID ); 875 875 876 876 if ( ! empty( $post->ID ) ) { 877 877 $obj = get_post_type_object( $post->post_type ); -
src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
581 581 } 582 582 583 583 /** 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, with587 * the additional functionality of having `the_post` action done on the588 * resultant post object. This is done so that plugins may manipulate the589 * post that is used in the REST API.590 *591 * @since 4.7.0592 * @access public593 *594 * @see get_post()595 * @global WP_Query $wp_query596 *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.0609 *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 /**619 584 * Sanitizes the slug value. 620 585 * 621 586 * @since 4.7.0 -
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
350 350 */ 351 351 public function get_item_permissions_check( $request ) { 352 352 353 $post = $this->get_post( (int) $request['id'] );353 $post = get_post( (int) $request['id'] ); 354 354 355 355 if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) { 356 356 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) ); … … 419 419 */ 420 420 public function get_item( $request ) { 421 421 $id = (int) $request['id']; 422 $post = $this->get_post( $id );422 $post = get_post( $id ); 423 423 424 424 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { 425 425 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) ); … … 531 531 return $terms_update; 532 532 } 533 533 534 $post = $this->get_post( $post_id );534 $post = get_post( $post_id ); 535 535 536 536 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 537 537 $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] ); … … 582 582 */ 583 583 public function update_item_permissions_check( $request ) { 584 584 585 $post = $this->get_post( $request['id'] );585 $post = get_post( $request['id'] ); 586 586 $post_type = get_post_type_object( $this->post_type ); 587 587 588 588 if ( $post && ! $this->check_update_permission( $post ) ) { … … 615 615 */ 616 616 public function update_item( $request ) { 617 617 $id = (int) $request['id']; 618 $post = $this->get_post( $id );618 $post = get_post( $id ); 619 619 620 620 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { 621 621 return new WP_Error( 'rest_post_invalid_id', __( 'Post id is invalid.' ), array( 'status' => 404 ) ); … … 667 667 return $terms_update; 668 668 } 669 669 670 $post = $this->get_post( $post_id );670 $post = get_post( $post_id ); 671 671 672 672 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 673 673 $meta_update = $this->meta->update_value( $request['meta'], $post->ID ); … … 704 704 */ 705 705 public function delete_item_permissions_check( $request ) { 706 706 707 $post = $this->get_post( $request['id'] );707 $post = get_post( $request['id'] ); 708 708 709 709 if ( $post && ! $this->check_delete_permission( $post ) ) { 710 710 return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete posts.' ), array( 'status' => rest_authorization_required_code() ) ); … … 726 726 $id = (int) $request['id']; 727 727 $force = (bool) $request['force']; 728 728 729 $post = $this->get_post( $id );729 $post = get_post( $id ); 730 730 731 731 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { 732 732 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) ); … … 779 779 // (Note that internally this falls through to `wp_delete_post` if 780 780 // the trash is disabled.) 781 781 $result = wp_trash_post( $id ); 782 $post = $this->get_post( $id );782 $post = get_post( $id ); 783 783 $response = $this->prepare_item_for_response( $post, $request ); 784 784 } 785 785 … … 1071 1071 1072 1072 // Parent. 1073 1073 if ( ! empty( $schema['properties']['parent'] ) && ! empty( $request['parent'] ) ) { 1074 $parent = $this->get_post( (int) $request['parent'] );1074 $parent = get_post( (int) $request['parent'] ); 1075 1075 1076 1076 if ( empty( $parent ) ) { 1077 1077 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent id.' ), array( 'status' => 400 ) ); … … 1183 1183 * @param integer $post_id Post ID. 1184 1184 */ 1185 1185 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 ) ) { 1187 1187 update_post_meta( $post_id, '_wp_page_template', $template ); 1188 1188 } else { 1189 1189 update_post_meta( $post_id, '_wp_page_template', '' ); … … 1300 1300 1301 1301 // Can we read the parent if we're inheriting? 1302 1302 if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) { 1303 $parent = $this->get_post( $post->post_parent );1303 $parent = get_post( $post->post_parent ); 1304 1304 return $this->check_read_permission( $parent ); 1305 1305 } 1306 1306 -
src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
117 117 */ 118 118 public function get_items_permissions_check( $request ) { 119 119 120 $parent = $this->get_post( $request['parent'] );120 $parent = get_post( $request['parent'] ); 121 121 if ( ! $parent ) { 122 122 return true; 123 123 } … … 139 139 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 140 140 */ 141 141 public function get_items( $request ) { 142 143 $parent = $this->get_post( $request['parent'] ); 142 $parent = get_post( $request['parent'] ); 144 143 if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) { 145 144 return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent id.' ), array( 'status' => 404 ) ); 146 145 } … … 178 177 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 179 178 */ 180 179 public function get_item( $request ) { 181 182 $parent = $this->get_post( $request['parent'] ); 180 $parent = get_post( $request['parent'] ); 183 181 if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) { 184 182 return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent id.' ), array( 'status' => 404 ) ); 185 183 } 186 184 187 $revision = $this->get_post( $request['id'] );185 $revision = get_post( $request['id'] ); 188 186 if ( ! $revision || 'revision' !== $revision->post_type ) { 189 187 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision id.' ), array( 'status' => 404 ) ); 190 188 } … … 209 207 return $response; 210 208 } 211 209 212 $post = $this->get_post( $request['id'] );210 $post = get_post( $request['id'] ); 213 211 if ( ! $post ) { 214 212 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision id.' ), array( 'status' => 404 ) ); 215 213 } … … 234 232 return new WP_Error( 'rest_trash_not_supported', __( 'Revisions do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); 235 233 } 236 234 237 $revision = $this->get_post( $request['id'] );235 $revision = get_post( $request['id'] ); 238 236 $previous = $this->prepare_item_for_response( $revision, $request ); 239 237 240 238 $result = wp_delete_post( $request['id'], true ); -
tests/phpunit/tests/rest-api/rest-controller.php
190 190 191 191 $this->assertEquals( 'a', $args['somedefault']['default'] ); 192 192 } 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 }217 193 }