class.wpcom-json-api-get-comment-counts-endpoint.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. new WPCOM_JSON_API_GET_Comment_Counts_Endpoint( array(
  3. 'description' => 'Get comment counts for each available status',
  4. 'group' => 'comments',
  5. 'stat' => 'comments:1:comment-counts',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/comment-counts',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. ),
  11. 'query_parameters' => array(
  12. 'post_id' => '(int) post ID for filtering the comment counts by post',
  13. ),
  14. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comment-counts',
  15. 'response_format' => array(
  16. 'all' => '(int) Combined number of approved and unapproved comments',
  17. 'approved' => '(int) Number of approved comments',
  18. 'pending' => '(int) Number of unapproved comments',
  19. 'trash' => '(int) Number of trash comments',
  20. 'spam' => '(int) Number of spam comments',
  21. 'post_trashed' => '(int) Number of comments whose parent post has been trashed',
  22. 'total_comments' => '(int) Combined number of comments in each category',
  23. )
  24. ) );
  25. class WPCOM_JSON_API_GET_Comment_Counts_Endpoint extends WPCOM_JSON_API_Endpoint {
  26. // /sites/%s/comment-counts
  27. public function callback( $path = '', $blog_id = 0 ) {
  28. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  29. if ( is_wp_error( $blog_id ) ) {
  30. return $blog_id;
  31. }
  32. if ( ! get_current_user_id() ) {
  33. return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment counts.', 403 );
  34. }
  35. if ( ! current_user_can_for_blog( $blog_id, 'edit_posts' ) ) {
  36. return new WP_Error( 'authorization_required', 'You are not authorized to view comment counts for this blog.', 403 );
  37. }
  38. $args = $this->query_args();
  39. // If 0 is passed wp_count_comments will default to fetching counts for the whole site.
  40. $post_id = ! empty( $args['post_id'] ) ? intval( $args['post_id'] ) : 0;
  41. // Check if post with given id exists.
  42. if ( ! empty( $post_id ) && ! is_object( get_post( $post_id ) ) ) {
  43. return new WP_Error( 'invalid_input', 'Provided post_id does not exist', 400 );
  44. }
  45. $comment_counts = get_object_vars( wp_count_comments( $post_id ) );
  46. // Keys coming from wp_count_comments don't match the ones that we use in
  47. // wp-admin and Calypso and are not consistent. Let's normalize the response.
  48. return array(
  49. 'all' => (int) $comment_counts['all'],
  50. 'approved' => (int) $comment_counts['approved'],
  51. 'pending' => (int) $comment_counts['moderated'],
  52. 'trash' => (int) $comment_counts['trash'],
  53. 'spam' => (int) $comment_counts['spam'],
  54. 'post_trashed' => (int) $comment_counts['post-trashed'],
  55. 'total_comments' => (int) $comment_counts['total_comments']
  56. );
  57. }
  58. }