Completed
Push — jetpack-fusion-mock-files ( e51750...3b1561 )
by
unknown
13:31
created

callback()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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