|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
new WPCOM_JSON_API_Delete_Media_Endpoint( array( |
|
4
|
|
|
'description' => 'Delete a piece of media.', |
|
5
|
|
|
'group' => 'media', |
|
6
|
|
|
'stat' => 'media:1:delete', |
|
7
|
|
|
'method' => 'POST', |
|
8
|
|
|
'path' => '/sites/%s/media/%d/delete', |
|
9
|
|
|
'deprecated' => true, |
|
10
|
|
|
'new_version' => '1.1', |
|
11
|
|
|
'max_version' => '1', |
|
12
|
|
|
'path_labels' => array( |
|
13
|
|
|
'$site' => '(int|string) Site ID or domain', |
|
14
|
|
|
'$media_ID' => '(int) The media ID', |
|
15
|
|
|
), |
|
16
|
|
|
|
|
17
|
|
|
'response_format' => array( |
|
18
|
|
|
'status' => '(string) Returns deleted if the media was successfully deleted', |
|
19
|
|
|
'id' => '(int) The ID of the media item', |
|
20
|
|
|
'date' => '(ISO 8601 datetime) The date the media was uploaded', |
|
21
|
|
|
'parent' => '(int) ID of the post this media is attached to', |
|
22
|
|
|
'link' => '(string) URL to the file', |
|
23
|
|
|
'title' => '(string) File name', |
|
24
|
|
|
'caption' => '(string) User provided caption of the file', |
|
25
|
|
|
'description' => '(string) Description of the file', |
|
26
|
|
|
'metadata' => '(array) Misc array of information about the file, such as exif data or sizes', |
|
27
|
|
|
), |
|
28
|
|
|
|
|
29
|
|
|
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/$media_ID/delete', |
|
30
|
|
|
'example_request_data' => array( |
|
31
|
|
|
'headers' => array( |
|
32
|
|
|
'authorization' => 'Bearer YOUR_API_TOKEN' |
|
33
|
|
|
) |
|
34
|
|
|
) |
|
35
|
|
|
) ); |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
class WPCOM_JSON_API_Delete_Media_Endpoint extends WPCOM_JSON_API_Endpoint { |
|
|
|
|
|
|
38
|
|
|
function callback( $path = '', $blog_id = 0, $media_id = 0 ) { |
|
39
|
|
|
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
|
40
|
|
|
if ( is_wp_error( $blog_id ) ) { |
|
41
|
|
|
return $blog_id; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( ! current_user_can( 'delete_post', $media_id ) ) { |
|
45
|
|
|
return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$item = $this->get_media_item( $media_id ); |
|
49
|
|
|
|
|
50
|
|
|
if ( is_wp_error( $item ) ) { |
|
51
|
|
|
return new WP_Error( 'unknown_media', 'Unknown Media', 404 ); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
wp_delete_post( $media_id ); |
|
55
|
|
|
$item->status = 'deleted'; |
|
56
|
|
|
return $item; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.