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

class.wpcom-json-api-list-media-endpoint.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_List_Media_Endpoint( array(
4
	'description' => 'Get a list of items in the media library.',
5
	'group'       => 'media',
6
	'stat'        => 'media',
7
8
	'method'      => 'GET',
9
	'path'        => '/sites/%s/media/',
10
	'deprecated'  => true,
11
	'new_version' => '1.1',
12
	'max_version' => '1',
13
	'path_labels' => array(
14
		'$site' => '(int|string) Site ID or domain',
15
	),
16
17
	'query_parameters' => array(
18
		'number'    => '(int=20) The number of media items to return. Limit: 100.',
19
		'offset'    => '(int=0) 0-indexed offset.',
20
		'parent_id' => '(int) Default is showing all items. The post where the media item is attached. 0 shows unattached media items.',
21
		'mime_type' => "(string) Default is empty. Filter by mime type (e.g., 'image/jpeg', 'application/pdf'). Partial searches also work (e.g. passing 'image' will search for all image files).",
22
	),
23
24
	'response_format' => array(
25
		'media' => '(array) Array of media',
26
		'found' => '(int) The number of total results found'
27
	),
28
29
	'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/?number=2',
30
	'example_request_data' =>  array(
31
		'headers' => array(
32
			'authorization' => 'Bearer YOUR_API_TOKEN'
33
		)
34
	)
35
) );
36
37
class WPCOM_JSON_API_List_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
38
39
	function callback( $path = '', $blog_id = 0 ) {
40
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
41
		if ( is_wp_error( $blog_id ) ) {
42
			return $blog_id;
43
		}
44
45
		//upload_files can probably be used for other endpoints but we want contributors to be able to use media too
46
		if ( !current_user_can( 'edit_posts' ) ) {
47
			return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
0 ignored issues
show
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...
48
		}
49
50
		$args = $this->query_args();
51
52
		if ( $args['number'] < 1 ) {
53
			$args['number'] = 20;
54
		} elseif ( 100 < $args['number'] ) {
55
			return new WP_Error( 'invalid_number',  'The NUMBER parameter must be less than or equal to 100.', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_number'.

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...
56
		}
57
58
		$media = get_posts( array(
59
			'post_type' => 'attachment',
60
			'post_parent' => $args['parent_id'],
61
			'offset' => $args['offset'],
62
			'numberposts' => $args['number'],
63
			'post_mime_type' => $args['mime_type']
64
		) );
65
66
		$response = array();
67
		foreach ( $media as $item ) {
68
			$response[] = $this->get_media_item( $item->ID );
69
		}
70
71
		$_num = (array) wp_count_attachments();
72
		$_total_media = array_sum( $_num ) - $_num['trash'];
73
74
		$return = array(
75
			'found' => $_total_media,
76
			'media' => $response
77
		);
78
79
		return $return;
80
	}
81
82
}
83