PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
jetpack / modules / videopress / class.videopress-player.php

class.videopress-player.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.1, at modules/videopress/class.videopress-player.php

936 lines 35.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 use Automattic\Jetpack\VideoPress\Inline_Player;
3 use Automattic\Jetpack\VideoPress\Jwt_Token_Bridge;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit( 0 );
7 }
8
9 /**
10 * VideoPress playback module markup generator.
11 *
12 * @since 1.3
13 */
14 class VideoPress_Player {
15 /**
16 * Video data for the requested guid and maximum width
17 *
18 * @since 1.3
19 * @var VideoPress_Video
20 */
21 protected $video;
22
23 /**
24 * DOM identifier of the video container
25 *
26 * @var string
27 * @since 1.3
28 */
29 protected $video_container_id;
30
31 /**
32 * DOM identifier of the video element (video, object, embed)
33 *
34 * @var string
35 * @since 1.3
36 */
37 protected $video_id;
38
39 /**
40 * Array of playback options: force_flash or freedom
41 *
42 * @var array
43 * @since 1.3
44 */
45 protected $options;
46
47 /**
48 * Array of video GUIDs shown and their counts,
49 * moved from the old VideoPress class.
50 *
51 * @var array
52 */
53 public static $shown = array();
54
55 /**
56 * Fallback video title.
57 *
58 * @var ?string
59 */
60 protected $title;
61
62 /**
63 * Initiate a player object based on shortcode values and possible blog-level option overrides
64 *
65 * @since 1.3
66 * @param string $guid VideoPress unique identifier.
67 * @param int $maxwidth Maximum desired width of the video player if specified.
68 * @param array $options Player customizations.
69 */
70 public function __construct( $guid, $maxwidth = 0, $options = array() ) {
71 if ( empty( self::$shown[ $guid ] ) ) {
72 self::$shown[ $guid ] = 0;
73 }
74
75 ++self::$shown[ $guid ];
76
77 $this->video_container_id = 'v-' . $guid . '-' . self::$shown[ $guid ];
78 $this->video_id = $this->video_container_id . '-video';
79
80 if ( is_array( $options ) ) {
81 $this->options = $options;
82 } else {
83 $this->options = array();
84 }
85
86 // set up the video
87 $cache_key = null;
88
89 // disable cache in debug mode
90 if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
91 $cached_video = null;
92 } else {
93 $cache_key_pieces = array( 'video' );
94
95 if ( is_multisite() && is_subdomain_install() ) {
96 $cache_key_pieces[] = get_current_blog_id();
97 }
98
99 $cache_key_pieces[] = $guid;
100 if ( $maxwidth > 0 ) {
101 $cache_key_pieces[] = $maxwidth;
102 }
103 if ( is_ssl() ) {
104 $cache_key_pieces[] = 'ssl';
105 }
106 $cache_key = implode( '-', $cache_key_pieces );
107 unset( $cache_key_pieces );
108 $cached_video = wp_cache_get( $cache_key, 'video' );
109 }
110 if ( empty( $cached_video ) ) {
111 $video = new VideoPress_Video( $guid, $maxwidth );
112 if ( isset( $video->error ) ) {
113 $this->video = $video->error;
114 return;
115 } elseif ( is_wp_error( $video ) ) {
116 $this->video = $video;
117 return;
118 }
119
120 $this->video = $video;
121 unset( $video );
122
123 if ( ! defined( 'WP_DEBUG' ) || WP_DEBUG !== true ) {
124 $expire = 3600;
125 if ( isset( $this->video->expires ) && is_int( $this->video->expires ) ) {
126 $expires_diff = time() - $this->video->expires;
127 if ( $expires_diff > 0 && $expires_diff < 86400 ) { // allowed range: 1 second to 1 day
128 $expire = $expires_diff;
129 }
130 unset( $expires_diff );
131 }
132
133 wp_cache_set( $cache_key, serialize( $this->video ), 'video', $expire ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
134 unset( $expire );
135 }
136 } else {
137 $this->video = unserialize( $cached_video ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- Make sure to unserialize as VideoPress_Video class.
138 }
139 unset( $cache_key );
140 unset( $cached_video );
141 }
142
143 /**
144 * Wrap output in a VideoPress player container.
145 *
146 * @since 1.3
147 * @param string $content HTML string.
148 * @return string HTML string or blank string if nothing to wrap.
149 */
150 private function html_wrapper( $content ) {
151 if ( empty( $content ) ) {
152 return '';
153 } else {
154 return '<div id="' . esc_attr( $this->video_container_id ) . '" class="video-player">' . $content . '</div>';
155 }
156 }
157
158 /**
159 * Output content suitable for a feed reader displaying RSS or Atom feeds
160 * We do not display error messages in the feed view due to caching concerns.
161 * Flash content presented using <embed> markup for feed reader compatibility.
162 *
163 * @since 1.3
164 * @return string HTML string or empty string if error
165 */
166 public function as_xml() {
167 if ( empty( $this->video ) || is_wp_error( $this->video ) ) {
168 return '';
169 }
170
171 if ( isset( $this->options['force_flash'] ) && true === $this->options['force_flash'] ) {
172 $content = $this->flash_embed();
173
174 } else {
175 $content = $this->html5_static();
176 }
177
178 return $this->html_wrapper( $content );
179 }
180
181 /**
182 * Video player markup for best matching the current request and publisher options
183 *
184 * @since 1.3
185 * @return string HTML markup string or empty string if no video property found
186 */
187 public function as_html() {
188 if ( empty( $this->video ) ) {
189 $content = '';
190
191 } elseif ( is_wp_error( $this->video ) ) {
192 $content = $this->error_message( $this->video );
193
194 } elseif ( isset( $this->options['force_flash'] ) && true === $this->options['force_flash'] ) {
195 $content = $this->flash_object();
196
197 } elseif ( isset( $this->video->restricted_embed ) && true === $this->video->restricted_embed ) {
198
199 if ( $this->options['forcestatic'] ) {
200 $content = $this->flash_object();
201
202 } else {
203 $content = $this->html5_dynamic();
204 }
205 } elseif ( isset( $this->options['freedom'] ) && true === $this->options['freedom'] ) {
206 $content = $this->html5_static();
207
208 } else {
209 $content = $this->html5_dynamic();
210 }
211
212 return $this->html_wrapper( $content );
213 }
214
215 /**
216 * Display an error message to users capable of doing something about the error
217 *
218 * @since 1.3
219 * @uses current_user_can() to test if current user has edit_posts capability.
220 * @param WP_Error $error WordPress error.
221 * @return string HTML string
222 */
223 private function error_message( $error ) {
224 if ( ! current_user_can( 'edit_posts' ) || empty( $error ) ) {
225 return '';
226 }
227
228 $html = '<div class="videopress-error" style="background-color:rgb(255,0,0);color:rgb(255,255,255);font-family:font-family:\'Helvetica Neue\',Arial,Helvetica,\'Nimbus Sans L\',sans-serif;font-size:140%;min-height:10em;padding-top:1.5em;padding-bottom:1.5em">';
229 /* translators: %s is 'VideoPress' */
230 $html .= '<h1 style="font-size:180%;font-style:bold;line-height:130%;text-decoration:underline">' . esc_html( sprintf( __( '%s Error', 'jetpack' ), 'VideoPress' ) ) . '</h1>';
231 foreach ( $error->get_error_messages() as $message ) {
232 $html .= $message;
233 }
234 $html .= '</div>';
235 return $html;
236 }
237
238 /**
239 * Rating agencies and industry associations require a potential viewer verify their age before a video or its poster frame are displayed.
240 * Content rated for audiences 17 years of age or older requires such verification across multiple rating agencies and industry associations
241 *
242 * @since 1.3
243 * @return bool true if video requires the viewer verify they are 17 years of age or older
244 */
245 private function age_gate_required() {
246 if ( isset( $this->video->age_rating ) && $this->video->age_rating >= 17 ) {
247 return true;
248 } else {
249 return false;
250 }
251 }
252
253 /**
254 * Select a date of birth using HTML form elements.
255 *
256 * @since 1.5
257 * @return string HTML markup
258 */
259 private function html_age_gate() {
260 global $wp_locale;
261 $text_align = 'left';
262 if ( $this->video->text_direction === 'rtl' ) {
263 $text_align = 'right';
264 }
265
266 $html = '<div class="videopress-age-gate" style="margin:0 60px">';
267 $html .= '<p class="instructions" style="color:rgb(255, 255, 255);font-size:21px;padding-top:60px;padding-bottom:20px;text-align:' . $text_align . '">' . esc_html( __( 'This video is intended for mature audiences.', 'jetpack' ) ) . '<br />' . esc_html( __( 'Please verify your birthday.', 'jetpack' ) ) . '</p>';
268 $html .= '<fieldset id="birthday" style="border:0 none;text-align:' . $text_align . ';padding:0;">';
269 $inputs_style = 'border:1px solid #444;margin-';
270 if ( $this->video->text_direction === 'rtl' ) {
271 $inputs_style .= 'left';
272 } else {
273 $inputs_style .= 'right';
274 }
275 $inputs_style .= ':10px;background-color:rgb(0, 0, 0);font-size:14px;color:rgb(255,255,255);padding:4px 6px;line-height: 2em;vertical-align: middle';
276
277 /**
278 * Display a list of months in the Gregorian calendar.
279 * Set values to 0-based to match JavaScript Date.
280 *
281 * @link https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date Mozilla JavaScript Reference: Date
282 */
283 $html .= '<select name="month" style="' . $inputs_style . '">';
284
285 for ( $i = 0; $i < 12; $i++ ) {
286 $html .= '<option value="' . esc_attr( $i ) . '">' . esc_html( $wp_locale->get_month( $i + 1 ) ) . '</option>';
287 }
288 $html .= '</select>';
289
290 /**
291 * Todo: numdays variance by month.
292 */
293 $html .= '<select name="day" style="' . $inputs_style . '">';
294 for ( $i = 1; $i < 32; $i++ ) {
295 $html .= '<option>' . $i . '</option>';
296 }
297 $html .= '</select>';
298
299 /**
300 * Current record for human life is 122. Go back 130 years and no one is left out.
301 * Don't ask infants younger than 2 for their birthday
302 * Default to 13
303 */
304 $html .= '<select name="year" style="' . $inputs_style . '">';
305 $start_year = gmdate( 'Y' ) - 2;
306 $default_year = $start_year - 11;
307 $end_year = $start_year - 128;
308 for ( $year = $start_year; $year > $end_year; $year-- ) {
309 $html .= '<option';
310 if ( $year === $default_year ) {
311 $html .= ' selected="selected"';
312 }
313 $html .= '>' . $year . '</option>';
314 }
315 unset( $start_year );
316 unset( $default_year );
317 unset( $end_year );
318 $html .= '</select>';
319
320 $html .= '<input type="submit" value="' . __( 'Submit', 'jetpack' ) . '" style="cursor:pointer;border-radius: 1em;border:1px solid #333;background-color:#333;background:-webkit-gradient( linear, left top, left bottom, color-stop(0.0, #444), color-stop(1, #111) );background:-moz-linear-gradient(center top, #444 0%, #111 100%);font-size:13px;padding:4px 10px 5px;line-height:1em;vertical-align:top;color:white;text-decoration:none;margin:0" />';
321
322 $html .= '</fieldset>';
323 $html .= '<p style="padding-top:20px;padding-bottom:60px;text-align:' . $text_align . ';"><a rel="nofollow noopener noreferrer" href="https://videopress.com/" target="_blank" style="color:rgb(128,128,128);text-decoration:underline;font-size:15px">' . __( 'More information', 'jetpack' ) . '</a></p>';
324
325 $html .= '</div>';
326 return $html;
327 }
328
329 /**
330 * Return HTML5 video static markup for the given video parameters.
331 * Use default browser player controls.
332 * No Flash fallback.
333 *
334 * @since 1.2
335 * @link https://html.spec.whatwg.org/multipage/media.html#the-video-element HTML5 video
336 * @return string HTML5 video element and children
337 */
338 private function html5_static() {
339 wp_enqueue_script( 'videopress' );
340 $thumbnail = esc_url( $this->video->poster_frame_uri );
341 $html = "<video id=\"{$this->video_id}\" width=\"{$this->video->calculated_width}\" height=\"{$this->video->calculated_height}\" poster=\"$thumbnail\" controls=\"true\"";
342
343 $preload = 'metadata';
344 if ( isset( $this->options['preloadContent'] ) && videopress_is_valid_preload( $this->options['preloadContent'] ) ) {
345 $preload = $this->options['preloadContent'];
346 }
347
348 if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true ) {
349 $html .= ' autoplay="true"';
350 } else {
351 $html .= ' preload="' . esc_attr( $preload ) . '"';
352 }
353 if ( isset( $this->video->text_direction ) ) {
354 $html .= ' dir="' . esc_attr( $this->video->text_direction ) . '"';
355 }
356 if ( isset( $this->video->language ) ) {
357 $html .= ' lang="' . esc_attr( $this->video->language ) . '"';
358 }
359 $html .= '>';
360 if (
361 ( ! isset( $this->options['freedom'] ) || $this->options['freedom'] === false )
362 && isset( $this->video->videos->mp4 )
363 ) {
364 $mp4 = $this->video->videos->mp4->url;
365 if ( ! empty( $mp4 ) ) {
366 $html .= '<source src="' . esc_url( $mp4 ) . '" type="video/mp4; codecs=&quot;' . esc_attr( $this->video->videos->mp4->codecs ) . '&quot;" />';
367 }
368 unset( $mp4 );
369 }
370
371 if ( isset( $this->video->videos->ogv ) ) {
372 $ogg = $this->video->videos->ogv->url;
373 if ( ! empty( $ogg ) ) {
374 $html .= '<source src="' . esc_url( $ogg ) . '" type="video/ogg; codecs=&quot;' . esc_attr( $this->video->videos->ogv->codecs ) . '&quot;" />';
375 }
376
377 unset( $ogg );
378 }
379
380 $html .= '<div><img alt="';
381 if ( isset( $this->video->title ) ) {
382 $html .= esc_attr( $this->video->title );
383 }
384 $html .= '" src="' . $thumbnail . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" /></div>';
385 if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) {
386 /* translators: %s url to the gnu.org website */
387 $html .= '<p class="robots-nocontent">' . sprintf( __( 'You do not have sufficient <a rel="nofollow noopener noreferrer" href="%s" target="_blank">freedom levels</a> to view this video. Support free software and upgrade.', 'jetpack' ), 'https://www.gnu.org/philosophy/free-sw.html' ) . '</p>';
388 } elseif ( isset( $this->video->title ) ) {
389 $html .= '<p>' . esc_html( $this->video->title ) . '</p>';
390 }
391 $html .= '</video>';
392 return $html;
393 }
394
395 /**
396 * Click to play dynamic HTML5-capable player.
397 * The player displays a video preview section including poster frame,
398 * video title, play button and watermark on the original page load
399 * and calculates the playback capabilities of the browser. The video player
400 * is loaded when the visitor clicks on the video preview area.
401 * If Flash Player 10 or above is available the browser will display
402 * the Flash version of the video. If HTML5 video appears to be supported
403 * and the browser may be capable of MP4 (H.264, AAC) or OGV (Theora, Vorbis)
404 * playback the browser will display its native HTML5 player.
405 *
406 * @since 1.5
407 * @return string HTML markup
408 */
409 private function html5_dynamic() {
410
411 /**
412 * Filter the VideoPress legacy player feature
413 *
414 * This filter allows you to control whether the legacy VideoPress player should be used
415 * instead of the improved one.
416 *
417 * @module videopress
418 *
419 * @since 3.7.0
420 *
421 * @param boolean $videopress_use_legacy_player
422 */
423 if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) {
424 return $this->html5_dynamic_next();
425 }
426
427 wp_enqueue_script( 'videopress' );
428 $video_placeholder_id = $this->video_container_id . '-placeholder';
429 $age_gate_required = $this->age_gate_required();
430 $width = absint( $this->video->calculated_width );
431 $height = absint( $this->video->calculated_height );
432
433 $html = '<div id="' . $video_placeholder_id . '" class="videopress-placeholder" style="';
434 if ( $age_gate_required ) {
435 $html .= "min-width:{$width}px;min-height:{$height}px";
436 } else {
437 $html .= "width:{$width}px;height:{$height}px";
438 }
439 $html .= ';display:none;cursor:pointer !important;position:relative;';
440 if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) ) {
441 $html .= 'background-color:' . esc_attr( $this->video->skin->background_color ) . ';';
442 }
443 $html .= 'font-family: \'Helvetica Neue\',Arial,Helvetica,\'Nimbus Sans L\',sans-serif;font-weight:bold;font-size:18px">' . PHP_EOL;
444
445 /**
446 * Do not display a poster frame, title, or any other content hints for mature content.
447 */
448 if ( ! $age_gate_required ) {
449 if ( ! empty( $this->video->title ) ) {
450 $html .= '<div class="videopress-title" style="display:inline;position:absolute;margin:20px 20px 0 20px;padding:4px 8px;vertical-align:top;text-align:';
451 if ( $this->video->text_direction === 'rtl' ) {
452 $html .= 'right" dir="rtl"';
453 } else {
454 $html .= 'left" dir="ltr"';
455 }
456 if ( isset( $this->video->language ) ) {
457 $html .= ' lang="' . esc_attr( $this->video->language ) . '"';
458 }
459 $html .= '><span style="padding:3px 0;line-height:1.5em;';
460 if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) ) {
461 $html .= 'background-color:';
462 if ( $this->video->skin->background_color === 'rgb(0,0,0)' ) {
463 $html .= 'rgba(0,0,0,0.8)';
464 } else {
465 $html .= esc_attr( $this->video->skin->background_color );
466 }
467 $html .= ';';
468 }
469 $html .= 'color:rgb(255,255,255)">' . esc_html( $this->video->title ) . '</span></div>';
470 }
471 $html .= '<img class="videopress-poster" alt="';
472 if ( ! empty( $this->video->title ) ) {
473 /* translators: %s is the video title */
474 $html .= esc_attr( $this->video->title ) . '" title="' . esc_attr( sprintf( _x( 'Watch: %s', 'watch a video title', 'jetpack' ), $this->video->title ) );
475 }
476 $html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $width . '" height="' . $height . '" />' . PHP_EOL;
477
478 // style a play button hovered over the poster frame
479 $html .= '<div class="play-button"><span style="z-index:2;display:block;position:absolute;top:50%;left:50%;text-align:center;vertical-align:middle;color:rgb(255,255,255);opacity:0.9;margin:0 0 0 -0.45em;padding:0;line-height:0;font-size:500%;text-shadow:0 0 40px rgba(0,0,0,0.5)">&#9654;</span></div>' . PHP_EOL;
480
481 // watermark
482 if ( isset( $this->video->skin ) && isset( $this->video->skin->watermark ) ) {
483 $html .= '<div style="position:relative;margin-top:-40px;height:25px;margin-bottom:35px;';
484 if ( $this->video->text_direction === 'rtl' ) {
485 $html .= 'margin-left:20px;text-align:left;';
486 } else {
487 $html .= 'margin-right:20px;text-align:right;';
488 }
489 $html .= 'vertical-align:bottom;z-index:3">';
490 $html .= '<img alt="" src="' . esc_url( $this->video->skin->watermark, array( 'http', 'https' ) ) . '" width="90" height="13" style="background-color:transparent;background-image:none;background-repeat:no-repeat;border:none;margin:0;padding:0"/>';
491 $html .= '</div>' . PHP_EOL;
492 }
493 }
494
495 $data = array(
496 'blog' => absint( $this->video->blog_id ),
497 'post' => absint( $this->video->post_id ),
498 'duration' => absint( $this->video->duration ),
499 'poster' => esc_url_raw( $this->video->poster_frame_uri, array( 'http', 'https' ) ),
500 'hd' => (bool) $this->options['hd'],
501 );
502 if ( isset( $this->video->videos ) ) {
503 if ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) ) {
504 $data['mp4'] = array(
505 'size' => $this->video->videos->mp4->format,
506 'uri' => esc_url_raw( $this->video->videos->mp4->url, array( 'http', 'https' ) ),
507 );
508 }
509 if ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) ) {
510 $data['ogv'] = array(
511 'size' => 'std',
512 'uri' => esc_url_raw( $this->video->videos->ogv->url, array( 'http', 'https' ) ),
513 );
514 }
515 }
516 $locale = array( 'dir' => $this->video->text_direction );
517 if ( isset( $this->video->language ) ) {
518 $locale['lang'] = $this->video->language;
519 }
520 $data['locale'] = $locale;
521 unset( $locale );
522
523 $guid = $this->video->guid;
524 $guid_js = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
525 $html .= '<script type="text/javascript">' . PHP_EOL;
526 $html .= 'jQuery(document).ready(function() {';
527
528 $html .= 'if ( !jQuery.VideoPress.data[' . $guid_js . '] ) { jQuery.VideoPress.data[' . $guid_js . '] = new Array(); }' . PHP_EOL;
529 $html .= 'jQuery.VideoPress.data[' . $guid_js . '][' . self::$shown[ $guid ] . ']=' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';' . PHP_EOL;
530 unset( $data );
531
532 $jq_container = wp_json_encode( '#' . $this->video_container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
533 $jq_placeholder = wp_json_encode( '#' . $video_placeholder_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
534 $player_config = "{width:{$width},height:{$height},";
535 if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) {
536 $player_config .= 'freedom:"true",';
537 }
538 $player_config .= 'container:jQuery(' . $jq_container . ')}';
539
540 $html .= "jQuery({$jq_placeholder}).show(0,function(){jQuery.VideoPress.analytics.impression({$guid_js})});" . PHP_EOL;
541
542 if ( $age_gate_required ) {
543 $html .= 'if ( jQuery.VideoPress.support.flash() ) {' . PHP_EOL;
544 /**
545 * Insert alternative content for Flash players.
546 *
547 * @link https://github.com/swfobject/swfobject/wiki/SWFObject-API#swfobjectembedswfswfurlstr-replaceelemidstr-widthstr-heightstr-swfversionstr-xiswfurlstr-flashvarsobj-parobj-attobj-callbackfn
548 */
549 $html .= 'swfobject.embedSWF(' . implode(
550 ',',
551 array(
552 'jQuery.VideoPress.video.flash.player_uri',
553 wp_json_encode( $this->video_container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ),
554 wp_json_encode( $width, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ),
555 wp_json_encode( $height, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ),
556 'jQuery.VideoPress.video.flash.min_version',
557 'jQuery.VideoPress.video.flash.expressinstall', // attempt to upgrade the Flash player if less than min_version. requires a 310x137 container or larger but we will always try to include
558 '{guid:' . $guid_js . '}', // FlashVars
559 'jQuery.VideoPress.video.flash.params',
560 'null', // no attributes
561 'jQuery.VideoPress.video.flash.embedCallback', // error fallback
562 )
563 ) . ');';
564 $html .= '} else {' . PHP_EOL;
565 $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js},{$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL;
566 $html .= 'if ( jQuery(' . $jq_container . ').data( "player" ) === "flash" ){jQuery.VideoPress.video.play(jQuery(' . wp_json_encode( '#' . $this->video_container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . '));}else{';
567 $html .= 'jQuery(' . $jq_placeholder . ').html(' . wp_json_encode( $this->html_age_date(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ');' . PHP_EOL;
568 $html .= 'jQuery(' . wp_json_encode( '#' . $video_placeholder_id . ' input[type="submit"]', JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ').one("click", function(event){jQuery.VideoPress.requirements.isSufficientAge(jQuery(' . $jq_container . '),' . absint( $this->video->age_rating ) . ')});' . PHP_EOL;
569 $html .= '}}}' . PHP_EOL;
570 } else {
571 $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js}, {$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL;
572 if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true ) {
573 $html .= "jQuery.VideoPress.video.play(jQuery({$jq_container}));";
574 } else {
575 $html .= 'jQuery(' . $jq_placeholder . ').one("click",function(){jQuery.VideoPress.video.play(jQuery(' . $jq_container . '))});';
576 }
577 $html .= '}';
578
579 // close the jQuery(document).ready() function
580 $html .= '});';
581 }
582 $html .= '</script>' . PHP_EOL;
583 $html .= '</div>' . PHP_EOL;
584
585 /*
586 * JavaScript required
587 */
588 $noun = __( 'this video', 'jetpack' );
589 if ( ! $age_gate_required ) {
590 $vid_type = '';
591 if ( ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) && ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) ) ) {
592 $vid_type = 'ogv';
593 } elseif ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) ) {
594 $vid_type = 'mp4';
595 } elseif ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) ) {
596 $vid_type = 'ogv';
597 }
598
599 if ( $vid_type !== '' ) {
600 $noun = '<a ';
601 if ( isset( $this->video->language ) ) {
602 $noun .= 'hreflang="' . esc_attr( $this->video->language ) . '" ';
603 }
604 if ( $vid_type === 'mp4' ) {
605 $noun .= 'type="video/mp4" href="' . esc_url( $this->video->videos->mp4->url, array( 'http', 'https' ) );
606 } elseif ( $vid_type === 'ogv' ) {
607 $noun .= 'type="video/ogv" href="' . esc_url( $this->video->videos->ogv->url, array( 'http', 'https' ) );
608 }
609 $noun .= '">';
610 if ( isset( $this->video->title ) ) {
611 $noun .= esc_html( $this->video->title );
612 } else {
613 $noun .= __( 'this video', 'jetpack' );
614 }
615 $noun .= '</a>';
616 } elseif ( ! empty( $this->title ) ) {
617 $noun = esc_html( $this->title );
618 }
619 unset( $vid_type );
620 }
621 /* translators: %s video title or generic 'this video' string */
622 $html .= '<noscript><p>' . sprintf( _x( 'JavaScript required to play %s.', 'Play as in playback or view a movie', 'jetpack' ), $noun ) . '</p></noscript>';
623
624 return $html;
625 }
626
627 /**
628 * Output for the non-legacy HTML5 player.
629 */
630 public function html5_dynamic_next() {
631 $video_container_id = 'v-' . $this->video->guid;
632
633 Jwt_Token_Bridge::enqueue_jwt_token_bridge();
634
635 // Must not use iframes for IE11 due to a fullscreen bug
636 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && stristr( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'Trident/7.0; rv:11.0' ) ) {
637 $iframe_embed = false;
638 } else {
639 // The site setting and the `jetpack_videopress_player_use_iframe` filter decide; see Inline_Player::is_enabled().
640 $iframe_embed = ! Inline_Player::is_enabled();
641 }
642
643 if ( ! array_key_exists( 'hd', $this->options ) ) {
644 $this->options['hd'] = (bool) get_option( 'video_player_high_quality', false );
645 }
646
647 if ( ! array_key_exists( 'cover', $this->options ) ) {
648 $this->options['cover'] = true;
649 }
650
651 $videopress_options = array(
652 'width' => absint( $this->video->calculated_width ),
653 'height' => absint( $this->video->calculated_height ),
654 );
655 foreach ( $this->options as $option => $value ) {
656 switch ( $option ) {
657 case 'at':
658 if ( (int) $value ) {
659 $videopress_options[ $option ] = (int) $value;
660 }
661 break;
662 case 'autoplay':
663 $option = 'autoPlay'; // Fall-through ok.
664 case 'hd':
665 case 'loop':
666 case 'permalink':
667 case 'cover':
668 case 'muted':
669 case 'controls':
670 case 'playsinline':
671 case 'useAverageColor':
672 if ( in_array( $value, array( true, 1, 'true' ), true ) ) {
673 $videopress_options[ $option ] = true;
674 } elseif ( in_array( $value, array( false, 0, 'false' ), true ) ) {
675 $videopress_options[ $option ] = false;
676 }
677 // phpcs:enable
678 break;
679 case 'defaultlangcode':
680 $option = 'defaultLangCode';
681 if ( $value ) {
682 $videopress_options[ $option ] = $value;
683 }
684 break;
685 case 'preloadContent':
686 if ( $value ) {
687 $videopress_options['preloadContent'] = $value;
688 }
689 }
690 }
691
692 if ( $iframe_embed ) {
693 $iframe_url = "https://videopress.com/embed/{$this->video->guid}";
694
695 foreach ( $videopress_options as $option => $value ) {
696 if ( ! in_array( $option, array( 'width', 'height' ), true ) ) {
697
698 // add_query_arg ignores false as a value, so replacing it with 0
699 // @phan-suppress-next-line PhanPluginSimplifyExpressionBool -- Probably it could, but semantically let's keep it as-is.
700 $iframe_url = add_query_arg( $option, ( false === $value ) ? 0 : $value, $iframe_url );
701 }
702 }
703
704 $cover = $videopress_options['cover'] ? ' data-resize-to-parent="true"' : '';
705
706 wp_enqueue_script( 'videopress-iframe', 'https://videopress.com/videopress-iframe.js', array(), JETPACK__VERSION, true );
707
708 return "<iframe title='" . __( 'VideoPress Video Player', 'jetpack' )
709 . "' aria-label='" . __( 'VideoPress Video Player', 'jetpack' )
710 . "' width='" . esc_attr( $videopress_options['width'] )
711 . "' height='" . esc_attr( $videopress_options['height'] )
712 . "' src='" . esc_attr( $iframe_url )
713 . "' frameborder='0' allowfullscreen"
714 . $cover
715 . " allow='clipboard-write; presentation'></iframe>";
716
717 } else {
718 $attributes = array(
719 'autoplay' => $videopress_options['autoPlay'] ?? false,
720 'controls' => $videopress_options['controls'] ?? true,
721 'loop' => $videopress_options['loop'] ?? false,
722 'muted' => $videopress_options['muted'] ?? false,
723 'playsinline' => $videopress_options['playsinline'] ?? false,
724 'useAverageColor' => $videopress_options['useAverageColor'] ?? true,
725 'cover' => $videopress_options['cover'],
726 'hd' => $videopress_options['hd'],
727 'at' => $videopress_options['at'] ?? 0,
728 'preload' => $videopress_options['preloadContent'] ?? 'metadata',
729 'defaultLangCode' => $videopress_options['defaultLangCode'] ?? '',
730 );
731 $ratio = $videopress_options['width'] > 0
732 ? ( $videopress_options['height'] / $videopress_options['width'] ) * 100
733 : null;
734
735 return "<div id='" . esc_attr( $video_container_id ) . "'>"
736 . Inline_Player::render( (string) $this->video->guid, Inline_Player::get_player_options( $attributes ), $ratio )
737 . '</div>';
738 }
739 }
740
741 /**
742 * Only allow legitimate Flash parameters and their values
743 *
744 * @since 1.2
745 * @link https://helpx.adobe.com/flash/kb/flash-object-embed-tag-attributes.html Flash object and embed attributes
746 * @link https://helpx.adobe.com/flash/kb/font-outlines-device-fonts.html devicefont
747 * @link https://helpx.adobe.com/flash/kb/control-access-scripts-host-web.html allowscriptaccess
748 * @link https://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html full screen mode
749 * @link https://help.adobe.com/en_US/as3/dev/WS1EFE2EDA-026D-4d14-864E-79DFD56F87C6.html allownetworking
750 * @param array $flash_params Flash parameters expressed in key-value form.
751 * @return array validated Flash parameters
752 */
753 public static function esc_flash_params( $flash_params ) {
754 $allowed_params = array(
755 'swliveconnect' => array( 'true', 'false' ),
756 'play' => array( 'true', 'false' ),
757 'loop' => array( 'true', 'false' ),
758 'menu' => array( 'true', 'false' ),
759 'quality' => array( 'low', 'autolow', 'autohigh', 'medium', 'high', 'best' ),
760 'scale' => array( 'default', 'noborder', 'exactfit', 'noscale' ),
761 'align' => array( 'l', 'r', 't' ),
762 'salign' => array( 'l', 'r', 't', 'tl', 'tr', 'bl', 'br' ),
763 'wmode' => array( 'window', 'opaque', 'transparent', 'direct', 'gpu' ),
764 'devicefont' => array( '_sans', '_serif', '_typewriter' ),
765 'allowscriptaccess' => array( 'always', 'samedomain', 'never' ),
766 'allownetworking' => array( 'all', 'internal', 'none' ),
767 'seamlesstabbing' => array( 'true', 'false' ),
768 'allowfullscreen' => array( 'true', 'false' ),
769 'fullScreenAspectRatio' => array( 'portrait', 'landscape' ),
770 'base',
771 'bgcolor',
772 'flashvars',
773 );
774
775 $allowed_params_keys = array_keys( $allowed_params );
776
777 $filtered_params = array();
778 foreach ( $flash_params as $param => $value ) {
779 if ( empty( $param ) || empty( $value ) ) {
780 continue;
781 }
782 $param = strtolower( $param );
783 if ( in_array( $param, $allowed_params_keys, true ) ) {
784 if ( isset( $allowed_params[ $param ] ) && is_array( $allowed_params[ $param ] ) ) {
785 $value = strtolower( $value );
786 if ( in_array( $value, $allowed_params[ $param ], true ) ) {
787 $filtered_params[ $param ] = $value;
788 }
789 } else {
790 $filtered_params[ $param ] = $value;
791 }
792 }
793 }
794 unset( $allowed_params_keys );
795
796 /**
797 * Flash specifies sameDomain, not samedomain. change from lowercase value for preciseness
798 */
799 if ( isset( $filtered_params['allowscriptaccess'] ) && $filtered_params['allowscriptaccess'] === 'samedomain' ) {
800 $filtered_params['allowscriptaccess'] = 'sameDomain';
801 }
802
803 return $filtered_params;
804 }
805
806 /**
807 * Filter Flash variables from the response, taking into consideration player options.
808 *
809 * @since 1.3
810 * @return array Flash variable key value pairs
811 */
812 private function get_flash_variables() {
813 if ( ! isset( $this->video->players->swf->vars ) ) {
814 return array();
815 }
816
817 $flashvars = (array) $this->video->players->swf->vars;
818 if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true ) {
819 $flashvars['autoPlay'] = 'true';
820 }
821 return $flashvars;
822 }
823
824 /**
825 * Validate and filter Flash parameters
826 *
827 * @since 1.3
828 * @return array Flash parameters passed through key and value validation
829 */
830 private function get_flash_parameters() {
831 if ( ! isset( $this->video->players->swf->params ) ) {
832 return array();
833 } else {
834 return self::esc_flash_params(
835 /**
836 * Filters the Flash parameters of the VideoPress player.
837 *
838 * @module videopress
839 *
840 * @since 1.2.0
841 *
842 * @param array $this->video->players->swf->params Array of swf parameters for the VideoPress flash player.
843 */
844 apply_filters( 'video_flash_params', (array) $this->video->players->swf->params, 10, 1 )
845 );
846 }
847 }
848
849 /**
850 * Flash player markup in a HTML embed element.
851 *
852 * @since 1.1
853 * @link https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-embed-element embed element
854 * @link http://www.google.com/support/reader/bin/answer.py?answer=70664 Google Reader markup support
855 * @return string HTML markup. Embed element with no children
856 */
857 private function flash_embed() {
858 wp_enqueue_script( 'videopress' );
859 if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) ) {
860 return '';
861 }
862
863 $embed = array(
864 'id' => $this->video_id,
865 'src' => esc_url_raw( $this->video->players->swf->url . '&' . http_build_query( $this->get_flash_variables(), '', '&' ), array( 'http', 'https' ) ),
866 'type' => 'application/x-shockwave-flash',
867 'width' => $this->video->calculated_width,
868 'height' => $this->video->calculated_height,
869 );
870 if ( isset( $this->video->title ) ) {
871 $embed['title'] = $this->video->title;
872 }
873 $embed = array_merge( $embed, $this->get_flash_parameters() );
874
875 $html = '<embed';
876 foreach ( $embed as $attribute => $value ) {
877 $html .= ' ' . esc_html( $attribute ) . '="' . esc_attr( $value ) . '"';
878 }
879 unset( $embed );
880 $html .= '></embed>';
881 return $html;
882 }
883
884 /**
885 * Double-baked Flash object markup for Internet Explorer and more standards-friendly consuming agents.
886 *
887 * @since 1.1
888 * @return string HTML markup. Object and children.
889 */
890 private function flash_object() {
891 wp_enqueue_script( 'videopress' );
892 if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) ) {
893 return '';
894 }
895
896 $thumbnail_html = '<img alt="';
897 if ( isset( $this->video->title ) ) {
898 $thumbnail_html .= esc_attr( $this->video->title );
899 }
900 $thumbnail_html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" />';
901 $flash_vars = esc_attr( http_build_query( $this->get_flash_variables(), '', '&' ) );
902 $flash_params = '';
903 foreach ( $this->get_flash_parameters() as $attribute => $value ) {
904 $flash_params .= '<param name="' . esc_attr( $attribute ) . '" value="' . esc_attr( $value ) . '" />';
905 }
906 /* translators: %s url to the Adobe Flash Player website */
907 $flash_help = sprintf( __( 'This video requires <a rel="nofollow noopener noreferrer" href="%s" target="_blank">Adobe Flash</a> for playback.', 'jetpack' ), 'https://get.adobe.com/flashplayer/' );
908 $flash_player_url = esc_url( $this->video->players->swf->url, array( 'http', 'https' ) );
909 $description = '';
910 if ( isset( $this->video->title ) ) {
911 $standby = $this->video->title;
912 $description = '<p><strong>' . esc_html( $this->video->title ) . '</strong></p>';
913 } else {
914 $standby = __( 'Loading video...', 'jetpack' );
915 }
916 $standby = ' standby="' . esc_attr( $standby ) . '"';
917 return <<<OBJECT
918 <script type="text/javascript">if(typeof swfobject!=="undefined"){swfobject.registerObject("{$this->video_id}", "{$this->video->players->swf->version}");}</script>
919 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}" id="{$this->video_id}"{$standby}>
920 <param name="movie" value="{$flash_player_url}" />
921 {$flash_params}
922 <param name="flashvars" value="{$flash_vars}" />
923 <!--[if !IE]>-->
924 <object type="application/x-shockwave-flash" data="{$flash_player_url}" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}"{$standby}>
925 {$flash_params}
926 <param name="flashvars" value="{$flash_vars}" />
927 <!--<![endif]-->
928 {$thumbnail_html}{$description}<p class="robots-nocontent">{$flash_help}</p>
929 <!--[if !IE]>-->
930 </object>
931 <!--<![endif]-->
932 </object>
933 OBJECT;
934 }
935 }
936