Completed
Push — sync/blowery/r159288-wpcom-150... ( 8dc5ae )
by
unknown
14:18
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() ) {
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
		$is_jetpack_site = false;
31
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
32
			// For jetpack sites, we send the media via a different method, because the sync is very different.
33
			$jetpack_sync = Jetpack_Media_Sync::summon( $blog_id );
34
			$is_jetpack_site = $jetpack_sync->is_jetpack_site();
35
		}
36
37
		$jetpack_media_files = array();
38
		$other_media_files   = array();
39
		$media_items         = array();
40
		$errors              = array();
41
42
		// We're splitting out videos for Jetpack sites
43
		foreach ( $media_files as $media_item ) {
44
			if ( preg_match( '@^video/@', $media_item['type'] ) && $is_jetpack_site ) {
45
				$jetpack_media_files[] = $media_item;
46
47
			} else {
48
				$other_media_files[] = $media_item;
49
			}
50
		}
51
52
		// New Jetpack / VideoPress media upload processing
53
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
54
			if ( count( $jetpack_media_files ) > 0 ) {
55
				add_filter( 'upload_mimes', array( $this, 'allow_video_uploads' ) );
56
57
				$media_items = $jetpack_sync->upload_media( $jetpack_media_files, $this->api );
0 ignored issues
show
The variable $jetpack_sync 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...
58
59
				$errors = $jetpack_sync->get_errors();
60
61
				foreach ( $media_items as & $media_item ) {
62
					// More than likely a post has not been created yet, so we pass in the media item we
63
					// got back from the Jetpack site.
64
					$post       = (object) $media_item['post'];
65
					$media_item = $this->get_media_item_v1_1( $media_item['ID'], $post, $media_item['file'] );
66
				}
67
			}
68
		}
69
70
        // Normal WPCOM upload processing
71
        if ( count( $other_media_files ) > 0 || count( $media_urls ) > 0 ) {
72
	        $create_media = $this->handle_media_creation_v1_1( $other_media_files, $media_urls, $media_attrs );
73
	        $media_ids = $create_media['media_ids'];
74
	        $errors = $create_media['errors'];
75
76
	        $media_items = array();
77
	        foreach ( $media_ids as $media_id ) {
78
		        $media_items[] = $this->get_media_item_v1_1( $media_id );
79
	        }
80
        }
81
82
		if ( count( $media_items ) <= 0 ) {
83
			return $this->api->output_early( 400, array( 'errors' => $errors ) );
84
		}
85
86
		$results = array();
87
		foreach ( $media_items as $media_item ) {
88
			if ( is_wp_error( $media_item ) ) {
89
				$errors[] =  array( 'file' => $media_item['ID'], 'error' => $media_item->get_error_code(), 'message' => $media_item->get_error_message() );
90
91
			} else {
92
				$results[] = $media_item;
93
			}
94
		}
95
96
		$response = array( 'media' => $results );
97
98
		if ( count( $errors ) > 0 ) {
99
			$response['errors'] = $errors;
100
		}
101
102
		return $response;
103
	}
104
105
	/**
106
	 * Force to use the WPCOM API instead of proxy back to the Jetpack API if the blog is a paid Jetpack
107
	 * blog w/ the VideoPress module enabled AND the uploaded file is a video.
108
	 *
109
	 * @param int $blog_id
110
	 * @return bool
111
	 */
112
	function force_wpcom_request( $blog_id ) {
113
114
		// We don't need to do anything if VideoPress is not enabled for the blog.
115
		if ( ! is_videopress_enabled_on_jetpack_blog( $blog_id ) ) {
116
			return false;
117
		}
118
119
		// Check to see if the upload is not a video type, if not then return false.
120
		$input = $this->input( true );
121
		$media_files = ! empty( $input['media'] ) ? $input['media'] : array();
122
123
		foreach ( $media_files as $media_item ) {
124
			if ( ! preg_match( '@^video/@', $media_item['type'] ) ) {
125
				return false;
126
			}
127
		}
128
129
		// The API request should be for a blog w/ Jetpack, A valid plan, has VideoPress enabled,
130
		// and is a video file. Let's let it through.
131
		return true;
132
	}
133
}
134