class.wpcom-json-api-get-comments-tree-v1-2-endpoint.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. new WPCOM_JSON_API_Get_Comments_Tree_v1_2_Endpoint( array(
  3. 'description' => 'Get a comments tree for site.',
  4. 'min_version' => '1.2',
  5. 'max_version' => '1.2',
  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. 'post_id' => '(int) Filter returned comments by a post.',
  15. 'status' => '(string) Filter returned comments based on this value (allowed values: all, approved, pending, trash, spam).',
  16. ),
  17. 'response_format' => array(
  18. 'comments_tree' => '(array) Array of post IDs representing the comments tree for given site or post (max 50000)',
  19. 'trackbacks_tree' => '(array) Array of post IDs representing the trackbacks tree for given site or post (max 50000)',
  20. 'pingbacks_tree' => '(array) Array of post IDs representing the pingbacks tree for given site or post (max 50000)',
  21. ),
  22. 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/comments-tree?&status=approved&post_id=123',
  23. ) );
  24. class WPCOM_JSON_API_Get_Comments_Tree_v1_2_Endpoint extends WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint {
  25. /**
  26. * Retrieves a list of comment data.
  27. *
  28. * @param array $args {
  29. * Optional. Arguments to control behavior. Default empty array.
  30. *
  31. * @type int $max_comment_count Maximum number of comments returned.
  32. * @type int $post_id Filter by post.
  33. * @type int $start_at First comment to search from going back in time.
  34. * @type string $status Filter by status: all, approved, pending, spam or trash.
  35. * }
  36. *
  37. * @return array
  38. */
  39. function get_site_tree_v1_2( $args = array() ) {
  40. global $wpdb;
  41. $defaults = array(
  42. 'max_comment_count' => 50000,
  43. 'post_id' => NULL,
  44. 'start_at' => PHP_INT_MAX,
  45. 'status' => 'all',
  46. );
  47. $args = wp_parse_args( $args, $defaults );
  48. $db_status = $this->get_comment_db_status( $args['status'] );
  49. if ( ! empty( $args['post_id'] ) ) {
  50. $db_comment_rows = $wpdb->get_results(
  51. $wpdb->prepare(
  52. "SELECT comment_ID, comment_parent, comment_type " .
  53. "FROM $wpdb->comments AS comments " .
  54. "WHERE comment_ID <= %d AND comment_post_ID = %d AND ( %s = 'all' OR comment_approved = %s ) " .
  55. "ORDER BY comment_ID DESC " .
  56. "LIMIT %d",
  57. (int) $args['start_at'], (int) $args['post_id'], $db_status, $db_status, $args['max_comment_count']
  58. ),
  59. ARRAY_N
  60. );
  61. } else {
  62. $db_comment_rows = $wpdb->get_results(
  63. $wpdb->prepare(
  64. "SELECT comment_ID, comment_parent, comment_type, comment_post_ID " .
  65. "FROM $wpdb->comments AS comments " .
  66. "INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
  67. "WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
  68. "ORDER BY comment_ID DESC " .
  69. "LIMIT %d",
  70. (int) $args['start_at'], $db_status, $db_status, $args['max_comment_count']
  71. ),
  72. ARRAY_N
  73. );
  74. }
  75. $comments = array();
  76. $trackbacks = array();
  77. $pingbacks = array();
  78. foreach ( $db_comment_rows as $row ) {
  79. $comment_id = intval( $row[0] );
  80. $comment_parent_id = intval( $row[1] );
  81. $comment_post_id = isset( $args['post_id'] ) ? intval( $args['post_id'] ) : intval( $row[3] );
  82. if ( ! isset( $comments[ $comment_post_id ] ) ) {
  83. $comments[ $comment_post_id ] = array( array(), array() );
  84. }
  85. switch ( $row[2] ) {
  86. case 'trackback':
  87. $trackbacks[ $comment_post_id ][] = $comment_id;
  88. break;
  89. case 'pingback':
  90. $pingbacks[ $comment_post_id ][] = $comment_id;
  91. break;
  92. default:
  93. if ( 0 === $comment_parent_id ) {
  94. $comments[ $comment_post_id ][0][] = $comment_id;
  95. } else {
  96. $comments[ $comment_post_id ][1][] = array( $comment_id, $comment_parent_id );
  97. }
  98. }
  99. }
  100. return array(
  101. 'comments_tree' => $comments,
  102. 'trackbacks_tree' => $trackbacks,
  103. 'pingbacks_tree' => $pingbacks,
  104. );
  105. }
  106. /**
  107. * Endpoint callback for /sites/%s/comments-tree
  108. *
  109. * @param string $path
  110. * @param int $blog_id
  111. *
  112. * @return array Site or post tree results by status.
  113. */
  114. function callback( $path = '', $blog_id = 0 ) {
  115. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  116. if ( is_wp_error( $blog_id ) ) {
  117. return $blog_id;
  118. }
  119. $args = $this->query_args();
  120. $filters = array();
  121. if ( ! empty( $args['status'] ) ) {
  122. if ( ! $this->validate_status_param( $args['status'] ) ) {
  123. return new WP_Error( 'invalid_status', 'Invalid comment status value provided: ' . $args['status'] . '.', 400 );
  124. }
  125. $filters['status'] = $args['status'];
  126. }
  127. if ( ! empty( $args['post_id'] ) ) {
  128. if ( is_null( get_post( absint( $args['post_id'] ) ) ) ) {
  129. return new WP_Error( 'invalid_post', 'Invalid post', 400 );
  130. }
  131. $filters['post_id'] = absint( $args['post_id'] );
  132. }
  133. return $this->get_site_tree_v1_2( $filters );
  134. }
  135. }