Completed
Push — fix/unify-packages-package-tag... ( a9c353 )
by Marin
06:50
created

...s.wpcom-json-api-get-comments-tree-endpoint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_Get_Comments_Tree_Endpoint( array(
4
	'description' => 'Get a comments tree for site.',
5
	'max_version' => '1',
6
	'new_version' => '1.1',
7
	'group'       => 'comments-tree',
8
	'stat'        => 'comments-tree:1',
9
10
	'method'      => 'GET',
11
	'path'        =>  '/sites/%s/comments-tree',
12
	'path_labels' => array(
13
		'$site'   => '(int|string) Site ID or domain',
14
	),
15
	'query_parameters' => array(
16
		'status' => '(string) Filter returned comments based on this value (allowed values: all, approved, unapproved, pending, trash, spam).'
17
	),
18
	'response_format' => array(
19
		'comments_count' => '(int) Total number of comments on the site',
20
		'comments_tree' => '(array) Array of arrays representing the comments tree for given site (max 50000)',
21
		'trackbacks_count' => '(int) Total number of trackbacks on the site',
22
		'trackbacks_tree' => '(array) Array of arrays representing the trackbacks tree for given site (max 50000)',
23
		'pingbacks_count' => '(int) Total number of pingbacks on the site',
24
		'pingbacks_tree' => '(array) Array of arrays representing the pingbacks tree for given site (max 50000)',
25
	),
26
27
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comments-tree?status=approved'
28
) );
29
30
class WPCOM_JSON_API_Get_Comments_Tree_Endpoint extends WPCOM_JSON_API_Endpoint {
31
	/**
32
	 * Retrieves a list of comment data for a given site.
33
	 *
34
	 * @param string $status Filter by status: all, approved, pending, spam or trash.
35
	 * @param int $start_at first comment to search from going back in time
36
	 *
37
	 * @return array
38
	 */
39
	function get_site_tree( $status, $start_at = PHP_INT_MAX ) {
40
		global $wpdb;
41
		$max_comment_count = 50000;
42
		$db_status = $this->get_comment_db_status( $status );
43
44
		$db_comment_rows = $wpdb->get_results(
45
			$wpdb->prepare(
46
				"SELECT comment_ID, comment_post_ID, comment_parent, comment_type " .
47
				"FROM $wpdb->comments AS comments " .
48
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
49
				"WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
50
				"ORDER BY comment_ID DESC " .
51
				"LIMIT %d",
52
				(int) $start_at, $db_status, $db_status, $max_comment_count
53
			),
54
			ARRAY_N
55
		);
56
57
		$comments = array();
58
		$trackbacks = array();
59
		$pingbacks = array();
60
		foreach ( $db_comment_rows as $row ) {
61
			list( $comment_id, $comment_post_id, $comment_parent, $comment_type ) = $row;
62
			switch ( $comment_type ) {
63
				case 'trackback':
64
					$trackbacks[] = array( $comment_id, $comment_post_id, $comment_parent );
65
					break;
66
				case 'pingback':
67
					$pingbacks[] = array( $comment_id, $comment_post_id, $comment_parent );
68
					break;
69
				default:
70
					$comments[] = array( $comment_id, $comment_post_id, $comment_parent );
71
			}
72
		}
73
74
		return array(
75
			'comments_count' => $this->get_site_tree_total_count( $status, 'comment' ),
76
			'comments_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $comments ),
77
			'trackbacks_count' => $this->get_site_tree_total_count( $status, 'trackback' ),
78
			'trackbacks_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $trackbacks ),
79
			'pingbacks_count' => $this->get_site_tree_total_count( $status, 'pingback' ),
80
			'pingbacks_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $pingbacks ),
81
		);
82
	}
83
84
	/**
85
	 * Ensure all values are integers.
86
	 *
87
	 * @param array $comments Collection of comments.
88
	 *
89
	 * @return array Comments with values as integers.
90
	 */
91
	function array_map_all_as_ints( $comments ) {
92
		return array_map( 'intval', $comments );
93
	}
94
95
	/**
96
	 * Retrieves a total count of comments by type for the given site.
97
	 *
98
	 * @param string $status Filter by status: all, approved, pending, spam or trash.
99
	 * @param string $type Comment type: 'trackback', 'pingback', or 'comment'.
100
	 *
101
	 * @return int Total count of comments for a site.
102
	 */
103
	function get_site_tree_total_count( $status, $type ) {
104
		global $wpdb;
105
		$db_status = $this->get_comment_db_status( $status );
106
		$type = $this->get_sanitized_comment_type( $type );
107
		// An empty value in the comments_type column denotes a regular comment.
108
		$type = ( 'comment' === $type ) ? '' : $type;
109
110
		$result = $wpdb->get_var(
111
			$wpdb->prepare(
112
				"SELECT COUNT(1) " .
113
				"FROM $wpdb->comments AS comments " .
114
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
115
				"WHERE comment_type = %s AND ( %s = 'all' OR comment_approved = %s )",
116
				$type, $db_status, $db_status
117
			)
118
		);
119
		return intval( $result );
120
	}
121
122
	/**
123
	 * Ensure a valid status is converted to a database-supported value if necessary.
124
	 *
125
	 * @param string $status Should be one of: all, approved, pending, spam or trash.
126
	 *
127
	 * @return string Corresponding value that exists in database.
128
	 */
129
	function get_comment_db_status( $status ) {
130
		if ( 'approved' === $status ) {
131
			return '1';
132
		}
133
		if ( 'pending' === $status || 'unapproved' === $status ) {
134
			return '0';
135
		}
136
		return $status;
137
	}
138
139
	/**
140
	 * Determine if the passed comment status is valid or not.
141
	 *
142
	 * @param string $status
143
	 *
144
	 * @return boolean
145
	 */
146
	function validate_status_param( $status ) {
147
		return in_array( $status, array( 'all', 'approved', 'unapproved', 'pending', 'spam', 'trash' ) );
148
	}
149
150
	/**
151
	 * Sanitize a given comment type.
152
	 *
153
	 * @param string Comment type: can be 'trackback', 'pingback', or 'comment'.
154
	 *
155
	 * @return string Sanitized comment type.
156
	 */
157
	function get_sanitized_comment_type( $type = 'comment' ) {
158
		if ( in_array( $type, array( 'trackback', 'pingback', 'comment' ) ) ) {
159
			return $type;
160
		}
161
		return 'comment';
162
	}
163
164
	/**
165
	 * Endpoint callback for /sites/%s/comments-tree
166
	 *
167
	 * @param string $path
168
	 * @param int    $blog_id
169
	 *
170
	 * @return array Site tree results by status.
171
	 */
172
	function callback( $path = '', $blog_id = 0 ) {
173
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
174
		if ( is_wp_error( $blog_id ) ) {
175
			return $blog_id;
176
		}
177
178
		$args = $this->query_args();
179
		$comment_status = empty( $args['status'] ) ? 'all' : $args['status'];
180
181
		if ( ! $this->validate_status_param( $comment_status ) ) {
182
			return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$comment_status'.", 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_status'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
183
		}
184
185
		return $this->get_site_tree( $comment_status );
186
	}
187
}
188