WPCOM_JSON_API_Comment_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Comment Endpoint class.

Defined (1)

The class is defined in the following location(s).

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