summaryrefslogtreecommitdiff
blob: fdc3bee7282dd0c2ce3ce7b387fe3da075d8c0de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

new WPCOM_JSON_API_Get_Comments_Tree_Endpoint( array(
	'description' => 'Get a comments tree for site.',
	'max_version' => '1',
	'new_version' => '1.1',
	'group'       => 'comments-tree',
	'stat'        => 'comments-tree:1',

	'method'      => 'GET',
	'path'        =>  '/sites/%s/comments-tree',
	'path_labels' => array(
		'$site'   => '(int|string) Site ID or domain',
	),
	'query_parameters' => array(
		'status' => '(string) Filter returned comments based on this value (allowed values: all, approved, unapproved, pending, trash, spam).'
	),
	'response_format' => array(
		'comments_count' => '(int) Total number of comments on the site',
		'comments_tree' => '(array) Array of arrays representing the comments tree for given site (max 50000)',
		'trackbacks_count' => '(int) Total number of trackbacks on the site',
		'trackbacks_tree' => '(array) Array of arrays representing the trackbacks tree for given site (max 50000)',
		'pingbacks_count' => '(int) Total number of pingbacks on the site',
		'pingbacks_tree' => '(array) Array of arrays representing the pingbacks tree for given site (max 50000)',
	),

	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comments-tree?status=approved'
) );

class WPCOM_JSON_API_Get_Comments_Tree_Endpoint extends WPCOM_JSON_API_Endpoint {
	/**
	 * Retrieves a list of comment data for a given site.
	 *
	 * @param string $status Filter by status: all, approved, pending, spam or trash.
	 * @param int $start_at first comment to search from going back in time
	 *
	 * @return array
	 */
	function get_site_tree( $status, $start_at = PHP_INT_MAX ) {
		global $wpdb;
		$max_comment_count = 50000;
		$db_status = $this->get_comment_db_status( $status );

		$db_comment_rows = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT comment_ID, comment_post_ID, comment_parent, comment_type " .
				"FROM $wpdb->comments AS comments " .
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
				"WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
				"ORDER BY comment_ID DESC " .
				"LIMIT %d",
				(int) $start_at, $db_status, $db_status, $max_comment_count
			),
			ARRAY_N
		);

		$comments = array();
		$trackbacks = array();
		$pingbacks = array();
		foreach ( $db_comment_rows as $row ) {
			list( $comment_id, $comment_post_id, $comment_parent, $comment_type ) = $row;
			switch ( $comment_type ) {
				case 'trackback':
					$trackbacks[] = array( $comment_id, $comment_post_id, $comment_parent );
					break;
				case 'pingback':
					$pingbacks[] = array( $comment_id, $comment_post_id, $comment_parent );
					break;
				default:
					$comments[] = array( $comment_id, $comment_post_id, $comment_parent );
			}
		}

		return array(
			'comments_count' => $this->get_site_tree_total_count( $status, 'comment' ),
			'comments_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $comments ),
			'trackbacks_count' => $this->get_site_tree_total_count( $status, 'trackback' ),
			'trackbacks_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $trackbacks ),
			'pingbacks_count' => $this->get_site_tree_total_count( $status, 'pingback' ),
			'pingbacks_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $pingbacks ),
		);
	}

	/**
	 * Ensure all values are integers.
	 *
	 * @param array $comments Collection of comments.
	 *
	 * @return array Comments with values as integers.
	 */
	function array_map_all_as_ints( $comments ) {
		return array_map( 'intval', $comments );
	}

	/**
	 * Retrieves a total count of comments by type for the given site.
	 *
	 * @param string $status Filter by status: all, approved, pending, spam or trash.
	 * @param string $type Comment type: 'trackback', 'pingback', or 'comment'.
	 *
	 * @return int Total count of comments for a site.
	 */
	function get_site_tree_total_count( $status, $type ) {
		global $wpdb;
		$db_status = $this->get_comment_db_status( $status );
		$type = $this->get_sanitized_comment_type( $type );
		// An empty value in the comments_type column denotes a regular comment.
		$type = ( 'comment' === $type ) ? '' : $type;

		$result = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT COUNT(1) " .
				"FROM $wpdb->comments AS comments " .
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
				"WHERE comment_type = %s AND ( %s = 'all' OR comment_approved = %s )",
				$type, $db_status, $db_status
			)
		);
		return intval( $result );
	}

	/**
	 * Ensure a valid status is converted to a database-supported value if necessary.
	 *
	 * @param string $status Should be one of: all, approved, pending, spam or trash.
	 *
	 * @return string Corresponding value that exists in database.
	 */
	function get_comment_db_status( $status ) {
		if ( 'approved' === $status ) {
			return '1';
		}
		if ( 'pending' === $status || 'unapproved' === $status ) {
			return '0';
		}
		return $status;
	}

	/**
	 * Determine if the passed comment status is valid or not.
	 *
	 * @param string $status
	 *
	 * @return boolean
	 */
	function validate_status_param( $status ) {
		return in_array( $status, array( 'all', 'approved', 'unapproved', 'pending', 'spam', 'trash' ) );
	}

	/**
	 * Sanitize a given comment type.
	 *
	 * @param string Comment type: can be 'trackback', 'pingback', or 'comment'.
	 *
	 * @return string Sanitized comment type.
	 */
	function get_sanitized_comment_type( $type = 'comment' ) {
		if ( in_array( $type, array( 'trackback', 'pingback', 'comment' ) ) ) {
			return $type;
		}
		return 'comment';
	}

	/**
	 * Endpoint callback for /sites/%s/comments-tree
	 *
	 * @param string $path
	 * @param int    $blog_id
	 *
	 * @return array Site tree results by status.
	 */
	function callback( $path = '', $blog_id = 0 ) {
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
		if ( is_wp_error( $blog_id ) ) {
			return $blog_id;
		}

		$args = $this->query_args();
		$comment_status = empty( $args['status'] ) ? 'all' : $args['status'];

		if ( ! $this->validate_status_param( $comment_status ) ) {
			return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$comment_status'.", 400 );
		}

		return $this->get_site_tree( $comment_status );
	}
}