Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

WPCOM_JSON_API_Get_Media_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A callback() 0 13 3
1
<?php
2
3
new WPCOM_JSON_API_Get_Media_Endpoint( array(
4
	'description' => 'Get a single media item (by ID).',
5
	'group'       => 'media',
6
	'stat'        => 'media:1',
7
	'method'      => 'GET',
8
	'path'        => '/sites/%s/media/%d',
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 ID of the media item',
15
	),
16
	'response_format' => array(
17
		'id'    => '(int) The ID of the media item',
18
		'date' =>  '(ISO 8601 datetime) The date the media was uploaded',
19
		'parent'           => '(int) ID of the post this media is attached to',
20
		'link'             => '(string) URL to the file',
21
		'title'            => '(string) Filename',
22
		'caption'          => '(string) User-provided caption of the file',
23
		'description'      => '(string) Description of the file',
24
		'metadata'         => '(array) Array of metadata about the file, such as Exif data or sizes',
25
	),
26
27
	'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/934',
28
	'example_request_data' =>  array(
29
		'headers' => array(
30
			'authorization' => 'Bearer YOUR_API_TOKEN'
31
		)
32
	)
33
) );
34
35
class WPCOM_JSON_API_Get_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
36
	function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
37
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
38
		if ( is_wp_error( $blog_id ) ) {
39
			return $blog_id;
40
		}
41
42
		//upload_files can probably be used for other endpoints but we want contributors to be able to use media too
43
		if ( !current_user_can( 'edit_posts', $media_id ) ) {
44
			return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
		}
46
47
		return $this->get_media_item( $media_id );
48
	}
49
}
50