Completed
Push — add/comments-tree-endpoint ( 62a633 )
by
unknown
11:35
created

WPCOM_JSON_API_Get_Comments_Tree_Endpoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
B get_site_tree() 0 26 1
A get_site_tree_total_count() 0 14 1
A get_comment_db_status() 0 9 3
A validate_status_param() 0 3 1
A callback() 0 15 3
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' | 'trash'
0 ignored issues
show
Documentation introduced by
The doc-type {string} could not be parsed: Unknown type name "{string}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
8
	 * @param   {int}    $start_at first comment to search from going back in time
0 ignored issues
show
Documentation introduced by
The doc-type {int} could not be parsed: Unknown type name "{int}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
9
	 * @returns {array}            list of comment information matching query
10
	 */
11
	public function get_site_tree( $status, $start_at = PHP_INT_MAX ) {
12
		global $wpdb;
13
		$max_comment_count = 10000;
14
		$total_count = $this->get_site_tree_total_count( $status );
15
		$db_status = $this->get_comment_db_status( $status );
16
17
		$db_comments = $wpdb->get_results(
18
			$wpdb->prepare(
19
				"SELECT comment_ID, comment_post_ID, comment_parent " .
20
				"FROM $wpdb->comments AS comments " .
21
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
22
				"WHERE comment_type = '' AND comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
23
				"ORDER BY comment_ID DESC " .
24
				"LIMIT %d",
25
				(int) $start_at, $db_status, $db_status, $max_comment_count
26
			),
27
			ARRAY_N
28
		);
29
30
		return array(
31
			$total_count,
32
			array_map( function( $comment ) {
33
				return array_map( 'intval', $comment );
34
			}, $db_comments )
35
		);
36
	}
37
38
	/**
39
	 * Retrieves a total count of comments for the given site.
40
	 *
41
	 * @param   {string} $status   filter by status: 'all' | 'approved' | 'pending' | 'spam' | 'trash'
0 ignored issues
show
Documentation introduced by
The doc-type {string} could not be parsed: Unknown type name "{string}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
42
	 * @returns {int}              total count of comments for a site
43
	 */
44
	public function get_site_tree_total_count( $status ) {
45
		global $wpdb;
46
		$db_status = $this->get_comment_db_status( $status );
47
48
		return $wpdb->get_var(
49
			$wpdb->prepare(
50
				"SELECT COUNT(1) " .
51
				"FROM $wpdb->comments AS comments " .
52
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
53
				"WHERE comment_type = '' AND ( %s = 'all' OR comment_approved = %s )",
54
				$db_status, $db_status
55
			)
56
		);
57
	}
58
59
	/**
60
	 * Ensure a valid status is converted to a database-supported value if necessary.
61
	 *
62
	 * @param   {string} $status 'all' | 'approved' | 'pending' | 'spam' | 'trash'
0 ignored issues
show
Documentation introduced by
The doc-type {string} could not be parsed: Unknown type name "{string}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
63
	 * @returns {string}         value that exists in database
64
	 */
65
	public function get_comment_db_status( $status ) {
66
		if ( 'approved' === $status ) {
67
			$status = '1';
68
		}
69
		if ( 'pending' === $status ) {
70
			$status = '0';
71
		}
72
		return $status;
73
	}
74
75
	public function validate_status_param( $status ) {
76
		return in_array( $status, array( 'all', 'approved', 'pending', 'spam', 'trash' ) );
77
	}
78
79
	// /sites/%s/comments-tree
80
	function callback( $path = '', $blog_id = 0 ) {
81
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
82
		if ( is_wp_error( $blog_id ) ) {
83
			return $blog_id;
84
		}
85
86
		$args = $this->query_args();
87
		$comment_status = $args['status'];
88
89
		if ( ! $this->validate_status_param( $comment_status ) ) {
90
			return new WP_Error( 'invalid_status', 'Invalid comment status value provided ', 400 );
91
		}
92
93
		return $this->get_site_tree( $comment_status );
94
	}
95
}
96