class.wpcom-json-api-get-comments-tree-v1-1-endpoint.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. new WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint ( array(
  3. 'description' => 'Get a comments tree for site.',
  4. 'min_version' => '1.1',
  5. 'max_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, 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 post IDs 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 post IDs 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 post IDs representing the pingbacks tree for given site (max 50000)',
  23. ),
  24. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/comments-tree?status=approved'
  25. ) );
  26. class WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint extends WPCOM_JSON_API_Get_Comments_Tree_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. $comment_id = intval( $row[0] );
  56. $comment_post_id = intval( $row[1] );
  57. $comment_parent_id = intval( $row[2] );
  58. if ( ! isset( $comments[ $comment_post_id ] ) ) {
  59. $comments[ $comment_post_id ] = array( array(), array() );
  60. }
  61. switch ( $row[3] ) {
  62. case 'trackback':
  63. $trackbacks[ $comment_post_id ][] = $comment_id;
  64. break;
  65. case 'pingback':
  66. $pingbacks[ $comment_post_id ][] = $comment_id;
  67. break;
  68. default:
  69. if ( 0 === $comment_parent_id ) {
  70. $comments[ $comment_post_id ][0][] = $comment_id;
  71. } else {
  72. $comments[ $comment_post_id ][1][] = array( $comment_id, $comment_parent_id );
  73. }
  74. }
  75. }
  76. return array(
  77. 'comments_count' => $this->get_site_tree_total_count( $status, 'comment' ),
  78. 'comments_tree' => $comments,
  79. 'trackbacks_count' => $this->get_site_tree_total_count( $status, 'trackback' ),
  80. 'trackbacks_tree' => $trackbacks,
  81. 'pingbacks_count' => $this->get_site_tree_total_count( $status, 'pingback' ),
  82. 'pingbacks_tree' => $pingbacks,
  83. );
  84. }
  85. }