class.wpcom-json-api-comment-endpoint.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. abstract class WPCOM_JSON_API_Comment_Endpoint extends WPCOM_JSON_API_Endpoint {
  3. public $comment_object_format = array(
  4. // explicitly document and cast all output
  5. 'ID' => '(int) The comment ID.',
  6. 'post' => "(object>post_reference) A reference to the comment's post.",
  7. 'author' => '(object>author) The author of the comment.',
  8. 'date' => "(ISO 8601 datetime) The comment's creation time.",
  9. 'URL' => '(URL) The full permalink URL to the comment.',
  10. 'short_URL' => '(URL) The wp.me short URL.',
  11. 'content' => '(HTML) <code>context</code> dependent.',
  12. 'raw_content' => '(string) Raw comment content.',
  13. 'status' => array(
  14. 'approved' => 'The comment has been approved.',
  15. 'unapproved' => 'The comment has been held for review in the moderation queue.',
  16. 'spam' => 'The comment has been marked as spam.',
  17. 'trash' => 'The comment is in the trash.',
  18. ),
  19. 'parent' => "(object>comment_reference|false) A reference to the comment's parent, if it has one.",
  20. 'type' => array(
  21. 'comment' => 'The comment is a regular comment.',
  22. 'trackback' => 'The comment is a trackback.',
  23. 'pingback' => 'The comment is a pingback.',
  24. ),
  25. 'like_count' => '(int) The number of likes for this comment.',
  26. 'i_like' => '(bool) Does the current user like this comment?',
  27. 'meta' => '(object) Meta data',
  28. 'can_moderate' => '(bool) Whether current user can moderate the comment.',
  29. );
  30. // public $response_format =& $this->comment_object_format;
  31. function __construct( $args ) {
  32. if ( !$this->response_format ) {
  33. $this->response_format =& $this->comment_object_format;
  34. }
  35. parent::__construct( $args );
  36. }
  37. function get_comment( $comment_id, $context ) {
  38. global $blog_id;
  39. $comment = get_comment( $comment_id );
  40. if ( !$comment || is_wp_error( $comment ) ) {
  41. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  42. }
  43. $types = array( '', 'comment', 'pingback', 'trackback' );
  44. if ( !in_array( $comment->comment_type, $types ) ) {
  45. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  46. }
  47. $post = get_post( $comment->comment_post_ID );
  48. if ( !$post || is_wp_error( $post ) ) {
  49. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  50. }
  51. $status = wp_get_comment_status( $comment->comment_ID );
  52. // Permissions
  53. switch ( $context ) {
  54. case 'edit' :
  55. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  56. return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
  57. }
  58. $GLOBALS['post'] = $post;
  59. $comment = get_comment_to_edit( $comment->comment_ID );
  60. foreach ( array( 'comment_author', 'comment_author_email', 'comment_author_url' ) as $field ) {
  61. $comment->$field = htmlspecialchars_decode( $comment->$field, ENT_QUOTES );
  62. }
  63. break;
  64. case 'display' :
  65. if ( 'approved' !== $status ) {
  66. $current_user_id = get_current_user_id();
  67. $user_can_read_comment = false;
  68. if ( $current_user_id && $comment->user_id && $current_user_id == $comment->user_id ) {
  69. $user_can_read_comment = true;
  70. } elseif (
  71. $comment->comment_author_email && $comment->comment_author
  72. &&
  73. isset( $this->api->token_details['user'] )
  74. &&
  75. isset( $this->api->token_details['user']['user_email'] )
  76. &&
  77. $this->api->token_details['user']['user_email'] === $comment->comment_author_email
  78. &&
  79. $this->api->token_details['user']['display_name'] === $comment->comment_author
  80. ) {
  81. $user_can_read_comment = true;
  82. } else {
  83. $user_can_read_comment = current_user_can( 'edit_posts' );
  84. }
  85. if ( !$user_can_read_comment ) {
  86. return new WP_Error( 'unauthorized', 'User cannot read unapproved comment', 403 );
  87. }
  88. }
  89. $GLOBALS['post'] = $post;
  90. setup_postdata( $post );
  91. break;
  92. default :
  93. return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
  94. }
  95. $can_view = $this->user_can_view_post( $post->ID );
  96. if ( !$can_view || is_wp_error( $can_view ) ) {
  97. return $can_view;
  98. }
  99. $GLOBALS['comment'] = $comment;
  100. $response = array();
  101. foreach ( array_keys( $this->comment_object_format ) as $key ) {
  102. switch ( $key ) {
  103. case 'ID' :
  104. // explicitly cast all output
  105. $response[$key] = (int) $comment->comment_ID;
  106. break;
  107. case 'post' :
  108. $response[$key] = (object) array(
  109. 'ID' => (int) $post->ID,
  110. 'title' => (string) get_the_title( $post->ID ),
  111. 'type' => (string) $post->post_type,
  112. 'link' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
  113. );
  114. break;
  115. case 'author' :
  116. $response[$key] = (object) $this->get_author( $comment, current_user_can( 'edit_comment', $comment->comment_ID ) );
  117. break;
  118. case 'date' :
  119. $response[$key] = (string) $this->format_date( $comment->comment_date_gmt, $comment->comment_date );
  120. break;
  121. case 'URL' :
  122. $response[$key] = (string) esc_url_raw( get_comment_link( $comment->comment_ID ) );
  123. break;
  124. case 'short_URL' :
  125. // @todo - pagination
  126. $response[$key] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) . "%23comment-{$comment->comment_ID}" );
  127. break;
  128. case 'content' :
  129. if ( 'display' === $context ) {
  130. ob_start();
  131. comment_text();
  132. $response[$key] = (string) ob_get_clean();
  133. } else {
  134. $response[$key] = (string) $comment->comment_content;
  135. }
  136. break;
  137. case 'raw_content':
  138. $response[$key] = (string) $comment->comment_content;
  139. break;
  140. case 'status' :
  141. $response[$key] = (string) $status;
  142. break;
  143. case 'parent' : // (object|false)
  144. if ( $comment->comment_parent ) {
  145. $parent = get_comment( $comment->comment_parent );
  146. $response[$key] = (object) array(
  147. 'ID' => (int) $parent->comment_ID,
  148. 'type' => (string) ( $parent->comment_type ? $parent->comment_type : 'comment' ),
  149. 'link' => (string) $this->links->get_comment_link( $blog_id, $parent->comment_ID ),
  150. );
  151. } else {
  152. $response[$key] = false;
  153. }
  154. break;
  155. case 'type' :
  156. $response[$key] = (string) ( $comment->comment_type ? $comment->comment_type : 'comment' );
  157. break;
  158. case 'like_count' :
  159. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  160. $response[ $key ] = (int) $this->api->comment_like_count( $blog_id, $post->ID, $comment->comment_ID );
  161. }
  162. break;
  163. case 'i_like' :
  164. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  165. $response[ $key ] = (bool) Likes::comment_like_current_user_likes( $blog_id, $comment->comment_ID );
  166. }
  167. break;
  168. case 'meta' :
  169. $response[$key] = (object) array(
  170. 'links' => (object) array(
  171. 'self' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID ),
  172. 'help' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'help' ),
  173. 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
  174. 'post' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $comment->comment_post_ID ),
  175. 'replies' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'replies/' ),
  176. 'likes' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'likes/' ),
  177. ),
  178. );
  179. break;
  180. case 'can_moderate':
  181. $response[ $key ] = (bool) current_user_can( 'edit_comment', $comment_id );
  182. break;
  183. }
  184. }
  185. unset( $GLOBALS['comment'], $GLOBALS['post'] );
  186. return $response;
  187. }
  188. }