Completed
Push — branch-5.0 ( 360082...ee9cce )
by Jeremy
96:49 queued 85:16
created

result()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 25
nc 3
nop 0
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_JSON_API_Get_Comment_Backup_Endpoint extends Jetpack_JSON_API_Endpoint {
4
	// /sites/%s/comments/%d/backup      -> $blog_id, $comment_id
5
6
	protected $needed_capabilities = array(); // This endpoint is only accessible using a site token
7
	protected $comment_id;
8
9
	function validate_input( $comment_id ) {
10
		if ( empty( $comment_id ) || ! is_numeric( $comment_id ) ) {
11
			return new WP_Error( 'comment_id_not_specified', __( 'You must specify a Comment ID', 'jetpack' ), 400 );
12
		}
13
14
		$this->comment_id = intval( $comment_id );
15
16
		return true;
17
	}
18
19
	protected function result() {
20
		$comment = get_comment( $this->comment_id );
21
		if ( empty( $comment ) ) {
22
			return new WP_Error( 'comment_not_found', __( 'Comment not found', 'jetpack' ), 404 );
23
		}
24
25
		$allowed_keys = array(
26
			'comment_ID',
27
			'comment_post_ID',
28
			'comment_author',
29
			'comment_author_email',
30
			'comment_author_url',
31
			'comment_author_IP',
32
			'comment_date',
33
			'comment_date_gmt',
34
			'comment_content',
35
			'comment_karma',
36
			'comment_approved',
37
			'comment_agent',
38
			'comment_type',
39
			'comment_parent',
40
			'user_id',
41
		);
42
43
		$comment = array_intersect_key( $comment->to_array(), array_flip( $allowed_keys ) );
44
		$comment_meta = get_comment_meta( $comment['comment_ID'] );
45
46
		return array(
47
			'comment' => $comment,
48
			'meta'    => is_array( $comment_meta ) ? $comment_meta : array(),
49
		);
50
	}
51
52
}
53