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

...wpcom-json-api-get-comment-history-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_History_Endpoint( array(
4
	'description'   => 'Get the audit history for given comment',
5
	'group'         => 'comments',
6
	'stat'          => 'comments:1:comment-history',
7
	'method'        => 'GET',
8
	'path'          => '/sites/%s/comment-history/%d',
9
	'path_labels'   => array(
10
		'$site'       => '(int|string) Site ID or domain',
11
		'$comment_ID' => '(int) The comment ID'
12
	),
13
14
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comment-history/11',
15
16
	'response_format' => array(
17
		'comment_history' => '(array) Array of arrays representing the comment history objects.'
18
	)
19
) );
20
21
class WPCOM_JSON_API_GET_Comment_History_Endpoint extends WPCOM_JSON_API_Endpoint {
22
23
	// /sites/%s/comment-history/%d
24
	public function callback( $path = '', $blog_id = 0, $comment_id = 0 ) {
25
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
26
27
		if ( is_wp_error( $blog_id ) ) {
28
			return $blog_id;
29
		}
30
31
		if ( ! get_current_user_id() ) {
32
			return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment history.', 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...
33
		}
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 history on 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...
37
		}
38
39
		if ( ! method_exists( 'Akismet', 'get_comment_history' ) ) {
40
			return new WP_Error( 'akismet_required', 'Akismet plugin must be active for this feature to work', 503 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'akismet_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...
41
		}
42
43
		$comment_history = Akismet::get_comment_history( $comment_id );
44
45
		foreach ( $comment_history as &$item ) {
46
			// Times are stored as floating point values in microseconds.
47
			// We don't need that precision on the client so let's get rid of the decimal part.
48
			$item['time'] = intval( $item['time'] );
49
		}
50
51
		return array( 'comment_history' => $comment_history );
52
	}
53
}
54