class.wpcom-json-api-list-comments-endpoint.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. class WPCOM_JSON_API_List_Comments_Walker extends Walker {
  3. public $tree_type = 'comment';
  4. public $db_fields = array(
  5. 'parent' => 'comment_parent',
  6. 'id' => 'comment_ID'
  7. );
  8. public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
  9. if ( ! is_array( $output ) ) {
  10. $output = array();
  11. }
  12. $output[] = $object->comment_ID;
  13. }
  14. /**
  15. * Taken from WordPress's Walker_Comment::display_element()
  16. *
  17. * This function is designed to enhance Walker::display_element() to
  18. * display children of higher nesting levels than selected inline on
  19. * the highest depth level displayed. This prevents them being orphaned
  20. * at the end of the comment list.
  21. *
  22. * Example: max_depth = 2, with 5 levels of nested content.
  23. * 1
  24. * 1.1
  25. * 1.1.1
  26. * 1.1.1.1
  27. * 1.1.1.1.1
  28. * 1.1.2
  29. * 1.1.2.1
  30. * 2
  31. * 2.2
  32. *
  33. * @see Walker_Comment::display_element()
  34. * @see Walker::display_element()
  35. * @see wp_list_comments()
  36. */
  37. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  38. if ( !$element )
  39. return;
  40. $id_field = $this->db_fields['id'];
  41. $id = $element->$id_field;
  42. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  43. // If we're at the max depth, and the current element still has children, loop over those and display them at this level
  44. // This is to prevent them being orphaned to the end of the list.
  45. if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
  46. foreach ( $children_elements[ $id ] as $child )
  47. $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
  48. unset( $children_elements[ $id ] );
  49. }
  50. }
  51. }
  52. new WPCOM_JSON_API_List_Comments_Endpoint( array(
  53. 'description' => 'Get a list of recent comments.',
  54. 'group' => 'comments',
  55. 'stat' => 'comments',
  56. 'method' => 'GET',
  57. 'path' => '/sites/%s/comments/',
  58. 'path_labels' => array(
  59. '$site' => '(int|string) Site ID or domain',
  60. ),
  61. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comments/?number=2'
  62. ) );
  63. new WPCOM_JSON_API_List_Comments_Endpoint( array(
  64. 'description' => 'Get a list of recent comments on a post.',
  65. 'group' => 'comments',
  66. 'stat' => 'posts:1:replies',
  67. 'method' => 'GET',
  68. 'path' => '/sites/%s/posts/%d/replies/',
  69. 'path_labels' => array(
  70. '$site' => '(int|string) Site ID or domain',
  71. '$post_ID' => '(int) The post ID',
  72. ),
  73. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/7/replies/?number=2'
  74. ) );
  75. // @todo permissions
  76. class WPCOM_JSON_API_List_Comments_Endpoint extends WPCOM_JSON_API_Comment_Endpoint {
  77. public $response_format = array(
  78. 'found' => '(int) The total number of comments found that match the request (ignoring limits, offsets, and pagination).',
  79. 'site_ID' => '(int) The site ID',
  80. 'comments' => '(array:comment) An array of comment objects.',
  81. );
  82. function __construct( $args ) {
  83. parent::__construct( $args );
  84. $this->query = array_merge( $this->query, array(
  85. '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.',
  86. 'offset' => '(int=0) 0-indexed offset. Not available if using hierarchical=1.',
  87. '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.',
  88. 'order' => array(
  89. 'DESC' => 'Return comments in descending order from newest to oldest.',
  90. 'ASC' => 'Return comments in ascending order from oldest to newest.',
  91. ),
  92. 'hierarchical' => array(
  93. 'false' => '',
  94. 'true' => '(BETA) Order the comment list hierarchically.',
  95. ),
  96. 'after' => '(ISO 8601 datetime) Return comments dated on or after the specified datetime. Not available if using hierarchical=1.',
  97. 'before' => '(ISO 8601 datetime) Return comments dated on or before the specified datetime. Not available if using hierarchical=1.',
  98. 'type' => array(
  99. 'any' => 'Return all comments regardless of type.',
  100. 'comment' => 'Return only regular comments.',
  101. 'trackback' => 'Return only trackbacks.',
  102. 'pingback' => 'Return only pingbacks.',
  103. 'pings' => 'Return both trackbacks and pingbacks.',
  104. ),
  105. 'status' => array(
  106. 'approved' => 'Return only approved comments.',
  107. 'unapproved' => 'Return only comments in the moderation queue.',
  108. 'spam' => 'Return only comments marked as spam.',
  109. 'trash' => 'Return only comments in the trash.',
  110. 'all' => 'Return comments of all statuses.',
  111. ),
  112. ) );
  113. }
  114. // /sites/%s/comments/ -> $blog_id
  115. // /sites/%s/posts/%d/replies/ -> $blog_id, $post_id
  116. // /sites/%s/comments/%d/replies/ -> $blog_id, $comment_id
  117. function callback( $path = '', $blog_id = 0, $object_id = 0 ) {
  118. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  119. if ( is_wp_error( $blog_id ) ) {
  120. return $blog_id;
  121. }
  122. $args = $this->query_args();
  123. if ( $args['number'] < 1 ) {
  124. $args['number'] = 20;
  125. } elseif ( 100 < $args['number'] ) {
  126. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 );
  127. }
  128. if ( false !== strpos( $path, '/posts/' ) ) {
  129. // We're looking for comments of a particular post
  130. $post_id = $object_id;
  131. $comment_id = 0;
  132. } else {
  133. // We're looking for comments for the whole blog, or replies to a single comment
  134. $comment_id = $object_id;
  135. $post_id = 0;
  136. }
  137. // We can't efficiently get the number of replies to a single comment
  138. $count = false;
  139. $found = -1;
  140. if ( !$comment_id ) {
  141. // We can get comment counts for the whole site or for a single post, but only for certain queries
  142. if ( 'any' === $args['type'] && !isset( $args['after'] ) && !isset( $args['before'] ) ) {
  143. $count = wp_count_comments( $post_id );
  144. }
  145. }
  146. switch ( $args['status'] ) {
  147. case 'approved' :
  148. $status = 'approve';
  149. if ( $count ) {
  150. $found = $count->approved;
  151. }
  152. break;
  153. default :
  154. if ( ! current_user_can( 'edit_posts' ) ) {
  155. return new WP_Error( 'unauthorized', 'User cannot read non-approved comments', 403 );
  156. }
  157. if ( 'unapproved' === $args['status'] ) {
  158. $status = 'hold';
  159. $count_status = 'moderated';
  160. } elseif ( 'all' === $args['status'] ) {
  161. $status = 'all';
  162. $count_status = 'total_comments';
  163. } else {
  164. $status = $count_status = $args['status'];
  165. }
  166. if ( $count ) {
  167. $found = $count->$count_status;
  168. }
  169. }
  170. $query = array(
  171. 'order' => $args['order'],
  172. 'type' => 'any' === $args['type'] ? false : $args['type'],
  173. 'status' => $status,
  174. );
  175. if ( isset( $args['page'] ) ) {
  176. if ( $args['page'] < 1 ) {
  177. $args['page'] = 1;
  178. }
  179. } else {
  180. if ( $args['offset'] < 0 ) {
  181. $args['offset'] = 0;
  182. }
  183. }
  184. if ( ! $args['hierarchical'] ) {
  185. $query['number'] = $args['number'];
  186. if ( isset( $args['page'] ) ) {
  187. $query['offset'] = ( $args['page'] - 1 ) * $args['number'];
  188. } else {
  189. $query['offset'] = $args['offset'];
  190. }
  191. $is_before = isset( $args['before_gmt'] );
  192. $is_after = isset( $args['after_gmt'] );
  193. if ( $is_before || $is_after ) {
  194. $query['date_query'] = array(
  195. 'column' => 'comment_date_gmt',
  196. 'inclusive' => true,
  197. );
  198. if ( $is_before ) {
  199. $query['date_query']['before'] = $args['before_gmt'];
  200. }
  201. if ( $is_after ) {
  202. $query['date_query']['after'] = $args['after_gmt'];
  203. }
  204. }
  205. }
  206. if ( $post_id ) {
  207. $post = get_post( $post_id );
  208. if ( !$post || is_wp_error( $post ) ) {
  209. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  210. }
  211. $query['post_id'] = $post->ID;
  212. if ( $this->api->ends_with( $this->path, '/replies' ) ) {
  213. $query['parent'] = 0;
  214. }
  215. } elseif ( $comment_id ) {
  216. $comment = get_comment( $comment_id );
  217. if ( !$comment || is_wp_error( $comment ) ) {
  218. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  219. }
  220. $query['parent'] = $comment_id;
  221. }
  222. $comments = get_comments( $query );
  223. update_comment_cache( $comments );
  224. if ( $args['hierarchical'] ) {
  225. $walker = new WPCOM_JSON_API_List_Comments_Walker;
  226. $comment_ids = $walker->paged_walk( $comments, get_option( 'thread_comments_depth', -1 ), isset( $args['page'] ) ? $args['page'] : 1 , $args['number'] );
  227. if ( ! empty( $comment_ids ) ) {
  228. $comments = array_map( 'get_comment', $comment_ids );
  229. }
  230. }
  231. $return = array();
  232. foreach ( array_keys( $this->response_format ) as $key ) {
  233. switch ( $key ) {
  234. case 'found' :
  235. $return[ $key ] = (int) $found;
  236. break;
  237. case 'site_ID' :
  238. $return[ $key ] = (int) $blog_id;
  239. break;
  240. case 'comments' :
  241. $return_comments = array();
  242. if ( ! empty( $comments ) ) {
  243. foreach ( $comments as $comment ) {
  244. $the_comment = $this->get_comment( $comment->comment_ID, $args['context'] );
  245. if ( $the_comment && !is_wp_error( $the_comment ) ) {
  246. $return_comments[] = $the_comment;
  247. }
  248. }
  249. }
  250. if ( $return_comments ) {
  251. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  252. do_action( 'wpcom_json_api_objects', 'comments', count( $return_comments ) );
  253. }
  254. $return[ $key ] = $return_comments;
  255. break;
  256. }
  257. }
  258. return $return;
  259. }
  260. }