Changeset 39957 for branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
- Timestamp:
- 01/26/2017 01:46:54 PM (8 years ago)
- Location:
- branches/4.7
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.7
-
branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r39631 r39957 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 … … 625 649 */ 626 650 public function update_item_permissions_check( $request ) { 627 628 $id = (int) $request['id'];629 630 $comment = get_comment( $id );631 632 if ( $comment &&! $this->check_edit_permission( $comment ) ) {651 $comment = $this->get_comment( $request['id'] ); 652 if ( is_wp_error( $comment ) ) { 653 return $comment; 654 } 655 656 if ( ! $this->check_edit_permission( $comment ) ) { 633 657 return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this comment.' ), array( 'status' => rest_authorization_required_code() ) ); 634 658 } … … 647 671 */ 648 672 public function update_item( $request ) { 649 $id = (int) $request['id']; 650 651 $comment = get_comment( $id ); 652 653 if ( empty( $comment ) ) { 654 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) ); 655 } 673 $comment = $this->get_comment( $request['id'] ); 674 if ( is_wp_error( $comment ) ) { 675 return $comment; 676 } 677 678 $id = $comment->comment_ID; 656 679 657 680 if ( isset( $request['type'] ) && get_comment_type( $id ) !== $request['type'] ) { … … 745 768 */ 746 769 public function delete_item_permissions_check( $request ) { 747 $id = (int) $request['id']; 748 $comment = get_comment( $id ); 749 750 if ( ! $comment ) { 751 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) ); 770 $comment = $this->get_comment( $request['id'] ); 771 if ( is_wp_error( $comment ) ) { 772 return $comment; 752 773 } 753 774 … … 768 789 */ 769 790 public function delete_item( $request ) { 770 $id = (int) $request['id']; 791 $comment = $this->get_comment( $request['id'] ); 792 if ( is_wp_error( $comment ) ) { 793 return $comment; 794 } 795 771 796 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 772 773 $comment = get_comment( $id );774 775 if ( empty( $comment ) ) {776 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment ID.' ), array( 'status' => 404 ) );777 }778 797 779 798 /**
Note: See TracChangeset
for help on using the changeset viewer.