Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Delete_Media_v1_1_Endpoint
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Delete_Media_v1_1_Endpoint(
8    array(
9        'description'          => 'Delete a piece of media. Note: Media is deleted and not trashed.',
10        'group'                => 'media',
11        'stat'                 => 'media:1:delete',
12        'min_version'          => '1.1',
13        'max_version'          => '1.1',
14        'method'               => 'POST',
15        'path'                 => '/sites/%s/media/%d/delete',
16        'path_labels'          => array(
17            '$site'     => '(int|string) Site ID or domain',
18            '$media_ID' => '(int) The media ID',
19        ),
20
21        'response_format'      => array(
22            'status'                     => '(string) Returns deleted if the media was successfully deleted',
23            'ID'                         => '(int) The ID of the media item',
24            'date'                       => '(ISO 8601 datetime) The date the media was uploaded',
25            'post_ID'                    => '(int) ID of the post this media is attached to',
26            'author_ID'                  => '(int) ID of the user who uploaded the media',
27            'URL'                        => '(string) URL to the file',
28            'guid'                       => '(string) Unique identifier',
29            'file'                       => '(string) File name',
30            'extension'                  => '(string) File extension',
31            'mime_type'                  => '(string) File mime type',
32            'title'                      => '(string) File name',
33            'caption'                    => '(string) User-provided caption of the file',
34            'description'                => '(string) Description of the file',
35            'alt'                        => '(string)  Alternative text for image files.',
36            'thumbnails'                 => '(object) Media item thumbnail URL options',
37            'height'                     => '(int) (Image & video only) Height of the media item',
38            'width'                      => '(int) (Image & video only) Width of the media item',
39            'length'                     => '(int) (Video & audio only) Duration of the media item, in seconds',
40            'exif'                       => '(array) (Image & audio only) Exif (meta) information about the media item',
41            'videopress_guid'            => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
42            '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',
43        ),
44
45        'example_request'      => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/$media_ID/delete',
46        'example_request_data' => array(
47            'headers' => array(
48                'authorization' => 'Bearer YOUR_API_TOKEN',
49            ),
50        ),
51    )
52);
53
54/**
55 * Delete media v1_1 endpoint class.
56 *
57 * @phan-constructor-used-for-side-effects
58 */
59class WPCOM_JSON_API_Delete_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint { //phpcs:ignore
60    /**
61     * API callback.
62     *
63     * @param string $path - the path.
64     * @param int    $blog_id - the blog ID.
65     * @param int    $media_id - the media ID.
66     */
67    public function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
68        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
69        if ( is_wp_error( $blog_id ) ) {
70            return $blog_id;
71        }
72
73        if ( ! current_user_can( 'delete_post', $media_id ) ) {
74            return new WP_Error( 'unauthorized', 'User is not authorized delete media', 403 );
75        }
76
77        $item = $this->get_media_item_v1_1( $media_id );
78
79        if ( is_wp_error( $item ) ) {
80            return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
81        }
82
83        wp_delete_post( $media_id, true );
84        $item->status = 'deleted';
85        return $item;
86    }
87}