class.videopress-video.php 9.0 KB

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