Completed
Push — master-stable ( 53f101...a82972 )
by
unknown
86:26 queued 76:28
created

class.wpcom-json-api-edit-media-v1-2-endpoint.php (1 issue)

Labels
Severity

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
class WPCOM_JSON_API_Edit_Media_v1_2_Endpoint extends WPCOM_JSON_API_Update_Media_v1_1_Endpoint {
6
	/**
7
	 * Update the media post grabbing the post values from
8
	 * the `attrs` parameter
9
	 *
10
	 * @param  {Number} $media_id - post media ID
11
	 * @param  {Object} $attrs - `attrs` parameter sent from the client in the request body
12
	 * @return bool|WP_Error `WP_Error` on failure. `true` on success.
13
	 */
14
	private function update_by_attrs_parameter( $media_id, $attrs ) {
15
		$insert = array();
16
17
		// Attributes: Title, Caption, Description
18
		if ( isset( $attrs['title'] ) ) {
19
			$insert['post_title'] = $attrs['title'];
20
		}
21
22
		if ( isset( $attrs['caption'] ) ) {
23
			$insert['post_excerpt'] = $attrs['caption'];
24
		}
25
26
		if ( isset( $attrs['description'] ) ) {
27
			$insert['post_content'] = $attrs['description'];
28
		}
29
30
		if ( ! empty( $insert ) ) {
31
			$insert['ID'] = $media_id;
32
			$update_action = wp_update_post( (object) $insert );
33
			if ( is_wp_error( $update_action ) ) {
34
				$update_action;
35
			}
36
		}
37
38
		// Attributes: Alt
39
		if ( isset( $attrs['alt'] ) ) {
40
			$alt = wp_strip_all_tags( $attrs['alt'], true );
41
			update_post_meta( $media_id, Jetpack_Media::$WP_ATTACHMENT_IMAGE_ALT, $alt );
42
		}
43
44
		// Attributes: Artist, Album
45
		$id3_meta = array();
46
47 View Code Duplication
		foreach ( array( 'artist', 'album' ) as $key ) {
48
			if ( isset( $attrs[ $key ] ) ) {
49
				$id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true );
50
			}
51
		}
52
53 View Code Duplication
		if ( ! empty( $id3_meta ) ) {
54
			// Before updating metadata, ensure that the item is audio
55
			$item = $this->get_media_item_v1_1( $media_id );
56
			if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
57
				wp_update_attachment_metadata( $media_id, $id3_meta );
58
			}
59
		}
60
61
		return $update_action;
62
	}
63
64
	/**
65
	 * Get the image from a remote url and then save it locally.
66
	 *
67
	 * @param  {Number} $media_id - media post ID
68
	 * @param  {String} $url - image URL to save locally
69
	 * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
70
	 */
71
	private function build_file_array_from_url( $media_id, $url ) {
72
		if ( ! $url ) {
73
			return null;
74
		}
75
76
		// if we didn't get a URL, let's bail
77
		$parsed = @parse_url( $url );
78
		if ( empty( $parsed ) ) {
79
			return new WP_Error( 'invalid_url', 'No media provided in url.' );
80
		}
81
82
		// save the remote image into a tmp file
83
		$tmp = download_url( $url );
84
		if ( is_wp_error( $tmp ) ) {
85
			return $tmp;
86
		}
87
88
		return array(
89
			'name' => basename( $url ),
90
			'tmp_name' => $tmp
91
		);
92
	}
93
94
	function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
95
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
96
		if ( is_wp_error( $blog_id ) ) {
97
			return $blog_id;
98
		}
99
100
		$media_item = get_post( $media_id );
101
102
		if ( ! $media_item ) {
103
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
104
		}
105
106
		if ( is_wp_error( $media_item ) ) {
107
			return $media_item;
108
		}
109
110
		if ( ! current_user_can( 'upload_files', $media_id ) ) {
111
			return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
112
		}
113
114
		$input = $this->input( true );
115
116
		// images
117
		$media_url = $input['media_url'];
118
		$media_attrs = $input['attrs'] ? (array) $input['attrs'] : null;
119
120
		if ( isset( $media_url ) ) {
121
			$user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
0 ignored issues
show
The method is_authorized_with_upload_token() does not seem to exist on object<WPCOM_JSON_API>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
			if ( ! $user_can_upload_files  ) {
124
				return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
125
			}
126
127
			// save the temporal file locally 
128
			$temporal_file = $this->build_file_array_from_url( $media_id, $media_url );
129
130
			$edited_media_item = Jetpack_Media::edit_media_file( $media_id, $temporal_file );
131
132
			if ( is_wp_error( $edited_media_item ) ) {
133
				return $edited_media_item;
134
			}
135
136
			unset( $input['media'] );
137
			unset( $input['media_url'] );
138
			unset( $input['attrs'] );
139
140
			// update media through of `attrs` value it it's defined
141
			if ( $media_attrs ) {
142
				$updated_by_attrs = $this->update_by_attrs_parameter( $media_id, $media_attrs );
143
144
				if ( is_wp_error( $updated_by_attrs ) ) {
145
					return $updated_by_attrs;
146
				}
147
			}
148
		}
149
150
		// call parent method
151
		$response = parent::callback( $path, $blog_id, $media_id );
152
153
		// expose `revision_history` object
154
		$response->revision_history = (object) array(
155
			'items'       => (array) Jetpack_Media::get_revision_history( $media_id ),
156
			'original'    => (object) Jetpack_Media::get_original_media( $media_id )
157
		);
158
159
		return $response;
160
	}
161
}
162
163