Completed
Push — fix/typo-pro-promo ( e5832e...fc698e )
by
unknown
29:20
created

class.wpcom-json-api-edit-media-v1-2-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
jetpack_require_lib( 'class.media' );
4
5
new WPCOM_JSON_API_Edit_Media_v1_2_Endpoint( array(
6
	'description' => 'Edit a media item.',
7
	'group'       => 'media',
8
	'stat'        => 'media:1:POST',
9
	'min_version' => '1',
10
	'max_version' => '1.2',
11
	'method'      => 'POST',
12
	'path'        => '/sites/%s/media/%d/edit',
13
	'path_labels' => array(
14
		'$site'    => '(int|string) Site ID or domain',
15
		'$media_ID' => '(int) The ID of the media item',
16
	),
17
18
	'request_format' => array(
19
		'parent_id'   => '(int) ID of the post this media is attached to',
20
		'title'       => '(string) The file name.',
21
		'caption'     => '(string) File caption.',
22
		'description' => '(HTML) Description of the file.',
23
		'alt'         => "(string) Alternative text for image files.",
24
		'artist'      => "(string) Audio Only. Artist metadata for the audio track.",
25
		'album'       => "(string) Audio Only. Album metadata for the audio track.",
26
		'media'       => "(object) An object file to attach to the post. To upload media, " .
27
		                   "the entire request should be multipart/form-data encoded. " .
28
		                   "Multiple media items will be displayed in a gallery. Accepts " .
29
		                   "jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. " .
30
		                   "Audio and Video may also be available. See <code>allowed_file_types</code> " .
31
		                   "in the options response of the site endpoint. " .
32
		                   "<br /><br /><strong>Example</strong>:<br />" .
33
						   "<code>curl \<br />--form 'title=Image' \<br />--form 'media=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
34
		'attrs'       => "(object) An Object of attributes (`title`, `description` and `caption`) " .
35
		                   "are supported to assign to the media uploaded via the `media` or `media_url`",
36
		'media_url'   => "(string) An URL of the image to attach to a post.",
37
	),
38
39
	'response_format' => array(
40
		'ID'               => '(int) The ID of the media item',
41
		'date'             => '(ISO 8601 datetime) The date the media was uploaded',
42
		'post_ID'          => '(int) ID of the post this media is attached to',
43
		'author_ID'        => '(int) ID of the user who uploaded the media',
44
		'URL'              => '(string) URL to the file',
45
		'guid'             => '(string) Unique identifier',
46
		'file'             => '(string) File name',
47
		'extension'        => '(string) File extension',
48
		'mime_type'        => '(string) File mime type',
49
		'title'            => '(string) File name',
50
		'caption'          => '(string) User provided caption of the file',
51
		'description'      => '(string) Description of the file',
52
		'alt'              => '(string)  Alternative text for image files.',
53
		'thumbnails'       => '(object) Media item thumbnail URL options',
54
		'height'           => '(int) (Image & video only) Height of the media item',
55
		'width'            => '(int) (Image & video only) Width of the media item',
56
		'length'           => '(int) (Video & audio only) Duration of the media item, in seconds',
57
		'exif'             => '(array) (Image & audio only) Exif (meta) information about the media item',
58
		'videopress_guid'  => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
59
		'videopress_processing_done'  => '(bool) (Video only) If the video is uploaded on a blog with VideoPress, this will return the status of processing on the video.',
60
		'revision_history' => '(object) An object with `items` and `original` keys. ' .
61
		                        '`original` is an object with data about the original image. ' .
62
		                        '`items` is an array of snapshots of the previous images of this Media. ' .
63
		                        'Each item has the `URL`, `file, `extension`, `date`, and `mime_type` fields.'
64
	),
65
66
	'example_request'      => 'https://public-api.wordpress.com/rest/v1.2/sites/82974409/media/446',
67
	'example_request_data' =>  array(
68
		'headers' => array(
69
			'authorization' => 'Bearer YOUR_API_TOKEN'
70
		),
71
		'body' => array(
72
			'title' => 'Updated Title'
73
		)
74
	)
75
) );
76
77
class WPCOM_JSON_API_Edit_Media_v1_2_Endpoint extends WPCOM_JSON_API_Update_Media_v1_1_Endpoint {
78
	/**
79
	 * Update the media post grabbing the post values from
80
	 * the `attrs` parameter
81
	 *
82
	 * @param  {Number} $media_id - post media ID
83
	 * @param  {Object} $attrs - `attrs` parameter sent from the client in the request body
84
	 * @return bool|WP_Error `WP_Error` on failure. `true` on success.
85
	 */
86
	private function update_by_attrs_parameter( $media_id, $attrs ) {
87
		$insert = array();
88
89
		// Attributes: Title, Caption, Description
90
		if ( isset( $attrs['title'] ) ) {
91
			$insert['post_title'] = $attrs['title'];
92
		}
93
94
		if ( isset( $attrs['caption'] ) ) {
95
			$insert['post_excerpt'] = $attrs['caption'];
96
		}
97
98
		if ( isset( $attrs['description'] ) ) {
99
			$insert['post_content'] = $attrs['description'];
100
		}
101
102
		if ( ! empty( $insert ) ) {
103
			$insert['ID'] = $media_id;
104
			$update_action = wp_update_post( (object) $insert );
105
			if ( is_wp_error( $update_action ) ) {
106
				$update_action;
107
			}
108
		}
109
110
		// Attributes: Alt
111
		if ( isset( $attrs['alt'] ) ) {
112
			$alt = wp_strip_all_tags( $attrs['alt'], true );
113
			update_post_meta( $media_id, Jetpack_Media::$WP_ATTACHMENT_IMAGE_ALT, $alt );
114
		}
115
116
		// Attributes: Artist, Album
117
		$id3_meta = array();
118
119 View Code Duplication
		foreach ( array( 'artist', 'album' ) as $key ) {
120
			if ( isset( $attrs[ $key ] ) ) {
121
				$id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true );
122
			}
123
		}
124
125 View Code Duplication
		if ( ! empty( $id3_meta ) ) {
126
			// Before updating metadata, ensure that the item is audio
127
			$item = $this->get_media_item_v1_1( $media_id );
128
			if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
129
				wp_update_attachment_metadata( $media_id, $id3_meta );
130
			}
131
		}
132
133
		return $update_action;
0 ignored issues
show
The variable $update_action does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
134
	}
135
136
	/**
137
	 * Get the image from a remote url and then save it locally.
138
	 *
139
	 * @param  {Number} $media_id - media post ID
140
	 * @param  {String} $url - image URL to save locally
141
	 * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
0 ignored issues
show
The doc-type {Array|WP_Error} could not be parsed: Unknown type name "{Array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
142
	 */
143
	private function build_file_array_from_url( $media_id, $url ) {
144
		if ( ! $url ) {
145
			return null;
146
		}
147
148
		// if we didn't get a URL, let's bail
149
		$parsed = @parse_url( $url );
150
		if ( empty( $parsed ) ) {
151
			return new WP_Error( 'invalid_url', 'No media provided in url.' );
152
		}
153
154
		// save the remote image into a tmp file
155
		$tmp = download_url( $url );
156
		if ( is_wp_error( $tmp ) ) {
157
			return $tmp;
158
		}
159
160
		return array(
161
			'name' => basename( $url ),
162
			'tmp_name' => $tmp
163
		);
164
	}
165
166
	function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
167
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
168
		if ( is_wp_error( $blog_id ) ) {
169
			return $blog_id;
170
		}
171
172
		$media_item = get_post( $media_id );
173
174
		if ( ! $media_item ) {
175
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
176
		}
177
178
		if ( is_wp_error( $media_item ) ) {
179
			return $media_item;
180
		}
181
182
		if ( ! current_user_can( 'upload_files', $media_id ) ) {
183
			return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
184
		}
185
186
		$input = $this->input( true );
187
188
		// images
189
		$media_url = $input['media_url'];
190
		$media_attrs = $input['attrs'] ? (array) $input['attrs'] : null;
191
192
		if ( isset( $media_url ) ) {
193
			$user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
194
195
			if ( ! $user_can_upload_files  ) {
196
				return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
197
			}
198
199
			// save the temporal file locally 
200
			$temporal_file = $this->build_file_array_from_url( $media_id, $media_url );
201
202
			$edited_media_item = Jetpack_Media::edit_media_file( $media_id, $temporal_file );
203
204
			if ( is_wp_error( $edited_media_item ) ) {
205
				return $edited_media_item;
206
			}
207
208
			unset( $input['media'] );
209
			unset( $input['media_url'] );
210
			unset( $input['attrs'] );
211
212
			// update media through of `attrs` value it it's defined
213
			if ( $media_attrs ) {
214
				$updated_by_attrs = $this->update_by_attrs_parameter( $media_id, $media_attrs );
215
216
				if ( is_wp_error( $updated_by_attrs ) ) {
217
					return $updated_by_attrs;
218
				}
219
			}
220
		}
221
222
		// call parent method
223
		$response = parent::callback( $path, $blog_id, $media_id );
224
225
		// expose `revision_history` object
226
		$response->revision_history = (object) array(
227
			'items'       => (array) Jetpack_Media::get_revision_history( $media_id ),
228
			'original'    => (object) Jetpack_Media::get_original_media( $media_id )
229
		);
230
231
		return $response;
232
	}
233
}
234
235