Ticket #38701: 38701.diff
| File 38701.diff, 15.6 KB (added by , 10 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 … … 275 275 public function prepare_item_for_response( $post, $request ) { 276 276 $response = parent::prepare_item_for_response( $post, $request ); 277 277 $data = $response->get_data(); 278 $parent = (int) wp_get_post_parent_id( $post ); 278 279 279 280 $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); 280 281 $data['caption'] = $post->post_excerpt; … … 282 283 $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file'; 283 284 $data['mime_type'] = $post->post_mime_type; 284 285 $data['media_details'] = wp_get_attachment_metadata( $post->ID ); 285 $data['post'] = ! empty( $p ost->post_parent ) ? (int) $post->post_parent : null;286 $data['post'] = ! empty( $parent ) ? $parent : null; 286 287 $data['source_url'] = wp_get_attachment_url( $post->ID ); 287 288 288 289 // Ensure empty details is an empty object. -
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'] ) ) { 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 ) { 394 396 return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) ); 395 397 } 396 398 397 if ( 'trash' === $post ->post_status ) {399 if ( 'trash' === $post_status ) { 398 400 return new WP_Error( 'rest_comment_trash_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) ); 399 401 } 400 402 … … 871 873 } 872 874 873 875 if ( 0 !== (int) $comment->comment_post_ID ) { 874 $post = $this->get_post( $comment->comment_post_ID );876 $post = get_post( $comment->comment_post_ID ); 875 877 876 878 if ( ! empty( $post->ID ) ) { 877 879 $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 ) ); … … 772 772 } 773 773 774 774 // Otherwise, only trash if we haven't already. 775 if ( 'trash' === $post->post_status) {775 if ( 'trash' === get_post_status( $post ) ) { 776 776 return new WP_Error( 'rest_already_trashed', __( 'The post has already been deleted.' ), array( 'status' => 410 ) ); 777 777 } 778 778 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', '' ); … … 1289 1289 } 1290 1290 1291 1291 // 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 ) ) { 1293 1294 return true; 1294 1295 } 1295 1296 1296 $post_status_obj = get_post_status_object( $ post->post_status );1297 $post_status_obj = get_post_status_object( $status ); 1297 1298 if ( $post_status_obj && $post_status_obj->public ) { 1298 1299 return true; 1299 1300 } 1300 1301 1301 1302 // 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 ); 1304 1306 return $this->check_read_permission( $parent ); 1305 1307 } 1306 1308 … … 1308 1310 * If there isn't a parent, but the status is set to inherit, assume 1309 1311 * it's published (as per get_post_status()). 1310 1312 */ 1311 if ( 'inherit' === $ post->post_status ) {1313 if ( 'inherit' === $status ) { 1312 1314 return true; 1313 1315 } 1314 1316 … … 1429 1431 } 1430 1432 1431 1433 if ( ! empty( $schema['properties']['status'] ) ) { 1432 $data['status'] = $post->post_status;1434 $data['status'] = get_post_status( $post ); 1433 1435 } 1434 1436 1435 1437 if ( ! empty( $schema['properties']['type'] ) ) { … … 1493 1495 } 1494 1496 1495 1497 if ( ! empty( $schema['properties']['parent'] ) ) { 1496 $data['parent'] = (int) $post->post_parent;1498 $data['parent'] = (int) wp_get_post_parent_id( $post ); 1497 1499 } 1498 1500 1499 1501 if ( ! empty( $schema['properties']['menu_order'] ) ) { … … 1631 1633 1632 1634 $post_type_obj = get_post_type_object( $post->post_type ); 1633 1635 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 ) { 1635 1638 $links['up'] = array( 1636 'href' => rest_url( trailingslashit( $base ) . (int) $post->post_parent),1639 'href' => rest_url( trailingslashit( $base ) . $parent_id ), 1637 1640 'embeddable' => true, 1638 1641 ); 1639 1642 } -
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 ); … … 301 299 } 302 300 303 301 if ( ! empty( $schema['properties']['parent'] ) ) { 304 $data['parent'] = (int) $post->post_parent;302 $data['parent'] = (int) wp_get_post_parent_id( $post ); 305 303 } 306 304 307 305 if ( ! empty( $schema['properties']['slug'] ) ) {