PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/chernushov881/charity-fund
PHP | 242 lines | 227 code | 0 blank | 15 comment | 1 complexity | 01fd35ebbd6375d7c8ef6637487833e9 MD5 | raw file
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Comment endpoint.
  4. *
  5. * @todo - can this file be written without overriding global variables?
  6. * @phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
  7. */
  8. /**
  9. * Comment endpoint class.
  10. */
  11. abstract class WPCOM_JSON_API_Comment_Endpoint extends WPCOM_JSON_API_Endpoint {
  12. /**
  13. * Comment object array.
  14. *
  15. * @var $comment_object_format
  16. */
  17. public $comment_object_format = array(
  18. // explicitly document and cast all output.
  19. 'ID' => '(int) The comment ID.',
  20. 'post' => "(object>post_reference) A reference to the comment's post.",
  21. 'author' => '(object>author) The author of the comment.',
  22. 'date' => "(ISO 8601 datetime) The comment's creation time.",
  23. 'URL' => '(URL) The full permalink URL to the comment.',
  24. 'short_URL' => '(URL) The wp.me short URL.',
  25. 'content' => '(HTML) <code>context</code> dependent.',
  26. 'raw_content' => '(string) Raw comment content.',
  27. 'status' => array(
  28. 'approved' => 'The comment has been approved.',
  29. 'unapproved' => 'The comment has been held for review in the moderation queue.',
  30. 'spam' => 'The comment has been marked as spam.',
  31. 'trash' => 'The comment is in the trash.',
  32. ),
  33. 'parent' => "(object>comment_reference|false) A reference to the comment's parent, if it has one.",
  34. 'type' => array(
  35. 'comment' => 'The comment is a regular comment.',
  36. 'trackback' => 'The comment is a trackback.',
  37. 'pingback' => 'The comment is a pingback.',
  38. 'review' => 'The comment is a product review.',
  39. ),
  40. 'like_count' => '(int) The number of likes for this comment.',
  41. 'i_like' => '(bool) Does the current user like this comment?',
  42. 'meta' => '(object) Meta data',
  43. 'can_moderate' => '(bool) Whether current user can moderate the comment.',
  44. 'i_replied' => '(bool) Has the current user replied to this comment?',
  45. );
  46. /**
  47. * Class constructor.
  48. *
  49. * @param object $args - arguments passed to constructor.
  50. */
  51. public function __construct( $args ) {
  52. if ( ! $this->response_format ) {
  53. $this->response_format =& $this->comment_object_format;
  54. }
  55. parent::__construct( $args );
  56. }
  57. /**
  58. * Get the comment.
  59. *
  60. * @param int $comment_id - the ID of the comment.
  61. * @param string $context - the context of the comment (displayed or edited).
  62. */
  63. public function get_comment( $comment_id, $context ) {
  64. global $blog_id;
  65. $comment = get_comment( $comment_id );
  66. if ( ! $comment || is_wp_error( $comment ) ) {
  67. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  68. }
  69. $types = array( '', 'comment', 'pingback', 'trackback', 'review' );
  70. // @todo - can we make this comparison strict without breaking anything?
  71. // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
  72. if ( ! in_array( $comment->comment_type, $types ) ) {
  73. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  74. }
  75. $post = get_post( $comment->comment_post_ID );
  76. if ( ! $post || is_wp_error( $post ) ) {
  77. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  78. }
  79. $status = wp_get_comment_status( $comment->comment_ID );
  80. // Permissions.
  81. switch ( $context ) {
  82. case 'edit':
  83. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  84. return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
  85. }
  86. $GLOBALS['post'] = $post;
  87. $comment = get_comment_to_edit( $comment->comment_ID );
  88. foreach ( array( 'comment_author', 'comment_author_email', 'comment_author_url' ) as $field ) {
  89. $comment->$field = htmlspecialchars_decode( $comment->$field, ENT_QUOTES );
  90. }
  91. break;
  92. case 'display':
  93. if ( 'approved' !== $status ) {
  94. $current_user_id = get_current_user_id();
  95. $user_can_read_comment = false;
  96. // @todo - can we make this comparison strict without breaking anything?
  97. // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
  98. if ( $current_user_id && $comment->user_id && $current_user_id == $comment->user_id ) {
  99. $user_can_read_comment = true;
  100. } elseif (
  101. $comment->comment_author_email && $comment->comment_author
  102. &&
  103. isset( $this->api->token_details['user'] )
  104. &&
  105. isset( $this->api->token_details['user']['user_email'] )
  106. &&
  107. $this->api->token_details['user']['user_email'] === $comment->comment_author_email
  108. &&
  109. $this->api->token_details['user']['display_name'] === $comment->comment_author
  110. ) {
  111. $user_can_read_comment = true;
  112. } else {
  113. $user_can_read_comment = current_user_can( 'edit_posts' );
  114. }
  115. if ( ! $user_can_read_comment ) {
  116. return new WP_Error( 'unauthorized', 'User cannot read unapproved comment', 403 );
  117. }
  118. }
  119. $GLOBALS['post'] = $post;
  120. setup_postdata( $post );
  121. break;
  122. default:
  123. return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
  124. }
  125. $can_view = $this->user_can_view_post( $post->ID );
  126. if ( ! $can_view || is_wp_error( $can_view ) ) {
  127. return $can_view;
  128. }
  129. $GLOBALS['comment'] = $comment;
  130. $response = array();
  131. foreach ( array_keys( $this->comment_object_format ) as $key ) {
  132. switch ( $key ) {
  133. case 'ID':
  134. // explicitly cast all output.
  135. $response[ $key ] = (int) $comment->comment_ID;
  136. break;
  137. case 'post':
  138. $response[ $key ] = (object) array(
  139. 'ID' => (int) $post->ID,
  140. 'title' => (string) get_the_title( $post->ID ),
  141. 'type' => (string) $post->post_type,
  142. 'link' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
  143. );
  144. break;
  145. case 'author':
  146. $response[ $key ] = (object) $this->get_author( $comment, current_user_can( 'edit_comment', $comment->comment_ID ) );
  147. break;
  148. case 'date':
  149. $response[ $key ] = (string) $this->format_date( $comment->comment_date_gmt, $comment->comment_date );
  150. break;
  151. case 'URL':
  152. $response[ $key ] = (string) esc_url_raw( get_comment_link( $comment->comment_ID ) );
  153. break;
  154. case 'short_URL':
  155. // @todo - pagination
  156. $response[ $key ] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) . "%23comment-{$comment->comment_ID}" );
  157. break;
  158. case 'content':
  159. if ( 'display' === $context ) {
  160. ob_start();
  161. comment_text();
  162. $response[ $key ] = (string) ob_get_clean();
  163. } else {
  164. $response[ $key ] = (string) $comment->comment_content;
  165. }
  166. break;
  167. case 'raw_content':
  168. $response[ $key ] = (string) $comment->comment_content;
  169. break;
  170. case 'status':
  171. $response[ $key ] = (string) $status;
  172. break;
  173. case 'parent': // May be object or false.
  174. if ( $comment->comment_parent ) {
  175. $parent = get_comment( $comment->comment_parent );
  176. $response[ $key ] = (object) array(
  177. 'ID' => (int) $parent->comment_ID,
  178. 'type' => (string) ( $parent->comment_type ? $parent->comment_type : 'comment' ),
  179. 'link' => (string) $this->links->get_comment_link( $blog_id, $parent->comment_ID ),
  180. );
  181. } else {
  182. $response[ $key ] = false;
  183. }
  184. break;
  185. case 'type':
  186. $response[ $key ] = (string) ( $comment->comment_type ? $comment->comment_type : 'comment' );
  187. break;
  188. case 'like_count':
  189. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  190. $response[ $key ] = (int) $this->api->comment_like_count( $blog_id, $post->ID, $comment->comment_ID );
  191. }
  192. break;
  193. case 'i_like':
  194. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  195. $response[ $key ] = (bool) Likes::comment_like_current_user_likes( $blog_id, $comment->comment_ID );
  196. }
  197. break;
  198. case 'meta':
  199. $response[ $key ] = (object) array(
  200. 'links' => (object) array(
  201. 'self' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID ),
  202. 'help' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'help' ),
  203. 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
  204. 'post' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $comment->comment_post_ID ),
  205. 'replies' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'replies/' ),
  206. 'likes' => (string) $this->links->get_comment_link( $this->api->get_blog_id_for_output(), $comment->comment_ID, 'likes/' ),
  207. ),
  208. );
  209. break;
  210. case 'can_moderate':
  211. $response[ $key ] = (bool) current_user_can( 'edit_comment', $comment_id );
  212. break;
  213. case 'i_replied':
  214. $response[ $key ] = (bool) 0 < get_comments(
  215. array(
  216. 'user_id' => get_current_user_id(),
  217. 'parent' => $comment->comment_ID,
  218. 'count' => true,
  219. )
  220. );
  221. break;
  222. }
  223. }
  224. unset( $GLOBALS['comment'], $GLOBALS['post'] );
  225. return $response;
  226. }
  227. }