Completed
Push — fix/nosara ( 09438e...93ba64 )
by
unknown
11:29
created

callback()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
eloc 9
nc 5
nop 2
1
<?php
2
3
class WPCOM_JSON_API_Get_Comments_Tree_Endpoint extends WPCOM_JSON_API_Endpoint {
4
	/**
5
	 * Retrieves a list of comment data for a given site.
6
	 *
7
	 * @param string $status Filter by status: all, approved, pending, spam or trash.
8
	 * @param int $start_at first comment to search from going back in time
9
	 *
10
	 * @return array
11
	 */
12
	public function get_site_tree( $status, $start_at = PHP_INT_MAX ) {
13
		global $wpdb;
14
		$max_comment_count = 10000;
15
		$total_count = $this->get_site_tree_total_count( $status );
16
		$db_status = $this->get_comment_db_status( $status );
17
18
		$db_comment_rows = $wpdb->get_results(
19
			$wpdb->prepare(
20
				"SELECT comment_ID, comment_post_ID, comment_parent " .
21
				"FROM $wpdb->comments AS comments " .
22
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
23
				"WHERE comment_type = '' AND comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
24
				"ORDER BY comment_ID DESC " .
25
				"LIMIT %d",
26
				(int) $start_at, $db_status, $db_status, $max_comment_count
27
			),
28
			ARRAY_N
29
		);
30
31
		// Avoid using anonymous function bellow in order to preserve PHP 5.2 compatibility.
32
		function array_map_all_as_ints( $comments ) {
33
			return array_map( 'intval', $comments );
34
		}
35
36
		return array(
37
			'comment_count' => intval( $total_count ),
38
			'comments_tree' => array_map( 'array_map_all_as_ints', $db_comment_rows ),
39
		);
40
	}
41
42
	/**
43
	 * Retrieves a total count of comments for the given site.
44
	 *
45
	 * @param string $status Filter by status: all, approved, pending, spam or trash.
46
	 *
47
	 * @return int Total count of comments for a site.
48
	 */
49
	public function get_site_tree_total_count( $status ) {
50
		global $wpdb;
51
		$db_status = $this->get_comment_db_status( $status );
52
53
		return $wpdb->get_var(
54
			$wpdb->prepare(
55
				"SELECT COUNT(1) " .
56
				"FROM $wpdb->comments AS comments " .
57
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
58
				"WHERE comment_type = '' AND ( %s = 'all' OR comment_approved = %s )",
59
				$db_status, $db_status
60
			)
61
		);
62
	}
63
64
	/**
65
	 * Ensure a valid status is converted to a database-supported value if necessary.
66
	 *
67
	 * @param string $status Should be one of: all, approved, pending, spam or trash.
68
	 *
69
	 * @return string Corresponding value that exists in database.
70
	 */
71
	public function get_comment_db_status( $status ) {
72
		if ( 'approved' === $status ) {
73
			return '1';
74
		}
75
		if ( 'pending' === $status ) {
76
			return '0';
77
		}
78
		return $status;
79
	}
80
81
	public function validate_status_param( $status ) {
82
		return in_array( $status, array( 'all', 'approved', 'pending', 'spam', 'trash' ) );
83
	}
84
85
	// /sites/%s/comments-tree
86
	function callback( $path = '', $blog_id = 0 ) {
87
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
88
		if ( is_wp_error( $blog_id ) ) {
89
			return $blog_id;
90
		}
91
92
		$args = $this->query_args();
93
		$comment_status = empty( $args['status'] ) ? 'all' : $args['status'];
94
95
		if ( ! $this->validate_status_param( $comment_status ) ) {
96
			return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$comment_status'.", 400 );
97
		}
98
99
		return $this->get_site_tree( $comment_status );
100
	}
101
}
102