class.wpcom-json-api-get-comments-tree-endpoint.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. new WPCOM_JSON_API_Get_Comments_Tree_Endpoint( array(
  3. 'description' => 'Get a comments tree for site.',
  4. 'max_version' => '1',
  5. 'new_version' => '1.1',
  6. 'group' => 'comments-tree',
  7. 'stat' => 'comments-tree:1',
  8. 'method' => 'GET',
  9. 'path' => '/sites/%s/comments-tree',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. ),
  13. 'query_parameters' => array(
  14. 'status' => '(string) Filter returned comments based on this value (allowed values: all, approved, unapproved, pending, trash, spam).'
  15. ),
  16. 'response_format' => array(
  17. 'comments_count' => '(int) Total number of comments on the site',
  18. 'comments_tree' => '(array) Array of arrays representing the comments tree for given site (max 50000)',
  19. 'trackbacks_count' => '(int) Total number of trackbacks on the site',
  20. 'trackbacks_tree' => '(array) Array of arrays representing the trackbacks tree for given site (max 50000)',
  21. 'pingbacks_count' => '(int) Total number of pingbacks on the site',
  22. 'pingbacks_tree' => '(array) Array of arrays representing the pingbacks tree for given site (max 50000)',
  23. ),
  24. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comments-tree?status=approved'
  25. ) );
  26. class WPCOM_JSON_API_Get_Comments_Tree_Endpoint extends WPCOM_JSON_API_Endpoint {
  27. /**
  28. * Retrieves a list of comment data for a given site.
  29. *
  30. * @param string $status Filter by status: all, approved, pending, spam or trash.
  31. * @param int $start_at first comment to search from going back in time
  32. *
  33. * @return array
  34. */
  35. function get_site_tree( $status, $start_at = PHP_INT_MAX ) {
  36. global $wpdb;
  37. $max_comment_count = 50000;
  38. $db_status = $this->get_comment_db_status( $status );
  39. $db_comment_rows = $wpdb->get_results(
  40. $wpdb->prepare(
  41. "SELECT comment_ID, comment_post_ID, comment_parent, comment_type " .
  42. "FROM $wpdb->comments AS comments " .
  43. "INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
  44. "WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
  45. "ORDER BY comment_ID DESC " .
  46. "LIMIT %d",
  47. (int) $start_at, $db_status, $db_status, $max_comment_count
  48. ),
  49. ARRAY_N
  50. );
  51. $comments = array();
  52. $trackbacks = array();
  53. $pingbacks = array();
  54. foreach ( $db_comment_rows as $row ) {
  55. list( $comment_id, $comment_post_id, $comment_parent, $comment_type ) = $row;
  56. switch ( $comment_type ) {
  57. case 'trackback':
  58. $trackbacks[] = array( $comment_id, $comment_post_id, $comment_parent );
  59. break;
  60. case 'pingback':
  61. $pingbacks[] = array( $comment_id, $comment_post_id, $comment_parent );
  62. break;
  63. default:
  64. $comments[] = array( $comment_id, $comment_post_id, $comment_parent );
  65. }
  66. }
  67. return array(
  68. 'comments_count' => $this->get_site_tree_total_count( $status, 'comment' ),
  69. 'comments_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $comments ),
  70. 'trackbacks_count' => $this->get_site_tree_total_count( $status, 'trackback' ),
  71. 'trackbacks_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $trackbacks ),
  72. 'pingbacks_count' => $this->get_site_tree_total_count( $status, 'pingback' ),
  73. 'pingbacks_tree' => array_map( array( $this, 'array_map_all_as_ints' ), $pingbacks ),
  74. );
  75. }
  76. /**
  77. * Ensure all values are integers.
  78. *
  79. * @param array $comments Collection of comments.
  80. *
  81. * @return array Comments with values as integers.
  82. */
  83. function array_map_all_as_ints( $comments ) {
  84. return array_map( 'intval', $comments );
  85. }
  86. /**
  87. * Retrieves a total count of comments by type for the given site.
  88. *
  89. * @param string $status Filter by status: all, approved, pending, spam or trash.
  90. * @param string $type Comment type: 'trackback', 'pingback', or 'comment'.
  91. *
  92. * @return int Total count of comments for a site.
  93. */
  94. function get_site_tree_total_count( $status, $type ) {
  95. global $wpdb;
  96. $db_status = $this->get_comment_db_status( $status );
  97. $type = $this->get_sanitized_comment_type( $type );
  98. // An empty value in the comments_type column denotes a regular comment.
  99. $type = ( 'comment' === $type ) ? '' : $type;
  100. $result = $wpdb->get_var(
  101. $wpdb->prepare(
  102. "SELECT COUNT(1) " .
  103. "FROM $wpdb->comments AS comments " .
  104. "INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
  105. "WHERE comment_type = %s AND ( %s = 'all' OR comment_approved = %s )",
  106. $type, $db_status, $db_status
  107. )
  108. );
  109. return intval( $result );
  110. }
  111. /**
  112. * Ensure a valid status is converted to a database-supported value if necessary.
  113. *
  114. * @param string $status Should be one of: all, approved, pending, spam or trash.
  115. *
  116. * @return string Corresponding value that exists in database.
  117. */
  118. function get_comment_db_status( $status ) {
  119. if ( 'approved' === $status ) {
  120. return '1';
  121. }
  122. if ( 'pending' === $status || 'unapproved' === $status ) {
  123. return '0';
  124. }
  125. return $status;
  126. }
  127. /**
  128. * Determine if the passed comment status is valid or not.
  129. *
  130. * @param string $status
  131. *
  132. * @return boolean
  133. */
  134. function validate_status_param( $status ) {
  135. return in_array( $status, array( 'all', 'approved', 'unapproved', 'pending', 'spam', 'trash' ) );
  136. }
  137. /**
  138. * Sanitize a given comment type.
  139. *
  140. * @param string Comment type: can be 'trackback', 'pingback', or 'comment'.
  141. *
  142. * @return string Sanitized comment type.
  143. */
  144. function get_sanitized_comment_type( $type = 'comment' ) {
  145. if ( in_array( $type, array( 'trackback', 'pingback', 'comment' ) ) ) {
  146. return $type;
  147. }
  148. return 'comment';
  149. }
  150. /**
  151. * Endpoint callback for /sites/%s/comments-tree
  152. *
  153. * @param string $path
  154. * @param int $blog_id
  155. *
  156. * @return array Site tree results by status.
  157. */
  158. function callback( $path = '', $blog_id = 0 ) {
  159. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  160. if ( is_wp_error( $blog_id ) ) {
  161. return $blog_id;
  162. }
  163. $args = $this->query_args();
  164. $comment_status = empty( $args['status'] ) ? 'all' : $args['status'];
  165. if ( ! $this->validate_status_param( $comment_status ) ) {
  166. return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$comment_status'.", 400 );
  167. }
  168. return $this->get_site_tree( $comment_status );
  169. }
  170. }