PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php

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