- Timestamp:
- 01/26/2017 01:38:27 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r39922 r39954 64 64 65 65 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 66 'args' => array( 67 'id' => array( 68 'description' => __( 'Unique identifier for the object.' ), 69 'type' => 'integer', 70 ), 71 ), 66 72 array( 67 73 'methods' => WP_REST_Server::READABLE, … … 301 307 302 308 /** 309 * Get the comment, if the ID is valid. 310 * 311 * @since 4.7.2 312 * 313 * @param int $id Supplied ID. 314 * @return WP_Comment|WP_Error Comment object if ID is valid, WP_Error otherwise. 315 */ 316 protected function get_comment( $id ) { 317 $error = new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) ); 318 if ( (int) $id <= 0 ) { 319 return $error; 320 } 321 322 $id = (int) $id; 323 $comment = get_comment( $id ); 324 if ( empty( $comment ) ) { 325 return $error; 326 } 327 328 if ( ! empty( $comment->comment_post_ID ) ) { 329 $post = get_post( (int) $comment->comment_post_ID ); 330 if ( empty( $post ) ) { 331 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) ); 332 } 333 } 334 335 return $comment; 336 } 337 338 /** 303 339 * Checks if a given request has access to read the comment. 304 340 * … … 310 346 */ 311 347 public function get_item_permissions_check( $request ) { 312 $id = (int) $request['id']; 313 314 $comment = get_comment( $id ); 315 316 if ( ! $comment ) { 317 return true; 348 $comment = $this->get_comment( $request['id'] ); 349 if ( is_wp_error( $comment ) ) { 350 return $comment; 318 351 } 319 352 … … 345 378 */ 346 379 public function get_item( $request ) { 347 $id = (int) $request['id']; 348 349 $comment = get_comment( $id ); 350 if ( empty( $comment ) ) { 351 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) ); 352 } 353 354 if ( ! empty( $comment->comment_post_ID ) ) { 355 $post = get_post( $comment->comment_post_ID ); 356 if ( empty( $post ) ) { 357 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) ); 358 } 380 $comment = $this->get_comment( $request['id'] ); 381 if ( is_wp_error( $comment ) ) { 382 return $comment; 359 383 } 360 384 … … 631 655 */ 632 656 public function update_item_permissions_check( $request ) { 633 634 $id = (int) $request['id'];635 636 $comment = get_comment( $id );637 638 if ( $comment &&! $this->check_edit_permission( $comment ) ) {657 $comment = $this->get_comment( $request['id'] ); 658 if ( is_wp_error( $comment ) ) { 659 return $comment; 660 } 661 662 if ( ! $this->check_edit_permission( $comment ) ) { 639 663 return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this comment.' ), array( 'status' => rest_authorization_required_code() ) ); 640 664 } … … 653 677 */ 654 678 public function update_item( $request ) { 655 $id = (int) $request['id']; 656 657 $comment = get_comment( $id ); 658 659 if ( empty( $comment ) ) { 660 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) ); 661 } 679 $comment = $this->get_comment( $request['id'] ); 680 if ( is_wp_error( $comment ) ) { 681 return $comment; 682 } 683 684 $id = $comment->comment_ID; 662 685 663 686 if ( isset( $request['type'] ) && get_comment_type( $id ) !== $request['type'] ) { … … 751 774 */ 752 775 public function delete_item_permissions_check( $request ) { 753 $id = (int) $request['id']; 754 $comment = get_comment( $id ); 755 756 if ( ! $comment ) { 757 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) ); 776 $comment = $this->get_comment( $request['id'] ); 777 if ( is_wp_error( $comment ) ) { 778 return $comment; 758 779 } 759 780 … … 774 795 */ 775 796 public function delete_item( $request ) { 776 $id = (int) $request['id']; 797 $comment = $this->get_comment( $request['id'] ); 798 if ( is_wp_error( $comment ) ) { 799 return $comment; 800 } 801 777 802 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 778 779 $comment = get_comment( $id );780 781 if ( empty( $comment ) ) {782 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );783 }784 803 785 804 /**
Note: See TracChangeset
for help on using the changeset viewer.