- Timestamp:
- 10/30/2016 06:54:49 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r38864 r39015 1 1 <?php 2 3 2 /** 4 * Access comments 3 * REST API: WP_REST_Comments_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core controller used to access comments via the REST API. 12 * 13 * @since 4.7.0 14 * 15 * @see WP_REST_Controller 5 16 */ 6 17 class WP_REST_Comments_Controller extends WP_REST_Controller { … … 9 20 * Instance of a comment meta fields object. 10 21 * 22 * @since 4.7.0 11 23 * @access protected 12 24 * @var WP_REST_Comment_Meta_Fields … … 14 26 protected $meta; 15 27 28 /** 29 * Constructor. 30 * 31 * @since 4.7.0 32 * @access public 33 */ 16 34 public function __construct() { 17 35 $this->namespace = 'wp/v2'; … … 22 40 23 41 /** 24 * Register the routes for the objects of the controller. 42 * Registers the routes for the objects of the controller. 43 * 44 * @since 4.7.0 45 * @access public 25 46 */ 26 47 public function register_routes() { … … 73 94 74 95 /** 75 * Check if a given request has access to read comments 76 * 77 * @param WP_REST_Request $request Full details about the request. 78 * @return WP_Error|boolean 96 * Checks if a given request has access to read comments. 97 * 98 * @since 4.7.0 99 * @access public 100 * 101 * @param WP_REST_Request $request Full details about the request. 102 * @return WP_Error|bool True if the request has read access, error object otherwise. 79 103 */ 80 104 public function get_items_permissions_check( $request ) { … … 83 107 foreach ( (array) $request['post'] as $post_id ) { 84 108 $post = $this->get_post( $post_id ); 109 85 110 if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) { 86 111 return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); … … 98 123 $protected_params = array( 'author', 'author_exclude', 'karma', 'author_email', 'type', 'status' ); 99 124 $forbidden_params = array(); 125 100 126 foreach ( $protected_params as $param ) { 101 127 if ( 'status' === $param ) { … … 111 137 } 112 138 } 139 113 140 if ( ! empty( $forbidden_params ) ) { 114 141 return new WP_Error( 'rest_forbidden_param', sprintf( __( 'Query parameter not permitted: %s' ), implode( ', ', $forbidden_params ) ), array( 'status' => rest_authorization_required_code() ) ); … … 120 147 121 148 /** 122 * Get a list of comments. 123 * 124 * @param WP_REST_Request $request Full details about the request. 125 * @return WP_Error|WP_REST_Response 149 * Retrieves a list of comment items. 150 * 151 * @since 4.7.0 152 * @access public 153 * 154 * @param WP_REST_Request $request Full details about the request. 155 * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. 126 156 */ 127 157 public function get_items( $request ) { … … 130 160 $registered = $this->get_collection_params(); 131 161 132 // This array defines mappings between public API query parameters whose 133 // values are accepted as-passed, and their internal WP_Query parameter 134 // name equivalents (some are the same). Only values which are also 135 // present in $registered will be set. 162 /* 163 * This array defines mappings between public API query parameters whose 164 * values are accepted as-passed, and their internal WP_Query parameter 165 * name equivalents (some are the same). Only values which are also 166 * present in $registered will be set. 167 */ 136 168 $parameter_mappings = array( 137 169 'author' => 'author__in', … … 154 186 $prepared_args = array(); 155 187 156 // For each known parameter which is both registered and present in the request, 157 // set the parameter's value on the query $prepared_args. 188 /* 189 * For each known parameter which is both registered and present in the request, 190 * set the parameter's value on the query $prepared_args. 191 */ 158 192 foreach ( $parameter_mappings as $api_param => $wp_param ) { 159 193 if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { … … 176 210 177 211 $prepared_args['date_query'] = array(); 212 178 213 // Set before into date query. Date query must be specified as an array of an array. 179 214 if ( isset( $registered['before'], $request['before'] ) ) { … … 191 226 192 227 /** 193 * Filter arguments, before passing to WP_Comment_Query, when querying comments via the REST API. 194 * 195 * @see https://developer.wordpress.org/reference/classes/wp_comment_query/ 228 * Filters arguments, before passing to WP_Comment_Query, when querying comments via the REST API. 229 * 230 * @since 4.7.0 231 * 232 * @link https://developer.wordpress.org/reference/classes/wp_comment_query/ 196 233 * 197 234 * @param array $prepared_args Array of arguments for WP_Comment_Query. … … 204 241 205 242 $comments = array(); 243 206 244 foreach ( $query_result as $comment ) { 207 245 if ( ! $this->check_read_permission( $comment ) ) { … … 214 252 215 253 $total_comments = (int) $query->found_comments; 216 $max_pages = (int) $query->max_num_pages; 254 $max_pages = (int) $query->max_num_pages; 255 217 256 if ( $total_comments < 1 ) { 218 // Out-of-bounds, run the query again without LIMIT for total count 257 // Out-of-bounds, run the query again without LIMIT for total count. 219 258 unset( $prepared_args['number'], $prepared_args['offset'] ); 259 220 260 $query = new WP_Comment_Query; 221 261 $prepared_args['count'] = true; … … 230 270 231 271 $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); 272 232 273 if ( $request['page'] > 1 ) { 233 274 $prev_page = $request['page'] - 1; 275 234 276 if ( $prev_page > $max_pages ) { 235 277 $prev_page = $max_pages; 236 278 } 279 237 280 $prev_link = add_query_arg( 'page', $prev_page, $base ); 238 281 $response->link_header( 'prev', $prev_link ); 239 282 } 283 240 284 if ( $max_pages > $request['page'] ) { 241 285 $next_page = $request['page'] + 1; 242 286 $next_link = add_query_arg( 'page', $next_page, $base ); 287 243 288 $response->link_header( 'next', $next_link ); 244 289 } … … 248 293 249 294 /** 250 * Check if a given request has access to read the comment 251 * 252 * @param WP_REST_Request $request Full details about the request. 253 * @return WP_Error|boolean 295 * Checks if a given request has access to read the comment. 296 * 297 * @since 4.7.0 298 * @access public 299 * 300 * @param WP_REST_Request $request Full details about the request. 301 * @return WP_Error|bool True if the request has read access for the item, error object otherwise. 254 302 */ 255 303 public function get_item_permissions_check( $request ) { … … 280 328 281 329 /** 282 * Get a comment. 283 * 284 * @param WP_REST_Request $request Full details about the request. 285 * @return WP_Error|WP_REST_Response 330 * Retrieves a comment. 331 * 332 * @since 4.7.0 333 * @access public 334 * 335 * @param WP_REST_Request $request Full details about the request. 336 * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. 286 337 */ 287 338 public function get_item( $request ) { … … 307 358 308 359 /** 309 * Check if a given request has access to create a comment 310 * 311 * @param WP_REST_Request $request Full details about the request. 312 * @return WP_Error|boolean 360 * Checks if a given request has access to create a comment. 361 * 362 * @since 4.7.0 363 * @access public 364 * 365 * @param WP_REST_Request $request Full details about the request. 366 * @return WP_Error|bool True if the request has access to create items, error object otherwise. 313 367 */ 314 368 public function create_item_permissions_check( $request ) { … … 322 376 return new WP_Error( 'rest_comment_invalid_author', __( 'Comment author invalid.' ), array( 'status' => rest_authorization_required_code() ) ); 323 377 } 378 324 379 if ( isset( $request['karma'] ) && $request['karma'] > 0 && ! current_user_can( 'moderate_comments' ) ) { 325 380 return new WP_Error( 'rest_comment_invalid_karma', __( 'Sorry, you cannot set karma for comments.' ), array( 'status' => rest_authorization_required_code() ) ); 326 381 } 382 327 383 if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { 328 384 return new WP_Error( 'rest_comment_invalid_status', __( 'Sorry, you cannot set status for comments.' ), array( 'status' => rest_authorization_required_code() ) ); … … 355 411 356 412 /** 357 * Create a comment. 358 * 359 * @param WP_REST_Request $request Full details about the request. 360 * @return WP_Error|WP_REST_Response 413 * Creates a comment. 414 * 415 * @since 4.7.0 416 * @access public 417 * 418 * @param WP_REST_Request $request Full details about the request. 419 * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. 361 420 */ 362 421 public function create_item( $request ) { … … 366 425 367 426 $prepared_comment = $this->prepare_item_for_database( $request ); 427 368 428 if ( is_wp_error( $prepared_comment ) ) { 369 429 return $prepared_comment; 370 430 } 371 431 372 /* *432 /* 373 433 * Do not allow a comment to be created with an empty string for 374 * comment_content. 375 * See `wp_handle_comment_submission()`. 434 * comment_content. See wp_handle_comment_submission(). 376 435 */ 377 436 if ( '' === $prepared_comment['comment_content'] ) { … … 379 438 } 380 439 381 // Setting remaining values before wp_insert_comment so we can 382 // use wp_allow_comment(). 440 // Setting remaining values before wp_insert_comment so we can use wp_allow_comment(). 383 441 if ( ! isset( $prepared_comment['comment_date_gmt'] ) ) { 384 442 $prepared_comment['comment_date_gmt'] = current_time( 'mysql', true ); 385 443 } 386 444 387 // Set author data if the user's logged in 445 // Set author data if the user's logged in. 388 446 $missing_author = empty( $prepared_comment['user_id'] ) 389 447 && empty( $prepared_comment['comment_author'] ) … … 393 451 if ( is_user_logged_in() && $missing_author ) { 394 452 $user = wp_get_current_user(); 453 395 454 $prepared_comment['user_id'] = $user->ID; 396 455 $prepared_comment['comment_author'] = $user->display_name; … … 399 458 } 400 459 401 // Honor the discussion setting that requires a name and email address 402 // of the comment author. 460 // Honor the discussion setting that requires a name and email address of the comment author. 403 461 if ( get_option( 'require_name_email' ) ) { 404 462 if ( ! isset( $prepared_comment['comment_author'] ) && ! isset( $prepared_comment['comment_author_email'] ) ) { 405 463 return new WP_Error( 'rest_comment_author_data_required', __( 'Creating a comment requires valid author name and email values.' ), array( 'status' => 400 ) ); 406 464 } 465 407 466 if ( ! isset( $prepared_comment['comment_author'] ) ) { 408 467 return new WP_Error( 'rest_comment_author_required', __( 'Creating a comment requires a valid author name.' ), array( 'status' => 400 ) ); 409 468 } 469 410 470 if ( ! isset( $prepared_comment['comment_author_email'] ) ) { 411 471 return new WP_Error( 'rest_comment_author_email_required', __( 'Creating a comment requires a valid author email.' ), array( 'status' => 400 ) ); … … 416 476 $prepared_comment['comment_author_email'] = ''; 417 477 } 478 418 479 if ( ! isset( $prepared_comment['comment_author_url'] ) ) { 419 480 $prepared_comment['comment_author_url'] = ''; … … 427 488 428 489 if ( is_wp_error( $prepared_comment['comment_approved'] ) ) { 429 $error_code = $prepared_comment['comment_approved']->get_error_code();490 $error_code = $prepared_comment['comment_approved']->get_error_code(); 430 491 $error_message = $prepared_comment['comment_approved']->get_error_message(); 431 492 … … 442 503 443 504 /** 444 * Filter a comment before it is inserted via the REST API. 445 * 446 * Allows modification of the comment right before it is inserted via `wp_insert_comment`. 447 * 448 * @param array $prepared_comment The prepared comment data for `wp_insert_comment`. 505 * Filters a comment before it is inserted via the REST API. 506 * 507 * Allows modification of the comment right before it is inserted via wp_insert_comment(). 508 * 509 * @since 4.7.0 510 * 511 * @param array $prepared_comment The prepared comment data for wp_insert_comment(). 449 512 * @param WP_REST_Request $request Request used to insert the comment. 450 513 */ … … 452 515 453 516 $comment_id = wp_insert_comment( $prepared_comment ); 517 454 518 if ( ! $comment_id ) { 455 519 return new WP_Error( 'rest_comment_failed_create', __( 'Creating comment failed.' ), array( 'status' => 500 ) ); … … 458 522 if ( isset( $request['status'] ) ) { 459 523 $comment = get_comment( $comment_id ); 524 460 525 $this->handle_status_param( $request['status'], $comment ); 461 526 } 462 527 463 528 $schema = $this->get_item_schema(); 529 464 530 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 465 531 $meta_update = $this->meta->update_value( $request['meta'], $comment_id ); 532 466 533 if ( is_wp_error( $meta_update ) ) { 467 534 return $meta_update; … … 470 537 471 538 $comment = get_comment( $comment_id ); 539 472 540 $fields_update = $this->update_additional_fields_for_object( $comment, $request ); 541 473 542 if ( is_wp_error( $fields_update ) ) { 474 543 return $fields_update; … … 476 545 477 546 $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view'; 547 478 548 $request->set_param( 'context', $context ); 549 479 550 $response = $this->prepare_item_for_response( $comment, $request ); 480 551 $response = rest_ensure_response( $response ); 552 481 553 $response->set_status( 201 ); 482 554 $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) ); … … 485 557 * Fires after a comment is created or updated via the REST API. 486 558 * 559 * @since 4.7.0 560 * 487 561 * @param array $comment Comment as it exists in the database. 488 562 * @param WP_REST_Request $request The request sent to the API. 489 * @param bool ean$creating True when creating a comment, false when updating.563 * @param bool $creating True when creating a comment, false when updating. 490 564 */ 491 565 do_action( 'rest_insert_comment', $comment, $request, true ); … … 495 569 496 570 /** 497 * Check if a given request has access to update a comment 498 * 499 * @param WP_REST_Request $request Full details about the request. 500 * @return WP_Error|boolean 571 * Checks if a given REST request has access to update a comment. 572 * 573 * @since 4.7.0 574 * @access public 575 * 576 * @param WP_REST_Request $request Full details about the request. 577 * @return WP_Error|bool True if the request has access to update the item, error object otherwise. 501 578 */ 502 579 public function update_item_permissions_check( $request ) { … … 514 591 515 592 /** 516 * Edit a comment 517 * 518 * @param WP_REST_Request $request Full details about the request. 519 * @return WP_Error|WP_REST_Response 593 * Updates a comment. 594 * 595 * @since 4.7.0 596 * @access public 597 * 598 * @param WP_REST_Request $request Full details about the request. 599 * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. 520 600 */ 521 601 public function update_item( $request ) { … … 523 603 524 604 $comment = get_comment( $id ); 605 525 606 if ( empty( $comment ) ) { 526 607 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); … … 532 613 533 614 $prepared_args = $this->prepare_item_for_database( $request ); 615 534 616 if ( is_wp_error( $prepared_args ) ) { 535 617 return $prepared_args; … … 539 621 // Only the comment status is being changed. 540 622 $change = $this->handle_status_param( $request['status'], $comment ); 623 541 624 if ( ! $change ) { 542 625 return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment status failed.' ), array( 'status' => 500 ) ); … … 550 633 551 634 $updated = wp_update_comment( $prepared_args ); 635 552 636 if ( 0 === $updated ) { 553 637 return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) ); … … 560 644 561 645 $schema = $this->get_item_schema(); 646 562 647 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 563 648 $meta_update = $this->meta->update_value( $request['meta'], $id ); 649 564 650 if ( is_wp_error( $meta_update ) ) { 565 651 return $meta_update; … … 568 654 569 655 $comment = get_comment( $id ); 656 570 657 $fields_update = $this->update_additional_fields_for_object( $comment, $request ); 658 571 659 if ( is_wp_error( $fields_update ) ) { 572 660 return $fields_update; … … 574 662 575 663 $request->set_param( 'context', 'edit' ); 664 576 665 $response = $this->prepare_item_for_response( $comment, $request ); 577 666 … … 583 672 584 673 /** 585 * Check if a given request has access to delete a comment 586 * 587 * @param WP_REST_Request $request Full details about the request. 588 * @return WP_Error|boolean 674 * Checks if a given request has access to delete a comment. 675 * 676 * @since 4.7.0 677 * @access public 678 * 679 * @param WP_REST_Request $request Full details about the request. 680 * @return WP_Error|bool True if the request has access to delete the item, error object otherwise. 589 681 */ 590 682 public function delete_item_permissions_check( $request ) { 591 $id = (int) $request['id'];683 $id = (int) $request['id']; 592 684 $comment = get_comment( $id ); 685 593 686 if ( ! $comment ) { 594 687 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); 595 688 } 689 596 690 if ( ! $this->check_edit_permission( $comment ) ) { 597 691 return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you can not delete this comment.' ), array( 'status' => rest_authorization_required_code() ) ); … … 601 695 602 696 /** 603 * Delete a comment. 604 * 605 * @param WP_REST_Request $request Full details about the request. 606 * @return WP_Error|WP_REST_Response 697 * Deletes a comment. 698 * 699 * @since 4.7.0 700 * @access public 701 * 702 * @param WP_REST_Request $request Full details about the request. 703 * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. 607 704 */ 608 705 public function delete_item( $request ) { 609 $id = (int) $request['id'];706 $id = (int) $request['id']; 610 707 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 611 708 612 709 $comment = get_comment( $id ); 710 613 711 if ( empty( $comment ) ) { 614 712 return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); … … 616 714 617 715 /** 618 * Filter whether a comment is trashable.716 * Filters whether a comment can be trashed. 619 717 * 620 718 * Return false to disable trash support for the post. 621 719 * 622 * @param boolean $supports_trash Whether the post type support trashing. 720 * @since 4.7.0 721 * 722 * @param bool $supports_trash Whether the post type support trashing. 623 723 * @param WP_Post $comment The comment object being considered for trashing support. 624 724 */ … … 626 726 627 727 $request->set_param( 'context', 'edit' ); 728 628 729 $response = $this->prepare_item_for_response( $comment, $request ); 629 730 … … 631 732 $result = wp_delete_comment( $comment->comment_ID, true ); 632 733 } else { 633 // If we don't support trashing for this type, error out734 // If this type doesn't support trashing, error out. 634 735 if ( ! $supports_trash ) { 635 736 return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing.' ), array( 'status' => 501 ) ); … … 650 751 * Fires after a comment is deleted via the REST API. 651 752 * 652 * @param object $comment The deleted comment data. 753 * @since 4.7.0 754 * 755 * @param WP_Comment $comment The deleted comment data. 653 756 * @param WP_REST_Response $response The response returned from the API. 654 757 * @param WP_REST_Request $request The request sent to the API. … … 660 763 661 764 /** 662 * Prepare a single comment output for response. 663 * 664 * @param object $comment Comment object. 665 * @param WP_REST_Request $request Request object. 666 * @return WP_REST_Response $response 765 * Prepares a single comment output for response. 766 * 767 * @since 4.7.0 768 * @access public 769 * 770 * @param WP_Comment $comment Comment object. 771 * @param WP_REST_Request $request Request object. 772 * @return WP_REST_Response Response object. 667 773 */ 668 774 public function prepare_item_for_response( $comment, $request ) { … … 680 786 'date_gmt' => mysql_to_rfc3339( $comment->comment_date_gmt ), 681 787 'content' => array( 788 /** This filter is documented in wp-includes/comment-template.php */ 682 789 'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ), 683 790 'raw' => $comment->comment_content, … … 700 807 701 808 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 702 $data = $this->add_additional_fields_to_object( $data, $request );703 $data = $this->filter_response_by_context( $data, $context );704 705 // Wrap the data in a response object 809 $data = $this->add_additional_fields_to_object( $data, $request ); 810 $data = $this->filter_response_by_context( $data, $context ); 811 812 // Wrap the data in a response object. 706 813 $response = rest_ensure_response( $data ); 707 814 … … 709 816 710 817 /** 711 * Filter a comment returned from the API.818 * Filters a comment returned from the API. 712 819 * 713 820 * Allows modification of the comment right before it is returned. 714 821 * 715 * @param WP_REST_Response $response The response object. 716 * @param object $comment The original comment object. 717 * @param WP_REST_Request $request Request used to generate the response. 822 * @since 4.7.0 823 * 824 * @param WP_REST_Response $response The response object. 825 * @param WP_Comment $comment The original comment object. 826 * @param WP_REST_Request $request Request used to generate the response. 718 827 */ 719 828 return apply_filters( 'rest_prepare_comment', $response, $comment, $request ); … … 721 830 722 831 /** 723 * Prepare links for the request. 724 * 725 * @param object $comment Comment object. 832 * Prepares links for the request. 833 * 834 * @since 4.7.0 835 * @access protected 836 * 837 * @param WP_Comment $comment Comment object. 726 838 * @return array Links for the given comment. 727 839 */ … … 745 857 if ( 0 !== (int) $comment->comment_post_ID ) { 746 858 $post = $this->get_post( $comment->comment_post_ID ); 859 747 860 if ( ! empty( $post->ID ) ) { 748 861 $obj = get_post_type_object( $post->post_type ); … … 765 878 766 879 // Only grab one comment to verify the comment has children. 767 $comment_children = $comment->get_children( array( 'number' => 1, 'count' => true ) ); 880 $comment_children = $comment->get_children( array( 881 'number' => 1, 882 'count' => true 883 ) ); 884 768 885 if ( ! empty( $comment_children ) ) { 769 $args = array( 'parent' => $comment->comment_ID ); 886 $args = array( 887 'parent' => $comment->comment_ID 888 ); 889 770 890 $rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) ); 771 891 … … 779 899 780 900 /** 781 * Prepend internal property prefix to query parameters to match our response fields. 782 * 783 * @param string $query_param 784 * @return string $normalized 901 * Prepends internal property prefix to query parameters to match our response fields. 902 * 903 * @since 4.7.0 904 * @access protected 905 * 906 * @param string $query_param Query parameter. 907 * @return string The normalized query parameter. 785 908 */ 786 909 protected function normalize_query_param( $query_param ) { … … 809 932 810 933 /** 811 * Check comment_approved to set comment status for single comment output. 812 * 813 * @param string|int $comment_approved 814 * @return string $status 934 * Checks comment_approved to set comment status for single comment output. 935 * 936 * @since 4.7.0 937 * @access protected 938 * 939 * @param string|int $comment_approved comment status. 940 * @return string Comment status. 815 941 */ 816 942 protected function prepare_status_response( $comment_approved ) { … … 838 964 839 965 /** 840 * Prepare a single comment to be inserted into the database. 841 * 842 * @param WP_REST_Request $request Request object. 843 * @return array|WP_Error $prepared_comment 966 * Prepares a single comment to be inserted into the database. 967 * 968 * @since 4.7.0 969 * @access protected 970 * 971 * @param WP_REST_Request $request Request object. 972 * @return array|WP_Error Prepared comment, otherwise WP_Error object. 844 973 */ 845 974 protected function prepare_item_for_database( $request ) { 846 975 $prepared_comment = array(); 847 976 848 /* *977 /* 849 978 * Allow the comment_content to be set via the 'content' or 850 979 * the 'content.raw' properties of the Request object. … … 866 995 if ( isset( $request['author'] ) ) { 867 996 $user = new WP_User( $request['author'] ); 997 868 998 if ( $user->exists() ) { 869 999 $prepared_comment['user_id'] = $user->ID; … … 919 1049 } 920 1050 921 // Require 'comment_content' unless only the 'comment_status' is being 922 // updated. 1051 // Require 'comment_content' unless only the 'comment_status' is being updated. 923 1052 if ( ! empty( $prepared_comment ) && ! isset( $prepared_comment['comment_content'] ) ) { 924 1053 return new WP_Error( 'rest_comment_content_required', __( 'Missing comment content.' ), array( 'status' => 400 ) ); 925 1054 } 926 1055 1056 /** 1057 * Filters a comment after it is prepared for the database. 1058 * 1059 * Allows modification of the comment right after it is prepared for the database. 1060 * 1061 * @since 4.7.0 1062 * 1063 * @param array $prepared_comment The prepared comment data for `wp_insert_comment`. 1064 * @param WP_REST_Request $request The current request. 1065 */ 927 1066 return apply_filters( 'rest_preprocess_comment', $prepared_comment, $request ); 928 1067 } 929 1068 930 1069 /** 931 * Get the Comment's schema, conforming to JSON Schema 1070 * Retrieves the comment's schema, conforming to JSON Schema. 1071 * 1072 * @since 4.7.0 1073 * @access public 932 1074 * 933 1075 * @return array … … 1092 1234 1093 1235 /** 1094 * Get the query params for collections 1095 * 1096 * @return array 1236 * Retrieves the query params for collections. 1237 * 1238 * @since 4.7.0 1239 * @access public 1240 * 1241 * @return array Comments collection parameters. 1097 1242 */ 1098 1243 public function get_collection_params() { … … 1107 1252 'validate_callback' => 'rest_validate_request_arg', 1108 1253 ); 1254 1109 1255 $query_params['author'] = array( 1110 1256 'description' => __( 'Limit result set to comments assigned to specific user ids. Requires authorization.' ), … … 1112 1258 'type' => 'array', 1113 1259 ); 1260 1114 1261 $query_params['author_exclude'] = array( 1115 1262 'description' => __( 'Ensure result set excludes comments assigned to specific user ids. Requires authorization.' ), … … 1117 1264 'type' => 'array', 1118 1265 ); 1266 1119 1267 $query_params['author_email'] = array( 1120 1268 'default' => null, … … 1124 1272 'type' => 'string', 1125 1273 ); 1274 1126 1275 $query_params['before'] = array( 1127 1276 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.' ), … … 1130 1279 'validate_callback' => 'rest_validate_request_arg', 1131 1280 ); 1281 1132 1282 $query_params['exclude'] = array( 1133 1283 'description' => __( 'Ensure result set excludes specific ids.' ), … … 1136 1286 'sanitize_callback' => 'wp_parse_id_list', 1137 1287 ); 1288 1138 1289 $query_params['include'] = array( 1139 1290 'description' => __( 'Limit result set to specific ids.' ), … … 1142 1293 'sanitize_callback' => 'wp_parse_id_list', 1143 1294 ); 1295 1144 1296 $query_params['karma'] = array( 1145 1297 'default' => null, … … 1149 1301 'validate_callback' => 'rest_validate_request_arg', 1150 1302 ); 1303 1151 1304 $query_params['offset'] = array( 1152 1305 'description' => __( 'Offset the result set by a specific number of comments.' ), … … 1155 1308 'validate_callback' => 'rest_validate_request_arg', 1156 1309 ); 1310 1157 1311 $query_params['order'] = array( 1158 1312 'description' => __( 'Order sort attribute ascending or descending.' ), … … 1166 1320 ), 1167 1321 ); 1322 1168 1323 $query_params['orderby'] = array( 1169 1324 'description' => __( 'Sort collection by object attribute.' ), … … 1182 1337 ), 1183 1338 ); 1339 1184 1340 $query_params['parent'] = array( 1185 1341 'default' => array(), … … 1188 1344 'type' => 'array', 1189 1345 ); 1346 1190 1347 $query_params['parent_exclude'] = array( 1191 1348 'default' => array(), … … 1194 1351 'type' => 'array', 1195 1352 ); 1353 1196 1354 $query_params['post'] = array( 1197 1355 'default' => array(), … … 1200 1358 'sanitize_callback' => 'wp_parse_id_list', 1201 1359 ); 1360 1202 1361 $query_params['status'] = array( 1203 1362 'default' => 'approve', … … 1207 1366 'validate_callback' => 'rest_validate_request_arg', 1208 1367 ); 1368 1209 1369 $query_params['type'] = array( 1210 1370 'default' => 'comment', … … 1214 1374 'validate_callback' => 'rest_validate_request_arg', 1215 1375 ); 1376 1216 1377 return $query_params; 1217 1378 } 1218 1379 1219 1380 /** 1220 * Set the comment_status of a given comment object when creating or updating a comment. 1221 * 1222 * @param string|int $new_status 1223 * @param object $comment 1224 * @return boolean $changed 1381 * Sets the comment_status of a given comment object when creating or updating a comment. 1382 * 1383 * @since 4.7.0 1384 * @access protected 1385 * 1386 * @param string|int $new_status New comment status. 1387 * @param WP_Comment $comment Comment data. 1388 * @return bool Whether the status was changed. 1225 1389 */ 1226 1390 protected function handle_status_param( $new_status, $comment ) { … … 1262 1426 1263 1427 /** 1264 * Check if we can read a post.1428 * Checks if the post can be read. 1265 1429 * 1266 1430 * Correctly handles posts with the inherit status. 1267 1431 * 1268 * @param WP_Post $post Post Object. 1269 * @return boolean Can we read it? 1432 * @since 4.7.0 1433 * @access protected 1434 * 1435 * @param WP_Post $post Post Object. 1436 * @return bool Whether post can be read. 1270 1437 */ 1271 1438 protected function check_read_post_permission( $post ) { … … 1276 1443 1277 1444 /** 1278 * Check if we can read a comment. 1279 * 1280 * @param object $comment Comment object. 1281 * @return boolean Can we read it? 1445 * Checks if the comment can be read. 1446 * 1447 * @since 4.7.0 1448 * @access protected 1449 * 1450 * @param WP_Comment $comment Comment object. 1451 * @return bool Whether the comment can be read. 1282 1452 */ 1283 1453 protected function check_read_permission( $comment ) { … … 1307 1477 1308 1478 /** 1309 * Check if we can edit or delete a comment. 1310 * 1311 * @param object $comment Comment object. 1312 * @return boolean Can we edit or delete it? 1479 * Checks if a comment can be edited or deleted. 1480 * 1481 * @since 4.7.0 1482 * @access protected 1483 * 1484 * @param object $comment Comment object. 1485 * @return bool Whether the comment can be edited or deleted. 1313 1486 */ 1314 1487 protected function check_edit_permission( $comment ) {
Note: See TracChangeset
for help on using the changeset viewer.