Completed
Push — fix/image-migration-script ( 1bd527...fdd408 )
by
unknown
09:39
created

VideoPress_Video::get_data()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 43
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 33
nc 32
nop 0
dl 0
loc 43
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * VideoPress video object retrieved from VideoPress servers and parsed.
4
 * @since 1.3
5
 */
6
class VideoPress_Video {
7
	public $version = 3;
8
9
	/**
10
	 * Manifest version returned by remote service.
11
	 *
12
	 * @var string
13
	 * @since 1.3
14
	 */
15
	const manifest_version = '1.5';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected MANIFEST_VERSION).
Loading history...
16
17
	/**
18
	 * Expiration of the video expressed in Unix time
19
	 *
20
	 * @var int
21
	 * @since 1.3
22
	 */
23
	public $expires;
24
25
	/**
26
	 * VideoPress unique identifier
27
	 *
28
	 * @var string
29
	 * @since 1.3
30
	 */
31
	public $guid;
32
33
	/**
34
	 * WordPress.com blog identifier
35
	 *
36
	 * @var int
37
	 * @since 1.5
38
	 */
39
	public $blog_id;
40
41
	/**
42
	 * Remote blog attachment identifier
43
	 *
44
	 * @var int
45
	 * @since 1.5
46
	 */
47
	public $post_id;
48
49
	/**
50
	 * Maximum desired width.
51
	 *
52
	 * @var int
53
	 * @since 1.3
54
	 */
55
	public $maxwidth;
56
57
	/**
58
	 * Video width calculated based on original video dimensions and the requested maxwidth
59
	 *
60
	 * @var int
61
	 * @since 1.3
62
	 */
63
	public $calculated_width;
64
65
	/**
66
	 * Video height calculated based on original video dimensions and the requested maxwidth
67
	 *
68
	 * @var int
69
	 * @since 1.3
70
	 */
71
	public $calculated_height;
72
73
	/**
74
	 * Video title
75
	 *
76
	 * @var string
77
	 * @since 1.3
78
	 */
79
	public $title;
80
81
	/**
82
	 * Video description
83
	 *
84
	 * @var string
85
	 * @since 4.4
86
	 */
87
	public $description;
88
89
	/**
90
	 * Directionality of title text. ltr or rtl
91
	 *
92
	 * @var string
93
	 * @since 1.3
94
	 */
95
	public $text_direction;
96
97
	/**
98
	 * Text and audio language as ISO 639-2 language code
99
	 *
100
	 * @var string
101
	 * @since 1.3
102
	 */
103
	public $language;
104
105
	/**
106
	 * Video duration in whole seconds
107
	 *
108
	 * @var int
109
	 * @since 1.3
110
	 */
111
	public $duration;
112
113
	/**
114
	 * Recommended minimum age of the viewer.
115
	 *
116
	 * @var int
117
	 * @since 1.3
118
	 */
119
	public $age_rating;
120
121
	/**
122
	 * Video author has restricted video embedding or sharing
123
	 *
124
	 * @var bool
125
	 * @since 1.3
126
	 */
127
	public $restricted_embed;
128
129
	/**
130
	 * Poster frame image URI for the given video guid and calculated dimensions.
131
	 *
132
	 * @var string
133
	 * @since 1.3
134
	 */
135
	public $poster_frame_uri;
136
137
	/**
138
	 * Video files associated with the given guid for the calculated dimensions.
139
	 *
140
	 * @var stdClass
141
	 * @since 1.3
142
	 */
143
	public $videos;
144
145
	/**
146
	 * Video player information
147
	 *
148
	 * @var stdClass
149
	 * @since 1.3
150
	 */
151
	public $players;
152
153
	/**
154
	 * Video player skinning preferences including background color and watermark
155
	 *
156
	 * @var array
157
	 * @since 1.5
158
	 */
159
	public $skin;
160
161
	/**
162
	 * Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI
163
	 *
164
	 * @var array
165
	 * @since 1.5
166
	 */
167
	public $captions;
168
169
	/**
170
	 * Setup the object.
171
	 * Request video information from VideoPress servers and process the response.
172
	 *
173
	 * @since 1.3
174
	 * @var string $guid VideoPress unique identifier
175
	 * @var int $maxwidth maximum requested video width. final width and height are calculated on VideoPress servers based on the aspect ratio of the original video upload.
176
	 */
177
	public function __construct( $guid, $maxwidth = 640 ) {
178
		$this->guid = $guid;
179
180
		$maxwidth = absint( $maxwidth );
181
		if ( $maxwidth > 0 )
182
			$this->maxwidth = $maxwidth;
183
184
		$data = $this->get_data();
185
		if ( is_wp_error( $data ) || empty( $data ) ) {
186
			/** This filter is documented in modules/videopress/class.videopress-player.php */
187
			if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) {
188
				// Unlike the Flash player, the new player does it's own error checking, age gate, etc.
189
				$data = (object) array( 'guid' => $guid, 'width' => $maxwidth, 'height' => $maxwidth / 16 * 9 );
190
			} else {
191
				$this->error = $data;
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
192
				return;
193
			}
194
		}
195
196
		if ( isset( $data->blog_id ) )
197
			$this->blog_id = absint( $data->blog_id );
198
199
		if ( isset( $data->post_id ) )
200
			$this->post_id = absint( $data->post_id );
201
202
		if ( isset( $data->title ) && $data->title !== '' )
203
			$this->title = trim( str_replace( '&nbsp;', ' ', $data->title ) );
204
205
		if ( isset( $data->description ) && $data->description !== '' )
206
			$this->description = trim( $data->description );
207
208
		if ( isset( $data->text_direction ) && $data->text_direction === 'rtl' )
209
			$this->text_direction = 'rtl';
210
		else
211
			$this->text_direction = 'ltr';
212
213
		if ( isset( $data->language ) )
214
			$this->language = $data->language;
215
216
		if ( isset( $data->duration ) && $data->duration > 0 )
217
			$this->duration = absint( $data->duration );
218
219
		if ( isset( $data->width ) && $data->width > 0 )
220
			$this->calculated_width = absint( $data->width );
221
222
		if ( isset( $data->height ) && $data->height > 0 )
223
			$this->calculated_height = absint( $data->height );
224
225
		if ( isset( $data->age_rating ) )
226
			$this->age_rating = absint( $this->age_rating );
227
228
		if ( isset( $data->restricted_embed ) && $data->restricted_embed === true )
229
			$this->restricted_embed = true;
230
		else
231
			$this->restricted_embed = false;
232
233
		if ( isset( $data->posterframe ) && $data->posterframe !== '' )
234
			$this->poster_frame_uri = esc_url_raw( $data->posterframe, array( 'http', 'https' ) );
235
236
		if ( isset( $data->mp4 ) || isset( $data->ogv ) ) {
237
			$this->videos = new stdClass();
238
			if ( isset( $data->mp4 ) )
239
				$this->videos->mp4 = $data->mp4;
240
			if ( isset( $data->ogv ) )
241
				$this->videos->ogv = $data->ogv;
242
		}
243
244
		if ( isset( $data->swf ) ) {
245
			if ( ! isset( $this->players ) )
246
				$this->players = new stdClass();
247
			$this->players->swf = $data->swf;
248
		}
249
250
		if ( isset( $data->skin ) )
251
			$this->skin = $data->skin;
252
253
		if ( isset( $data->captions ) )
254
			$this->captions = (array) $data->captions;
255
	}
256
257
	/**
258
	 * Convert an Expires HTTP header value into Unix time for use in WP Cache
259
	 *
260
	 * @since 1.3
261
	 * @var string $expires_header
262
	 * @return int|bool Unix time or false
263
	 */
264
	public static function calculate_expiration( $expires_header ) {
265
		if ( empty( $expires_header ) || ! is_string( $expires_header ) )
266
			return false;
267
268
		if (
269
			class_exists( 'DateTimeZone' )
270
			&& method_exists( 'DateTime', 'createFromFormat' )
271
		) {
272
			$expires_date = DateTime::createFromFormat( 'D, d M Y H:i:s T', $expires_header, new DateTimeZone( 'UTC' ) );
273
			if ( $expires_date instanceOf DateTime )
274
				return date_format( $expires_date, 'U' );
275
		} else {
276
			$expires_array = strptime( $expires_header, '%a, %d %b %Y %H:%M:%S %Z' );
277
			if ( is_array( $expires_array ) && isset( $expires_array['tm_hour'] ) && isset( $expires_array['tm_min'] ) && isset( $expires_array['tm_sec'] ) && isset( $expires_array['tm_mon'] ) && isset( $expires_array['tm_mday'] ) && isset( $expires_array['tm_year'] ) )
278
				return gmmktime( $expires_array['tm_hour'], $expires_array['tm_min'], $expires_array['tm_sec'], 1 + $expires_array['tm_mon'], $expires_array['tm_mday'], 1900 + $expires_array['tm_year'] );
279
		}
280
		return false;
281
	}
282
283
	/**
284
	 * Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds.
285
	 *
286
	 * @since 1.2
287
	 * @param string $url absolute URL
288
	 * @return bool|string host component of the URL, or false if none found
289
	 */
290
	public static function hostname( $url ) {
291
		return parse_url( esc_url_raw( $url ), PHP_URL_HOST );
292
	}
293
294
295
	/**
296
	 * Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname.
297
	 *
298
	 * @since 1.3
299
	 * @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful
300
	 */
301
	private function get_data() {
302
		global $wp_version;
303
304
		$domain = self::hostname( home_url() );
305
		$request_params = array( 'guid' => $this->guid, 'domain' => $domain );
306
		if ( isset( $this->maxwidth ) && $this->maxwidth > 0 )
307
			$request_params['maxwidth'] = $this->maxwidth;
308
309
		$url = 'http://videopress.com/data/wordpress.json';
310
		if ( is_ssl() )
311
			$url = 'https://v.wordpress.com/data/wordpress.json';
312
313
		$response = wp_remote_get( add_query_arg( $request_params, $url ), array(
314
			'redirection' => 1,
315
			'user-agent' => 'VideoPress plugin ' . $this->version . '; WordPress ' . $wp_version . ' (' . home_url('/') . ')',
316
		) );
317
318
		unset( $request_params );
319
		unset( $url );
320
		$response_body = wp_remote_retrieve_body( $response );
321
		$response_code = absint( wp_remote_retrieve_response_code( $response ) );
322
323
		if ( is_wp_error( $response ) ) {
324
			return $response;
325
		} elseif ( $response_code === 400 ) {
326
			return new WP_Error( 'bad_config', __( 'The VideoPress plugin could not communicate with the VideoPress servers. This error is most likely caused by a misconfigured plugin. Please reinstall or upgrade.', 'jetpack' ) );
327
		} elseif ( $response_code === 403 ) {
328
			return new WP_Error( 'http_forbidden', '<p>' . sprintf( __( '<strong>%s</strong> is not an allowed embed site.' , 'jetpack' ), esc_html( $domain ) ) . '</p><p>' . __( 'Publisher limits playback of video embeds.', 'jetpack' ) . '</p>' );
329
		} elseif ( $response_code === 404 ) {
330
			return new WP_Error( 'http_not_found', '<p>' . sprintf( __( 'No data found for VideoPress identifier: <strong>%s</strong>.', 'jetpack' ), $this->guid ) . '</p>' );
331
		} elseif ( $response_code !== 200 || empty( $response_body ) ) {
332
			return;
333
		} else {
334
			$expires_header = wp_remote_retrieve_header( $response, 'Expires' );
335
			if ( ! empty( $expires_header ) ) {
336
				$expires = self::calculate_expiration( $expires_header );
337
				if ( ! empty( $expires ) )
338
					$this->expires = $expires;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expires can also be of type boolean. However, the property $expires is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
339
340
			}
341
			return json_decode( $response_body );
342
		}
343
	}
344
}
345