WPCOM_JSON_API_List_Comments_Endpoint

The Jetpack by WordPress.com WPCOM JSON API List Comments Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php  
  1. class WPCOM_JSON_API_List_Comments_Endpoint extends WPCOM_JSON_API_Comment_Endpoint { 
  2. public $response_format = array( 
  3. 'found' => '(int) The total number of comments found that match the request (ignoring limits, offsets, and pagination).',  
  4. 'site_ID' => '(int) The site ID',  
  5. 'comments' => '(array:comment) An array of comment objects.',  
  6. ); 
  7.  
  8. function __construct( $args ) { 
  9. parent::__construct( $args ); 
  10. $this->query = array_merge( $this->query, array( 
  11. '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.',  
  12. 'offset' => '(int=0) 0-indexed offset. Not available if using hierarchical=1.',  
  13. '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.',  
  14. 'order' => array( 
  15. 'DESC' => 'Return comments in descending order from newest to oldest.',  
  16. 'ASC' => 'Return comments in ascending order from oldest to newest.',  
  17. ),  
  18. 'hierarchical' => array( 
  19. 'false' => '',  
  20. 'true' => '(BETA) Order the comment list hierarchically.',  
  21. ),  
  22. 'after' => '(ISO 8601 datetime) Return comments dated on or after the specified datetime. Not available if using hierarchical=1.',  
  23. 'before' => '(ISO 8601 datetime) Return comments dated on or before the specified datetime. Not available if using hierarchical=1.',  
  24. 'type' => array( 
  25. 'any' => 'Return all comments regardless of type.',  
  26. 'comment' => 'Return only regular comments.',  
  27. 'trackback' => 'Return only trackbacks.',  
  28. 'pingback' => 'Return only pingbacks.',  
  29. 'pings' => 'Return both trackbacks and pingbacks.',  
  30. ),  
  31. 'status' => array( 
  32. 'approved' => 'Return only approved comments.',  
  33. 'unapproved' => 'Return only comments in the moderation queue.',  
  34. 'spam' => 'Return only comments marked as spam.',  
  35. 'trash' => 'Return only comments in the trash.',  
  36. 'all' => 'Return comments of all statuses.',  
  37. ),  
  38. ) ); 
  39.  
  40. // /sites/%s/comments/ -> $blog_id 
  41. // /sites/%s/posts/%d/replies/ -> $blog_id, $post_id 
  42. // /sites/%s/comments/%d/replies/ -> $blog_id, $comment_id 
  43. function callback( $path = '', $blog_id = 0, $object_id = 0 ) { 
  44. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  45. if ( is_wp_error( $blog_id ) ) { 
  46. return $blog_id; 
  47.  
  48. $args = $this->query_args(); 
  49.  
  50. if ( $args['number'] < 1 ) { 
  51. $args['number'] = 20; 
  52. } elseif ( 100 < $args['number'] ) { 
  53. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 ); 
  54.  
  55. if ( false !== strpos( $path, '/posts/' ) ) { 
  56. // We're looking for comments of a particular post 
  57. $post_id = $object_id; 
  58. $comment_id = 0; 
  59. } else { 
  60. // We're looking for comments for the whole blog, or replies to a single comment 
  61. $comment_id = $object_id; 
  62. $post_id = 0; 
  63.  
  64. // We can't efficiently get the number of replies to a single comment 
  65. $count = false; 
  66. $found = -1; 
  67.  
  68. if ( !$comment_id ) { 
  69. // We can get comment counts for the whole site or for a single post, but only for certain queries 
  70. if ( 'any' === $args['type'] && !isset( $args['after'] ) && !isset( $args['before'] ) ) { 
  71. $count = wp_count_comments( $post_id ); 
  72.  
  73. switch ( $args['status'] ) { 
  74. case 'approved' : 
  75. $status = 'approve'; 
  76. if ( $count ) { 
  77. $found = $count->approved; 
  78. break; 
  79. default : 
  80. if ( !current_user_can( 'moderate_comments' ) ) { 
  81. return new WP_Error( 'unauthorized', 'User cannot read non-approved comments', 403 ); 
  82. if ( 'unapproved' === $args['status'] ) { 
  83. $status = 'hold'; 
  84. $count_status = 'moderated'; 
  85. } elseif ( 'all' === $args['status'] ) { 
  86. $status = 'all'; 
  87. $count_status = 'total_comments'; 
  88. } else { 
  89. $status = $count_status = $args['status']; 
  90. if ( $count ) { 
  91. $found = $count->$count_status; 
  92.  
  93. $query = array( 
  94. 'order' => $args['order'],  
  95. 'type' => 'any' === $args['type'] ? false : $args['type'],  
  96. 'status' => $status,  
  97. ); 
  98.  
  99. if ( isset( $args['page'] ) ) { 
  100. if ( $args['page'] < 1 ) { 
  101. $args['page'] = 1; 
  102. } else { 
  103. if ( $args['offset'] < 0 ) { 
  104. $args['offset'] = 0; 
  105.  
  106. if ( ! $args['hierarchical'] ) { 
  107. $query['number'] = $args['number']; 
  108.  
  109. if ( isset( $args['page'] ) ) { 
  110. $query['offset'] = ( $args['page'] - 1 ) * $args['number']; 
  111. } else { 
  112. $query['offset'] = $args['offset']; 
  113.  
  114. $is_before = isset( $args['before_gmt'] ); 
  115. $is_after = isset( $args['after_gmt'] ); 
  116.  
  117. if ( $is_before || $is_after ) { 
  118. $query['date_query'] = array( 
  119. 'column' => 'comment_date_gmt',  
  120. 'inclusive' => true,  
  121. ); 
  122.  
  123. if ( $is_before ) { 
  124. $query['date_query']['before'] = $args['before_gmt']; 
  125.  
  126. if ( $is_after ) { 
  127. $query['date_query']['after'] = $args['after_gmt']; 
  128.  
  129. if ( $post_id ) { 
  130. $post = get_post( $post_id ); 
  131. if ( !$post || is_wp_error( $post ) ) { 
  132. return new WP_Error( 'unknown_post', 'Unknown post', 404 ); 
  133. $query['post_id'] = $post->ID; 
  134. if ( $this->api->ends_with( $this->path, '/replies' ) ) { 
  135. $query['parent'] = 0; 
  136. } elseif ( $comment_id ) { 
  137. $comment = get_comment( $comment_id ); 
  138. if ( !$comment || is_wp_error( $comment ) ) { 
  139. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 ); 
  140. $query['parent'] = $comment_id; 
  141.  
  142. $comments = get_comments( $query ); 
  143.  
  144. update_comment_cache( $comments ); 
  145.  
  146. if ( $args['hierarchical'] ) { 
  147. $walker = new WPCOM_JSON_API_List_Comments_Walker; 
  148. $comment_ids = $walker->paged_walk( $comments, get_option( 'thread_comments_depth', -1 ), isset( $args['page'] ) ? $args['page'] : 1 , $args['number'] ); 
  149. $comments = array_map( 'get_comment', $comment_ids ); 
  150.  
  151. $return = array(); 
  152.  
  153. foreach ( array_keys( $this->response_format ) as $key ) { 
  154. switch ( $key ) { 
  155. case 'found' : 
  156. $return[ $key ] = (int) $found; 
  157. break; 
  158. case 'site_ID' : 
  159. $return[ $key ] = (int) $blog_id; 
  160. break; 
  161. case 'comments' : 
  162. $return_comments = array(); 
  163. foreach ( $comments as $comment ) { 
  164. $the_comment = $this->get_comment( $comment->comment_ID, $args['context'] ); 
  165. if ( $the_comment && !is_wp_error( $the_comment ) ) { 
  166. $return_comments[] = $the_comment; 
  167.  
  168. if ( $return_comments ) { 
  169. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ 
  170. do_action( 'wpcom_json_api_objects', 'comments', count( $return_comments ) ); 
  171.  
  172. $return[ $key ] = $return_comments; 
  173. break; 
  174.  
  175. return $return;