Completed
Push — fix/7357 ( 13a0c4...764bff )
by
unknown
11:52
created

WPCOM_JSON_API_Comment_Endpoint::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
		),
27
		'like_count'   => '(int) The number of likes for this comment.',
28
		'i_like'       => '(bool) Does the current user like this comment?',
29
		'meta'         => '(object) Meta data',
30
		'can_moderate' => '(bool) Whether current user can moderate the comment.',
31
	);
32
33
	// public $response_format =& $this->comment_object_format;
34
35
	function __construct( $args ) {
36
		if ( !$this->response_format ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->response_format of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37
			$this->response_format =& $this->comment_object_format;
38
		}
39
		parent::__construct( $args );
40
	}
41
42
	function get_comment( $comment_id, $context ) {
43
		global $blog_id;
44
45
		$comment = get_comment( $comment_id );
46
		if ( !$comment || is_wp_error( $comment ) ) {
47
			return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
48
		}
49
50
		$types = array( '', 'comment', 'pingback', 'trackback' );
51
		if ( !in_array( $comment->comment_type, $types ) ) {
52
			return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
53
		}
54
55
		$post = get_post( $comment->comment_post_ID );
56
		if ( !$post || is_wp_error( $post ) ) {
57
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
58
		}
59
60
		$status = wp_get_comment_status( $comment->comment_ID );
61
62
		// Permissions
63
		switch ( $context ) {
64
		case 'edit' :
65
			if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) {
66
				return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
67
			}
68
69
			$GLOBALS['post'] = $post;
70
			$comment         = get_comment_to_edit( $comment->comment_ID );
71
			foreach ( array( 'comment_author', 'comment_author_email', 'comment_author_url' ) as $field ) {
72
				$comment->$field = htmlspecialchars_decode( $comment->$field, ENT_QUOTES );
73
			}
74
			break;
75
		case 'display' :
76
			if ( 'approved' !== $status ) {
77
				$current_user_id = get_current_user_id();
78
				$user_can_read_comment = false;
0 ignored issues
show
Unused Code introduced by
$user_can_read_comment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
				if ( $current_user_id && $comment->user_id && $current_user_id == $comment->user_id ) {
80
					$user_can_read_comment = true;
81
				} elseif (
82
					$comment->comment_author_email && $comment->comment_author
83
				&&
84
					isset( $this->api->token_details['user'] )
85
				&&
86
					isset( $this->api->token_details['user']['user_email'] )
87
				&&
88
					$this->api->token_details['user']['user_email'] === $comment->comment_author_email
89
				&&
90
					$this->api->token_details['user']['display_name'] === $comment->comment_author
91
				) {
92
					$user_can_read_comment = true;
93
				} else {
94
					$user_can_read_comment = current_user_can( 'edit_posts' );
95
				}
96
97
				if ( !$user_can_read_comment ) {
98
					return new WP_Error( 'unauthorized', 'User cannot read unapproved comment', 403 );
99
				}
100
			}
101
102
			$GLOBALS['post'] = $post;
103
			setup_postdata( $post );
104
			break;
105
		default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
106
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
107
		}
108
109
		$can_view = $this->user_can_view_post( $post->ID );
110
		if ( !$can_view || is_wp_error( $can_view ) ) {
111
			return $can_view;
112
		}
113
114
		$GLOBALS['comment'] = $comment;
115
		$response           = array();
116
117
		foreach ( array_keys( $this->comment_object_format ) as $key ) {
118
			switch ( $key ) {
119
			case 'ID' :
120
				// explicitly cast all output
121
				$response[$key] = (int) $comment->comment_ID;
122
				break;
123
			case 'post' :
124
				$response[$key] = (object) array(
125
					'ID'   => (int) $post->ID,
126
					'title' => (string) get_the_title( $post->ID ),
127
					'type' => (string) $post->post_type,
128
					'link' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
129
				);
130
				break;
131
			case 'author' :
132
				$response[$key] = (object) $this->get_author( $comment, current_user_can( 'edit_comment', $comment->comment_ID ) );
133
				break;
134
			case 'date' :
135
				$response[$key] = (string) $this->format_date( $comment->comment_date_gmt, $comment->comment_date );
136
				break;
137
			case 'URL' :
138
				$response[$key] = (string) esc_url_raw( get_comment_link( $comment->comment_ID ) );
139
				break;
140
			case 'short_URL' :
141
				// @todo - pagination
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
142
				$response[$key] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) . "%23comment-{$comment->comment_ID}" );
143
				break;
144
			case 'content' :
145
				if ( 'display' === $context ) {
146
					ob_start();
147
					comment_text();
148
					$response[$key] = (string) ob_get_clean();
149
				} else {
150
					$response[$key] = (string) $comment->comment_content;
151
				}
152
				break;
153
			case 'raw_content':
154
				$response[$key] = (string) $comment->comment_content;
155
				break;
156
			case 'status' :
157
				$response[$key] = (string) $status;
158
				break;
159
			case 'parent' : // (object|false)
160
				if ( $comment->comment_parent ) {
161
					$parent = get_comment( $comment->comment_parent );
162
					$response[$key] = (object) array(
163
						'ID'   => (int) $parent->comment_ID,
164
						'type' => (string) ( $parent->comment_type ? $parent->comment_type : 'comment' ),
165
						'link' => (string) $this->links->get_comment_link( $blog_id, $parent->comment_ID ),
166
					);
167
				} else {
168
					$response[$key] = false;
169
				}
170
				break;
171
			case 'type' :
172
				$response[$key] = (string) ( $comment->comment_type ? $comment->comment_type : 'comment' );
173
				break;
174
			case 'like_count' :
175
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
176
					$response[ $key ] = (int) $this->api->comment_like_count( $blog_id, $post->ID, $comment->comment_ID );
0 ignored issues
show
Bug introduced by
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...
177
				}
178
				break;
179
			case 'i_like' :
180
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
181
					$response[ $key ] = (bool) Likes::comment_like_current_user_likes( $blog_id, $comment->comment_ID );
182
				}
183
				break;
184
			case 'meta' :
185
				$response[$key] = (object) array(
186
					'links' => (object) array(
187
						'self'    => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID ),
188
						'help'    => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'help' ),
189
						'site'    => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
190
						'post'    => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $comment->comment_post_ID ),
191
						'replies' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'replies/' ),
192
						'likes'   => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'likes/' ),
193
					),
194
				);
195
				break;
196
			case 'can_moderate':
197
				$response[ $key ] = (bool) current_user_can( 'edit_comment', $comment_id );
198
				break;
199
			}
200
		}
201
202
		unset( $GLOBALS['comment'], $GLOBALS['post'] );
203
		return $response;
204
	}
205
}
206
207