Completed
Push — update/dialogue-focus-on-conte... ( 9f1745...fa862f )
by
unknown
80:03 queued 71:18
created

class.wpcom-json-api-upload-media-endpoint.php (1 issue)

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_Upload_Media_Endpoint( array(
4
	'description' => 'Upload a new media item.',
5
	'group'       => 'media',
6
	'stat'        => 'media:new',
7
	'method'      => 'POST',
8
	'path'        => '/sites/%s/media/new',
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
	),
15
16
	'request_format' => array(
17
		'media'      => "(media) An array of media to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Accepts images (image/gif, image/jpeg, image/png) only at this time.<br /><br /><strong>Example</strong>:<br />" .
18
		                "<code>curl \<br />--form 'media[]=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/media/new'</code>",
19
		'media_urls' => "(array) An array of URLs to upload to the post."
20
	),
21
22
	'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/new/',
23
24
	'response_format' => array(
25
		'media' => '(array) Array of uploaded media',
26
		'errors' => '(array) Array of error messages of uploading media failures'
27
	),
28
	'example_request_data' =>  array(
29
		'headers' => array(
30
			'authorization' => 'Bearer YOUR_API_TOKEN'
31
		),
32
		'body' => array(
33
			'media_urls' => "https://s.w.org/about/images/logos/codeispoetry-rgb.png"
34
		)
35
	)
36
) );
37
38
class WPCOM_JSON_API_Upload_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
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
		if ( ! current_user_can( 'upload_files' ) ) {
46
			return new WP_Error( 'unauthorized', 'User cannot upload 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...
47
		}
48
49
		$input = $this->input( true );
50
51
		$has_media      = isset( $input['media'] ) && $input['media'] ? count( $input['media'] ) : false;
52
		$has_media_urls = isset( $input['media_urls'] ) && $input['media_urls'] ? count( $input['media_urls'] ) : false;
53
54
		$media_ids = $files = $errors = array();
55
56
		if ( $has_media ) {
57
			$this->api->trap_wp_die( 'upload_error' );
58
			foreach ( $input['media'] as $index => $media_item ) {
59
				$_FILES['.api.media.item.'] = $media_item;
60
				// check for WP_Error if we ever actually need $media_id
61
				$media_id = media_handle_upload( '.api.media.item.', 0 );
62
				if ( is_wp_error( $media_id ) ) {
63
					if ( 1 === count( $input['media'] ) && ! $has_media_urls ) {
64
						unset( $_FILES['.api.media.item.'] );
65
						return $media_id;
66
					}
67
					$errors[ $index ]['error']   = $media_id->get_error_code();
68
					$errors[ $index ]['message'] = $media_id->get_error_message();
69
				} else {
70
					$media_ids[ $index ] = $media_id;
71
				}
72
				$files[] = $media_item;
73
			}
74
			$this->api->trap_wp_die( null );
75
76
			unset( $_FILES['.api.media.item.'] );
77
		}
78
79
		if ( $has_media_urls ) {
80
			foreach ( $input['media_urls'] as $url ) {
81
				$id = $this->handle_media_sideload( $url );
82
				if ( ! empty( $id ) && is_int( $id ) )
83
					$media_ids[] = $id;
84
			}
85
		}
86
87
		$results = array();
88
		foreach ( $media_ids as $media_id ) {
89
			$results[] = $this->get_media_item( $media_id );
90
		}
91
92
		return array( 'media' => $results, 'errors' => $errors );
93
	}
94
}
95