Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
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
106
		$db_status = $this->get_comment_db_status( $status );
107
		$type      = $this->get_sanitized_comment_type( $type );
108
109
		/*
110
		 * An empty value in the comments_type column denotes a regular comment in WP 5.5-
111
		 * @to-do: remove empty value logic when the minimum required WP is 5.5.
112
		 */
113
		$type = ( 'comment' === $type ) ? 'comment' : $type;
114
115
		$result = $wpdb->get_var(
116
			$wpdb->prepare(
117
				"SELECT COUNT(1) " .
118
				"FROM $wpdb->comments AS comments " .
119
				"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
120
				"WHERE comment_type %s AND ( %s = 'all' OR comment_approved = %s )",
121
				( 'comment' === $type ? "in ( '', 'comment' )" : '= ' . $type ),
122
				$db_status,
123
				$db_status
124
			)
125
		);
126
		return intval( $result );
127
	}
128
129
	/**
130
	 * Ensure a valid status is converted to a database-supported value if necessary.
131
	 *
132
	 * @param string $status Should be one of: all, approved, pending, spam or trash.
133
	 *
134
	 * @return string Corresponding value that exists in database.
135
	 */
136
	function get_comment_db_status( $status ) {
137
		if ( 'approved' === $status ) {
138
			return '1';
139
		}
140
		if ( 'pending' === $status || 'unapproved' === $status ) {
141
			return '0';
142
		}
143
		return $status;
144
	}
145
146
	/**
147
	 * Determine if the passed comment status is valid or not.
148
	 *
149
	 * @param string $status
150
	 *
151
	 * @return boolean
152
	 */
153
	function validate_status_param( $status ) {
154
		return in_array( $status, array( 'all', 'approved', 'unapproved', 'pending', 'spam', 'trash' ) );
155
	}
156
157
	/**
158
	 * Sanitize a given comment type.
159
	 *
160
	 * @param string Comment type: can be 'trackback', 'pingback', or 'comment'.
161
	 *
162
	 * @return string Sanitized comment type.
163
	 */
164
	function get_sanitized_comment_type( $type = 'comment' ) {
165
		if ( in_array( $type, array( 'trackback', 'pingback', 'comment' ) ) ) {
166
			return $type;
167
		}
168
		return 'comment';
169
	}
170
171
	/**
172
	 * Endpoint callback for /sites/%s/comments-tree
173
	 *
174
	 * @param string $path
175
	 * @param int    $blog_id
176
	 *
177
	 * @return array Site tree results by status.
178
	 */
179
	function callback( $path = '', $blog_id = 0 ) {
180
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
181
		if ( is_wp_error( $blog_id ) ) {
182
			return $blog_id;
183
		}
184
185
		$args = $this->query_args();
186
		$comment_status = empty( $args['status'] ) ? 'all' : $args['status'];
187
188
		if ( ! $this->validate_status_param( $comment_status ) ) {
189
			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...
190
		}
191
192
		return $this->get_site_tree( $comment_status );
193
	}
194
}
195