Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Delete_Media_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 Squiz.Commenting.FileComment.Missing
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Delete_Media_Endpoint(
8    array(
9        'description'          => 'Delete a piece of media.',
10        'group'                => 'media',
11        'stat'                 => 'media:1:delete',
12        'method'               => 'POST',
13        'path'                 => '/sites/%s/media/%d/delete',
14        'deprecated'           => true,
15        'new_version'          => '1.1',
16        'max_version'          => '1',
17        'path_labels'          => array(
18            '$site'     => '(int|string) Site ID or domain',
19            '$media_ID' => '(int) The media ID',
20        ),
21
22        'response_format'      => array(
23            'status'      => '(string) Returns deleted if the media was successfully deleted',
24            'id'          => '(int) The ID of the media item',
25            'date'        => '(ISO 8601 datetime) The date the media was uploaded',
26            'parent'      => '(int) ID of the post this media is attached to',
27            'link'        => '(string) URL to the file',
28            'title'       => '(string) File name',
29            'caption'     => '(string) User provided caption of the file',
30            'description' => '(string) Description of the file',
31            'metadata'    => '(array) Misc array of information about the file, such as exif data or sizes',
32        ),
33
34        'example_request'      => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/$media_ID/delete',
35        'example_request_data' => array(
36            'headers' => array(
37                'authorization' => 'Bearer YOUR_API_TOKEN',
38            ),
39        ),
40    )
41);
42
43/**
44 * Delete media endpoint class.
45 *
46 * @phan-constructor-used-for-side-effects
47 */
48class WPCOM_JSON_API_Delete_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
49    /**
50     * API callback.
51     *
52     * @param string $path - the path.
53     * @param int    $blog_id - the blog ID.
54     * @param int    $media_id - the media ID.
55     */
56    public function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
57        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
58        if ( is_wp_error( $blog_id ) ) {
59            return $blog_id;
60        }
61
62        if ( ! current_user_can( 'delete_post', $media_id ) ) {
63            return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
64        }
65
66        $item = $this->get_media_item( $media_id );
67
68        if ( is_wp_error( $item ) ) {
69            return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
70        }
71
72        wp_delete_post( $media_id );
73        $item->status = 'deleted';
74        return $item;
75    }
76}