Bez popisu

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

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