class.wpcom-json-api-update-comment-endpoint.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. new WPCOM_JSON_API_Update_Comment_Endpoint( array(
  3. 'description' => 'Create a comment on a post.',
  4. 'group' => 'comments',
  5. 'stat' => 'posts:1:replies:new',
  6. 'method' => 'POST',
  7. 'path' => '/sites/%s/posts/%d/replies/new',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$post_ID' => '(int) The post ID'
  11. ),
  12. 'request_format' => array(
  13. // explicitly document all input
  14. 'content' => '(HTML) The comment text.',
  15. // @todo Should we open this up to unauthenticated requests too?
  16. // 'author' => '(author object) The author of the comment.',
  17. ),
  18. 'pass_wpcom_user_details' => true,
  19. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/posts/843/replies/new/',
  20. 'example_request_data' => array(
  21. 'headers' => array(
  22. 'authorization' => 'Bearer YOUR_API_TOKEN'
  23. ),
  24. 'body' => array(
  25. 'content' => 'Your reply is very interesting. This is a reply.'
  26. )
  27. )
  28. ) );
  29. new WPCOM_JSON_API_Update_Comment_Endpoint( array(
  30. 'description' => 'Create a comment as a reply to another comment.',
  31. 'group' => 'comments',
  32. 'stat' => 'comments:1:replies:new',
  33. 'method' => 'POST',
  34. 'path' => '/sites/%s/comments/%d/replies/new',
  35. 'path_labels' => array(
  36. '$site' => '(int|string) Site ID or domain',
  37. '$comment_ID' => '(int) The comment ID'
  38. ),
  39. 'request_format' => array(
  40. 'content' => '(HTML) The comment text.',
  41. // @todo Should we open this up to unauthenticated requests too?
  42. // 'author' => '(author object) The author of the comment.',
  43. ),
  44. 'pass_wpcom_user_details' => true,
  45. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/29/replies/new',
  46. 'example_request_data' => array(
  47. 'headers' => array(
  48. 'authorization' => 'Bearer YOUR_API_TOKEN'
  49. ),
  50. 'body' => array(
  51. 'content' => 'This reply is very interesting. This is editing a comment reply via the API.',
  52. )
  53. )
  54. ) );
  55. new WPCOM_JSON_API_Update_Comment_Endpoint( array(
  56. 'description' => 'Edit a comment.',
  57. 'group' => 'comments',
  58. 'stat' => 'comments:1:POST',
  59. 'method' => 'POST',
  60. 'path' => '/sites/%s/comments/%d',
  61. 'path_labels' => array(
  62. '$site' => '(int|string) Site ID or domain',
  63. '$comment_ID' => '(int) The comment ID'
  64. ),
  65. 'request_format' => array(
  66. 'author' => "(string) The comment author's name.",
  67. 'author_email' => "(string) The comment author's email.",
  68. 'author_url' => "(string) The comment author's URL.",
  69. 'content' => '(HTML) The comment text.',
  70. 'date' => "(ISO 8601 datetime) The comment's creation time.",
  71. 'status' => array(
  72. 'approved' => 'Approve the comment.',
  73. 'unapproved' => 'Remove the comment from public view and send it to the moderation queue.',
  74. 'spam' => 'Mark the comment as spam.',
  75. 'unspam' => 'Unmark the comment as spam. Will attempt to set it to the previous status.',
  76. 'trash' => 'Send a comment to the trash if trashing is enabled (see constant: EMPTY_TRASH_DAYS).',
  77. 'untrash' => 'Untrash a comment. Only works when the comment is in the trash.',
  78. ),
  79. ),
  80. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/29',
  81. 'example_request_data' => array(
  82. 'headers' => array(
  83. 'authorization' => 'Bearer YOUR_API_TOKEN'
  84. ),
  85. 'body' => array(
  86. 'content' => 'This reply is now edited via the API.',
  87. 'status' => 'approved',
  88. )
  89. )
  90. ) );
  91. new WPCOM_JSON_API_Update_Comment_Endpoint( array(
  92. 'description' => 'Delete a comment.',
  93. 'group' => 'comments',
  94. 'stat' => 'comments:1:delete',
  95. 'method' => 'POST',
  96. 'path' => '/sites/%s/comments/%d/delete',
  97. 'path_labels' => array(
  98. '$site' => '(int|string) Site ID or domain',
  99. '$comment_ID' => '(int) The comment ID'
  100. ),
  101. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/$comment_ID/delete',
  102. 'example_request_data' => array(
  103. 'headers' => array(
  104. 'authorization' => 'Bearer YOUR_API_TOKEN'
  105. )
  106. )
  107. ) );
  108. class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endpoint {
  109. function __construct( $args ) {
  110. parent::__construct( $args );
  111. if ( $this->api->ends_with( $this->path, '/delete' ) ) {
  112. $this->comment_object_format['status']['deleted'] = 'The comment has been deleted permanently.';
  113. }
  114. }
  115. // /sites/%s/posts/%d/replies/new -> $blog_id, $post_id
  116. // /sites/%s/comments/%d/replies/new -> $blog_id, $comment_id
  117. // /sites/%s/comments/%d -> $blog_id, $comment_id
  118. // /sites/%s/comments/%d/delete -> $blog_id, $comment_id
  119. function callback( $path = '', $blog_id = 0, $object_id = 0 ) {
  120. if ( $this->api->ends_with( $path, '/new' ) )
  121. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
  122. else
  123. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  124. if ( is_wp_error( $blog_id ) ) {
  125. return $blog_id;
  126. }
  127. if ( $this->api->ends_with( $path, '/delete' ) ) {
  128. return $this->delete_comment( $path, $blog_id, $object_id );
  129. } elseif ( $this->api->ends_with( $path, '/new' ) ) {
  130. if ( false !== strpos( $path, '/posts/' ) ) {
  131. return $this->new_comment( $path, $blog_id, $object_id, 0 );
  132. } else {
  133. return $this->new_comment( $path, $blog_id, 0, $object_id );
  134. }
  135. }
  136. return $this->update_comment( $path, $blog_id, $object_id );
  137. }
  138. // /sites/%s/posts/%d/replies/new -> $blog_id, $post_id
  139. // /sites/%s/comments/%d/replies/new -> $blog_id, $comment_id
  140. function new_comment( $path, $blog_id, $post_id, $comment_parent_id ) {
  141. if ( !$post_id ) {
  142. $comment_parent = get_comment( $comment_parent_id );
  143. if ( !$comment_parent_id || !$comment_parent || is_wp_error( $comment_parent ) ) {
  144. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  145. }
  146. $post_id = $comment_parent->comment_post_ID;
  147. }
  148. $post = get_post( $post_id );
  149. if ( !$post || is_wp_error( $post ) ) {
  150. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  151. }
  152. if (
  153. -1 == get_option( 'blog_public' ) &&
  154. /**
  155. * Filter allowing non-registered users on the site to comment.
  156. *
  157. * @module json-api
  158. *
  159. * @since 3.4.0
  160. *
  161. * @param bool is_user_member_of_blog() Is the user member of the site.
  162. */
  163. ! apply_filters( 'wpcom_json_api_user_is_member_of_blog', is_user_member_of_blog() ) &&
  164. ! is_super_admin()
  165. ) {
  166. return new WP_Error( 'unauthorized', 'User cannot create comments', 403 );
  167. }
  168. if ( ! comments_open( $post->ID ) && ! current_user_can( 'edit_post', $post->ID ) ) {
  169. return new WP_Error( 'unauthorized', 'Comments on this post are closed', 403 );
  170. }
  171. $can_view = $this->user_can_view_post( $post->ID );
  172. if ( !$can_view || is_wp_error( $can_view ) ) {
  173. return $can_view;
  174. }
  175. $post_status = get_post_status_object( get_post_status( $post ) );
  176. if ( !$post_status->public && !$post_status->private ) {
  177. return new WP_Error( 'unauthorized', 'Comments on drafts are not allowed', 403 );
  178. }
  179. $args = $this->query_args();
  180. $input = $this->input();
  181. if ( !is_array( $input ) || !$input || !strlen( $input['content'] ) ) {
  182. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  183. }
  184. $user = wp_get_current_user();
  185. if ( !$user || is_wp_error( $user ) || !$user->ID ) {
  186. $auth_required = false;
  187. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  188. $auth_required = true;
  189. } elseif ( isset( $this->api->token_details['user'] ) ) {
  190. $user = (object) $this->api->token_details['user'];
  191. foreach ( array( 'display_name', 'user_email', 'user_url' ) as $user_datum ) {
  192. if ( !isset( $user->$user_datum ) ) {
  193. $auth_required = true;
  194. }
  195. }
  196. if ( !isset( $user->ID ) ) {
  197. $user->ID = 0;
  198. }
  199. // If we have a user with an external ID saved, we can use it.
  200. if (
  201. ! $auth_required
  202. && $user->ID
  203. && (
  204. $author = get_user_by( 'id', intval( $user->ID ) )
  205. )
  206. ) {
  207. $user = $author;
  208. }
  209. } else {
  210. $auth_required = true;
  211. }
  212. if ( $auth_required ) {
  213. return new WP_Error( 'authorization_required', 'An active access token must be used to comment.', 403 );
  214. }
  215. }
  216. $insert = array(
  217. 'comment_post_ID' => $post->ID,
  218. 'user_ID' => $user->ID,
  219. 'comment_author' => $user->display_name,
  220. 'comment_author_email' => $user->user_email,
  221. 'comment_author_url' => $user->user_url,
  222. 'comment_content' => $input['content'],
  223. 'comment_parent' => $comment_parent_id,
  224. 'comment_type' => '',
  225. );
  226. if ( $comment_parent_id ) {
  227. if ( $comment_parent->comment_approved === '0' && current_user_can( 'edit_comment', $comment_parent->comment_ID ) ) {
  228. wp_set_comment_status( $comment_parent->comment_ID, 'approve' );
  229. }
  230. }
  231. $this->api->trap_wp_die( 'comment_failure' );
  232. $comment_id = wp_new_comment( add_magic_quotes( $insert ) );
  233. $this->api->trap_wp_die( null );
  234. $return = $this->get_comment( $comment_id, $args['context'] );
  235. if ( !$return ) {
  236. return new WP_Error( 400, __( 'Comment cache problem?', 'jetpack' ) );
  237. }
  238. if ( is_wp_error( $return ) ) {
  239. return $return;
  240. }
  241. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  242. do_action( 'wpcom_json_api_objects', 'comments' );
  243. return $return;
  244. }
  245. // /sites/%s/comments/%d -> $blog_id, $comment_id
  246. function update_comment( $path, $blog_id, $comment_id ) {
  247. $comment = get_comment( $comment_id );
  248. if ( !$comment || is_wp_error( $comment ) ) {
  249. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  250. }
  251. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  252. return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
  253. }
  254. $args = $this->query_args();
  255. $input = $this->input( false );
  256. if ( !is_array( $input ) || !$input ) {
  257. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  258. }
  259. $update = array();
  260. foreach ( $input as $key => $value ) {
  261. $update["comment_$key"] = $value;
  262. }
  263. $comment_status = wp_get_comment_status( $comment->comment_ID );
  264. if ( isset( $update['comment_status'] ) ) {
  265. switch ( $update['comment_status'] ) {
  266. case 'approved' :
  267. if ( 'approve' !== $comment_status ) {
  268. wp_set_comment_status( $comment->comment_ID, 'approve' );
  269. }
  270. break;
  271. case 'unapproved' :
  272. if ( 'hold' !== $comment_status ) {
  273. wp_set_comment_status( $comment->comment_ID, 'hold' );
  274. }
  275. break;
  276. case 'spam' :
  277. if ( 'spam' !== $comment_status ) {
  278. wp_spam_comment( $comment->comment_ID );
  279. }
  280. break;
  281. case 'unspam' :
  282. if ( 'spam' === $comment_status ) {
  283. wp_unspam_comment( $comment->comment_ID );
  284. }
  285. break;
  286. case 'trash' :
  287. if ( ! EMPTY_TRASH_DAYS ) {
  288. return new WP_Error( 'trash_disabled', 'Cannot trash comment', 403 );
  289. }
  290. if ( 'trash' !== $comment_status ) {
  291. wp_trash_comment( $comment_id );
  292. }
  293. break;
  294. case 'untrash' :
  295. if ( 'trash' === $comment_status ) {
  296. wp_untrash_comment( $comment->comment_ID );
  297. }
  298. break;
  299. default:
  300. $update['comment_approved'] = 1;
  301. break;
  302. }
  303. unset( $update['comment_status'] );
  304. }
  305. if ( ! empty( $update ) ) {
  306. $update['comment_ID'] = $comment->comment_ID;
  307. wp_update_comment( add_magic_quotes( $update ) );
  308. }
  309. $return = $this->get_comment( $comment->comment_ID, $args['context'] );
  310. if ( !$return || is_wp_error( $return ) ) {
  311. return $return;
  312. }
  313. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  314. do_action( 'wpcom_json_api_objects', 'comments' );
  315. return $return;
  316. }
  317. // /sites/%s/comments/%d/delete -> $blog_id, $comment_id
  318. function delete_comment( $path, $blog_id, $comment_id ) {
  319. $comment = get_comment( $comment_id );
  320. if ( !$comment || is_wp_error( $comment ) ) {
  321. return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
  322. }
  323. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) { // [sic] There is no delete_comment cap
  324. return new WP_Error( 'unauthorized', 'User cannot delete comment', 403 );
  325. }
  326. $args = $this->query_args();
  327. $return = $this->get_comment( $comment->comment_ID, $args['context'] );
  328. if ( !$return || is_wp_error( $return ) ) {
  329. return $return;
  330. }
  331. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  332. do_action( 'wpcom_json_api_objects', 'comments' );
  333. wp_delete_comment( $comment->comment_ID );
  334. $status = wp_get_comment_status( $comment->comment_ID );
  335. if ( false === $status ) {
  336. $return['status'] = 'deleted';
  337. return $return;
  338. }
  339. return $this->get_comment( $comment->comment_ID, $args['context'] );
  340. }
  341. }