PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-comment-endpoint.php

https://gitlab.com/thisishayat/itv-2016
PHP | 198 lines | 195 code | 2 blank | 1 comment | 1 complexity | 5b71eac8cc025b443f13457689424cd8 MD5 | raw file
  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. 'status' => array(
  13. 'approved' => 'The comment has been approved.',
  14. 'unapproved' => 'The comment has been held for review in the moderation queue.',
  15. 'spam' => 'The comment has been marked as spam.',
  16. 'trash' => 'The comment is in the trash.',
  17. ),
  18. 'parent' => "(object>comment_reference|false) A reference to the comment's parent, if it has one.",
  19. 'type' => array(
  20. 'comment' => 'The comment is a regular comment.',
  21. 'trackback' => 'The comment is a trackback.',
  22. 'pingback' => 'The comment is a pingback.',
  23. ),
  24. 'like_count' => '(int) The number of likes for this comment.',
  25. 'i_like' => '(bool) Does the current user like this comment?',
  26. 'meta' => '(object) Meta data',
  27. );
  28. // public $response_format =& $this->comment_object_format;
  29. function __construct( $args ) {
  30. if ( !$this->response_format ) {
  31. $this->response_format =& $this->comment_object_format;
  32. }
  33. parent::__construct( $args );
  34. }
  35. function get_comment( $comment_id, $context ) {
  36. global $blog_id;
  37. $comment = get_comment( $comment_id );
  38. if ( !$comment || is_wp_error( $comment ) ) {
  39. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  40. }
  41. $types = array( '', 'comment', 'pingback', 'trackback' );
  42. if ( !in_array( $comment->comment_type, $types ) ) {
  43. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  44. }
  45. $post = get_post( $comment->comment_post_ID );
  46. if ( !$post || is_wp_error( $post ) ) {
  47. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  48. }
  49. $status = wp_get_comment_status( $comment->comment_ID );
  50. // Permissions
  51. switch ( $context ) {
  52. case 'edit' :
  53. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  54. return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
  55. }
  56. $GLOBALS['post'] = $post;
  57. $comment = get_comment_to_edit( $comment->comment_ID );
  58. foreach ( array( 'comment_author', 'comment_author_email', 'comment_author_url' ) as $field ) {
  59. $comment->$field = htmlspecialchars_decode( $comment->$field, ENT_QUOTES );
  60. }
  61. break;
  62. case 'display' :
  63. if ( 'approved' !== $status ) {
  64. $current_user_id = get_current_user_id();
  65. $user_can_read_coment = false;
  66. if ( $current_user_id && $comment->user_id && $current_user_id == $comment->user_id ) {
  67. $user_can_read_coment = true;
  68. } elseif (
  69. $comment->comment_author_email && $comment->comment_author
  70. &&
  71. isset( $this->api->token_details['user'] )
  72. &&
  73. $this->api->token_details['user']['user_email'] === $comment->comment_author_email
  74. &&
  75. $this->api->token_details['user']['display_name'] === $comment->comment_author
  76. ) {
  77. $user_can_read_coment = true;
  78. } else {
  79. $user_can_read_coment = current_user_can( 'edit_comment', $comment->comment_ID );
  80. }
  81. if ( !$user_can_read_coment ) {
  82. return new WP_Error( 'unauthorized', 'User cannot read unapproved comment', 403 );
  83. }
  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. foreach ( array_keys( $this->comment_object_format ) as $key ) {
  98. switch ( $key ) {
  99. case 'ID' :
  100. // explicitly cast all output
  101. $response[$key] = (int) $comment->comment_ID;
  102. break;
  103. case 'post' :
  104. $response[$key] = (object) array(
  105. 'ID' => (int) $post->ID,
  106. 'title' => (string) get_the_title( $post->ID ),
  107. 'type' => (string) $post->post_type,
  108. 'link' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
  109. );
  110. break;
  111. case 'author' :
  112. $response[$key] = (object) $this->get_author( $comment, 'edit' === $context && current_user_can( 'edit_comment', $comment->comment_ID ) );
  113. break;
  114. case 'date' :
  115. $response[$key] = (string) $this->format_date( $comment->comment_date_gmt, $comment->comment_date );
  116. break;
  117. case 'URL' :
  118. $response[$key] = (string) esc_url_raw( get_comment_link( $comment->comment_ID ) );
  119. break;
  120. case 'short_URL' :
  121. // @todo - pagination
  122. $response[$key] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) . "%23comment-{$comment->comment_ID}" );
  123. break;
  124. case 'content' :
  125. if ( 'display' === $context ) {
  126. ob_start();
  127. comment_text();
  128. $response[$key] = (string) ob_get_clean();
  129. } else {
  130. $response[$key] = (string) $comment->comment_content;
  131. }
  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. }
  147. break;
  148. case 'type' :
  149. $response[$key] = (string) ( $comment->comment_type ? $comment->comment_type : 'comment' );
  150. break;
  151. case 'like_count' :
  152. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  153. $response[ $key ] = (int) $this->api->comment_like_count( $blog_id, $post->ID, $comment->comment_ID );
  154. }
  155. break;
  156. case 'i_like' :
  157. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  158. $response[ $key ] = (bool) Likes::comment_like_current_user_likes( $blog_id, $comment->comment_ID );
  159. }
  160. break;
  161. case 'meta' :
  162. $response[$key] = (object) array(
  163. 'links' => (object) array(
  164. 'self' => (string) $this->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID ),
  165. 'help' => (string) $this->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'help' ),
  166. 'site' => (string) $this->get_site_link( $this->api->get_blog_id_for_output() ),
  167. 'post' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $comment->comment_post_ID ),
  168. 'replies' => (string) $this->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'replies/' ),
  169. // 'author' => (string) $this->get_user_link( $comment->user_id ),
  170. // 'via' => (string) $this->get_post_link( $ping_origin_blog_id, $ping_origin_post_id ), // Ping/trackbacks
  171. 'likes' => (string) $this->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'likes/' ),
  172. ),
  173. );
  174. break;
  175. }
  176. }
  177. unset( $GLOBALS['comment'], $GLOBALS['post'] );
  178. return $response;
  179. }
  180. }