Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Update_Media_Endpoint
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Update media item info endpoint.
4 *
5 * Endpoint: /sites/%s/media/%d
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12new WPCOM_JSON_API_Update_Media_Endpoint(
13    array(
14        'description'          => 'Edit basic information about a media item.',
15        'group'                => 'media',
16        'stat'                 => 'media:1:POST',
17        'method'               => 'POST',
18        'path'                 => '/sites/%s/media/%d',
19        'deprecated'           => true,
20        'max_version'          => '1',
21        'new_version'          => '1.1',
22        'path_labels'          => array(
23            '$site'     => '(int|string) Site ID or domain',
24            '$media_ID' => '(int) The ID of the media item',
25        ),
26
27        'request_format'       => array(
28            'title'       => '(string) The file name.',
29            'caption'     => '(string) File caption.',
30            'description' => '(HTML) Description of the file.',
31        ),
32
33        'response_format'      => array(
34            'id'          => '(int) The ID of the media item',
35            'date'        => '(ISO 8601 datetime) The date the media was uploaded',
36            'parent'      => '(int) ID of the post this media is attached to',
37            'link'        => '(string) URL to the file',
38            'title'       => '(string) File name',
39            'caption'     => '(string) User provided caption of the file',
40            'description' => '(string) Description of the file',
41            'metadata'    => '(array) Array of metadata about the file, such as Exif data or sizes',
42        ),
43        'example_request'      => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/446',
44        'example_request_data' => array(
45            'headers' => array(
46                'authorization' => 'Bearer YOUR_API_TOKEN',
47            ),
48            'body'    => array(
49                'title' => 'Updated Title',
50            ),
51        ),
52    )
53);
54
55/**
56 * Update media item info class.
57 *
58 * @phan-constructor-used-for-side-effects
59 */
60class WPCOM_JSON_API_Update_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
61    /**
62     * Update media item info API callback.
63     *
64     * @param string $path API path.
65     * @param int    $blog_id Blog ID.
66     * @param int    $media_id Media ID.
67     *
68     * @return object|WP_Error
69     */
70    public function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
71        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
72        if ( is_wp_error( $blog_id ) ) {
73            return $blog_id;
74        }
75
76        if ( ! current_user_can( 'upload_files', $media_id ) ) {
77            return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
78        }
79
80        $item = $this->get_media_item( $media_id );
81
82        if ( is_wp_error( $item ) ) {
83            return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
84        }
85
86        $input  = $this->input( true );
87        $insert = array();
88
89        if ( ! empty( $input['title'] ) ) {
90            $insert['post_title'] = $input['title'];
91        }
92
93        if ( ! empty( $input['caption'] ) ) {
94            $insert['post_excerpt'] = $input['caption'];
95        }
96
97        if ( ! empty( $input['description'] ) ) {
98            $insert['post_content'] = $input['description'];
99        }
100
101        $insert['ID'] = $media_id;
102        wp_update_post( (object) $insert );
103
104        $item = $this->get_media_item( $media_id );
105        return $item;
106    }
107}