No Description

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

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