← All changes
|
json-endpoints/class.wpcom-json-api-get-comment-counts-endpoint.php
+91
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,91 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Endpoint: /sites/%s/comment-counts | |
| 4 | + */ | |
| 5 | + | |
| 6 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | + exit( 0 ); | |
| 8 | +} | |
| 9 | + | |
| 10 | +new WPCOM_JSON_API_GET_Comment_Counts_Endpoint( | |
| 11 | + array( | |
| 12 | + 'description' => 'Get comment counts for each available status', | |
| 13 | + 'group' => 'comments', | |
| 14 | + 'stat' => 'comments:1:comment-counts', | |
| 15 | + 'method' => 'GET', | |
| 16 | + 'path' => '/sites/%s/comment-counts', | |
| 17 | + 'path_labels' => array( | |
| 18 | + '$site' => '(int|string) Site ID or domain', | |
| 19 | + ), | |
| 20 | + | |
| 21 | + 'query_parameters' => array( | |
| 22 | + 'post_id' => '(int) post ID for filtering the comment counts by post', | |
| 23 | + ), | |
| 24 | + | |
| 25 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comment-counts', | |
| 26 | + | |
| 27 | + 'response_format' => array( | |
| 28 | + 'all' => '(int) Combined number of approved and unapproved comments', | |
| 29 | + 'approved' => '(int) Number of approved comments', | |
| 30 | + 'pending' => '(int) Number of unapproved comments', | |
| 31 | + 'trash' => '(int) Number of trash comments', | |
| 32 | + 'spam' => '(int) Number of spam comments', | |
| 33 | + 'post_trashed' => '(int) Number of comments whose parent post has been trashed', | |
| 34 | + 'total_comments' => '(int) Combined number of comments in each category', | |
| 35 | + ), | |
| 36 | + ) | |
| 37 | +); | |
| 38 | + | |
| 39 | +/** | |
| 40 | + * GET Comment Counts endpoint class. | |
| 41 | + * | |
| 42 | + * @phan-constructor-used-for-side-effects | |
| 43 | + */ | |
| 44 | +class WPCOM_JSON_API_GET_Comment_Counts_Endpoint extends WPCOM_JSON_API_Endpoint { | |
| 45 | + /** | |
| 46 | + * | |
| 47 | + * API callback. | |
| 48 | + * | |
| 49 | + * @param string $path - the path. | |
| 50 | + * @param int $blog_id - the blog ID. | |
| 51 | + */ | |
| 52 | + public function callback( $path = '', $blog_id = 0 ) { | |
| 53 | + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); | |
| 54 | + | |
| 55 | + if ( is_wp_error( $blog_id ) ) { | |
| 56 | + return $blog_id; | |
| 57 | + } | |
| 58 | + | |
| 59 | + if ( ! get_current_user_id() ) { | |
| 60 | + return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment counts.', 403 ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + if ( ! current_user_can_for_site( $blog_id, 'edit_posts' ) ) { | |
| 64 | + return new WP_Error( 'authorization_required', 'You are not authorized to view comment counts for this blog.', 403 ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + $args = $this->query_args(); | |
| 68 | + | |
| 69 | + // If 0 is passed wp_count_comments will default to fetching counts for the whole site. | |
| 70 | + $post_id = ! empty( $args['post_id'] ) ? (int) $args['post_id'] : 0; | |
| 71 | + | |
| 72 | + // Check if post with given id exists. | |
| 73 | + if ( ! empty( $post_id ) && ! is_object( get_post( $post_id ) ) ) { | |
| 74 | + return new WP_Error( 'invalid_input', 'Provided post_id does not exist', 400 ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + $comment_counts = get_object_vars( $this->api->wp_count_comments( $post_id ) ); | |
| 78 | + | |
| 79 | + // Keys coming from wp_count_comments don't match the ones that we use in | |
| 80 | + // wp-admin and Calypso and are not consistent. Let's normalize the response. | |
| 81 | + return array( | |
| 82 | + 'all' => (int) $comment_counts['all'], | |
| 83 | + 'approved' => (int) $comment_counts['approved'], | |
| 84 | + 'pending' => (int) $comment_counts['moderated'], | |
| 85 | + 'trash' => (int) $comment_counts['trash'], | |
| 86 | + 'spam' => (int) $comment_counts['spam'], | |
| 87 | + 'post_trashed' => (int) $comment_counts['post-trashed'], | |
| 88 | + 'total_comments' => (int) $comment_counts['total_comments'], | |
| 89 | + ); | |
| 90 | + } | |
| 91 | +} | |