Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

....wpcom-json-api-get-comment-counts-endpoint.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
		'all'            => '(int) Combined number of approved and unapproved comments',
21
		'approved'       => '(int) Number of approved comments',
22
		'pending'        => '(int) Number of unapproved comments',
23
		'trash'          => '(int) Number of trash comments',
24
		'spam'           => '(int) Number of spam comments',
25
		'post_trashed'   => '(int) Number of comments whose parent post has been trashed',
26
		'total_comments' => '(int) Combined number of comments in each category',
27
	)
28
) );
29
30
class WPCOM_JSON_API_GET_Comment_Counts_Endpoint extends WPCOM_JSON_API_Endpoint {
31
32
	// /sites/%s/comment-counts
33
	public function callback( $path = '', $blog_id = 0 ) {
34
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
35
36
		if ( is_wp_error( $blog_id ) ) {
37
			return $blog_id;
38
		}
39
40
		if ( ! get_current_user_id() ) {
41
			return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment counts.', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'authorization_required'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
		}
43
44
		if ( ! current_user_can_for_blog( $blog_id, 'edit_posts' ) ) {
45
			return new WP_Error( 'authorization_required', 'You are not authorized to view comment counts for this blog.', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'authorization_required'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
		}
47
48
		$args = $this->query_args();
49
50
		// If 0 is passed wp_count_comments will default to fetching counts for the whole site.
51
		$post_id = ! empty( $args['post_id'] ) ? intval( $args['post_id'] ) : 0;
52
53
		// Check if post with given id exists.
54
		if ( ! empty( $post_id ) && ! is_object( get_post( $post_id ) ) ) {
55
			return new WP_Error( 'invalid_input', 'Provided post_id does not exist', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_input'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
		}
57
58
		$comment_counts = get_object_vars( $this->api->wp_count_comments( $post_id ) );
59
60
		// Keys coming from wp_count_comments don't match the ones that we use in
61
		// wp-admin and Calypso and are not consistent. Let's normalize the response.
62
		return array(
63
			'all'            => (int) $comment_counts['all'],
64
			'approved'       => (int) $comment_counts['approved'],
65
			'pending'        => (int) $comment_counts['moderated'],
66
			'trash'          => (int) $comment_counts['trash'],
67
			'spam'           => (int) $comment_counts['spam'],
68
			'post_trashed'   => (int) $comment_counts['post-trashed'],
69
			'total_comments' => (int) $comment_counts['total_comments']
70
		);
71
	}
72
}
73