class.videopress-player.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. <?php
  2. /**
  3. * VideoPress playback module markup generator.
  4. *
  5. * @since 1.3
  6. */
  7. class VideoPress_Player {
  8. /**
  9. * Video data for the requested guid and maximum width
  10. *
  11. * @since 1.3
  12. * @var VideoPress_Video
  13. */
  14. protected $video;
  15. /**
  16. * DOM identifier of the video container
  17. *
  18. * @var string
  19. * @since 1.3
  20. */
  21. protected $video_container_id;
  22. /**
  23. * DOM identifier of the video element (video, object, embed)
  24. *
  25. * @var string
  26. * @since 1.3
  27. */
  28. protected $video_id;
  29. /**
  30. * Array of playback options: force_flash or freedom
  31. *
  32. * @var array
  33. * @since 1.3
  34. */
  35. protected $options;
  36. /**
  37. * Array of video GUIDs shown and their counts,
  38. * moved from the old VideoPress class.
  39. */
  40. public static $shown = array();
  41. /**
  42. * Initiate a player object based on shortcode values and possible blog-level option overrides
  43. *
  44. * @since 1.3
  45. * @var string $guid VideoPress unique identifier
  46. * @var int $maxwidth maximum desired width of the video player if specified
  47. * @var array $options player customizations
  48. */
  49. public function __construct( $guid, $maxwidth = 0, $options = array() ) {
  50. if ( empty( self::$shown[ $guid ] ) )
  51. self::$shown[ $guid ] = 0;
  52. self::$shown[ $guid ]++;
  53. $this->video_container_id = 'v-' . $guid . '-' . self::$shown[ $guid ];
  54. $this->video_id = $this->video_container_id . '-video';
  55. if ( is_array( $options ) )
  56. $this->options = $options;
  57. else
  58. $this->options = array();
  59. // set up the video
  60. $cache_key = null;
  61. // disable cache in debug mode
  62. if ( defined('WP_DEBUG') && WP_DEBUG === true ) {
  63. $cached_video = null;
  64. } else {
  65. $cache_key_pieces = array( 'video' );
  66. if ( is_multisite() && is_subdomain_install() )
  67. $cache_key_pieces[] = get_current_blog_id();
  68. $cache_key_pieces[] = $guid;
  69. if ( $maxwidth > 0 )
  70. $cache_key_pieces[] = $maxwidth;
  71. if ( is_ssl() )
  72. $cache_key_pieces[] = 'ssl';
  73. $cache_key = implode( '-', $cache_key_pieces );
  74. unset( $cache_key_pieces );
  75. $cached_video = wp_cache_get( $cache_key, 'video' );
  76. }
  77. if ( empty( $cached_video ) ) {
  78. $video = new VideoPress_Video( $guid, $maxwidth );
  79. if ( empty( $video ) ) {
  80. return;
  81. } elseif ( isset( $video->error ) ) {
  82. $this->video = $video->error;
  83. return;
  84. } elseif ( is_wp_error( $video ) ) {
  85. $this->video = $video;
  86. return;
  87. }
  88. $this->video = $video;
  89. unset( $video );
  90. if ( ! defined( 'WP_DEBUG' ) || WP_DEBUG !== true ) {
  91. $expire = 3600;
  92. if ( isset( $video->expires ) && is_int( $video->expires ) ) {
  93. $expires_diff = time() - $video->expires;
  94. if ( $expires_diff > 0 && $expires_diff < 86400 ) // allowed range: 1 second to 1 day
  95. $expire = $expires_diff;
  96. unset( $expires_diff );
  97. }
  98. wp_cache_set( $cache_key, serialize( $this->video ), 'video', $expire );
  99. unset( $expire );
  100. }
  101. } else {
  102. $this->video = unserialize( $cached_video );
  103. }
  104. unset( $cache_key );
  105. unset( $cached_video );
  106. }
  107. /**
  108. * Wrap output in a VideoPress player container
  109. *
  110. * @since 1.3
  111. * @var string $content HTML string
  112. * @return string HTML string or blank string if nothing to wrap
  113. */
  114. private function html_wrapper( $content ) {
  115. if ( empty( $content ) )
  116. return '';
  117. else
  118. return '<div id="' . esc_attr( $this->video_container_id ) . '" class="video-player">' . $content . '</div>';
  119. }
  120. /**
  121. * Output content suitable for a feed reader displaying RSS or Atom feeds
  122. * We do not display error messages in the feed view due to caching concerns.
  123. * Flash content presented using <embed> markup for feed reader compatibility.
  124. *
  125. * @since 1.3
  126. * @return string HTML string or empty string if error
  127. */
  128. public function asXML() {
  129. if ( empty( $this->video ) || is_wp_error( $this->video ) ) {
  130. return '';
  131. }
  132. if ( isset( $this->options['force_flash'] ) && true === $this->options['force_flash'] ) {
  133. $content = $this->flash_embed();
  134. } else {
  135. $content = $this->html5_static();
  136. }
  137. return $this->html_wrapper( $content );
  138. }
  139. /**
  140. * Video player markup for best matching the current request and publisher options
  141. * @since 1.3
  142. * @return string HTML markup string or empty string if no video property found
  143. */
  144. public function asHTML() {
  145. if ( empty( $this->video ) ) {
  146. $content = '';
  147. } elseif ( is_wp_error( $this->video ) ) {
  148. $content = $this->error_message( $this->video );
  149. } elseif ( isset( $this->options['force_flash'] ) && true === $this->options['force_flash'] ) {
  150. $content = $this->flash_object();
  151. } elseif ( isset( $this->video->restricted_embed ) && true === $this->video->restricted_embed ) {
  152. if ( $this->options['forcestatic'] ) {
  153. $content = $this->flash_object();
  154. } else {
  155. $content = $this->html5_dynamic();
  156. }
  157. } elseif ( isset( $this->options['freedom'] ) && true === $this->options['freedom'] ) {
  158. $content = $this->html5_static();
  159. } else {
  160. $content = $this->html5_dynamic();
  161. }
  162. return $this->html_wrapper( $content );
  163. }
  164. /**
  165. * Display an error message to users capable of doing something about the error
  166. *
  167. * @since 1.3
  168. * @uses current_user_can() to test if current user has edit_posts capability
  169. * @var WP_Error $error WordPress error
  170. * @return string HTML string
  171. */
  172. private function error_message( $error ) {
  173. if ( ! current_user_can( 'edit_posts' ) || empty( $error ) )
  174. return '';
  175. $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">';
  176. $html .= '<h1 style="font-size:180%;font-style:bold;line-height:130%;text-decoration:underline">' . esc_html( sprintf( __( '%s Error', 'jetpack' ), 'VideoPress' ) ) . '</h1>';
  177. foreach( $error->get_error_messages() as $message ) {
  178. $html .= $message;
  179. }
  180. $html .= '</div>';
  181. return $html;
  182. }
  183. /**
  184. * Rating agencies and industry associations require a potential viewer verify his or her age before a video or its poster frame are displayed.
  185. * Content rated for audiences 17 years of age or older requires such verification across multiple rating agencies and industry associations
  186. *
  187. * @since 1.3
  188. * @return bool true if video requires the viewer verify he or she is 17 years of age or older
  189. */
  190. private function age_gate_required() {
  191. if ( isset( $this->video->age_rating ) && $this->video->age_rating >= 17 )
  192. return true;
  193. else
  194. return false;
  195. }
  196. /**
  197. * Select a date of birth using HTML form elements.
  198. *
  199. * @since 1.5
  200. * @return string HTML markup
  201. */
  202. private function html_age_gate() {
  203. global $wp_locale;
  204. $text_align = 'left';
  205. if ( $this->video->text_direction === 'rtl' )
  206. $text_align = 'right';
  207. $html = '<div class="videopress-age-gate" style="margin:0 60px">';
  208. $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>';
  209. $html .= '<fieldset id="birthday" style="border:0 none;text-align:' . $text_align . ';padding:0;">';
  210. $inputs_style = 'border:1px solid #444;margin-';
  211. if ( $this->video->text_direction === 'rtl' )
  212. $inputs_style .= 'left';
  213. else
  214. $inputs_style .= 'right';
  215. $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';
  216. /**
  217. * Display a list of months in the Gregorian calendar.
  218. * Set values to 0-based to match JavaScript Date.
  219. * @link https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date Mozilla JavaScript Reference: Date
  220. */
  221. $html .= '<select name="month" style="' . $inputs_style . '">';
  222. for( $i=0; $i<12; $i++ ) {
  223. $html .= '<option value="' . esc_attr( $i ) . '">' . esc_html( $wp_locale->get_month( $i + 1 ) ) . '</option>';
  224. }
  225. $html .= '</select>';
  226. /**
  227. * todo: numdays variance by month
  228. */
  229. $html .= '<select name="day" style="' . $inputs_style . '">';
  230. for ( $i=1; $i<32; $i++ ) {
  231. $html .= '<option>' . $i . '</option>';
  232. }
  233. $html .= '</select>';
  234. /**
  235. * Current record for human life is 122. Go back 130 years and no one is left out.
  236. * Don't ask infants younger than 2 for their birthday
  237. * Default to 13
  238. */
  239. $html .= '<select name="year" style="' . $inputs_style . '">';
  240. $start_year = date('Y') - 2;
  241. $default_year = $start_year - 11;
  242. $end_year = $start_year - 128;
  243. for ( $year=$start_year; $year>$end_year; $year-- ) {
  244. $html .= '<option';
  245. if ( $year === $default_year )
  246. $html .= ' selected="selected"';
  247. $html .= '>' . $year . '</option>';
  248. }
  249. unset( $start_year );
  250. unset( $default_year );
  251. unset( $end_year );
  252. $html .= '</select>';
  253. $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" />';
  254. $html .= '</fieldset>';
  255. $html .= '<p style="padding-top:20px;padding-bottom:60px;text-align:' . $text_align . ';"><a rel="nofollow" href="http://videopress.com/" rel="noopener noreferrer" target="_blank" style="color:rgb(128,128,128);text-decoration:underline;font-size:15px">' . __( 'More information', 'jetpack' ) . '</a></p>';
  256. $html .= '</div>';
  257. return $html;
  258. }
  259. /**
  260. * Return HTML5 video static markup for the given video parameters.
  261. * Use default browser player controls.
  262. * No Flash fallback.
  263. *
  264. * @since 1.2
  265. * @link http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html HTML5 video
  266. * @return string HTML5 video element and children
  267. */
  268. private function html5_static() {
  269. wp_enqueue_script( 'videopress' );
  270. $thumbnail = esc_url( $this->video->poster_frame_uri );
  271. $html = "<video id=\"{$this->video_id}\" width=\"{$this->video->calculated_width}\" height=\"{$this->video->calculated_height}\" poster=\"$thumbnail\" controls=\"true\"";
  272. if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
  273. $html .= ' autoplay="true"';
  274. else
  275. $html .= ' preload="metadata"';
  276. if ( isset( $this->video->text_direction ) )
  277. $html .= ' dir="' . esc_attr( $this->video->text_direction ) . '"';
  278. if ( isset( $this->video->language ) )
  279. $html .= ' lang="' . esc_attr( $this->video->language ) . '"';
  280. $html .= '>';
  281. if ( ! isset( $this->options['freedom'] ) || $this->options['freedom'] === false ) {
  282. $mp4 = $this->video->videos->mp4->url;
  283. if ( ! empty( $mp4 ) )
  284. $html .= '<source src="' . esc_url( $mp4 ) . '" type="video/mp4; codecs=&quot;' . esc_attr( $this->video->videos->mp4->codecs ) . '&quot;" />';
  285. unset( $mp4 );
  286. }
  287. if ( isset( $this->video->videos->ogv ) ) {
  288. $ogg = $this->video->videos->ogv->url;
  289. if ( ! empty( $ogg ) ) {
  290. $html .= '<source src="' . esc_url( $ogg ) . '" type="video/ogg; codecs=&quot;' . esc_attr( $this->video->videos->ogv->codecs ) . '&quot;" />';
  291. }
  292. unset( $ogg );
  293. }
  294. $html .= '<div><img alt="';
  295. if ( isset( $this->video->title ) )
  296. $html .= esc_attr( $this->video->title );
  297. $html .= '" src="' . $thumbnail . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" /></div>';
  298. if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
  299. $html .= '<p class="robots-nocontent">' . sprintf( __( 'You do not have sufficient <a rel="nofollow" href="%s" rel="noopener noreferrer" target="_blank">freedom levels</a> to view this video. Support free software and upgrade.', 'jetpack' ), 'http://www.gnu.org/philosophy/free-sw.html' ) . '</p>';
  300. elseif ( isset( $this->video->title ) )
  301. $html .= '<p>' . esc_html( $this->video->title ) . '</p>';
  302. $html .= '</video>';
  303. return $html;
  304. }
  305. /**
  306. * Click to play dynamic HTML5-capable player.
  307. * The player displays a video preview section including poster frame,
  308. * video title, play button and watermark on the original page load
  309. * and calculates the playback capabilities of the browser. The video player
  310. * is loaded when the visitor clicks on the video preview area.
  311. * If Flash Player 10 or above is available the browser will display
  312. * the Flash version of the video. If HTML5 video appears to be supported
  313. * and the browser may be capable of MP4 (H.264, AAC) or OGV (Theora, Vorbis)
  314. * playback the browser will display its native HTML5 player.
  315. *
  316. * @since 1.5
  317. * @return string HTML markup
  318. */
  319. private function html5_dynamic() {
  320. /**
  321. * Filter the VideoPress legacy player feature
  322. *
  323. * This filter allows you to control whether the legacy VideoPress player should be used
  324. * instead of the improved one.
  325. *
  326. * @module videopress
  327. *
  328. * @since 3.7.0
  329. *
  330. * @param boolean $videopress_use_legacy_player
  331. */
  332. if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) {
  333. return $this->html5_dynamic_next();
  334. }
  335. wp_enqueue_script( 'videopress' );
  336. $video_placeholder_id = $this->video_container_id . '-placeholder';
  337. $age_gate_required = $this->age_gate_required();
  338. $width = absint( $this->video->calculated_width );
  339. $height = absint( $this->video->calculated_height );
  340. $html = '<div id="' . $video_placeholder_id . '" class="videopress-placeholder" style="';
  341. if ( $age_gate_required )
  342. $html .= "min-width:{$width}px;min-height:{$height}px";
  343. else
  344. $html .= "width:{$width}px;height:{$height}px";
  345. $html .= ';display:none;cursor:pointer !important;position:relative;';
  346. if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) )
  347. $html .= 'background-color:' . esc_attr( $this->video->skin->background_color ) . ';';
  348. $html .= 'font-family: \'Helvetica Neue\',Arial,Helvetica,\'Nimbus Sans L\',sans-serif;font-weight:bold;font-size:18px">' . PHP_EOL;
  349. /**
  350. * Do not display a poster frame, title, or any other content hints for mature content.
  351. */
  352. if ( ! $age_gate_required ) {
  353. if ( ! empty( $this->video->title ) ) {
  354. $html .= '<div class="videopress-title" style="display:inline;position:absolute;margin:20px 20px 0 20px;padding:4px 8px;vertical-align:top;text-align:';
  355. if ( $this->video->text_direction === 'rtl' )
  356. $html .= 'right" dir="rtl"';
  357. else
  358. $html .= 'left" dir="ltr"';
  359. if ( isset( $this->video->language ) )
  360. $html .= ' lang="' . esc_attr( $this->video->language ) . '"';
  361. $html .= '><span style="padding:3px 0;line-height:1.5em;';
  362. if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) ) {
  363. $html .= 'background-color:';
  364. if ( $this->video->skin->background_color === 'rgb(0,0,0)' )
  365. $html .= 'rgba(0,0,0,0.8)';
  366. else
  367. $html .= esc_attr( $this->video->skin->background_color );
  368. $html .= ';';
  369. }
  370. $html .= 'color:rgb(255,255,255)">' . esc_html( $this->video->title ) . '</span></div>';
  371. }
  372. $html .= '<img class="videopress-poster" alt="';
  373. if ( ! empty( $this->video->title ) )
  374. $html .= esc_attr( $this->video->title ) . '" title="' . esc_attr( sprintf( _x( 'Watch: %s', 'watch a video title', 'jetpack' ), $this->video->title ) );
  375. $html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $width . '" height="' . $height . '" />' . PHP_EOL;
  376. //style a play button hovered over the poster frame
  377. $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;
  378. // watermark
  379. if ( isset( $this->video->skin ) && isset( $this->video->skin->watermark ) ) {
  380. $html .= '<div style="position:relative;margin-top:-40px;height:25px;margin-bottom:35px;';
  381. if ( $this->video->text_direction === 'rtl' )
  382. $html .= 'margin-left:20px;text-align:left;';
  383. else
  384. $html .= 'margin-right:20px;text-align:right;';
  385. $html .= 'vertical-align:bottom;z-index:3">';
  386. $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"/>';
  387. $html .= '</div>' . PHP_EOL;
  388. }
  389. }
  390. $data = array(
  391. 'blog' => absint( $this->video->blog_id ),
  392. 'post' => absint( $this->video->post_id ),
  393. 'duration'=> absint( $this->video->duration ),
  394. 'poster' => esc_url_raw( $this->video->poster_frame_uri, array( 'http', 'https' ) ),
  395. 'hd' => (bool) $this->options['hd']
  396. );
  397. if ( isset( $this->video->videos ) ) {
  398. if ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) )
  399. $data['mp4'] = array( 'size' => $this->video->videos->mp4->format, 'uri' => esc_url_raw( $this->video->videos->mp4->url, array( 'http', 'https' ) ) );
  400. if ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) )
  401. $data['ogv'] = array( 'size' => 'std', 'uri' => esc_url_raw( $this->video->videos->ogv->url, array( 'http', 'https' ) ) );
  402. }
  403. $locale = array( 'dir' => $this->video->text_direction );
  404. if ( isset( $this->video->language ) )
  405. $locale['lang'] = $this->video->language;
  406. $data['locale'] = $locale;
  407. unset( $locale );
  408. $guid = $this->video->guid;
  409. $guid_js = json_encode( $guid );
  410. $html .= '<script type="text/javascript">' . PHP_EOL;
  411. $html .= 'jQuery(document).ready(function() {';
  412. $html .= 'if ( !jQuery.VideoPress.data[' . json_encode($guid) . '] ) { jQuery.VideoPress.data[' . json_encode($guid) . '] = new Array(); }' . PHP_EOL;
  413. $html .= 'jQuery.VideoPress.data[' . json_encode( $guid ) . '][' . self::$shown[ $guid ] . ']=' . json_encode($data) . ';' . PHP_EOL;
  414. unset( $data );
  415. $jq_container = json_encode( '#' . $this->video_container_id );
  416. $jq_placeholder = json_encode( '#' . $video_placeholder_id );
  417. $player_config = "{width:{$width},height:{$height},";
  418. if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
  419. $player_config .= 'freedom:"true",';
  420. $player_config .= 'container:jQuery(' . $jq_container . ')}';
  421. $html .= "jQuery({$jq_placeholder}).show(0,function(){jQuery.VideoPress.analytics.impression({$guid_js})});" . PHP_EOL;
  422. if ( $age_gate_required ) {
  423. $html .= 'if ( jQuery.VideoPress.support.flash() ) {' . PHP_EOL;
  424. /**
  425. * @link http://code.google.com/p/swfobject/wiki/api#swfobject.embedSWF(swfUrlStr,_replaceElemIdStr,_widthStr,_height
  426. */
  427. $html .= 'swfobject.embedSWF(' . implode( ',', array(
  428. 'jQuery.VideoPress.video.flash.player_uri',
  429. json_encode( $this->video_container_id ),
  430. json_encode( $width ),
  431. json_encode( $height ),
  432. 'jQuery.VideoPress.video.flash.min_version',
  433. '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
  434. '{guid:' . $guid_js . '}', // FlashVars
  435. 'jQuery.VideoPress.video.flash.params',
  436. 'null', // no attributes
  437. 'jQuery.VideoPress.video.flash.embedCallback' // error fallback
  438. ) ) . ');';
  439. $html .= '} else {' . PHP_EOL;
  440. $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js},{$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL;
  441. $html .= 'if ( jQuery(' . $jq_container . ').data( "player" ) === "flash" ){jQuery.VideoPress.video.play(jQuery(' . json_encode('#' . $this->video_container_id) . '));}else{';
  442. $html .= 'jQuery(' . $jq_placeholder . ').html(' . json_encode( $this->html_age_date() ) . ');' . PHP_EOL;
  443. $html .= 'jQuery(' . json_encode( '#' . $video_placeholder_id . ' input[type=submit]' ) . ').one("click", function(event){jQuery.VideoPress.requirements.isSufficientAge(jQuery(' . $jq_container . '),' . absint( $this->video->age_rating ) . ')});' . PHP_EOL;
  444. $html .= '}}}' . PHP_EOL;
  445. } else {
  446. $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js}, {$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL;
  447. if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
  448. $html .= "jQuery.VideoPress.video.play(jQuery({$jq_container}));";
  449. else
  450. $html .= 'jQuery(' . $jq_placeholder . ').one("click",function(){jQuery.VideoPress.video.play(jQuery(' . $jq_container . '))});';
  451. $html .= '}';
  452. // close the jQuery(document).ready() function
  453. $html .= '});';
  454. }
  455. $html .= '</script>' . PHP_EOL;
  456. $html .= '</div>' . PHP_EOL;
  457. /*
  458. * JavaScript required
  459. */
  460. $noun = __( 'this video', 'jetpack' );
  461. if ( ! $age_gate_required ) {
  462. $vid_type = '';
  463. if ( ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) && ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) ) )
  464. $vid_type = 'ogv';
  465. elseif ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) )
  466. $vid_type = 'mp4';
  467. elseif ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) )
  468. $vid_type = 'ogv';
  469. if ( $vid_type !== '' ) {
  470. $noun = '<a ';
  471. if ( isset( $this->video->language ) )
  472. $noun .= 'hreflang="' . esc_attr( $this->video->language ) . '" ';
  473. if ( $vid_type === 'mp4' )
  474. $noun .= 'type="video/mp4" href="' . esc_url( $this->video->videos->mp4->url, array( 'http', 'https' ) );
  475. elseif ( $vid_type === 'ogv' )
  476. $noun .= 'type="video/ogv" href="' . esc_url( $this->video->videos->ogv->url, array( 'http', 'https' ) );
  477. $noun .= '">';
  478. if ( isset( $this->video->title ) )
  479. $noun .= esc_html( $this->video->title );
  480. else
  481. $noun .= __( 'this video', 'jetpack' );
  482. $noun .= '</a>';
  483. } elseif ( ! empty( $this->title ) ) {
  484. $noun = esc_html( $this->title );
  485. }
  486. unset( $vid_type );
  487. }
  488. $html .= '<noscript><p>' . sprintf( _x( 'JavaScript required to play %s.', 'Play as in playback or view a movie', 'jetpack' ), $noun ) . '</p></noscript>';
  489. return $html;
  490. }
  491. function html5_dynamic_next() {
  492. $video_container_id = 'v-' . $this->video->guid;
  493. // Must not use iframes for IE11 due to a fullscreen bug
  494. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && stristr( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0' ) ) {
  495. $iframe_embed = false;
  496. } else {
  497. /**
  498. * Filter the VideoPress iframe embed
  499. *
  500. * This filter allows you to control whether the videos will be embedded using an iframe.
  501. * Set this to false in order to use an in-page embed rather than an iframe.
  502. *
  503. * @module videopress
  504. *
  505. * @since 3.7.0
  506. *
  507. * @param boolean $videopress_player_use_iframe
  508. */
  509. $iframe_embed = apply_filters( 'jetpack_videopress_player_use_iframe', true );
  510. }
  511. if ( ! array_key_exists( 'hd', $this->options ) ) {
  512. $this->options['hd'] = (bool) get_option( 'video_player_high_quality', false );
  513. }
  514. $videopress_options = array(
  515. 'width' => absint( $this->video->calculated_width ),
  516. 'height' => absint( $this->video->calculated_height ),
  517. );
  518. foreach ( $this->options as $option => $value ) {
  519. switch ( $option ) {
  520. case 'at':
  521. if ( intval( $value ) ) {
  522. $videopress_options[ $option ] = intval( $value );
  523. }
  524. break;
  525. case 'autoplay':
  526. $option = 'autoPlay';
  527. case 'hd':
  528. case 'loop':
  529. case 'permalink':
  530. if ( in_array( $value, array( 1, 'true' ) ) ) {
  531. $videopress_options[ $option ] = true;
  532. } elseif ( in_array( $value, array( 0, 'false' ) ) ) {
  533. $videopress_options[ $option ] = false;
  534. }
  535. break;
  536. case 'defaultlangcode':
  537. $option = 'defaultLangCode';
  538. if ( $value ) {
  539. $videopress_options[ $option ] = $value;
  540. }
  541. break;
  542. }
  543. }
  544. if ( $iframe_embed ) {
  545. $iframe_url = "https://videopress.com/embed/{$this->video->guid}";
  546. foreach ( $videopress_options as $option => $value ) {
  547. if ( ! in_array( $option, array( 'width', 'height' ) ) ) {
  548. // add_query_arg ignores false as a value, so replacing it with 0
  549. $iframe_url = add_query_arg( $option, ( false === $value ) ? 0 : $value, $iframe_url );
  550. }
  551. }
  552. $js_url = 'https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress-iframe.js';
  553. return "<iframe width='" . esc_attr( $videopress_options['width'] )
  554. . "' height='" . esc_attr( $videopress_options['height'] )
  555. . "' src='" . esc_attr( $iframe_url )
  556. . "' frameborder='0' allowfullscreen></iframe>"
  557. . "<script src='" . esc_attr( $js_url ) . "'></script>";
  558. } else {
  559. $videopress_options = json_encode( $videopress_options );
  560. $js_url = 'https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress.js';
  561. return "<div id='{$video_container_id}'></div>
  562. <script src='{$js_url}'></script>
  563. <script>
  564. videopress('{$this->video->guid}', document.querySelector('#{$video_container_id}'), {$videopress_options});
  565. </script>";
  566. }
  567. }
  568. /**
  569. * Only allow legitimate Flash parameters and their values
  570. *
  571. * @since 1.2
  572. * @link http://kb2.adobe.com/cps/127/tn_12701.html Flash object and embed attributes
  573. * @link http://kb2.adobe.com/cps/133/tn_13331.html devicefont
  574. * @link http://kb2.adobe.com/cps/164/tn_16494.html allowscriptaccess
  575. * @link http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html full screen mode
  576. * @link http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001079.html allownetworking
  577. * @param array $flash_params Flash parameters expressed in key-value form
  578. * @return array validated Flash parameters
  579. */
  580. public static function esc_flash_params( $flash_params ) {
  581. $allowed_params = array(
  582. 'swliveconnect' => array('true', 'false'),
  583. 'play' => array('true', 'false'),
  584. 'loop' => array('true', 'false'),
  585. 'menu' => array('true', 'false'),
  586. 'quality' => array('low', 'autolow', 'autohigh', 'medium', 'high', 'best'),
  587. 'scale' => array('default', 'noborder', 'exactfit', 'noscale'),
  588. 'align' => array('l', 'r', 't'),
  589. 'salign' => array('l', 'r', 't', 'tl', 'tr', 'bl', 'br'),
  590. 'wmode' => array('window', 'opaque', 'transparent','direct','gpu'),
  591. 'devicefont' => array('_sans', '_serif', '_typewriter'),
  592. 'allowscriptaccess' => array('always', 'samedomain', 'never'),
  593. 'allownetworking' => array('all','internal', 'none'),
  594. 'seamlesstabbing' => array('true', 'false'),
  595. 'allowfullscreen' => array('true', 'false'),
  596. 'fullScreenAspectRatio' => array('portrait', 'landscape'),
  597. 'base',
  598. 'bgcolor',
  599. 'flashvars'
  600. );
  601. $allowed_params_keys = array_keys( $allowed_params );
  602. $filtered_params = array();
  603. foreach( $flash_params as $param=>$value ) {
  604. if ( empty($param) || empty($value) )
  605. continue;
  606. $param = strtolower($param);
  607. if ( in_array($param, $allowed_params_keys) ) {
  608. if ( isset( $allowed_params[$param] ) && is_array( $allowed_params[$param] ) ) {
  609. $value = strtolower($value);
  610. if ( in_array( $value, $allowed_params[$param] ) )
  611. $filtered_params[$param] = $value;
  612. } else {
  613. $filtered_params[$param] = $value;
  614. }
  615. }
  616. }
  617. unset( $allowed_params_keys );
  618. /**
  619. * Flash specifies sameDomain, not samedomain. change from lowercase value for preciseness
  620. */
  621. if ( isset( $filtered_params['allowscriptaccess'] ) && $filtered_params['allowscriptaccess'] === 'samedomain' )
  622. $filtered_params['allowscriptaccess'] = 'sameDomain';
  623. return $filtered_params;
  624. }
  625. /**
  626. * Filter Flash variables from the response, taking into consideration player options.
  627. *
  628. * @since 1.3
  629. * @return array Flash variable key value pairs
  630. */
  631. private function get_flash_variables() {
  632. if ( ! isset( $this->video->players->swf->vars ) )
  633. return array();
  634. $flashvars = (array) $this->video->players->swf->vars;
  635. if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
  636. $flashvars['autoPlay'] = 'true';
  637. return $flashvars;
  638. }
  639. /**
  640. * Validate and filter Flash parameters
  641. *
  642. * @since 1.3
  643. * @return array Flash parameters passed through key and value validation
  644. */
  645. private function get_flash_parameters() {
  646. if ( ! isset( $this->video->players->swf->params ) )
  647. return array();
  648. else
  649. return self::esc_flash_params(
  650. /**
  651. * Filters the Flash parameters of the VideoPress player.
  652. *
  653. * @module videopress
  654. *
  655. * @since 1.2.0
  656. *
  657. * @param array $this->video->players->swf->params Array of swf parameters for the VideoPress flash player.
  658. */
  659. apply_filters( 'video_flash_params', (array) $this->video->players->swf->params, 10, 1 )
  660. );
  661. }
  662. /**
  663. * Flash player markup in a HTML embed element.
  664. *
  665. * @since 1.1
  666. * @link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-iframe-element.html#the-embed-element embed element
  667. * @link http://www.google.com/support/reader/bin/answer.py?answer=70664 Google Reader markup support
  668. * @return string HTML markup. Embed element with no children
  669. */
  670. private function flash_embed() {
  671. wp_enqueue_script( 'videopress' );
  672. if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) )
  673. return '';
  674. $embed = array(
  675. 'id' => $this->video_id,
  676. 'src' => esc_url_raw( $this->video->players->swf->url . '&' . http_build_query( $this->get_flash_variables(), null, '&' ) , array( 'http', 'https' ) ),
  677. 'type' => 'application/x-shockwave-flash',
  678. 'width' => $this->video->calculated_width,
  679. 'height' => $this->video->calculated_height
  680. );
  681. if ( isset( $this->video->title ) )
  682. $embed['title'] = $this->video->title;
  683. $embed = array_merge( $embed, $this->get_flash_parameters() );
  684. $html = '<embed';
  685. foreach ( $embed as $attribute => $value ) {
  686. $html .= ' ' . esc_html( $attribute ) . '="' . esc_attr( $value ) . '"';
  687. }
  688. unset( $embed );
  689. $html .= '></embed>';
  690. return $html;
  691. }
  692. /**
  693. * Double-baked Flash object markup for Internet Explorer and more standards-friendly consuming agents.
  694. *
  695. * @since 1.1
  696. * @return HTML markup. Object and children.
  697. */
  698. private function flash_object() {
  699. wp_enqueue_script( 'videopress' );
  700. if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) )
  701. return '';
  702. $thumbnail_html = '<img alt="';
  703. if ( isset( $this->video->title ) )
  704. $thumbnail_html .= esc_attr( $this->video->title );
  705. $thumbnail_html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" />';
  706. $flash_vars = esc_attr( http_build_query( $this->get_flash_variables(), null, '&' ) );
  707. $flash_params = '';
  708. foreach ( $this->get_flash_parameters() as $attribute => $value ) {
  709. $flash_params .= '<param name="' . esc_attr( $attribute ) . '" value="' . esc_attr( $value ) . '" />';
  710. }
  711. $flash_help = sprintf( __( 'This video requires <a rel="nofollow" href="%s" rel="noopener noreferrer" target="_blank">Adobe Flash</a> for playback.', 'jetpack' ), 'http://www.adobe.com/go/getflashplayer');
  712. $flash_player_url = esc_url( $this->video->players->swf->url, array( 'http', 'https' ) );
  713. $description = '';
  714. if ( isset( $this->video->title ) ) {
  715. $standby = $this->video->title;
  716. $description = '<p><strong>' . esc_html( $this->video->title ) . '</strong></p>';
  717. } else {
  718. $standby = __( 'Loading video...', 'jetpack' );
  719. }
  720. $standby = ' standby="' . esc_attr( $standby ) . '"';
  721. return <<<OBJECT
  722. <script type="text/javascript">if(typeof swfobject!=="undefined"){swfobject.registerObject("{$this->video_id}", "{$this->video->players->swf->version}");}</script>
  723. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}" id="{$this->video_id}"{$standby}>
  724. <param name="movie" value="{$flash_player_url}" />
  725. {$flash_params}
  726. <param name="flashvars" value="{$flash_vars}" />
  727. <!--[if !IE]>-->
  728. <object type="application/x-shockwave-flash" data="{$flash_player_url}" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}"{$standby}>
  729. {$flash_params}
  730. <param name="flashvars" value="{$flash_vars}" />
  731. <!--<![endif]-->
  732. {$thumbnail_html}{$description}<p class="robots-nocontent">{$flash_help}</p>
  733. <!--[if !IE]>-->
  734. </object>
  735. <!--<![endif]-->
  736. </object>
  737. OBJECT;
  738. }
  739. }