Нема описа

class.videopress-player.php 33KB

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