PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/videopress/class.videopress-video.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 324 lines | 130 code | 48 blank | 146 comment | 50 complexity | 71499e526ca9aac157c7e030704e74d3 MD5 | raw file
  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. * Directionality of title text. ltr or rtl
  73. *
  74. * @var string
  75. * @since 1.3
  76. */
  77. public $text_direction;
  78. /**
  79. * Text and audio language as ISO 639-2 language code
  80. *
  81. * @var string
  82. * @since 1.3
  83. */
  84. public $language;
  85. /**
  86. * Video duration in whole seconds
  87. *
  88. * @var int
  89. * @since 1.3
  90. */
  91. public $duration;
  92. /**
  93. * Recommended minimum age of the viewer.
  94. *
  95. * @var int
  96. * @since 1.3
  97. */
  98. public $age_rating;
  99. /**
  100. * Video author has restricted video embedding or sharing
  101. *
  102. * @var bool
  103. * @since 1.3
  104. */
  105. public $restricted_embed;
  106. /**
  107. * Poster frame image URI for the given video guid and calculated dimensions.
  108. *
  109. * @var string
  110. * @since 1.3
  111. */
  112. public $poster_frame_uri;
  113. /**
  114. * Video files associated with the given guid for the calculated dimensions.
  115. *
  116. * @var stdClass
  117. * @since 1.3
  118. */
  119. public $videos;
  120. /**
  121. * Video player information
  122. *
  123. * @var stdClass
  124. * @since 1.3
  125. */
  126. public $players;
  127. /**
  128. * Video player skinning preferences including background color and watermark
  129. *
  130. * @var array
  131. * @since 1.5
  132. */
  133. public $skin;
  134. /**
  135. * Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI
  136. *
  137. * @var array
  138. * @since 1.5
  139. */
  140. public $captions;
  141. /**
  142. * Setup the object.
  143. * Request video information from VideoPress servers and process the response.
  144. *
  145. * @since 1.3
  146. * @var string $guid VideoPress unique identifier
  147. * @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.
  148. */
  149. public function __construct( $guid, $maxwidth = 0 ) {
  150. $this->guid = $guid;
  151. $maxwidth = absint( $maxwidth );
  152. if ( $maxwidth > 0 )
  153. $this->maxwidth = $maxwidth;
  154. $data = $this->get_data();
  155. if ( is_wp_error( $data ) || empty( $data ) ) {
  156. $this->error = $data;
  157. return;
  158. }
  159. if ( isset( $data->blog_id ) )
  160. $this->blog_id = absint( $data->blog_id );
  161. if ( isset( $data->post_id ) )
  162. $this->post_id = absint( $data->post_id );
  163. if ( isset( $data->title ) && $data->title !== '' )
  164. $this->title = trim( str_replace( '&nbsp;', ' ', $data->title ) );
  165. if ( isset( $data->text_direction ) && $data->text_direction === 'rtl' )
  166. $this->text_direction = 'rtl';
  167. else
  168. $this->text_direction = 'ltr';
  169. if ( isset( $data->language ) )
  170. $this->language = $data->language;
  171. if ( isset( $data->duration ) && $data->duration > 0 )
  172. $this->duration = absint( $data->duration );
  173. if ( isset( $data->width ) && $data->width > 0 )
  174. $this->calculated_width = absint( $data->width );
  175. if ( isset( $data->height ) && $data->height > 0 )
  176. $this->calculated_height = absint( $data->height );
  177. if ( isset( $data->age_rating ) )
  178. $this->age_rating = absint( $this->age_rating );
  179. if ( isset( $data->restricted_embed ) && $data->restricted_embed === true )
  180. $this->restricted_embed = true;
  181. else
  182. $this->restricted_embed = false;
  183. if ( isset( $data->posterframe ) && $data->posterframe !== '' )
  184. $this->poster_frame_uri = esc_url_raw( $data->posterframe, array( 'http', 'https' ) );
  185. if ( isset( $data->mp4 ) || isset( $data->ogv ) ) {
  186. $this->videos = new stdClass();
  187. if ( isset( $data->mp4 ) )
  188. $this->videos->mp4 = $data->mp4;
  189. if ( isset( $data->ogv ) )
  190. $this->videos->ogv = $data->ogv;
  191. }
  192. if ( isset( $data->swf ) ) {
  193. if ( ! isset( $this->players ) )
  194. $this->players = new stdClass();
  195. $this->players->swf = $data->swf;
  196. }
  197. if ( isset( $data->skin ) )
  198. $this->skin = $data->skin;
  199. if ( isset( $data->captions ) )
  200. $this->captions = (array) $data->captions;
  201. }
  202. /**
  203. * Convert an Expires HTTP header value into Unix time for use in WP Cache
  204. *
  205. * @since 1.3
  206. * @var string $expires_header
  207. * @return int|bool Unix time or false
  208. */
  209. public static function calculate_expiration( $expires_header ) {
  210. if ( empty( $expires_header ) || ! is_string( $expires_header ) )
  211. return false;
  212. if ( class_exists( 'DateTime' ) && class_exists( 'DateTimeZone' ) ) {
  213. $expires_date = DateTime::createFromFormat( 'D, d M Y H:i:s T', $expires_header, new DateTimeZone( 'UTC' ) );
  214. if ( $expires_date instanceOf DateTime )
  215. return date_format( $expires_date, 'U' );
  216. } else {
  217. $expires_array = strptime( $expires_header, '%a, %d %b %Y %H:%M:%S %Z' );
  218. 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'] ) )
  219. 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'] );
  220. }
  221. return false;
  222. }
  223. /**
  224. * Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds.
  225. *
  226. * @since 1.2
  227. * @param string $url absolute URL
  228. * @return bool|string host component of the URL, or false if none found
  229. */
  230. public static function hostname( $url ) {
  231. return parse_url( esc_url_raw( $url ), PHP_URL_HOST );
  232. }
  233. /**
  234. * Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname.
  235. *
  236. * @since 1.3
  237. * @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful
  238. */
  239. private function get_data() {
  240. global $wp_version;
  241. $domain = self::hostname( home_url() );
  242. $request_params = array( 'guid' => $this->guid, 'domain' => $domain );
  243. if ( isset( $this->maxwidth ) && $this->maxwidth > 0 )
  244. $request_params['maxwidth'] = $this->maxwidth;
  245. $url = 'http://videopress.com/data/wordpress.json';
  246. if ( is_ssl() )
  247. $url = 'https://v.wordpress.com/data/wordpress.json';
  248. $response = wp_remote_get( add_query_arg( $request_params, $url ), array(
  249. 'redirection' => 1,
  250. 'user-agent' => 'VideoPress plugin ' . $this->version . '; WordPress ' . $wp_version . ' (' . home_url('/') . ')',
  251. ) );
  252. unset( $request_params );
  253. unset( $url );
  254. $response_body = wp_remote_retrieve_body( $response );
  255. $response_code = absint( wp_remote_retrieve_response_code( $response ) );
  256. if ( is_wp_error( $response ) ) {
  257. return $response;
  258. } elseif ( $response_code === 400 ) {
  259. 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' ) );
  260. } elseif ( $response_code === 403 ) {
  261. 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>' );
  262. } elseif ( $response_code === 404 ) {
  263. return new WP_Error( 'http_not_found', '<p>' . sprintf( __( 'No data found for VideoPress identifier: <strong>%s</strong>.', 'jetpack' ), $this->guid ) . '</p>' );
  264. } elseif ( $response_code !== 200 || empty( $response_body ) ) {
  265. return;
  266. } else {
  267. $expires_header = wp_remote_retrieve_header( $response, 'Expires' );
  268. if ( ! empty( $expires_header ) ) {
  269. $expires = self::calculate_expiration( $expires_header );
  270. if ( ! empty( $expires ) )
  271. $this->expires = $expires;
  272. }
  273. return json_decode( $response_body );
  274. }
  275. }
  276. }