Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Get_Media_Endpoint
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Get_Media_Endpoint(
8    array(
9        'description'          => 'Get a single media item (by ID).',
10        'group'                => 'media',
11        'stat'                 => 'media:1',
12        'method'               => 'GET',
13        'path'                 => '/sites/%s/media/%d',
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 ID of the media item',
20        ),
21        'response_format'      => array(
22            'id'          => '(int) The ID of the media item',
23            'date'        => '(ISO 8601 datetime) The date the media was uploaded',
24            'parent'      => '(int) ID of the post this media is attached to',
25            'link'        => '(string) URL to the file',
26            'title'       => '(string) Filename',
27            'caption'     => '(string) User-provided caption of the file',
28            'description' => '(string) Description of the file',
29            'metadata'    => '(array) Array of metadata about the file, such as Exif data or sizes',
30        ),
31
32        'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/934',
33        'example_request_data' => array(
34            'headers' => array(
35                'authorization' => 'Bearer YOUR_API_TOKEN',
36            ),
37        ),
38    )
39);
40
41/**
42 * GET Media endpoint class.
43 *
44 * @phan-constructor-used-for-side-effects
45 */
46class WPCOM_JSON_API_Get_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
47    /**
48     *
49     * API callback.
50     *
51     * @param string $path - the path.
52     * @param int    $blog_id - the blog ID.
53     * @param int    $media_id - the media ID.
54     */
55    public function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
56        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
57        if ( is_wp_error( $blog_id ) ) {
58            return $blog_id;
59        }
60
61        // upload_files can probably be used for other endpoints but we want contributors to be able to use media too.
62        if ( ! current_user_can( 'edit_posts', $media_id ) ) {
63            return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
64        }
65
66        return $this->get_media_item( $media_id );
67    }
68}