hulu.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Hulu Shortcode
  4. *
  5. * [hulu 369061]
  6. * [hulu id=369061]
  7. * [hulu id=369061 width=512 height=288 start_time="10" end_time="20" thumbnail_frame="10"]
  8. * [hulu http://www.hulu.com/watch/369061]
  9. * [hulu id=gQ6Z0I990IWv_VFQI2J7Eg width=512 height=288]
  10. *
  11. * <object width="512" height="288">
  12. * <param name="movie" value="http://www.hulu.com/embed/gQ6Z0I990IWv_VFQI2J7Eg"></param>
  13. * <param name="allowFullScreen" value="true"></param>
  14. * <embed src="http://www.hulu.com/embed/gQ6Z0I990IWv_VFQI2J7Eg" type="application/x-shockwave-flash" width="512" height="288" allowFullScreen="true"></embed>
  15. * </object>
  16. */
  17. if ( get_option( 'embed_autourls' ) ) {
  18. // Convert hulu URLS to shortcodes for old comments, saved before comments for shortcodes were enabled
  19. add_filter( 'comment_text', 'jetpack_hulu_link', 1 );
  20. }
  21. add_shortcode( 'hulu', 'jetpack_hulu_shortcode' );
  22. /**
  23. * Return a Hulu video ID from a given set to attributes.
  24. *
  25. * @since 4.5.0
  26. *
  27. * @param array $atts Shortcode parameters.
  28. *
  29. * @return string $id Hulu video ID.
  30. */
  31. function jetpack_shortcode_get_hulu_id( $atts ) {
  32. // This will catch an id explicitly defined as such, or assume any param without a label is the id. First found is used.
  33. if ( isset( $atts['id'] ) ) {
  34. // First we check to see if [hulu id=369061] or [hulu id=gQ6Z0I990IWv_VFQI2J7Eg] was used
  35. $id = esc_attr( $atts['id'] );
  36. } else if ( isset( $atts[0] ) && preg_match( '|www\.hulu\.com/watch/(\d+)|i', $atts[0], $match ) ) {
  37. // this checks for [hulu http://www.hulu.com/watch/369061]
  38. $id = (int) $match[1];
  39. } else if ( isset( $atts[0] ) ) {
  40. // This checks for [hulu 369061] or [hulu 65yppv6xqa45s5n7_m1wng]
  41. $id = esc_attr( $atts[0] );
  42. } else {
  43. $id = 0;
  44. }
  45. return $id;
  46. }
  47. /**
  48. * Convert a Hulu shortcode into an embed code.
  49. *
  50. * @since 4.5.0
  51. *
  52. * @param array $atts An array of shortcode attributes.
  53. *
  54. * @return string The embed code for the Hulu video.
  55. */
  56. function jetpack_hulu_shortcode( $atts ) {
  57. global $content_width;
  58. // Set a default content width, if it's not specified.
  59. $attr = shortcode_atts(
  60. array(
  61. 'id' => '',
  62. 'width' => $content_width ? $content_width : 640,
  63. 'start_time' => '',
  64. 'end_time' => '',
  65. 'thumbnail_frame' => ''
  66. ), $atts
  67. );
  68. $id = jetpack_shortcode_get_hulu_id( $atts );
  69. if ( ! $id ) {
  70. return '<!-- Hulu Error: Hulu shortcode syntax invalid. -->';
  71. }
  72. $start_time = 0;
  73. if ( is_numeric( $attr['start_time'] ) ) {
  74. $start_time = intval( $attr['start_time'] );
  75. }
  76. if ( is_numeric( $attr['end_time'] ) && intval( $attr['end_time'] ) > $start_time ) {
  77. $end_time = intval( $attr['end_time'] );
  78. }
  79. if ( is_numeric( $attr['thumbnail_frame'] ) ) {
  80. $thumbnail_frame = intval( $attr['thumbnail_frame'] );
  81. }
  82. // check to see if $id is 76560 else we assume it's gQ6Z0I990IWv_VFQI2J7Eg
  83. // If id is numeric, we'll send it off to the hulu oembed api to get the embed URL (and non-numeric id)
  84. if ( is_numeric( $id ) ) {
  85. $transient_key = "hulu-$id";
  86. if ( false === ( $transient_value = get_transient( $transient_key ) ) ) {
  87. // let's make a cross-site http request out to the hulu oembed api
  88. $response = wp_remote_get( 'http://www.hulu.com/api/oembed.json?url=' . urlencode( 'http://www.hulu.com/watch/' . esc_attr( $id ) ) );
  89. $response_code = wp_remote_retrieve_response_code( $response );
  90. $response_message = wp_remote_retrieve_response_message( $response );
  91. if ( 200 !== $response_code && ! empty( $response_message ) ) {
  92. return "<!-- Hulu Error: Hulu shortcode http error $response_message -->";
  93. } elseif ( 200 !== $response_code ) {
  94. return "<!-- Hulu Error: Hulu shortcode unknown error occurred, $response_code -->";
  95. } else {
  96. $response_body = wp_remote_retrieve_body( $response );
  97. $json = json_decode( $response_body );
  98. // Pull out id from embed url (from oembed API)
  99. $embed_url_params = array();
  100. parse_str( parse_url( $json->embed_url, PHP_URL_QUERY ), $embed_url_params );
  101. if ( isset( $embed_url_params['eid'] ) ) {
  102. $id = $embed_url_params['eid'];
  103. }
  104. // let's cache this response indefinitely.
  105. set_transient( $transient_key, $id );
  106. }
  107. } else {
  108. $id = $transient_value;
  109. }
  110. }
  111. if ( ! $id ) {
  112. return '<!-- Hulu Error: Not a Hulu video. -->';
  113. }
  114. $width = intval( $attr['width'] );
  115. $height = round( ( $width / 640 ) * 360 );
  116. $iframe_url = 'http://www.hulu.com/embed.html';
  117. if ( is_ssl() ) {
  118. $iframe_url = 'https://secure.hulu.com/embed.html';
  119. }
  120. $query_args = array();
  121. $query_args['eid'] = esc_attr( $id );
  122. if ( isset( $start_time ) ) {
  123. $query_args['st'] = intval( $start_time );
  124. }
  125. if ( isset( $end_time ) ) {
  126. $query_args['et'] = intval( $end_time );
  127. }
  128. if ( isset( $thumbnail_frame ) ) {
  129. $query_args['it'] = 'i' . intval( $thumbnail_frame );
  130. }
  131. $iframe_url = add_query_arg( $query_args, $iframe_url );
  132. $html = sprintf(
  133. '<div class="embed-hulu" style="text-align: center;"><iframe src="%s" width="%s" height="%s" style="border:0;" scrolling="no" webkitAllowFullScreen
  134. mozallowfullscreen allowfullscreen></iframe></div>',
  135. esc_url( $iframe_url ),
  136. esc_attr( $width ),
  137. esc_attr( $height )
  138. );
  139. $html = apply_filters( 'video_embed_html', $html );
  140. return $html;
  141. }
  142. /**
  143. * Callback to convert Hulu links in comments into a embed src.
  144. *
  145. * @since 4.5.0
  146. *
  147. * @param array $matches
  148. *
  149. * @return string
  150. */
  151. function jetpack_hulu_link_callback( $matches ) {
  152. $video_id = $matches[4];
  153. $src = is_ssl()
  154. ? 'https://secure.hulu.com'
  155. : 'http://www.hulu.com';
  156. // Make up an embed src to pass to the shortcode reversal function
  157. $attrs['src'] = $src . '/embed.html?eid=' . esc_attr( $video_id );
  158. return wpcom_shortcodereverse_huluhelper( $attrs );
  159. }
  160. /**
  161. * Convert Hulu links in comments into a Hulu shortcode.
  162. *
  163. * @since 4.5.0
  164. *
  165. * @param string $content
  166. *
  167. * @return string
  168. */
  169. function jetpack_hulu_link( $content ) {
  170. $content = preg_replace_callback( '!^(http(s)?://)?(www\.)?hulu\.com\/watch\/([0-9]+)$!im', 'jetpack_hulu_link_callback', $content );
  171. return $content;
  172. }
  173. /**
  174. * Makes a Hulu shortcode from $attrs and $pattern
  175. *
  176. * @since 4.5.0
  177. *
  178. * @param array $attrs
  179. *
  180. * @return string
  181. */
  182. function wpcom_shortcodereverse_huluhelper( $attrs ) {
  183. $attrs = wpcom_shortcodereverse_parseattr( $attrs );
  184. $src_attributes = array();
  185. parse_str( parse_url( $attrs['src'], PHP_URL_QUERY ), $src_attributes );
  186. $attrs = array_merge( $attrs, $src_attributes );
  187. // If we don't have an eid, we can't do anything. Just send back the src string.
  188. if ( ! isset( $attrs['eid'] ) ) {
  189. return $attrs['src'];
  190. }
  191. $shortcode = '[hulu id=' . esc_attr( $attrs['eid'] );
  192. if ( $attrs['width'] ) {
  193. $shortcode .= ' width=' . intval( $attrs['width'] );
  194. }
  195. if ( $attrs['height'] ) {
  196. $shortcode .= ' height=' . intval( $attrs['height'] );
  197. }
  198. if ( $attrs['st'] ) {
  199. $shortcode .= ' start_time=' . intval( $attrs['st'] );
  200. }
  201. if ( $attrs['et'] ) {
  202. $shortcode .= ' end_time=' . intval( $attrs['et'] );
  203. }
  204. if ( $attrs['it'] ) {
  205. // the thumbnail frame attribute comes with an i in front of the value, so we've got to remove that
  206. $shortcode .= ' thumbnail_frame=' . intval( ltrim( $attrs['it'], 'i' ) );
  207. }
  208. $shortcode .= ']';
  209. return $shortcode;
  210. }
  211. /**
  212. * Initiates process to convert iframe HTML into a Hulu shortcode.
  213. *
  214. * Example:
  215. * <iframe width="512" height="288" src="http://www.hulu.com/embed.html?eid=nlg_ios3tutcfrhatkiaow&et=20&st=10&it=i11" frameborder="0" scrolling="no" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>
  216. *
  217. * Converts to:
  218. * [hulu id=nlg_ios3tutcfrhatkiaow width=512 height=288 start_time=10 end_time=20 thumbnail_frame=11]
  219. *
  220. * @since 4.5.0
  221. *
  222. * @param array $attrs
  223. *
  224. * @return string
  225. */
  226. function wpcom_shortcodereverse_huluembed( $attrs ) {
  227. $shortcode = wpcom_shortcodereverse_huluhelper( $attrs );
  228. if ( substr( $shortcode, 0, 1 ) == '[' ) {
  229. /** This action is documented in modules/widgets/social-media-icons.php */
  230. do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'hulu-embed' );
  231. }
  232. return $shortcode;
  233. }
  234. Filter_Embedded_HTML_Objects::register( '#^http://www.hulu.com/embed.html#i', 'wpcom_shortcodereverse_huluembed', true );