PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/jetpack/modules/shortcodes/videopress.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 1333 lines | 740 code | 150 blank | 443 comment | 196 complexity | 5561b37cd466662a512ecf8f4c9c66e7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package video
  4. * @category video
  5. * @author Automattic Inc
  6. * @link http://automattic.com/wordpress-plugins/#videopress VideoPress
  7. * @version 1.5.4
  8. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  9. */
  10. /*
  11. Plugin Name: VideoPress
  12. Plugin URI: http://wordpress.org/extend/plugins/video/
  13. Description: Upload new videos to <a href="http://videopress.com/">VideoPress</a>, edit metadata, and easily insert VideoPress videos into posts and pages using shortcodes. Requires a <a href="http://wordpress.com/">WordPress.com</a> account and a WordPress.com blog with the <a href="http://en.wordpress.com/products/#videopress">VideoPress upgrade</a> to store and serve uploaded videos.
  14. Author: Automattic, Niall Kennedy, Joseph Scott, Gary Pendergast
  15. Contributor: Hailin Wu
  16. Author URI: http://automattic.com/wordpress-plugins/#videopress
  17. Version: 1.5.4
  18. Stable tag: 1.5.4
  19. License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. */
  21. if ( ! class_exists( 'VideoPress' ) ):
  22. /**
  23. * VideoPress main handler.
  24. * Attach actions and filters. Handle shortcodes. Add video button to rich text editor.
  25. * @since 1.3
  26. */
  27. class VideoPress {
  28. /**
  29. * Plugin version in PHP-addressable form
  30. * @var string
  31. * @since 1.3
  32. */
  33. const version = '1.5.4';
  34. /**
  35. * Minimum allowed width. We don't expect videos viewed below this width to be useful; we drop small values to help save publishers from themselves.
  36. * @var int
  37. * @since 1.3
  38. */
  39. const min_width = 60;
  40. /**
  41. * Remember if videopress.js and dependencies have already been loaded
  42. * @var bool
  43. * @since 1.5
  44. */
  45. var $js_loaded;
  46. /**
  47. * Remember all of the videos loaded on this page
  48. * @var array
  49. * @since 1.5
  50. */
  51. var $shown;
  52. /**
  53. * Attach actions, filters, and shortcode handlers
  54. * @since 1.3
  55. */
  56. public function __construct() {
  57. /**
  58. * json_decode should be initialized by compat.php. It's a PHP extension that might not be turned on, or could not be compatible with older version of PHP. We won't be able to unpack the server response without it, so let's fail early.
  59. */
  60. if ( ! function_exists( 'json_decode' ) )
  61. return;
  62. add_action( 'wp_head', array( $this, 'html_head' ), -1 ); // load before enqueue_scripts action
  63. //allow either [videopress xyz] or [wpvideo xyz] for backward compatibility
  64. add_shortcode( 'videopress', array( $this, 'shortcode' ) );
  65. add_shortcode( 'wpvideo', array( $this, 'shortcode' ) );
  66. // set default values
  67. $this->js_loaded = false;
  68. $this->shown = array();
  69. }
  70. /**
  71. * PHP 4 constructor compatibility
  72. *
  73. * @since 1.5
  74. * @todo remove when targeting PHP 5 (WordPress 3.2 requirement) or above.
  75. */
  76. public function VideoPress() {
  77. $this->__construct();
  78. }
  79. /**
  80. * Validate user-supplied guid values against expected inputs
  81. *
  82. * @since 1.1
  83. * @param string $guid video identifier
  84. * @return bool true if passes validation test
  85. */
  86. public static function is_valid_guid( $guid ) {
  87. if ( ! empty( $guid ) && strlen( $guid ) === 8 && ctype_alnum( $guid ) )
  88. return true;
  89. else
  90. return false;
  91. }
  92. /**
  93. * Search a given content string for VideoPress shortcodes. Return an array of shortcodes with guid and attribute values.
  94. *
  95. * @since 1.2
  96. * @see do_shortcode()
  97. * @param string $content post content string
  98. * @return array Array of shortcode data. GUID as the key and other customization parameters as value. empty array if no matches found.
  99. */
  100. public static function find_all_shortcodes( $content ) {
  101. $r = preg_match_all( '/(.?)\[(wpvideo|videopress)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/s', $content, $matches, PREG_SET_ORDER );
  102. if ( $r === false || $r === 0 )
  103. return array();
  104. $guids = array();
  105. foreach ( $matches as $m ) {
  106. // allow [[foo]] syntax for escaping a tag
  107. if ( $m[1] === '[' && $m[6] === ']' )
  108. continue;
  109. $attr = shortcode_parse_atts( $m[3] );
  110. if ( self::is_valid_guid( $attr[0] ) ) {
  111. $guid = $attr[0];
  112. unset( $attr[0] );
  113. $guids[$guid] = $attr;
  114. }
  115. }
  116. return $guids;
  117. }
  118. /**
  119. * Insert video handlers into HTML <head> if posts with video shortcodes exist.
  120. * If video posts are present then queue VideoPress JavaScript files.
  121. * If a video is present and is single post or page then add Open Graph protocol markup for first video found
  122. *
  123. * @since 1.3
  124. */
  125. public function html_head() {
  126. if ( is_feed() || ! have_posts() )
  127. return;
  128. $guid = '';
  129. while ( have_posts() ) {
  130. the_post();
  131. $guids = self::find_all_shortcodes( get_the_content() );
  132. if ( ! empty( $guids ) ) {
  133. $guid = trim( key( $guids ) );
  134. break;
  135. }
  136. unset( $guids );
  137. }
  138. rewind_posts();
  139. if ( ! empty( $guid ) )
  140. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 20 );
  141. }
  142. /**
  143. * Add VideoPress JavaScript files to the script queue.
  144. * A blog with the video_player_freedom option set to true may still require the VideoPress JS for stats purposes and therefore is not a reason for exclusion.
  145. *
  146. * @uses wp_enqueue_script()
  147. * @since 1.3
  148. * @return bool true if queued; else false
  149. */
  150. public function enqueue_scripts() {
  151. if ( $this->js_loaded === true )
  152. return false;
  153. $jquery = '://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js';
  154. $swfobject = '://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js';
  155. if ( is_ssl() ) {
  156. $vpjs = 'https://v0.wordpress.com/js/videopress.js';
  157. $swfobject = 'https' . $swfobject;
  158. $jquery = 'https' . $jquery;
  159. } else {
  160. $vpjs = 'http://s0.videopress.com/js/videopress.js';
  161. $swfobject = 'http' . $swfobject;
  162. $jquery = 'http' . $jquery;
  163. }
  164. wp_enqueue_script( 'swfobject', $swfobject, false. '2.2' );
  165. wp_enqueue_script( 'jquery', $jquery, false, '1.4.4' );
  166. wp_enqueue_script( 'videopress', $vpjs, array( 'jquery','swfobject' ), '1.09' );
  167. $this->js_loaded = true;
  168. return true;
  169. }
  170. /**
  171. * Print the VideoPress JS files now.
  172. * Used to load the JS in the footer, if it hasn't already been loaded in the header.
  173. *
  174. * @uses wp_enqueue_script()
  175. * @uses wp_print_scripts()
  176. * @since 1.5
  177. */
  178. public function print_scripts() {
  179. if ( $this->enqueue_scripts() === true )
  180. wp_print_scripts( array( 'swfobject', 'videopress' ) );
  181. }
  182. /**
  183. * Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display.
  184. *
  185. * @link http://codex.wordpress.org/Shortcode_API Shortcode API
  186. * @param array $attr shortcode attributes
  187. * @return string HTML markup or blank string on fail
  188. */
  189. public function shortcode( $attr ) {
  190. global $content_width;
  191. $guid = $attr[0];
  192. if ( ! self::is_valid_guid( $guid ) )
  193. return '';
  194. if ( array_key_exists( $guid, $this->shown ) )
  195. $this->shown[$guid]++;
  196. else
  197. $this->shown[$guid] = 1;
  198. extract( shortcode_atts( array(
  199. 'w' => 0,
  200. 'freedom' => false,
  201. 'flashonly' => false,
  202. 'autoplay' => false,
  203. 'hd' => false
  204. ), $attr ) );
  205. $freedom = (bool) $freedom;
  206. /**
  207. * Test if embedded blog prefers videos only displayed in Freedom-loving formats
  208. */
  209. if ( $freedom === false && (bool) get_option( 'video_player_freedom', false ) )
  210. $freedom = true;
  211. $forcestatic = get_option( 'video_player_static', false );
  212. /**
  213. * Set the video to HD if the blog option has it enabled
  214. */
  215. if ( (bool) get_option( 'video_player_high_quality', false ) )
  216. $hd = true;
  217. $width = absint($w);
  218. unset($w);
  219. if ( $width < self::min_width )
  220. $width = 0;
  221. elseif ( isset($content_width) && $content_width > self::min_width && $width > $content_width )
  222. $width = 0;
  223. if ( $width === 0 && isset( $content_width ) && $content_width > self::min_width )
  224. $width = $content_width;
  225. if ( ($width % 2) === 1 )
  226. $width--;
  227. $options = array(
  228. 'freedom' => $freedom,
  229. 'force_flash' => (bool) $flashonly,
  230. 'autoplay' => (bool) $autoplay,
  231. 'forcestatic' => $forcestatic,
  232. 'hd' => (bool) $hd
  233. );
  234. unset( $freedom );
  235. unset( $flashonly );
  236. add_action( 'wp_footer', array( $this, 'print_scripts' ), -1 );
  237. $player = new VideoPress_Player( $guid, $width, $options );
  238. if ( $player instanceOf VideoPress_Player ) {
  239. if ( is_feed() )
  240. return $player->asXML();
  241. else
  242. return $player->asHTML();
  243. } else {
  244. return 'error';
  245. }
  246. }
  247. /**
  248. * Add a video button above the post composition screen linking to a thickbox view of WordPress.com videos
  249. *
  250. * @since 0.1.0
  251. */
  252. public function media_button() {
  253. echo '<a href="https://public-api.wordpress.com/videopress-plugin.php?page=video-plugin&amp;video_plugin=1&amp;iframe&amp;TB_iframe=true" id="add_video" class="thickbox" title="VideoPress"><img src="' . esc_url( plugins_url( ) . '/' . dirname( plugin_basename( __FILE__ ) ) . '/camera-video.png' ) . '" alt="VideoPress" width="16" height="16" /></a>';
  254. }
  255. }
  256. /**
  257. * VideoPress video object retrieved from VideoPress servers and parsed.
  258. * @since 1.3
  259. */
  260. class VideoPress_Video {
  261. /**
  262. * Manifest version returned by remote service.
  263. *
  264. * @var string
  265. * @since 1.3
  266. */
  267. const manifest_version = '1.5';
  268. /**
  269. * Expiration of the video expressed in Unix time
  270. *
  271. * @var int
  272. * @since 1.3
  273. */
  274. public $expires;
  275. /**
  276. * VideoPress unique identifier
  277. *
  278. * @var string
  279. * @since 1.3
  280. */
  281. public $guid;
  282. /**
  283. * WordPress.com blog identifier
  284. *
  285. * @var int
  286. * @since 1.5
  287. */
  288. public $blog_id;
  289. /**
  290. * Remote blog attachment identifier
  291. *
  292. * @var int
  293. * @since 1.5
  294. */
  295. public $post_id;
  296. /**
  297. * Maximum desired width.
  298. *
  299. * @var int
  300. * @since 1.3
  301. */
  302. public $maxwidth;
  303. /**
  304. * Video width calculated based on original video dimensions and the requested maxwidth
  305. *
  306. * @var int
  307. * @since 1.3
  308. */
  309. public $calculated_width;
  310. /**
  311. * Video height calculated based on original video dimensions and the requested maxwidth
  312. *
  313. * @var int
  314. * @since 1.3
  315. */
  316. public $calculated_height;
  317. /**
  318. * Video title
  319. *
  320. * @var string
  321. * @since 1.3
  322. */
  323. public $title;
  324. /**
  325. * Directionality of title text. ltr or rtl
  326. *
  327. * @var string
  328. * @since 1.3
  329. */
  330. public $text_direction;
  331. /**
  332. * Text and audio language as ISO 639-2 language code
  333. *
  334. * @var string
  335. * @since 1.3
  336. */
  337. public $language;
  338. /**
  339. * Video duration in whole seconds
  340. *
  341. * @var int
  342. * @since 1.3
  343. */
  344. public $duration;
  345. /**
  346. * Recommended minimum age of the viewer.
  347. *
  348. * @var int
  349. * @since 1.3
  350. */
  351. public $age_rating;
  352. /**
  353. * Video author has restricted video embedding or sharing
  354. *
  355. * @var bool
  356. * @since 1.3
  357. */
  358. public $restricted_embed;
  359. /**
  360. * Poster frame image URI for the given video guid and calculated dimensions.
  361. *
  362. * @var string
  363. * @since 1.3
  364. */
  365. public $poster_frame_uri;
  366. /**
  367. * Video files associated with the given guid for the calculated dimensions.
  368. *
  369. * @var stdClass
  370. * @since 1.3
  371. */
  372. public $videos;
  373. /**
  374. * Video player information
  375. *
  376. * @var stdClass
  377. * @since 1.3
  378. */
  379. public $players;
  380. /**
  381. * Video player skinning preferences including background color and watermark
  382. *
  383. * @var array
  384. * @since 1.5
  385. */
  386. public $skin;
  387. /**
  388. * Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI
  389. *
  390. * @var array
  391. * @since 1.5
  392. */
  393. public $captions;
  394. /**
  395. * Setup the object.
  396. * Request video information from VideoPress servers and process the response.
  397. *
  398. * @since 1.3
  399. * @var string $guid VideoPress unique identifier
  400. * @var int $maxwidth maximum requested video width. final width and height are calculated on VideoPress servers based on the aspect ratio of the original video upload.
  401. */
  402. public function __construct( $guid, $maxwidth=0 ) {
  403. if ( VideoPress::is_valid_guid( $guid ) )
  404. $this->guid = $guid;
  405. $maxwidth = absint( $maxwidth );
  406. if ( $maxwidth > 0 )
  407. $this->maxwidth = $maxwidth;
  408. $data = $this->get_data();
  409. if ( is_wp_error( $data ) || empty( $data ) ) {
  410. $this->error = $data;
  411. return;
  412. }
  413. if ( isset( $data->blog_id ) )
  414. $this->blog_id = absint( $data->blog_id );
  415. if ( isset( $data->post_id ) )
  416. $this->post_id = absint( $data->post_id );
  417. if ( isset( $data->title ) && $data->title !== '' )
  418. $this->title = trim( str_replace( '&nbsp;', ' ', $data->title ) );
  419. if ( isset( $data->text_direction ) && $data->text_direction === 'rtl' )
  420. $this->text_direction = 'rtl';
  421. else
  422. $this->text_direction = 'ltr';
  423. if ( isset( $data->language ) )
  424. $this->language = $data->language;
  425. if ( isset( $data->duration ) && $data->duration > 0 )
  426. $this->duration = absint( $data->duration );
  427. if ( isset( $data->width ) && $data->width > 0 )
  428. $this->calculated_width = absint( $data->width );
  429. if ( isset( $data->height ) && $data->height > 0 )
  430. $this->calculated_height = absint( $data->height );
  431. if ( isset( $data->age_rating ) )
  432. $this->age_rating = absint( $this->age_rating );
  433. if ( isset( $data->restricted_embed ) && $data->restricted_embed === true )
  434. $this->restricted_embed = true;
  435. else
  436. $this->restricted_embed = false;
  437. if ( isset( $data->posterframe ) && $data->posterframe !== '' )
  438. $this->poster_frame_uri = esc_url_raw( $data->posterframe, array( 'http', 'https' ) );
  439. if ( isset( $data->mp4 ) || isset( $data->ogv ) ) {
  440. $this->videos = new stdClass();
  441. if ( isset( $data->mp4 ) )
  442. $this->videos->mp4 = $data->mp4;
  443. if ( isset( $data->ogv ) )
  444. $this->videos->ogv = $data->ogv;
  445. }
  446. if ( isset( $data->swf ) ) {
  447. if ( ! isset( $this->players ) )
  448. $this->players = new stdClass();
  449. $this->players->swf = $data->swf;
  450. }
  451. if ( isset( $data->skin ) )
  452. $this->skin = $data->skin;
  453. if ( isset( $data->captions ) )
  454. $this->captions = (array) $data->captions;
  455. }
  456. /**
  457. * PHP 4 constructor compatibility
  458. *
  459. * @since 1.5
  460. * @todo remove when targeting PHP 5 (WordPress 3.2 requirement) or above.
  461. */
  462. public function VideoPress_Video( $guid, $maxwidth=0 ) {
  463. $this->__construct( $guid, $maxwidth );
  464. }
  465. /**
  466. * Convert an Expires HTTP header value into Unix time for use in WP Cache
  467. *
  468. * @since 1.3
  469. * @var string $expires_header
  470. * @return int|bool Unix time or false
  471. */
  472. public static function calculate_expiration( $expires_header ) {
  473. if ( empty( $expires_header ) || ! is_string( $expires_header ) )
  474. return false;
  475. if ( class_exists( 'DateTime' ) && class_exists( 'DateTimeZone' ) ) {
  476. $expires_date = DateTime::createFromFormat( 'D, d M Y H:i:s T', $expires_header, new DateTimeZone( 'UTC' ) );
  477. if ( $expires_date instanceOf DateTime )
  478. return date_format( $expires_date, 'U' );
  479. } else {
  480. $expires_array = strptime( $expires_header, '%a, %d %b %Y %H:%M:%S %Z' );
  481. if ( is_array( $expires_array ) && isset( $expires_array['tm_hour'] ) && isset( $expires_array['tm_min'] ) && isset( $expires_array['tm_sec'] ) && isset( $expires_array['tm_mon'] ) && isset( $expires_array['tm_mday'] ) && isset( $expires_array['tm_year'] ) )
  482. return gmmktime( $expires_array['tm_hour'], $expires_array['tm_min'], $expires_array['tm_sec'], 1 + $expires_array['tm_mon'], $expires_array['tm_mday'], 1900 + $expires_array['tm_year'] );
  483. }
  484. return false;
  485. }
  486. /**
  487. * Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds.
  488. *
  489. * @since 1.2
  490. * @param string $url absolute URL
  491. * @return bool|string host component of the URL, or false if none found
  492. */
  493. public static function hostname( $url ) {
  494. if ( empty($url) || ! function_exists('parse_url') )
  495. return false;
  496. // PHP 5.3.3 or newer can throw a warning on a bad input URI. catch that occurance just in case
  497. try {
  498. return parse_url( $url, PHP_URL_HOST );
  499. } catch (Exception $e){}
  500. return false;
  501. }
  502. /**
  503. * Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname.
  504. *
  505. * @since 1.3
  506. * @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful
  507. */
  508. private function get_data() {
  509. global $wp_version;
  510. $domain = self::hostname( home_url() );
  511. $request_params = array( 'guid' => $this->guid, 'domain' => $domain );
  512. if ( isset( $this->maxwidth ) && $this->maxwidth > 0 )
  513. $request_params['maxwidth'] = $this->maxwidth;
  514. $url = 'http://videopress.com/data/wordpress.json';
  515. if ( is_ssl() )
  516. $url = 'https://v.wordpress.com/data/wordpress.json';
  517. $response = wp_remote_get( $url . '?' . http_build_query( $request_params, null, '&' ), array(
  518. 'redirection' => 1,
  519. 'user-agent' => 'VideoPress plugin ' . VideoPress::version . '; WordPress ' . $wp_version . ' (' . home_url('/') . ')'
  520. ) );
  521. unset( $request_params );
  522. unset( $url );
  523. $response_body = wp_remote_retrieve_body( $response );
  524. $response_code = absint( wp_remote_retrieve_response_code( $response ) );
  525. if ( is_wp_error( $response ) ) {
  526. return $response;
  527. } elseif ( $response_code === 400 ) {
  528. return new WP_Error( 'bad_config', __( 'The VideoPress plugin could not communicate with the VideoPress servers. This error is most likely caused by a misconfigured plugin. Please reinstall or upgrade.', 'jetpack' ) );
  529. } elseif ( $response_code === 403 ) {
  530. return new WP_Error( 'http_forbidden', '<p>' . sprintf( __( '<strong>%s</strong> is not an allowed embed site.' , 'jetpack' ), esc_html( $domain ) ) . '</p><p>' . __( 'Publisher limits playback of video embeds.', 'jetpack' ) . '</p>' );
  531. } elseif ( $response_code === 404 ) {
  532. return new WP_Error( 'http_not_found', '<p>' . sprintf( __( 'No data found for VideoPress identifier: <strong>%s</strong>.', 'jetpack' ), $this->guid ) . '</p>' );
  533. } elseif ( $response_code !== 200 || empty( $response_body ) ) {
  534. return;
  535. } else {
  536. $expires_header = wp_remote_retrieve_header( $response, 'Expires' );
  537. if ( ! empty( $expires_header ) ) {
  538. $expires = self::calculate_expiration( $expires_header );
  539. if ( ! empty( $expires ) )
  540. $this->expires = $expires;
  541. }
  542. return json_decode( $response_body );
  543. }
  544. }
  545. }
  546. /**
  547. * VideoPress playback module markup generator.
  548. *
  549. * @since 1.3
  550. */
  551. class VideoPress_Player {
  552. /**
  553. * Video data for the requested guid and maximum width
  554. *
  555. * @since 1.3
  556. * @var VideoPress_Video
  557. */
  558. protected $video;
  559. /**
  560. * DOM identifier of the video container
  561. *
  562. * @var string
  563. * @since 1.3
  564. */
  565. protected $video_container_id;
  566. /**
  567. * DOM identifier of the video element (video, object, embed)
  568. *
  569. * @var string
  570. * @since 1.3
  571. */
  572. protected $video_id;
  573. /**
  574. * Array of playback options: force_flash or freedom
  575. *
  576. * @var array
  577. * @since 1.3
  578. */
  579. protected $options;
  580. /**
  581. * Initiate a player object based on shortcode values and possible blog-level option overrides
  582. *
  583. * @since 1.3
  584. * @var string $guid VideoPress unique identifier
  585. * @var int $maxwidth maximum desired width of the video player if specified
  586. * @var array $options player customizations
  587. */
  588. public function __construct( $guid, $maxwidth = 0, $options = array() ) {
  589. global $videopress;
  590. $this->video_container_id = 'v-' . $guid . '-' . $videopress->shown[$guid];
  591. $this->video_id = $this->video_container_id . '-video';
  592. if ( is_array( $options ) )
  593. $this->options = $options;
  594. else
  595. $this->options = array();
  596. // set up the video
  597. $cache_key = null;
  598. // disable cache in debug mode
  599. if ( defined('WP_DEBUG') && WP_DEBUG === true ) {
  600. $cached_video = null;
  601. } else {
  602. $cache_key_pieces = array( 'video' );
  603. if ( is_multisite() && is_subdomain_install() ) {
  604. /**
  605. * Compatibility wrapper for less than WordPress 3.1
  606. *
  607. * @todo remove when targeting WordPress 3.2 or above.
  608. */
  609. if ( function_exists( 'get_current_blog_id' ) )
  610. $cache_key_pieces[] = get_current_blog_id();
  611. elseif ( isset( $GLOBALS ) && isset( $GLOBALS['blog_id'] ) )
  612. $cache_key_pieces[] = absint( $GLOBALS['blog_id'] );
  613. else
  614. $cache_key_pieces[] = 1;
  615. }
  616. $cache_key_pieces[] = $guid;
  617. if ( $width > 0 )
  618. $cache_key_pieces[] = $maxwidth;
  619. if ( is_ssl() )
  620. $cache_key_pieces[] = 'ssl';
  621. $cache_key = implode( '-', $cache_key_pieces );
  622. unset( $cache_key_pieces );
  623. $cached_video = wp_cache_get( $cache_key, 'video' );
  624. }
  625. if ( empty( $cached_video ) ) {
  626. $video = new VideoPress_Video( $guid, $maxwidth );
  627. if ( empty( $video ) ) {
  628. return;
  629. } elseif ( isset( $video->error ) ) {
  630. $this->video = $video->error;
  631. return;
  632. } elseif ( is_wp_error( $video ) ) {
  633. $this->video = $video;
  634. return;
  635. }
  636. $this->video = $video;
  637. unset( $video );
  638. if ( ! defined( 'WP_DEBUG' ) || WP_DEBUG !== true ) {
  639. $expire = 3600;
  640. if ( isset( $video->expires ) && is_int( $video->expires ) ) {
  641. $expires_diff = time() - $video->expires;
  642. if ( $expires_diff > 0 && $expires_diff < 86400 ) // allowed range: 1 second to 1 day
  643. $expire = $expires_diff;
  644. unset( $expires_diff );
  645. }
  646. wp_cache_set( $cache_key, serialize($this->video), 'video', $expire );
  647. unset( $expire );
  648. }
  649. } else {
  650. $this->video = unserialize( $cached_video );
  651. }
  652. unset( $cache_key );
  653. unset( $cached_video );
  654. }
  655. /**
  656. * PHP 4 constructor compatibility
  657. *
  658. * @since 1.5
  659. * @todo remove when targeting PHP 5 (WordPress 3.2 min requirement) or above.
  660. */
  661. public function VideoPress_Player( $guid, $maxwidth = 0, $options = array() ) {
  662. $this->__construct( $guid, $maxwidth, $options );
  663. }
  664. /**
  665. * Wrap output in a VideoPress player container
  666. *
  667. * @since 1.3
  668. * @var string $content HTML string
  669. * @return string HTML string or blank string if nothing to wrap
  670. */
  671. private function html_wrapper( $content ) {
  672. if ( empty( $content ) )
  673. return '';
  674. else
  675. return '<div id="' . esc_attr( $this->video_container_id ) . '" class="video-player">' . $content . '</div>';
  676. }
  677. /**
  678. * Output content suitable for a feed reader displaying RSS or Atom feeds
  679. * We do not display error messages in the feed view due to caching concerns.
  680. * Flash content presented using <embed> markup for feed reader compatibility.
  681. *
  682. * @since 1.3
  683. * @return string HTML string or empty string if error
  684. */
  685. public function asXML() {
  686. if ( empty( $this->video ) || is_wp_error( $this->video ) )
  687. return '';
  688. if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
  689. $content = $this->html5_static();
  690. else
  691. $content = $this->flash_embed();
  692. return $this->html_wrapper( $content );
  693. }
  694. /**
  695. * Video player markup for best matching the current request and publisher options
  696. * @since 1.3
  697. * @return string HTML markup string or empty string if no video property found
  698. */
  699. public function asHTML() {
  700. if ( empty( $this->video ) ) {
  701. $content = '';
  702. } elseif ( is_wp_error( $this->video ) ) {
  703. $content = $this->error_message( $this->video );
  704. } elseif ( isset( $this->options['force_flash'] ) && $this->options['force_flash'] === true ) {
  705. $content = $this->flash_object();
  706. } elseif ( isset( $this->video->restricted_embed ) && $this->video->restricted_embed === true ) {
  707. if( $this->options['forcestatic'] )
  708. $content = $this->flash_object();
  709. else
  710. $content = $this->html5_dynamic();
  711. } elseif ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) {
  712. $content = $this->html5_static();
  713. } else {
  714. $content = $this->html5_dynamic();
  715. }
  716. return $this->html_wrapper( $content );
  717. }
  718. /**
  719. * Display an error message to users capable of doing something about the error
  720. *
  721. * @since 1.3
  722. * @uses current_user_can() to test if current user has edit_posts capability
  723. * @var WP_Error $error WordPress error
  724. * @return string HTML string
  725. */
  726. private function error_message( $error ) {
  727. if ( ! current_user_can( 'edit_posts' ) || empty( $error ) )
  728. return '';
  729. $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">';
  730. $html .= '<h1 style="font-size:180%;font-style:bold;line-height:130%;text-decoration:underline">' . esc_html( sprintf( __( '%s Error', 'jetpack' ), 'VideoPress' ) ) . '</h1>';
  731. foreach( $error->get_error_messages() as $message ) {
  732. $html .= $message;
  733. }
  734. $html .= '</div>';
  735. return $html;
  736. }
  737. /**
  738. * Rating agencies and industry associations require a potential viewer verify his or her age before a video or its poster frame are displayed.
  739. * Content rated for audiences 17 years of age or older requires such verification across multiple rating agencies and industry associations
  740. *
  741. * @since 1.3
  742. * @return bool true if video requires the viewer verify he or she is 17 years of age or older
  743. */
  744. private function age_gate_required() {
  745. if ( isset( $this->video->age_rating ) && $this->video->age_rating >= 17 )
  746. return true;
  747. else
  748. return false;
  749. }
  750. /**
  751. * Select a date of birth using HTML form elements.
  752. *
  753. * @since 1.5
  754. * @return string HTML markup
  755. */
  756. private function html_age_gate() {
  757. $text_align = 'left';
  758. if ( $this->video->text_direction === 'rtl' )
  759. $text_align = 'right';
  760. $html = '<div class="videopress-age-gate" style="margin:0 60px">';
  761. $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>';
  762. $html .= '<fieldset id="birthday" style="border:0 none;text-align:' . $text_align . ';padding:0;">';
  763. $inputs_style = 'border:1px solid #444;margin-';
  764. if ( $this->video->text_direction === 'rtl' )
  765. $inputs_style .= 'left';
  766. else
  767. $inputs_style .= 'right';
  768. $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';
  769. /**
  770. * Display a list of months in the Gregorian calendar.
  771. * Set values to 0-based to match JavaScript Date.
  772. * @link https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date Mozilla JavaScript Reference: Date
  773. */
  774. $html .= '<select name="month" style="' . $inputs_style . '">';
  775. $months = array( __('January', 'jetpack'), __('February', 'jetpack'), __('March', 'jetpack'), __('April', 'jetpack'), __('May', 'jetpack'), __('June', 'jetpack'), __('July', 'jetpack'), __('August', 'jetpack'), __('September', 'jetpack'), __('October', 'jetpack'), __('November', 'jetpack'), __('December', 'jetpack') );
  776. for( $i=0; $i<12; $i++ ) {
  777. $html .= '<option value="' . esc_attr( $i ) . '">' . esc_html( $months[$i] ) . '</option>';
  778. }
  779. $html .= '</select>';
  780. unset( $months );
  781. /**
  782. * todo: numdays variance by month
  783. */
  784. $html .= '<select name="day" style="' . $inputs_style . '">';
  785. for ( $i=1; $i<32; $i++ ) {
  786. $html .= '<option>' . $i . '</option>';
  787. }
  788. $html .= '</select>';
  789. /**
  790. * Current record for human life is 122. Go back 130 years and no one is left out.
  791. * Don't ask infants younger than 2 for their birthday
  792. * Default to 13
  793. */
  794. $html .= '<select name="year" style="' . $inputs_style . '">';
  795. $start_year = date('Y') - 2;
  796. $default_year = $start_year - 11;
  797. $end_year = $start_year - 128;
  798. for ( $year=$start_year; $year>$end_year; $year-- ) {
  799. $html .= '<option';
  800. if ( $year === $default_year )
  801. $html .= ' selected="selected"';
  802. $html .= '>' . $year . '</option>';
  803. }
  804. unset( $start_year );
  805. unset( $default_year );
  806. unset( $end_year );
  807. $html .= '</select>';
  808. $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" />';
  809. $html .= '</fieldset>';
  810. $html .= '<p style="padding-top:20px;padding-bottom:60px;text-align:' . $text_align . ';"><a rel="nofollow" href="http://videopress.com/" style="color:rgb(128,128,128);text-decoration:underline;font-size:15px">' . __( 'More information', 'jetpack' ) . '</a></p>';
  811. $html .= '</div>';
  812. return $html;
  813. }
  814. /**
  815. * Return HTML5 video static markup for the given video parameters.
  816. * Use default browser player controls.
  817. * No Flash fallback.
  818. *
  819. * @since 1.2
  820. * @link http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html HTML5 video
  821. * @return string HTML5 video element and children
  822. */
  823. private function html5_static() {
  824. $thumbnail = esc_url( $this->video->poster_frame_uri );
  825. $html = "<video id=\"{$this->video_id}\" width=\"{$this->video->calculated_width}\" height=\"{$this->video->calculated_height}\" poster=\"$thumbnail\" controls=\"true\"";
  826. if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
  827. $html .= ' autoplay="true"';
  828. else
  829. $html .= ' preload="metadata"';
  830. if ( isset( $this->video->text_direction ) )
  831. $html .= ' dir="' . esc_attr( $this->video->text_direction ) . '"';
  832. if ( isset( $this->video->language ) )
  833. $html .= ' lang="' . esc_attr( $this->video->language ) . '"';
  834. $html .= '>';
  835. if ( ! isset( $this->options['freedom'] ) || $this->options['freedom'] === false ) {
  836. $mp4 = $this->video->videos->mp4->url;
  837. if ( ! empty( $mp4 ) )
  838. $html .= '<source src="' . esc_url( $mp4 ) . '" type="video/mp4; codecs=&quot;' . esc_attr( $this->video->videos->mp4->codecs ) . '&quot;" />';
  839. unset( $mp4 );
  840. }
  841. $ogg = $this->video->videos->ogv->url;
  842. if ( ! empty( $ogg ) )
  843. $html .= '<source src="' . esc_url( $ogg ) . '" type="video/ogg; codecs=&quot;' . esc_attr( $this->video->videos->ogv->codecs ) . '&quot;" />';
  844. unset( $ogg );
  845. $html .= '<div><img alt="';
  846. if ( isset( $this->video->title ) )
  847. $html .= esc_attr( $this->video->title );
  848. $html .= '" src="' . $thumbnail . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" /></div>';
  849. if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
  850. $html .= '<p class="robots-nocontent">' . sprintf( __( 'You do not have sufficient <a rel="nofollow" href="%s">freedom levels</a> to view this video. Support free software and upgrade.', 'jetpack' ), 'http://www.gnu.org/philosophy/free-sw.html' ) . '</p>';
  851. elseif ( isset( $this->video->title ) )
  852. $html .= '<p>' . esc_html( $this->video->title ) . '</p>';
  853. $html .= '</video>';
  854. return $html;
  855. }
  856. /**
  857. * Click to play dynamic HTML5-capable player.
  858. * The player displays a video preview section including poster frame, video title, play button and watermark on the original page load and calculates the playback capabilities of the browser. The video player is loaded when the visitor clicks on the video preview area.
  859. * If Flash Player 10 or above is available the browser will display the Flash version of the video. If HTML5 video appears to be supported and the browser may be capable of MP4 (H.264, AAC) or OGV (Theora, Vorbis) playback the browser will display its native HTML5 player.
  860. *
  861. * @since 1.5
  862. * @return string HTML markup
  863. */
  864. private function html5_dynamic() {
  865. global $videopress;
  866. $video_placeholder_id = $this->video_container_id . '-placeholder';
  867. $age_gate_required = $this->age_gate_required();
  868. $width = absint( $this->video->calculated_width );
  869. $height = absint( $this->video->calculated_height );
  870. $html = '<div id="' . $video_placeholder_id . '" class="videopress-placeholder" style="';
  871. if ( $age_gate_required )
  872. $html .= "min-width:{$width}px;min-height:{$height}px";
  873. else
  874. $html .= "width:{$width}px;height:{$height}px";
  875. $html .= ';display:none;cursor:pointer !important;position:relative;';
  876. if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) )
  877. $html .= 'background-color:' . esc_attr( $this->video->skin->background_color ) . ';';
  878. $html .= 'font-family: \'Helvetica Neue\',Arial,Helvetica,\'Nimbus Sans L\',sans-serif;font-weight:bold;font-size:18px">' . PHP_EOL;
  879. /**
  880. * Do not display a poster frame, title, or any other content hints for mature content.
  881. */
  882. if ( ! $age_gate_required ) {
  883. if ( ! empty( $this->video->title ) ) {
  884. $html .= '<div class="videopress-title" style="display:inline;position:absolute;margin:20px 20px 0 20px;padding:4px 8px;vertical-align:top;text-align:';
  885. if ( $this->video->text_direction === 'rtl' )
  886. $html .= 'right" dir="rtl"';
  887. else
  888. $html .= 'left" dir="ltr"';
  889. if ( isset( $this->video->language ) )
  890. $html .= ' lang="' . esc_attr( $this->video->language ) . '"';
  891. $html .= '><span style="padding:3px 0;line-height:1.5em;';
  892. if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) ) {
  893. $html .= 'background-color:';
  894. if ( $this->video->skin->background_color === 'rgb(0,0,0)' )
  895. $html .= 'rgba(0,0,0,0.8)';
  896. else
  897. $html .= esc_attr( $this->video->skin->background_color );
  898. $html .= ';';
  899. }
  900. $html .= 'color:rgb(255,255,255)">' . esc_html( $this->video->title ) . '</span></div>';
  901. }
  902. $html .= '<img class="videopress-poster" alt="';
  903. if ( ! empty( $this->video->title ) )
  904. $html .= esc_attr( $this->video->title ) . '" title="' . esc_attr( sprintf( _x( 'Watch: %s', 'watch a video title', 'jetpack' ), $this->video->title ) );
  905. $html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width=' . $width . '" height="' . $height . '" />' . PHP_EOL;
  906. //style a play button hovered over the poster frame
  907. $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;
  908. // watermark
  909. if ( isset( $this->video->skin ) && isset( $this->video->skin->watermark ) ) {
  910. $html .= '<div style="position:relative;margin-top:-40px;height:25px;margin-bottom:35px;';
  911. if ( $this->video->text_direction === 'rtl' )
  912. $html .= 'margin-left:20px;text-align:left;';
  913. else
  914. $html .= 'margin-right:20px;text-align:right;';
  915. $html .= 'vertical-align:bottom;z-index:3">';
  916. $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"/>';
  917. $html .= '</div>' . PHP_EOL;
  918. }
  919. }
  920. $data = array(
  921. 'blog' => absint( $this->video->blog_id ),
  922. 'post' => absint( $this->video->post_id ),
  923. 'duration'=> absint( $this->video->duration ),
  924. 'poster' => esc_url_raw( $this->video->poster_frame_uri, array( 'http', 'https' ) ),
  925. 'hd' => (bool) $this->options['hd']
  926. );
  927. if ( isset( $this->video->videos ) ) {
  928. if ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) )
  929. $data['mp4'] = array( 'size' => $this->video->videos->mp4->format, 'uri' => esc_url_raw( $this->video->videos->mp4->url, array( 'http', 'https' ) ) );
  930. if ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) )
  931. $data['ogv'] = array( 'size' => 'std', 'uri' => esc_url_raw( $this->video->videos->ogv->url, array( 'http', 'https' ) ) );
  932. }
  933. $locale = array( 'dir' => $this->video->text_direction );
  934. if ( isset( $this->video->language ) )
  935. $locale['lang'] = $this->video->language;
  936. $data['locale'] = $locale;
  937. unset( $locale );
  938. $guid = $this->video->guid;
  939. $guid_js = json_encode( $guid );
  940. $html .= '<script type="text/javascript">' . PHP_EOL;
  941. $html .= 'jQuery(document).ready(function() {';
  942. $html .= 'if ( !jQuery.VideoPress.data[' . json_encode($guid) . '] ) { jQuery.VideoPress.data[' . json_encode($guid) . '] = new Array(); }' . PHP_EOL;
  943. $html .= 'jQuery.VideoPress.data[' . json_encode( $guid ) . '][' . $videopress->shown[$guid] . ']=' . json_encode($data) . ';' . PHP_EOL;
  944. unset( $data );
  945. $jq_container = json_encode( '#' . $this->video_container_id );
  946. $jq_placeholder = json_encode( '#' . $video_placeholder_id );
  947. $player_config = "{width:{$width},height:{$height},";
  948. if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
  949. $player_config .= 'freedom:"true",';
  950. $player_config .= 'container:jQuery(' . $jq_container . ')}';
  951. $html .= "jQuery({$jq_placeholder}).show(0,function(){jQuery.VideoPress.analytics.impression({$guid_js})});" . PHP_EOL;
  952. if ( $age_gate_required ) {
  953. $html .= 'if ( jQuery.VideoPress.support.flash() ) {' . PHP_EOL;
  954. /**
  955. * @link http://code.google.com/p/swfobject/wiki/api#swfobject.embedSWF(swfUrlStr,_replaceElemIdStr,_widthStr,_height
  956. */
  957. $html .= 'swfobject.embedSWF(' . implode( ',', array(
  958. 'jQuery.VideoPress.video.flash.player_uri',
  959. json_encode( $this->video_container_id ),
  960. json_encode( $width ),
  961. json_encode( $height ),
  962. 'jQuery.VideoPress.video.flash.min_version',
  963. '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
  964. '{guid:' . $guid_js . '}', // FlashVars
  965. 'jQuery.VideoPress.video.flash.params',
  966. 'null', // no attributes
  967. 'jQuery.VideoPress.video.flash.embedCallback' // error fallback
  968. ) ) . ');';
  969. $html .= '} else {' . PHP_EOL;
  970. $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js},{$player_config}," . $videopress->shown[$guid] . ') ) {' . PHP_EOL;
  971. $html .= 'if ( jQuery(' . $jq_container . ').data( "player" ) === "flash" ){jQuery.VideoPress.video.play(jQuery(' . json_encode('#' . $this->video_container_id) . '));}else{';
  972. $html .= 'jQuery(' . $jq_placeholder . ').html(' . json_encode( $this->html_age_date() ) . ');' . PHP_EOL;
  973. $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;
  974. $html .= '}}}' . PHP_EOL;
  975. } else {
  976. $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js}, {$player_config}," . $videopress->shown[$guid] . ') ) {' . PHP_EOL;
  977. if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
  978. $html .= "jQuery.VideoPress.video.play(jQuery({$jq_container}));";
  979. else
  980. $html .= 'jQuery(' . $jq_placeholder . ').one("click",function(){jQuery.VideoPress.video.play(jQuery(' . $jq_container . '))});';
  981. $html .= '}';
  982. // close the jQuery(document).ready() function
  983. $html .= '});';
  984. }
  985. $html .= '</script>' . PHP_EOL;
  986. $html .= '</div>' . PHP_EOL;
  987. /*
  988. * JavaScript required
  989. */
  990. $noun = __( 'this video', 'jetpack' );
  991. if ( ! $age_gate_required ) {
  992. $vid_type = '';
  993. if ( ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) && ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) ) )
  994. $vid_type = 'ogv';
  995. elseif ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) )
  996. $vid_type = 'mp4';
  997. elseif ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) )
  998. $vid_type = 'ogv';
  999. if ( $vid_type !== '' ) {
  1000. $noun = '<a ';
  1001. if ( isset( $this->video->language ) )
  1002. $noun .= 'hreflang="' . esc_attr( $this->video->language ) . '" ';
  1003. if ( $vid_type === 'mp4' )
  1004. $noun .= 'type="video/mp4" href="' . esc_url( $this->video->videos->mp4->url, array( 'http', 'https' ) );
  1005. elseif ( $vid_type === 'ogv' )
  1006. $noun .= 'type="video/ogv" href="' . esc_url( $this->video->videos->ogv->url, array( 'http', 'https' ) );
  1007. $noun .= '">';
  1008. if ( isset( $this->video->title ) )
  1009. $noun .= esc_html( $this->video->title );
  1010. else
  1011. $noun .= __( 'this video', 'jetpack' );
  1012. $noun .= '</a>';
  1013. } elseif ( ! empty( $this->title ) ) {
  1014. $noun = esc_html( $this->title );
  1015. }
  1016. unset( $vid_type );
  1017. }
  1018. $html .= '<noscript><p>' . sprintf( _x( 'JavaScript required to play %s.', 'Play as in playback or view a movie', 'jetpack' ), $noun ) . '</p></noscript>';
  1019. return $html;
  1020. }
  1021. /**
  1022. * Only allow legitimate Flash parameters and their values
  1023. *
  1024. * @since 1.2
  1025. * @link http://kb2.adobe.com/cps/127/tn_12701.html Flash object and embed attributes
  1026. * @link http://kb2.adobe.com/cps/133/tn_13331.html devicefont
  1027. * @link http://kb2.adobe.com/cps/164/tn_16494.html allowscriptaccess
  1028. * @link http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html full screen mode
  1029. * @link http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001079.html allownetworking
  1030. * @param array $flash_params Flash parameters expressed in key-value form
  1031. * @return array validated Flash parameters
  1032. */
  1033. public static function esc_flash_params( $flash_params ) {
  1034. $allowed_params = array(
  1035. 'swliveconnect' => array('true', 'false'),
  1036. 'play' => array('true', 'false'),
  1037. 'loop' => array('true', 'false'),
  1038. 'menu' => array('true', 'false'),
  1039. 'quality' => array('low', 'autolow', 'autohigh', 'medium', 'high', 'best'),
  1040. 'scale' => array('default', 'noborder', 'exactfit', 'noscale'),
  1041. 'align' => array('l', 'r', 't'),
  1042. 'salign' => array('l', 'r', 't', 'tl', 'tr', 'bl', 'br'),
  1043. 'wmode' => array('window', 'opaque', 'transparent','direct','gpu'),
  1044. 'devicefont' => array('_sans', '_serif', '_typewriter'),
  1045. 'allowscriptaccess' => array('always', 'samedomain', 'never'),
  1046. 'allownetworking' => array('all','internal', 'none'),
  1047. 'seamlesstabbing' => array('true', 'false'),
  1048. 'allowfullscreen' => array('true', 'false'),
  1049. 'fullScreenAspectRatio' => array('portrait', 'landscape'),
  1050. 'base',
  1051. 'bgcolor',
  1052. 'flashvars'
  1053. );
  1054. $allowed_params_keys = array_keys( $allowed_params );
  1055. $filtered_params = array();
  1056. foreach( $flash_params as $param=>$value ) {
  1057. if ( empty($param) || empty($value) )
  1058. continue;
  1059. $param = strtolower($param);
  1060. if ( in_array($param, $allowed_params_keys) ) {
  1061. if ( isset( $allowed_params[$param] ) && is_array( $allowed_params[$param] ) ) {
  1062. $value = strtolower($value);
  1063. if ( in_array( $value, $allowed_params[$param] ) )
  1064. $filtered_params[$param] = $value;
  1065. } else {
  1066. $filtered_params[$param] = $value;
  1067. }
  1068. }
  1069. }
  1070. unset( $allowed_params_keys );
  1071. /**
  1072. * Flash specifies sameDomain, not samedomain. change from lowercase value for preciseness
  1073. */
  1074. if ( isset( $filtered_params['allowscriptaccess'] ) && $filtered_params['allowscriptaccess'] === 'samedomain' )
  1075. $filtered_params['allowscriptaccess'] = 'sameDomain';
  1076. return $filtered_params;
  1077. }
  1078. /**
  1079. * Filter Flash variables from the response, taking into consideration player options.
  1080. *
  1081. * @since 1.3
  1082. * @return array Flash variable key value pairs
  1083. */
  1084. private function get_flash_variables() {
  1085. if ( ! isset( $this->video->players->swf->vars ) )
  1086. return array();
  1087. $flashvars = (array) $this->video->players->swf->vars;
  1088. if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
  1089. $flashvars['autoPlay'] = 'true';
  1090. return $flashvars;
  1091. }
  1092. /**
  1093. * Validate and filter Flash parameters
  1094. *
  1095. * @since 1.3
  1096. * @return array Flash parameters passed through key and value validation
  1097. */
  1098. private function get_flash_parameters() {
  1099. if ( ! isset( $this->video->players->swf->params ) )
  1100. return array();
  1101. else
  1102. return self::esc_flash_params( apply_filters( 'video_flash_params', (array) $this->video->players->swf->params, 10, 1 ) );
  1103. }
  1104. /**
  1105. * Flash player markup in a HTML embed element.
  1106. *
  1107. * @since 1.1
  1108. * @link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-iframe-element.html#the-embed-element embed element
  1109. * @link http://www.google.com/support/reader/bin/answer.py?answer=70664 Google Reader markup support
  1110. * @return string HTML markup. Embed element with no children
  1111. */
  1112. private function flash_embed() {
  1113. if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) )
  1114. return '';
  1115. $embed = array(
  1116. 'id' => $this->video_id,
  1117. 'src' => esc_url_raw( $this->video->players->swf->url . '&' . http_build_query( $this->get_flash_variables(), null, '&' ) , array( 'http', 'https' ) ),
  1118. 'type' => 'application/x-shockwave-flash',
  1119. 'width' => $this->video->calculated_width,
  1120. 'height' => $this->video->calculated_height
  1121. );
  1122. if ( isset( $this->video->title ) )
  1123. $embed['title'] = $this->video->title;
  1124. $embed = array_merge( $embed, $this->get_flash_parameters() );
  1125. $html = '<embed';
  1126. foreach ( $embed as $attribute => $value ) {
  1127. $html .= ' ' . esc_html( $attribute ) . '="' . esc_attr( $value ) . '"';
  1128. }
  1129. unset( $embed );
  1130. $html .= '></embed>';
  1131. return $html;
  1132. }
  1133. /**
  1134. * Double-baked Flash object markup for Internet Explorer and more standards-friendly consuming agents.
  1135. *
  1136. * @since 1.1
  1137. * @return HTML markup. Object and children.
  1138. */
  1139. private function flash_object() {
  1140. if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) )
  1141. return '';
  1142. $thumbnail_html = '<img alt="';
  1143. if ( isset( $this->video->title ) )
  1144. $thumbnail_html .= esc_attr( $this->video->title );
  1145. $thumbnail_html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" />';
  1146. $flash_vars = esc_attr( http_build_query( $this->get_flash_variables(), null, '&' ) );
  1147. $flash_params = '';
  1148. foreach ( $this->get_flash_parameters() as $attribute => $value ) {
  1149. $flash_params .= '<param name="' . esc_attr( $attribute ) . '" value="' . esc_attr( $value ) . '" />';
  1150. }
  1151. $flash_help = sprintf( __( 'This video requires <a rel="nofollow" href="%s">Adobe Flash</a> for playback.', 'jetpack' ), 'http://www.adobe.com/go/getflashplayer');
  1152. $flash_player_url = esc_url( $this->video->players->swf->url, array( 'http', 'https' ) );
  1153. $description = '';
  1154. if ( isset( $this->video->title ) ) {
  1155. $standby = $this->video->title;
  1156. $description = '<p><strong>' . esc_html( $this->video->title ) . '</strong></p>';
  1157. } else {
  1158. $standby = __( 'Loading video...', 'jetpack' );
  1159. }
  1160. $standby = ' standby="' . esc_attr( $standby ) . '"';
  1161. return <<<OBJECT
  1162. <script type="text/javascript">if(typeof swfobject!=="undefined"){swfobject.registerObject("{$this->video_id}", "{$this->video->players->swf->version}");}</script>
  1163. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}" id="{$this->video_id}"{$standby}>
  1164. <param name="movie" value="{$flash_player_url}" />
  1165. {$flash_params}
  1166. <param name="flashvars" value="{$flash_vars}" />
  1167. <!--[if !IE]>-->
  1168. <object type="application/x-shockwave-flash" data="{$flash_player_url}" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}"{$standby}>
  1169. {$flash_params}
  1170. <param name="flashvars" value="{$flash_vars}" />
  1171. <!--<![endif]-->
  1172. {$thumbnail_html}{$description}<p class="robots-nocontent">{$flash_help}</p>
  1173. <!--[if !IE]>-->
  1174. </object>
  1175. <!--<![endif]-->
  1176. </object>
  1177. OBJECT;
  1178. }
  1179. }
  1180. global $videopress;
  1181. $videopress = new VideoPress();
  1182. endif;
  1183. ?>