PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | json-endpoints/class.wpcom-json-api-list-comments-endpoint.php +26 -13 12.2.3 → 16.3-a.1 View file →
@@ -1,6 +1,10 @@
1 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 2
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit( 0 );
5 +}
6 +
3 7 /**
4 8 * Comments Walker Class.
5 9 */
6 10 class WPCOM_JSON_API_List_Comments_Walker extends Walker {
@@ -136,8 +140,10 @@
136 140 * /sites/%s/posts/%d/replies/ -> $blog_id, $post_id
137 141 * /sites/%s/comments/%d/replies/ -> $blog_id, $comment_id
138 142 *
139 143 * @todo permissions
144 + *
145 + * @phan-constructor-used-for-side-effects
140 146 */
141 147 class WPCOM_JSON_API_List_Comments_Endpoint extends WPCOM_JSON_API_Comment_Endpoint { // phpcs:ignore
142 148
143 149 /**
@@ -160,22 +166,22 @@
160 166 parent::__construct( $args );
161 167 $this->query = array_merge(
162 168 $this->query,
163 169 array(
164 - 'number' => '(int=20) The number of comments to return. Limit: 100. When using hierarchical=1, number refers to the number of top-level comments returned.',
165 - 'offset' => '(int=0) 0-indexed offset. Not available if using hierarchical=1.',
166 - 'page' => '(int) Return the Nth 1-indexed page of comments. Takes precedence over the <code>offset</code> parameter. When using hierarchical=1, pagination is a bit different. See the note on the number parameter.',
167 - 'order' => array(
170 + 'number' => '(int=20) The number of comments to return. Limit: 100. When using hierarchical=1, number refers to the number of top-level comments returned.',
171 + 'offset' => '(int=0) 0-indexed offset. Not available if using hierarchical=1.',
172 + 'page' => '(int) Return the Nth 1-indexed page of comments. Takes precedence over the <code>offset</code> parameter. When using hierarchical=1, pagination is a bit different. See the note on the number parameter.',
173 + 'order' => array(
168 174 'DESC' => 'Return comments in descending order from newest to oldest.',
169 175 'ASC' => 'Return comments in ascending order from oldest to newest.',
170 176 ),
171 - 'hierarchical' => array(
177 + 'hierarchical' => array(
172 178 'false' => '',
173 179 'true' => '(BETA) Order the comment list hierarchically.',
174 180 ),
175 - 'after' => '(ISO 8601 datetime) Return comments dated on or after the specified datetime. Not available if using hierarchical=1.',
176 - 'before' => '(ISO 8601 datetime) Return comments dated on or before the specified datetime. Not available if using hierarchical=1.',
177 - 'type' => array(
181 + 'after' => '(ISO 8601 datetime) Return comments dated on or after the specified datetime. Not available if using hierarchical=1.',
182 + 'before' => '(ISO 8601 datetime) Return comments dated on or before the specified datetime. Not available if using hierarchical=1.',
183 + 'type' => array(
178 184 'any' => 'Return all comments regardless of type.',
179 185 'comment' => 'Return only regular comments.',
180 186 'trackback' => 'Return only trackbacks.',
181 187 'pingback' => 'Return only pingbacks.',
@@ -180,9 +186,9 @@
180 186 'trackback' => 'Return only trackbacks.',
181 187 'pingback' => 'Return only pingbacks.',
182 188 'pings' => 'Return both trackbacks and pingbacks.',
183 189 ),
184 - 'status' => array(
190 + 'status' => array(
185 191 'approved' => 'Return only approved comments.',
186 192 'unapproved' => 'Return only comments in the moderation queue.',
187 193 'spam' => 'Return only comments marked as spam.',
188 194 'trash' => 'Return only comments in the trash.',
@@ -187,8 +193,12 @@
187 193 'spam' => 'Return only comments marked as spam.',
188 194 'trash' => 'Return only comments in the trash.',
189 195 'all' => 'Return comments of all statuses.',
190 196 ),
197 + 'author_wpcom_data' => array(
198 + 'false' => 'Do not add wpcom_id and wpcom_login fields to comment author responses (default)',
199 + 'true' => 'Add wpcom_id and wpcom_login fields to comment author responses',
200 + ),
191 201 )
192 202 );
193 203 }
194 204
@@ -212,9 +222,9 @@
212 222 } elseif ( 100 < $args['number'] ) {
213 223 return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 );
214 224 }
215 225
216 - if ( false !== strpos( $path, '/posts/' ) ) {
226 + if ( str_contains( $path, '/posts/' ) ) {
217 227 // We're looking for comments of a particular post.
218 228 $post_id = $object_id;
219 229 $comment_id = 0;
220 230 } else {
@@ -307,8 +317,12 @@
307 317 $query['date_query']['after'] = $args['after_gmt'];
308 318 }
309 319 }
310 320 }
321 + if ( $args['hierarchical'] && $found > 5000 ) {
322 + // Massive comment thread found; don't pre-load comment metadata to reduce memory used.
323 + $query['update_comment_meta_cache'] = false;
324 + }
311 325
312 326 if ( $post_id ) {
313 327 $post = get_post( $post_id );
314 328 if ( ! $post || is_wp_error( $post ) ) {
@@ -327,13 +341,12 @@
327 341 }
328 342
329 343 $comments = get_comments( $query );
330 344
331 - update_comment_cache( $comments );
332 -
333 345 if ( $args['hierarchical'] ) {
334 346 $walker = new WPCOM_JSON_API_List_Comments_Walker();
335 - $comment_ids = $walker->paged_walk( $comments, get_option( 'thread_comments_depth', -1 ), isset( $args['page'] ) ? $args['page'] : 1, $args['number'] );
347 + $comment_ids = $walker->paged_walk( $comments, get_option( 'thread_comments_depth', -1 ), $args['page'] ?? 1, $args['number'] );
348 + '@phan-var int[] $comment_ids';
336 349 if ( ! empty( $comment_ids ) ) {
337 350 $comments = array_map( 'get_comment', $comment_ids );
338 351 }
339 352 }