Completed
Push — sync/json-endpoints-19apr2017 ( 4d1744 )
by
unknown
64:46 queued 53:25
created

class.wpcom-json-api-edit-media-v1-2-endpoint.php (5 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
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;
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...
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
0 ignored issues
show
The doc-type {Number} could not be parsed: Unknown type name "{Number}" 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...
68
	 * @param  {String} $url - image URL to save locally
0 ignored issues
show
The doc-type {String} could not be parsed: Unknown type name "{String}" 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...
69
	 * @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...
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