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

WPCOM_JSON_API_Upload_Media_Endpoint   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
D callback() 0 55 17
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
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...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $has_media of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $has_media_urls of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $has_media_urls of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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