summaryrefslogtreecommitdiff
blob: a8c3a6b80491efc228eaba4b2c092f00a6bf60e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
 * VideoPress video object retrieved from VideoPress servers and parsed.
 *
 * @since 1.3
 */
class VideoPress_Video {
	public $version = 3;

	/**
	 * Manifest version returned by remote service.
	 *
	 * @var string
	 * @since 1.3
	 */
	const manifest_version = '1.5';

	/**
	 * Expiration of the video expressed in Unix time
	 *
	 * @var int
	 * @since 1.3
	 */
	public $expires;

	/**
	 * VideoPress unique identifier
	 *
	 * @var string
	 * @since 1.3
	 */
	public $guid;

	/**
	 * WordPress.com blog identifier
	 *
	 * @var int
	 * @since 1.5
	 */
	public $blog_id;

	/**
	 * Remote blog attachment identifier
	 *
	 * @var int
	 * @since 1.5
	 */
	public $post_id;

	/**
	 * Maximum desired width.
	 *
	 * @var int
	 * @since 1.3
	 */
	public $maxwidth;

	/**
	 * Video width calculated based on original video dimensions and the requested maxwidth
	 *
	 * @var int
	 * @since 1.3
	 */
	public $calculated_width;

	/**
	 * Video height calculated based on original video dimensions and the requested maxwidth
	 *
	 * @var int
	 * @since 1.3
	 */
	public $calculated_height;

	/**
	 * Video title
	 *
	 * @var string
	 * @since 1.3
	 */
	public $title;

	/**
	 * Video description
	 *
	 * @var string
	 * @since 4.4
	 */
	public $description;

	/**
	 * Directionality of title text. ltr or rtl
	 *
	 * @var string
	 * @since 1.3
	 */
	public $text_direction;

	/**
	 * Text and audio language as ISO 639-2 language code
	 *
	 * @var string
	 * @since 1.3
	 */
	public $language;

	/**
	 * Video duration in whole seconds
	 *
	 * @var int
	 * @since 1.3
	 */
	public $duration;

	/**
	 * Recommended minimum age of the viewer.
	 *
	 * @var int
	 * @since 1.3
	 */
	public $age_rating;

	/**
	 * Video author has restricted video embedding or sharing
	 *
	 * @var bool
	 * @since 1.3
	 */
	public $restricted_embed;

	/**
	 * Poster frame image URI for the given video guid and calculated dimensions.
	 *
	 * @var string
	 * @since 1.3
	 */
	public $poster_frame_uri;

	/**
	 * Video files associated with the given guid for the calculated dimensions.
	 *
	 * @var stdClass
	 * @since 1.3
	 */
	public $videos;

	/**
	 * Video player information
	 *
	 * @var stdClass
	 * @since 1.3
	 */
	public $players;

	/**
	 * Video player skinning preferences including background color and watermark
	 *
	 * @var array
	 * @since 1.5
	 */
	public $skin;

	/**
	 * Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI
	 *
	 * @var array
	 * @since 1.5
	 */
	public $captions;

	/**
	 * Setup the object.
	 * Request video information from VideoPress servers and process the response.
	 *
	 * @since 1.3
	 * @var string $guid VideoPress unique identifier
	 * @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.
	 */
	public function __construct( $guid, $maxwidth = 640 ) {
		$this->guid = $guid;

		$maxwidth = absint( $maxwidth );
		if ( $maxwidth > 0 ) {
			$this->maxwidth = $maxwidth;
		}

		$data = $this->get_data();
		if ( is_wp_error( $data ) || empty( $data ) ) {
			/** This filter is documented in modules/videopress/class.videopress-player.php */
			if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) {
				// Unlike the Flash player, the new player does it's own error checking, age gate, etc.
				$data = (object) array(
					'guid'   => $guid,
					'width'  => $maxwidth,
					'height' => $maxwidth / 16 * 9,
				);
			} else {
				$this->error = $data;
				return;
			}
		}

		if ( isset( $data->blog_id ) ) {
			$this->blog_id = absint( $data->blog_id );
		}

		if ( isset( $data->post_id ) ) {
			$this->post_id = absint( $data->post_id );
		}

		if ( isset( $data->title ) && $data->title !== '' ) {
			$this->title = trim( str_replace( '&nbsp;', ' ', $data->title ) );
		}

		if ( isset( $data->description ) && $data->description !== '' ) {
			$this->description = trim( $data->description );
		}

		if ( isset( $data->text_direction ) && $data->text_direction === 'rtl' ) {
			$this->text_direction = 'rtl';
		} else {
			$this->text_direction = 'ltr';
		}

		if ( isset( $data->language ) ) {
			$this->language = $data->language;
		}

		if ( isset( $data->duration ) && $data->duration > 0 ) {
			$this->duration = absint( $data->duration );
		}

		if ( isset( $data->width ) && $data->width > 0 ) {
			$this->calculated_width = absint( $data->width );
		}

		if ( isset( $data->height ) && $data->height > 0 ) {
			$this->calculated_height = absint( $data->height );
		}

		if ( isset( $data->age_rating ) ) {
			$this->age_rating = absint( $this->age_rating );
		}

		if ( isset( $data->restricted_embed ) && $data->restricted_embed === true ) {
			$this->restricted_embed = true;
		} else {
			$this->restricted_embed = false;
		}

		if ( isset( $data->posterframe ) && $data->posterframe !== '' ) {
			$this->poster_frame_uri = esc_url_raw( $data->posterframe, array( 'http', 'https' ) );
		}

		if ( isset( $data->mp4 ) || isset( $data->ogv ) ) {
			$this->videos = new stdClass();
			if ( isset( $data->mp4 ) ) {
				$this->videos->mp4 = $data->mp4;
			}
			if ( isset( $data->ogv ) ) {
				$this->videos->ogv = $data->ogv;
			}
		}

		if ( isset( $data->swf ) ) {
			if ( ! isset( $this->players ) ) {
				$this->players = new stdClass();
			}
			$this->players->swf = $data->swf;
		}

		if ( isset( $data->skin ) ) {
			$this->skin = $data->skin;
		}

		if ( isset( $data->captions ) ) {
			$this->captions = (array) $data->captions;
		}
	}

	/**
	 * Convert an Expires HTTP header value into Unix time for use in WP Cache
	 *
	 * @since 1.3
	 * @var string $expires_header
	 * @return int|bool Unix time or false
	 */
	public static function calculate_expiration( $expires_header ) {
		if ( empty( $expires_header ) || ! is_string( $expires_header ) ) {
			return false;
		}

		if (
			class_exists( 'DateTimeZone' )
			&& method_exists( 'DateTime', 'createFromFormat' )
		) {
			$expires_date = DateTime::createFromFormat( 'D, d M Y H:i:s T', $expires_header, new DateTimeZone( 'UTC' ) );
			if ( $expires_date instanceof DateTime ) {
				return date_format( $expires_date, 'U' );
			}
		} else {
			$expires_array = strptime( $expires_header, '%a, %d %b %Y %H:%M:%S %Z' );
			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'] ) ) {
				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'] );
			}
		}
		return false;
	}

	/**
	 * Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds.
	 *
	 * @since 1.2
	 * @param string $url absolute URL
	 * @return bool|string host component of the URL, or false if none found
	 */
	public static function hostname( $url ) {
		return parse_url( esc_url_raw( $url ), PHP_URL_HOST );
	}


	/**
	 * Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname.
	 *
	 * @since 1.3
	 * @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful
	 */
	private function get_data() {
		global $wp_version;

		$domain         = self::hostname( home_url() );
		$request_params = array(
			'guid'   => $this->guid,
			'domain' => $domain,
		);
		if ( isset( $this->maxwidth ) && $this->maxwidth > 0 ) {
			$request_params['maxwidth'] = $this->maxwidth;
		}

		$url = 'http://videopress.com/data/wordpress.json';
		if ( is_ssl() ) {
			$url = 'https://v.wordpress.com/data/wordpress.json';
		}

		$response = wp_remote_get(
			add_query_arg( $request_params, $url ),
			array(
				'redirection' => 1,
				'user-agent'  => 'VideoPress plugin ' . $this->version . '; WordPress ' . $wp_version . ' (' . home_url( '/' ) . ')',
			)
		);

		unset( $request_params );
		unset( $url );
		$response_body = wp_remote_retrieve_body( $response );
		$response_code = absint( wp_remote_retrieve_response_code( $response ) );

		if ( is_wp_error( $response ) ) {
			return $response;
		} elseif ( $response_code === 400 ) {
			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' ) );
		} elseif ( $response_code === 403 ) {
			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>' );
		} elseif ( $response_code === 404 ) {
			return new WP_Error( 'http_not_found', '<p>' . sprintf( __( 'No data found for VideoPress identifier: <strong>%s</strong>.', 'jetpack' ), $this->guid ) . '</p>' );
		} elseif ( $response_code !== 200 || empty( $response_body ) ) {
			return;
		} else {
			$expires_header = wp_remote_retrieve_header( $response, 'Expires' );
			if ( ! empty( $expires_header ) ) {
				$expires = self::calculate_expiration( $expires_header );
				if ( ! empty( $expires ) ) {
					$this->expires = $expires;
				}
			}
			return json_decode( $response_body );
		}
	}
}