- Timestamp:
- 01/26/2017 01:38:27 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r39671 r39954 89 89 } 90 90 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 91 'args' => array( 92 'id' => array( 93 'description' => __( 'Unique identifier for the object.' ), 94 'type' => 'integer', 95 ), 96 ), 91 97 array( 92 98 'methods' => WP_REST_Server::READABLE, … … 351 357 352 358 /** 359 * Get the post, if the ID is valid. 360 * 361 * @since 4.7.2 362 * 363 * @param int $id Supplied ID. 364 * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. 365 */ 366 protected function get_post( $id ) { 367 $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) ); 368 if ( (int) $id <= 0 ) { 369 return $error; 370 } 371 372 $post = get_post( (int) $id ); 373 if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { 374 return $error; 375 } 376 377 return $post; 378 } 379 380 /** 353 381 * Checks if a given request has access to read a post. 354 382 * … … 360 388 */ 361 389 public function get_item_permissions_check( $request ) { 362 363 $post = get_post( (int) $request['id'] ); 390 $post = $this->get_post( $request['id'] ); 391 if ( is_wp_error( $post ) ) { 392 return $post; 393 } 364 394 365 395 if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) { … … 429 459 */ 430 460 public function get_item( $request ) { 431 $id = (int) $request['id']; 432 $post = get_post( $id ); 433 434 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { 435 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) ); 461 $post = $this->get_post( $request['id'] ); 462 if ( is_wp_error( $post ) ) { 463 return $post; 436 464 } 437 465 … … 440 468 441 469 if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) { 442 $response->link_header( 'alternate', get_permalink( $ id), array( 'type' => 'text/html' ) );470 $response->link_header( 'alternate', get_permalink( $post->ID ), array( 'type' => 'text/html' ) ); 443 471 } 444 472 … … 456 484 */ 457 485 public function create_item_permissions_check( $request ) { 486 if ( ! empty( $request['id'] ) ) { 487 return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) ); 488 } 458 489 459 490 $post_type = get_post_type_object( $this->post_type ); … … 592 623 */ 593 624 public function update_item_permissions_check( $request ) { 594 595 $post = get_post( $request['id'] ); 625 $post = $this->get_post( $request['id'] ); 626 if ( is_wp_error( $post ) ) { 627 return $post; 628 } 629 596 630 $post_type = get_post_type_object( $this->post_type ); 597 631 … … 625 659 */ 626 660 public function update_item( $request ) { 627 $id = (int) $request['id']; 628 $post = get_post( $id ); 629 630 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { 631 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) ); 661 $valid_check = $this->get_post( $request['id'] ); 662 if ( is_wp_error( $valid_check ) ) { 663 return $valid_check; 632 664 } 633 665 … … 715 747 */ 716 748 public function delete_item_permissions_check( $request ) { 717 718 $post = get_post( $request['id'] ); 749 $post = $this->get_post( $request['id'] ); 750 if ( is_wp_error( $post ) ) { 751 return $post; 752 } 719 753 720 754 if ( $post && ! $this->check_delete_permission( $post ) ) { … … 735 769 */ 736 770 public function delete_item( $request ) { 737 $id = (int) $request['id']; 771 $post = $this->get_post( $request['id'] ); 772 if ( is_wp_error( $post ) ) { 773 return $post; 774 } 775 776 $id = $post->ID; 738 777 $force = (bool) $request['force']; 739 740 $post = get_post( $id );741 742 if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {743 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );744 }745 778 746 779 $supports_trash = ( EMPTY_TRASH_DAYS > 0 ); … … 902 935 // Post ID. 903 936 if ( isset( $request['id'] ) ) { 904 $prepared_post->ID = absint( $request['id'] ); 937 $existing_post = $this->get_post( $request['id'] ); 938 if ( is_wp_error( $existing_post ) ) { 939 return $existing_post; 940 } 941 942 $prepared_post->ID = $existing_post->ID; 905 943 } 906 944
Note: See TracChangeset
for help on using the changeset viewer.