VideoPress_Video

VideoPress video object retrieved from VideoPress servers and parsed.

Defined (1)

The class is defined in the following location(s).

/modules/videopress/class.videopress-video.php  
  1. class VideoPress_Video { 
  2. public $version = 3; 
  3.  
  4. /** 
  5. * Manifest version returned by remote service. 
  6. * @var string 
  7. * @since 1.3 
  8. */ 
  9. const manifest_version = '1.5'; 
  10.  
  11. /** 
  12. * Expiration of the video expressed in Unix time 
  13. * @var int 
  14. * @since 1.3 
  15. */ 
  16. public $expires; 
  17.  
  18. /** 
  19. * VideoPress unique identifier 
  20. * @var string 
  21. * @since 1.3 
  22. */ 
  23. public $guid; 
  24.  
  25. /** 
  26. * WordPress.com blog identifier 
  27. * @var int 
  28. * @since 1.5 
  29. */ 
  30. public $blog_id; 
  31.  
  32. /** 
  33. * Remote blog attachment identifier 
  34. * @var int 
  35. * @since 1.5 
  36. */ 
  37. public $post_id; 
  38.  
  39. /** 
  40. * Maximum desired width. 
  41. * @var int 
  42. * @since 1.3 
  43. */ 
  44. public $maxwidth; 
  45.  
  46. /** 
  47. * Video width calculated based on original video dimensions and the requested maxwidth 
  48. * @var int 
  49. * @since 1.3 
  50. */ 
  51. public $calculated_width; 
  52.  
  53. /** 
  54. * Video height calculated based on original video dimensions and the requested maxwidth 
  55. * @var int 
  56. * @since 1.3 
  57. */ 
  58. public $calculated_height; 
  59.  
  60. /** 
  61. * Video title 
  62. * @var string 
  63. * @since 1.3 
  64. */ 
  65. public $title; 
  66.  
  67. /** 
  68. * Directionality of title text. ltr or rtl 
  69. * @var string 
  70. * @since 1.3 
  71. */ 
  72. public $text_direction; 
  73.  
  74. /** 
  75. * Text and audio language as ISO 639-2 language code 
  76. * @var string 
  77. * @since 1.3 
  78. */ 
  79. public $language; 
  80.  
  81. /** 
  82. * Video duration in whole seconds 
  83. * @var int 
  84. * @since 1.3 
  85. */ 
  86. public $duration; 
  87.  
  88. /** 
  89. * Recommended minimum age of the viewer. 
  90. * @var int 
  91. * @since 1.3 
  92. */ 
  93. public $age_rating; 
  94.  
  95. /** 
  96. * Video author has restricted video embedding or sharing 
  97. * @var bool 
  98. * @since 1.3 
  99. */ 
  100. public $restricted_embed; 
  101.  
  102. /** 
  103. * Poster frame image URI for the given video guid and calculated dimensions. 
  104. * @var string 
  105. * @since 1.3 
  106. */ 
  107. public $poster_frame_uri; 
  108.  
  109. /** 
  110. * Video files associated with the given guid for the calculated dimensions. 
  111. * @var stdClass 
  112. * @since 1.3 
  113. */ 
  114. public $videos; 
  115.  
  116. /** 
  117. * Video player information 
  118. * @var stdClass 
  119. * @since 1.3 
  120. */ 
  121. public $players; 
  122.  
  123. /** 
  124. * Video player skinning preferences including background color and watermark 
  125. * @var array 
  126. * @since 1.5 
  127. */ 
  128. public $skin; 
  129.  
  130. /** 
  131. * Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI 
  132. * @var array 
  133. * @since 1.5 
  134. */ 
  135. public $captions; 
  136.  
  137. /** 
  138. * Setup the object. 
  139. * Request video information from VideoPress servers and process the response. 
  140. * @since 1.3 
  141. * @var string $guid VideoPress unique identifier 
  142. * @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. 
  143. */ 
  144. public function __construct( $guid, $maxwidth = 640 ) { 
  145. $this->guid = $guid; 
  146.  
  147. $maxwidth = absint( $maxwidth ); 
  148. if ( $maxwidth > 0 ) 
  149. $this->maxwidth = $maxwidth; 
  150.  
  151. $data = $this->get_data(); 
  152. if ( is_wp_error( $data ) || empty( $data ) ) { 
  153. /** This filter is documented in modules/videopress/class.videopress-player.php */ 
  154. if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) { 
  155. // Unlike the Flash player, the new player does it's own error checking, age gate, etc. 
  156. $data = (object) array( 'guid' => $guid, 'width' => $maxwidth, 'height' => $maxwidth / 16 * 9 ); 
  157. } else { 
  158. $this->error = $data; 
  159. return; 
  160.  
  161. if ( isset( $data->blog_id ) ) 
  162. $this->blog_id = absint( $data->blog_id ); 
  163.  
  164. if ( isset( $data->post_id ) ) 
  165. $this->post_id = absint( $data->post_id ); 
  166.  
  167. if ( isset( $data->title ) && $data->title !== '' ) 
  168. $this->title = trim( str_replace( ' ', ' ', $data->title ) ); 
  169.  
  170. if ( isset( $data->text_direction ) && $data->text_direction === 'rtl' ) 
  171. $this->text_direction = 'rtl'; 
  172. else 
  173. $this->text_direction = 'ltr'; 
  174.  
  175. if ( isset( $data->language ) ) 
  176. $this->language = $data->language; 
  177.  
  178. if ( isset( $data->duration ) && $data->duration > 0 ) 
  179. $this->duration = absint( $data->duration ); 
  180.  
  181. if ( isset( $data->width ) && $data->width > 0 ) 
  182. $this->calculated_width = absint( $data->width ); 
  183.  
  184. if ( isset( $data->height ) && $data->height > 0 ) 
  185. $this->calculated_height = absint( $data->height ); 
  186.  
  187. if ( isset( $data->age_rating ) ) 
  188. $this->age_rating = absint( $this->age_rating ); 
  189.  
  190. if ( isset( $data->restricted_embed ) && $data->restricted_embed === true ) 
  191. $this->restricted_embed = true; 
  192. else 
  193. $this->restricted_embed = false; 
  194.  
  195. if ( isset( $data->posterframe ) && $data->posterframe !== '' ) 
  196. $this->poster_frame_uri = esc_url_raw( $data->posterframe, array( 'http', 'https' ) ); 
  197.  
  198. if ( isset( $data->mp4 ) || isset( $data->ogv ) ) { 
  199. $this->videos = new stdClass(); 
  200. if ( isset( $data->mp4 ) ) 
  201. $this->videos->mp4 = $data->mp4; 
  202. if ( isset( $data->ogv ) ) 
  203. $this->videos->ogv = $data->ogv; 
  204.  
  205. if ( isset( $data->swf ) ) { 
  206. if ( ! isset( $this->players ) ) 
  207. $this->players = new stdClass(); 
  208. $this->players->swf = $data->swf; 
  209.  
  210. if ( isset( $data->skin ) ) 
  211. $this->skin = $data->skin; 
  212.  
  213. if ( isset( $data->captions ) ) 
  214. $this->captions = (array) $data->captions; 
  215.  
  216. /** 
  217. * Convert an Expires HTTP header value into Unix time for use in WP Cache 
  218. * @since 1.3 
  219. * @var string $expires_header 
  220. * @return int|bool Unix time or false 
  221. */ 
  222. public static function calculate_expiration( $expires_header ) { 
  223. if ( empty( $expires_header ) || ! is_string( $expires_header ) ) 
  224. return false; 
  225.  
  226. if ( class_exists( 'DateTime' ) && class_exists( 'DateTimeZone' ) ) { 
  227. $expires_date = DateTime::createFromFormat( 'D, d M Y H:i:s T', $expires_header, new DateTimeZone( 'UTC' ) ); 
  228. if ( $expires_date instanceOf DateTime ) 
  229. return date_format( $expires_date, 'U' ); 
  230. } else { 
  231. $expires_array = strptime( $expires_header, '%a, %d %b %Y %H:%M:%S %Z' ); 
  232. 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'] ) ) 
  233. 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'] ); 
  234. return false; 
  235.  
  236. /** 
  237. * Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds. 
  238. * @since 1.2 
  239. * @param string $url absolute URL 
  240. * @return bool|string host component of the URL, or false if none found 
  241. */ 
  242. public static function hostname( $url ) { 
  243. return parse_url( esc_url_raw( $url ), PHP_URL_HOST ); 
  244.  
  245.  
  246. /** 
  247. * Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname. 
  248. * @since 1.3 
  249. * @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful 
  250. */ 
  251. private function get_data() { 
  252. global $wp_version; 
  253.  
  254. $domain = self::hostname( home_url() ); 
  255. $request_params = array( 'guid' => $this->guid, 'domain' => $domain ); 
  256. if ( isset( $this->maxwidth ) && $this->maxwidth > 0 ) 
  257. $request_params['maxwidth'] = $this->maxwidth; 
  258.  
  259. $url = 'http://videopress.com/data/wordpress.json'; 
  260. if ( is_ssl() ) 
  261. $url = 'https://v.wordpress.com/data/wordpress.json'; 
  262.  
  263. $response = wp_remote_get( add_query_arg( $request_params, $url ), array( 
  264. 'redirection' => 1,  
  265. 'user-agent' => 'VideoPress plugin ' . $this->version . '; WordPress ' . $wp_version . ' (' . home_url('/') . ')',  
  266. ) ); 
  267.  
  268. unset( $request_params ); 
  269. unset( $url ); 
  270. $response_body = wp_remote_retrieve_body( $response ); 
  271. $response_code = absint( wp_remote_retrieve_response_code( $response ) ); 
  272.  
  273. if ( is_wp_error( $response ) ) { 
  274. return $response; 
  275. } elseif ( $response_code === 400 ) { 
  276. 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' ) ); 
  277. } elseif ( $response_code === 403 ) { 
  278. 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>' ); 
  279. } elseif ( $response_code === 404 ) { 
  280. return new WP_Error( 'http_not_found', '<p>' . sprintf( __( 'No data found for VideoPress identifier: <strong>%s</strong>.', 'jetpack' ), $this->guid ) . '</p>' ); 
  281. } elseif ( $response_code !== 200 || empty( $response_body ) ) { 
  282. return; 
  283. } else { 
  284. $expires_header = wp_remote_retrieve_header( $response, 'Expires' ); 
  285. if ( ! empty( $expires_header ) ) { 
  286. $expires = self::calculate_expiration( $expires_header ); 
  287. if ( ! empty( $expires ) ) 
  288. $this->expires = $expires; 
  289.  
  290. return json_decode( $response_body );