class.wpcom-json-api-delete-media-v1-1-endpoint.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. new WPCOM_JSON_API_Delete_Media_v1_1_Endpoint( array(
  3. 'description' => 'Delete a piece of media. Note: Media is deleted and not trashed.',
  4. 'group' => 'media',
  5. 'stat' => 'media:1:delete',
  6. 'min_version' => '1.1',
  7. 'max_version' => '1.1',
  8. 'method' => 'POST',
  9. 'path' => '/sites/%s/media/%d/delete',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. '$media_ID' => '(int) The media ID',
  13. ),
  14. 'response_format' => array(
  15. 'status' => '(string) Returns deleted if the media was successfully deleted',
  16. 'ID' => '(int) The ID of the media item',
  17. 'date' => '(ISO 8601 datetime) The date the media was uploaded',
  18. 'post_ID' => '(int) ID of the post this media is attached to',
  19. 'author_ID' => '(int) ID of the user who uploaded the media',
  20. 'URL' => '(string) URL to the file',
  21. 'guid' => '(string) Unique identifier',
  22. 'file' => '(string) File name',
  23. 'extension' => '(string) File extension',
  24. 'mime_type' => '(string) File mime type',
  25. 'title' => '(string) File name',
  26. 'caption' => '(string) User-provided caption of the file',
  27. 'description' => '(string) Description of the file',
  28. 'alt' => '(string) Alternative text for image files.',
  29. 'thumbnails' => '(object) Media item thumbnail URL options',
  30. 'height' => '(int) (Image & video only) Height of the media item',
  31. 'width' => '(int) (Image & video only) Width of the media item',
  32. 'length' => '(int) (Video & audio only) Duration of the media item, in seconds',
  33. 'exif' => '(array) (Image & audio only) Exif (meta) information about the media item',
  34. 'videopress_guid' => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
  35. 'videopress_processing_done' => '(bool) (Video only) If the video is Uuploaded on a blog with VideoPress, this will return the status of processing on the Video'
  36. ),
  37. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/$media_ID/delete',
  38. 'example_request_data' => array(
  39. 'headers' => array(
  40. 'authorization' => 'Bearer YOUR_API_TOKEN'
  41. )
  42. )
  43. ) );
  44. class WPCOM_JSON_API_Delete_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
  45. function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
  46. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  47. if ( is_wp_error( $blog_id ) ) {
  48. return $blog_id;
  49. }
  50. if ( ! current_user_can( 'delete_post', $media_id ) ) {
  51. return new WP_Error( 'unauthorized', 'User is not authorized delete media', 403 );
  52. }
  53. $item = $this->get_media_item_v1_1( $media_id );
  54. if ( is_wp_error( $item ) ) {
  55. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  56. }
  57. wp_delete_post( $media_id, true );
  58. $item->status = 'deleted';
  59. return $item;
  60. }
  61. }