Brak opisu

class.wpcom-json-api-bulk-update-comments-endpoint.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. new WPCOM_JSON_API_Bulk_Update_Comments_Endpoint( array(
  3. 'description' => 'Update multiple comment\'s status.',
  4. 'group' => 'comments',
  5. 'stat' => 'comments:1:bulk-update-status',
  6. 'min_version' => '1',
  7. 'max_version' => '1',
  8. 'method' => 'POST',
  9. 'path' => '/sites/%s/comments/status',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. ),
  13. 'request_format' => array(
  14. 'comment_ids' => '(array|string) An array, or comma-separated list, of Comment IDs to update.',
  15. 'status' => '(string) The new status value. Allowed values: approved, unapproved, spam, trash',
  16. ),
  17. 'response_format' => array(
  18. 'results' => '(array) An array of updated Comment IDs.'
  19. ),
  20. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/status',
  21. 'example_request_data' => array(
  22. 'headers' => array(
  23. 'authorization' => 'Bearer YOUR_API_TOKEN'
  24. ),
  25. 'body' => array(
  26. 'comment_ids' => array( 881, 882 ),
  27. 'status' => 'approved',
  28. ),
  29. )
  30. ) );
  31. new WPCOM_JSON_API_Bulk_Update_Comments_Endpoint( array(
  32. 'description' => 'Permanently delete multiple comments. Note: this request will send non-trashed comments to the trash. Trashed comments will be permanently deleted.',
  33. 'group' => 'comments',
  34. 'stat' => 'comments:1:bulk-delete',
  35. 'min_version' => '1',
  36. 'max_version' => '1',
  37. 'method' => 'POST',
  38. 'path' => '/sites/%s/comments/delete',
  39. 'path_labels' => array(
  40. '$site' => '(int|string) Site ID or domain',
  41. ),
  42. 'request_format' => array(
  43. 'comment_ids' => '(array|string) An array, or comma-separated list, of Comment IDs to delete or trash. (optional)',
  44. 'empty_status' => '(string) Force to permanently delete all spam or trash comments. (optional). Allowed values: spam, trash',
  45. ),
  46. 'response_format' => array(
  47. 'results' => '(array) An array of deleted or trashed Comment IDs.'
  48. ),
  49. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/delete',
  50. 'example_request_data' => array(
  51. 'headers' => array(
  52. 'authorization' => 'Bearer YOUR_API_TOKEN'
  53. ),
  54. 'body' => array(
  55. 'comment_ids' => array( 881, 882 ),
  56. ),
  57. )
  58. ) );
  59. class WPCOM_JSON_API_Bulk_Update_Comments_Endpoint extends WPCOM_JSON_API_Endpoint {
  60. // /sites/%s/comments/status
  61. // /sites/%s/comments/delete
  62. function callback( $path = '', $blog_id = 0 ) {
  63. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  64. if ( is_wp_error( $blog_id ) ) {
  65. return $blog_id;
  66. }
  67. $input = $this->input();
  68. if ( isset( $input['comment_ids'] ) && is_array( $input['comment_ids'] ) ) {
  69. $comment_ids = $input['comment_ids'];
  70. } else if ( isset( $input['comment_ids'] ) && ! empty( $input['comment_ids'] ) ) {
  71. $comment_ids = explode( ',', $input['comment_ids'] );
  72. } else {
  73. $comment_ids = array();
  74. }
  75. $result = array(
  76. 'results' => array(),
  77. );
  78. wp_defer_comment_counting( true );
  79. if ( $this->api->ends_with( $path, '/delete' ) ) {
  80. if ( isset( $input['empty_status'] ) && $this->validate_empty_status_param( $input['empty_status'] ) ) {
  81. $result['results'] = $this->delete_all( $input['empty_status'] );
  82. } else {
  83. $result['results'] = $this->bulk_delete_comments( $comment_ids );
  84. }
  85. } else {
  86. $status = isset( $input['status'] ) ? $input['status'] : '';
  87. $result['results'] = $this->bulk_update_comments_status( $comment_ids, $status );
  88. }
  89. wp_defer_comment_counting( false );
  90. return $result;
  91. }
  92. /**
  93. * Determine if the passed comment status is valid or not.
  94. *
  95. * @param string $status
  96. *
  97. * @return boolean
  98. */
  99. function validate_status_param( $status ) {
  100. return in_array( $status, array( 'approved', 'unapproved', 'pending', 'spam', 'trash' ), true );
  101. }
  102. /**
  103. * Determine if the passed empty status is valid or not.
  104. *
  105. * @param string $empty_status
  106. *
  107. * @return boolean
  108. */
  109. function validate_empty_status_param( $empty_status ) {
  110. return in_array( $empty_status, array( 'spam', 'trash' ), true );
  111. }
  112. /**
  113. * Update the status of multiple comments.
  114. *
  115. * @param array $comment_ids Comments to update.
  116. * @param string $status New status value.
  117. *
  118. * @return array Updated comments IDs.
  119. */
  120. function bulk_update_comments_status( $comment_ids, $status ) {
  121. if ( count( $comment_ids ) < 1 ) {
  122. return new WP_Error( 'empty_comment_ids', 'The request must include comment_ids', 400 );
  123. }
  124. if ( ! $this->validate_status_param( $status ) ) {
  125. return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$status'.", 400 );
  126. }
  127. $results = array();
  128. foreach( $comment_ids as $comment_id ) {
  129. if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
  130. continue;
  131. }
  132. $result = false;
  133. switch( $status ) {
  134. case 'approved':
  135. $result = wp_set_comment_status( $comment_id, 'approve' );
  136. break;
  137. case 'unapproved':
  138. case 'pending':
  139. $result = wp_set_comment_status( $comment_id, 'hold' );
  140. break;
  141. case 'spam':
  142. $result = wp_spam_comment( $comment_id );
  143. break;
  144. case 'trash':
  145. $result = wp_trash_comment( $comment_id );
  146. break;
  147. }
  148. if ( $result ) {
  149. $results[] = $comment_id;
  150. }
  151. }
  152. return $results;
  153. }
  154. /**
  155. * Permanenty delete multiple comments.
  156. *
  157. * Comments are only permanently deleted if trash is disabled or their status is `trash` or `spam`.
  158. * Otherwise they are moved to trash.
  159. *
  160. * @param array $comment_ids Comments to trash or delete.
  161. *
  162. * @return array Deleted comments IDs.
  163. */
  164. function bulk_delete_comments( $comment_ids ) {
  165. if ( count( $comment_ids ) < 1 ) {
  166. return new WP_Error( 'empty_comment_ids', 'The request must include comment_ids', 400 );
  167. }
  168. $results = array();
  169. foreach( $comment_ids as $comment_id ) {
  170. if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
  171. continue;
  172. }
  173. if ( wp_delete_comment( $comment_id ) ) {
  174. $results[] = $comment_id;
  175. }
  176. }
  177. return $results;
  178. }
  179. /**
  180. * Delete all spam or trash comments.
  181. *
  182. * Comments are only permanently deleted if trash is disabled or their status is `trash` or `spam`.
  183. * Otherwise they are moved to trash.
  184. *
  185. * @param string $status Can be `spam` or `trash`.
  186. *
  187. * @return array Deleted comments IDs.
  188. */
  189. function delete_all( $status ) {
  190. global $wpdb;
  191. // This could potentially take a long time, so we only want to delete comments created
  192. // before this operation.
  193. // Comments marked `spam` or `trash` after this moment won't be touched.
  194. // Core uses the `pagegen_timestamp` hidden field for this same reason.
  195. $delete_time = gmdate('Y-m-d H:i:s');
  196. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = %s AND %s > comment_date_gmt", $status, $delete_time ) );
  197. if ( count( $comment_ids ) < 1 ) {
  198. return array();
  199. }
  200. return $this->bulk_delete_comments( $comment_ids );
  201. }
  202. }