class.wpcom-json-api-delete-media-endpoint.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. new WPCOM_JSON_API_Delete_Media_Endpoint( array(
  3. 'description' => 'Delete a piece of media.',
  4. 'group' => 'media',
  5. 'stat' => 'media:1:delete',
  6. 'method' => 'POST',
  7. 'path' => '/sites/%s/media/%d/delete',
  8. 'deprecated' => true,
  9. 'new_version' => '1.1',
  10. 'max_version' => '1',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. '$media_ID' => '(int) The media ID',
  14. ),
  15. 'response_format' => array(
  16. 'status' => '(string) Returns deleted if the media was successfully deleted',
  17. 'id' => '(int) The ID of the media item',
  18. 'date' => '(ISO 8601 datetime) The date the media was uploaded',
  19. 'parent' => '(int) ID of the post this media is attached to',
  20. 'link' => '(string) URL to the file',
  21. 'title' => '(string) File name',
  22. 'caption' => '(string) User provided caption of the file',
  23. 'description' => '(string) Description of the file',
  24. 'metadata' => '(array) Misc array of information about the file, such as exif data or sizes',
  25. ),
  26. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/$media_ID/delete',
  27. 'example_request_data' => array(
  28. 'headers' => array(
  29. 'authorization' => 'Bearer YOUR_API_TOKEN'
  30. )
  31. )
  32. ) );
  33. class WPCOM_JSON_API_Delete_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
  34. function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
  35. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  36. if ( is_wp_error( $blog_id ) ) {
  37. return $blog_id;
  38. }
  39. if ( ! current_user_can( 'delete_post', $media_id ) ) {
  40. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  41. }
  42. $item = $this->get_media_item( $media_id );
  43. if ( is_wp_error( $item ) ) {
  44. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  45. }
  46. wp_delete_post( $media_id );
  47. $item->status = 'deleted';
  48. return $item;
  49. }
  50. }