class.wpcom-json-api-bulk-delete-post-endpoint.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. new WPCOM_JSON_API_Bulk_Delete_Post_Endpoint( array(
  3. 'description' => 'Delete multiple posts. Note: If the trash is enabled, this request will send non-trashed posts to the trash. Trashed posts will be permanently deleted.',
  4. 'group' => 'posts',
  5. 'stat' => 'posts:1:bulk-delete',
  6. 'min_version' => '1.1',
  7. 'max_version' => '1.1',
  8. 'method' => 'POST',
  9. 'path' => '/sites/%s/posts/delete',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. ),
  13. 'request_format' => array(
  14. 'post_ids' => '(array|string) An array, or comma-separated list, of Post IDs to delete or trash.',
  15. ),
  16. 'response_format' => array(
  17. 'results' => '(object) An object containing results, '
  18. ),
  19. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/delete',
  20. 'example_request_data' => array(
  21. 'headers' => array(
  22. 'authorization' => 'Bearer YOUR_API_TOKEN'
  23. ),
  24. 'body' => array(
  25. 'post_ids' => array( 881, 882 ),
  26. ),
  27. )
  28. ) );
  29. class WPCOM_JSON_API_Bulk_Delete_Post_Endpoint extends WPCOM_JSON_API_Update_Post_v1_1_Endpoint {
  30. // /sites/%s/posts/delete
  31. function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
  32. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  33. if ( is_wp_error( $blog_id ) ) {
  34. return $blog_id;
  35. }
  36. $input = $this->input();
  37. if ( is_array( $input['post_ids'] ) ) {
  38. $post_ids = (array) $input['post_ids'];
  39. } else if ( ! empty( $input['post_ids'] ) ) {
  40. $post_ids = explode( ',', $input['post_ids'] );
  41. } else {
  42. $post_ids = array();
  43. }
  44. if ( count( $post_ids ) < 1 ) {
  45. return new WP_Error( 'empty_post_ids', 'The request must include post_ids' );
  46. }
  47. $result = array(
  48. 'results' => array(),
  49. );
  50. foreach( $post_ids as $post_id ) {
  51. $result['results'][ $post_id ] = $this->delete_post( $path, $blog_id, $post_id );
  52. }
  53. return $result;
  54. }
  55. }