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

...s.wpcom-json-api-upload-media-v1-1-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
class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
3
4
	/**
5
	 * @param string $path
6
	 * @param int $blog_id
7
	 *
8
	 * @return array|int|WP_Error|void
9
	 */
10
	function callback( $path = '', $blog_id = 0 ) {
11
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
12
		if ( is_wp_error( $blog_id ) ) {
13
			return $blog_id;
14
		}
15
16
		if ( ! 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...
17
			return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
18
		}
19
20
		$input = $this->input( true );
21
22
		$media_files = ! empty( $input['media'] ) ? $input['media'] : array();
23
		$media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array();
24
		$media_attrs = ! empty( $input['attrs'] ) ? $input['attrs'] : array();
25
26
		if ( empty( $media_files ) && empty( $media_urls ) ) {
27
			return new WP_Error( 'invalid_input', 'No media provided in input.' );
28
		}
29
30
		// For jetpack sites, we send the media via a different method, because the sync is very different.
31
		$jetpack_sync = Jetpack_Media_Sync::summon( $blog_id );
32
33
		$jetpack_media_files = array();
34
		$other_media_files   = array();
35
		$media_items         = array();
36
		$errors              = array();
37
38
		// We're splitting out videos for Jetpack sites
39
		foreach ( $media_files as $media_item ) {
40
			if ( preg_match( '@^video/@', $media_item['type'] ) && $jetpack_sync->is_jetpack_site() ) {
41
				$jetpack_media_files[] = $media_item;
42
43
			} else {
44
				$other_media_files[] = $media_item;
45
			}
46
		}
47
48
		// New Jetpack / VideoPress media upload processing
49
        if ( count( $jetpack_media_files ) > 0  ) {
50
	        add_filter( 'upload_mimes', array( $this, 'allow_video_uploads' ) );
51
52
	        $media_items = $jetpack_sync->upload_media( $jetpack_media_files, $this->api );
53
54
	        $errors = $jetpack_sync->get_errors();
55
56
	        foreach ( $media_items as & $media_item ) {
57
		        // More than likely a post has not been created yet, so we pass in the media item we
58
		        // got back from the Jetpack site.
59
		        $post = (object) $media_item['post'];
60
		        $media_item = $this->get_media_item_v1_1( $media_item['ID'], $post, $media_item['file'] );
61
	        }
62
        }
63
64
        // Normal WPCOM upload processing
65
        if ( count( $other_media_files ) > 0 || count( $media_urls ) > 0 ) {
66
	        $create_media = $this->handle_media_creation_v1_1( $other_media_files, $media_urls, $media_attrs );
67
	        $media_ids = $create_media['media_ids'];
68
	        $errors = $create_media['errors'];
69
70
	        $media_items = array();
71
	        foreach ( $media_ids as $media_id ) {
72
		        $media_items[] = $this->get_media_item_v1_1( $media_id );
73
	        }
74
        }
75
76
		if ( count( $media_items ) <= 0 ) {
77
			return $this->api->output_early( 400, array( 'errors' => $errors ) );
78
		}
79
80
		$results = array();
81
		foreach ( $media_items as $media_item ) {
82
			if ( is_wp_error( $media_item ) ) {
83
				$errors[] =  array( 'file' => $media_item['ID'], 'error' => $media_item->get_error_code(), 'message' => $media_item->get_error_message() );
84
85
			} else {
86
				$results[] = $media_item;
87
			}
88
		}
89
90
		$response = array( 'media' => $results );
91
92
		if ( count( $errors ) > 0 ) {
93
			$response['errors'] = $errors;
94
		}
95
96
		return $response;
97
	}
98
99
	/**
100
	 * Force to use the WPCOM API instead of proxy back to the Jetpack API if the blog is a paid Jetpack
101
	 * blog w/ the VideoPress module enabled AND the uploaded file is a video.
102
	 *
103
	 * @param int $blog_id
104
	 * @return bool
105
	 */
106
	function force_wpcom_request( $blog_id ) {
107
108
		// We don't need to do anything if VideoPress is not enabled for the blog.
109
		if ( ! is_videopress_enabled_on_jetpack_blog( $blog_id ) ) {
110
			return false;
111
		}
112
113
		// Check to see if the upload is not a video type, if not then return false.
114
		$input = $this->input( true );
115
		$media_files = ! empty( $input['media'] ) ? $input['media'] : array();
116
117
		foreach ( $media_files as $media_item ) {
118
			if ( ! preg_match( '@^video/@', $media_item['type'] ) ) {
119
				return false;
120
			}
121
		}
122
123
		// The API request should be for a blog w/ Jetpack, A valid plan, has VideoPress enabled,
124
		// and is a video file. Let's let it through.
125
		return true;
126
	}
127
}
128