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

class.wpcom-json-api-comment-endpoint.php (7 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
4
abstract class WPCOM_JSON_API_Comment_Endpoint extends WPCOM_JSON_API_Endpoint {
5
	public $comment_object_format = array(
6
		// explicitly document and cast all output
7
		'ID'          => '(int) The comment ID.',
8
		'post'        => "(object>post_reference) A reference to the comment's post.",
9
		'author'      => '(object>author) The author of the comment.',
10
		'date'        => "(ISO 8601 datetime) The comment's creation time.",
11
		'URL'         => '(URL) The full permalink URL to the comment.',
12
		'short_URL'   => '(URL) The wp.me short URL.',
13
		'content'     => '(HTML) <code>context</code> dependent.',
14
		'raw_content' => '(string) Raw comment content.',
15
		'status'      => array(
16
			'approved'   => 'The comment has been approved.',
17
			'unapproved' => 'The comment has been held for review in the moderation queue.',
18
			'spam'       => 'The comment has been marked as spam.',
19
			'trash'      => 'The comment is in the trash.',
20
		),
21
		'parent' => "(object>comment_reference|false) A reference to the comment's parent, if it has one.",
22
		'type'   => array(
23
			'comment'   => 'The comment is a regular comment.',
24
			'trackback' => 'The comment is a trackback.',
25
			'pingback'  => 'The comment is a pingback.',
26
			'review'    => 'The comment is a product review.',
27
		),
28
		'like_count'   => '(int) The number of likes for this comment.',
29
		'i_like'       => '(bool) Does the current user like this comment?',
30
		'meta'         => '(object) Meta data',
31
		'can_moderate' => '(bool) Whether current user can moderate the comment.',
32
	);
33
34
	// public $response_format =& $this->comment_object_format;
35
36
	function __construct( $args ) {
37
		if ( !$this->response_format ) {
38
			$this->response_format =& $this->comment_object_format;
39
		}
40
		parent::__construct( $args );
41
	}
42
43
	function get_comment( $comment_id, $context ) {
44
		global $blog_id;
45
46
		$comment = get_comment( $comment_id );
47
		if ( !$comment || is_wp_error( $comment ) ) {
48
			return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_comment'.

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...
49
		}
50
51
		$types = array( '', 'comment', 'pingback', 'trackback', 'review' );
52
		if ( !in_array( $comment->comment_type, $types ) ) {
53
			return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_comment'.

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...
54
		}
55
56
		$post = get_post( $comment->comment_post_ID );
57
		if ( !$post || is_wp_error( $post ) ) {
58
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_post'.

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...
59
		}
60
61
		$status = wp_get_comment_status( $comment->comment_ID );
62
63
		// Permissions
64
		switch ( $context ) {
65
		case 'edit' :
66
			if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) {
67
				return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

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...
68
			}
69
70
			$GLOBALS['post'] = $post;
71
			$comment         = get_comment_to_edit( $comment->comment_ID );
72
			foreach ( array( 'comment_author', 'comment_author_email', 'comment_author_url' ) as $field ) {
73
				$comment->$field = htmlspecialchars_decode( $comment->$field, ENT_QUOTES );
74
			}
75
			break;
76
		case 'display' :
77
			if ( 'approved' !== $status ) {
78
				$current_user_id = get_current_user_id();
79
				$user_can_read_comment = false;
80
				if ( $current_user_id && $comment->user_id && $current_user_id == $comment->user_id ) {
81
					$user_can_read_comment = true;
82
				} elseif (
83
					$comment->comment_author_email && $comment->comment_author
84
				&&
85
					isset( $this->api->token_details['user'] )
86
				&&
87
					isset( $this->api->token_details['user']['user_email'] )
88
				&&
89
					$this->api->token_details['user']['user_email'] === $comment->comment_author_email
90
				&&
91
					$this->api->token_details['user']['display_name'] === $comment->comment_author
92
				) {
93
					$user_can_read_comment = true;
94
				} else {
95
					$user_can_read_comment = current_user_can( 'edit_posts' );
96
				}
97
98
				if ( !$user_can_read_comment ) {
99
					return new WP_Error( 'unauthorized', 'User cannot read unapproved comment', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

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...
100
				}
101
			}
102
103
			$GLOBALS['post'] = $post;
104
			setup_postdata( $post );
105
			break;
106
		default :
107
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_context'.

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...
108
		}
109
110
		$can_view = $this->user_can_view_post( $post->ID );
111
		if ( !$can_view || is_wp_error( $can_view ) ) {
112
			return $can_view;
113
		}
114
115
		$GLOBALS['comment'] = $comment;
116
		$response           = array();
117
118
		foreach ( array_keys( $this->comment_object_format ) as $key ) {
119
			switch ( $key ) {
120
			case 'ID' :
121
				// explicitly cast all output
122
				$response[$key] = (int) $comment->comment_ID;
123
				break;
124
			case 'post' :
125
				$response[$key] = (object) array(
126
					'ID'   => (int) $post->ID,
127
					'title' => (string) get_the_title( $post->ID ),
128
					'type' => (string) $post->post_type,
129
					'link' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
130
				);
131
				break;
132
			case 'author' :
133
				$response[$key] = (object) $this->get_author( $comment, current_user_can( 'edit_comment', $comment->comment_ID ) );
134
				break;
135
			case 'date' :
136
				$response[$key] = (string) $this->format_date( $comment->comment_date_gmt, $comment->comment_date );
137
				break;
138
			case 'URL' :
139
				$response[$key] = (string) esc_url_raw( get_comment_link( $comment->comment_ID ) );
140
				break;
141
			case 'short_URL' :
142
				// @todo - pagination
143
				$response[$key] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) . "%23comment-{$comment->comment_ID}" );
144
				break;
145
			case 'content' :
146
				if ( 'display' === $context ) {
147
					ob_start();
148
					comment_text();
149
					$response[$key] = (string) ob_get_clean();
150
				} else {
151
					$response[$key] = (string) $comment->comment_content;
152
				}
153
				break;
154
			case 'raw_content':
155
				$response[$key] = (string) $comment->comment_content;
156
				break;
157
			case 'status' :
158
				$response[$key] = (string) $status;
159
				break;
160
			case 'parent' : // (object|false)
161
				if ( $comment->comment_parent ) {
162
					$parent = get_comment( $comment->comment_parent );
163
					$response[$key] = (object) array(
164
						'ID'   => (int) $parent->comment_ID,
165
						'type' => (string) ( $parent->comment_type ? $parent->comment_type : 'comment' ),
166
						'link' => (string) $this->links->get_comment_link( $blog_id, $parent->comment_ID ),
167
					);
168
				} else {
169
					$response[$key] = false;
170
				}
171
				break;
172
			case 'type' :
173
				$response[$key] = (string) ( $comment->comment_type ? $comment->comment_type : 'comment' );
174
				break;
175
			case 'like_count' :
176
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
177
					$response[ $key ] = (int) $this->api->comment_like_count( $blog_id, $post->ID, $comment->comment_ID );
0 ignored issues
show
The method comment_like_count() does not seem to exist on object<WPCOM_JSON_API>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
				}
179
				break;
180
			case 'i_like' :
181
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
182
					$response[ $key ] = (bool) Likes::comment_like_current_user_likes( $blog_id, $comment->comment_ID );
183
				}
184
				break;
185
			case 'meta' :
186
				$response[$key] = (object) array(
187
					'links' => (object) array(
188
						'self'    => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID ),
189
						'help'    => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'help' ),
190
						'site'    => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
191
						'post'    => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $comment->comment_post_ID ),
192
						'replies' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'replies/' ),
193
						'likes'   => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'likes/' ),
194
					),
195
				);
196
				break;
197
			case 'can_moderate':
198
				$response[ $key ] = (bool) current_user_can( 'edit_comment', $comment_id );
199
				break;
200
			}
201
		}
202
203
		unset( $GLOBALS['comment'], $GLOBALS['post'] );
204
		return $response;
205
	}
206
}
207
208