← All changes
|
json-endpoints/class.wpcom-json-api-list-comments-endpoint.php
+35
-13
12.3.2
→
16.3-beta
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 { |
| @@ -325,15 +335,27 @@ | ||
| 325 | 335 | } |
| 326 | 336 | $query['parent'] = $comment_id; |
| 327 | 337 | } |
| 328 | 338 | |
| 339 | + // A single post's count uses the comment_post_ID index; site-wide typed counts would not. | |
| 340 | + if ( -1 === $found && $post_id ) { | |
| 341 | + $count_query = $query; | |
| 342 | + $count_query['count'] = true; | |
| 343 | + unset( $count_query['number'], $count_query['offset'] ); | |
| 344 | + $found = (int) get_comments( $count_query ); | |
| 345 | + } | |
| 346 | + | |
| 347 | + if ( $args['hierarchical'] && $found > 5000 ) { | |
| 348 | + // Massive comment thread found; don't pre-load comment metadata to reduce memory used. | |
| 349 | + $query['update_comment_meta_cache'] = false; | |
| 350 | + } | |
| 351 | + | |
| 329 | 352 | $comments = get_comments( $query ); |
| 330 | 353 | |
| 331 | - update_comment_cache( $comments ); | |
| 332 | - | |
| 333 | 354 | if ( $args['hierarchical'] ) { |
| 334 | 355 | $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'] ); | |
| 356 | + $comment_ids = $walker->paged_walk( $comments, get_option( 'thread_comments_depth', -1 ), $args['page'] ?? 1, $args['number'] ); | |
| 357 | + '@phan-var int[] $comment_ids'; | |
| 336 | 358 | if ( ! empty( $comment_ids ) ) { |
| 337 | 359 | $comments = array_map( 'get_comment', $comment_ids ); |
| 338 | 360 | } |
| 339 | 361 | } |