jetpack_hulu_shortcode

Convert a Hulu shortcode into an embed code.

Description

(string) jetpack_hulu_shortcode( (array) $atts ); 

Returns (string)

The embed code for the Hulu video.

Parameters (1)

0. $atts (array)
An array of shortcode attributes.

Usage

  1. if ( !function_exists( 'jetpack_hulu_shortcode' ) ) { 
  2. require_once '/modules/shortcodes/hulu.php'; 
  3.  
  4. // An array of shortcode attributes. 
  5. $atts = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = jetpack_hulu_shortcode($atts); 
  9.  

Defined (1)

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

/modules/shortcodes/hulu.php  
  1. function jetpack_hulu_shortcode( $atts ) { 
  2. global $content_width; 
  3.  
  4. // Set a default content width, if it's not specified. 
  5. $attr = shortcode_atts( 
  6. array( 
  7. 'id' => '',  
  8. 'width' => $content_width ? $content_width : 640,  
  9. 'start_time' => '',  
  10. 'end_time' => '',  
  11. 'thumbnail_frame' => '' 
  12. ), $atts 
  13. ); 
  14.  
  15. $id = jetpack_shortcode_get_hulu_id( $atts ); 
  16. if ( ! $id ) { 
  17. return '<!-- Hulu Error: Hulu shortcode syntax invalid. -->'; 
  18.  
  19. $start_time = 0; 
  20. if ( is_numeric( $attr['start_time'] ) ) { 
  21. $start_time = intval( $attr['start_time'] ); 
  22. if ( is_numeric( $attr['end_time'] ) && intval( $attr['end_time'] ) > $start_time ) { 
  23. $end_time = intval( $attr['end_time'] ); 
  24. if ( is_numeric( $attr['thumbnail_frame'] ) ) { 
  25. $thumbnail_frame = intval( $attr['thumbnail_frame'] ); 
  26.  
  27. // check to see if $id is 76560 else we assume it's gQ6Z0I990IWv_VFQI2J7Eg 
  28. // If id is numeric, we'll send it off to the hulu oembed api to get the embed URL (and non-numeric id) 
  29. if ( is_numeric( $id ) ) { 
  30. $transient_key = "hulu-$id"; 
  31. if ( false === ( $transient_value = get_transient( $transient_key ) ) ) { 
  32. // let's make a cross-site http request out to the hulu oembed api 
  33. $response = wp_remote_get( 'http://www.hulu.com/api/oembed.json?url=' . urlencode( 'http://www.hulu.com/watch/' . esc_attr( $id ) ) ); 
  34. $response_code = wp_remote_retrieve_response_code( $response ); 
  35. $response_message = wp_remote_retrieve_response_message( $response ); 
  36. if ( 200 !== $response_code && ! empty( $response_message ) ) { 
  37. return "<!-- Hulu Error: Hulu shortcode http error $response_message -->"; 
  38. } elseif ( 200 !== $response_code ) { 
  39. return "<!-- Hulu Error: Hulu shortcode unknown error occurred, $response_code -->"; 
  40. } else { 
  41. $response_body = wp_remote_retrieve_body( $response ); 
  42. $json = json_decode( $response_body ); 
  43.  
  44. // Pull out id from embed url (from oembed API) 
  45. $embed_url_params = array(); 
  46. parse_str( parse_url( $json->embed_url, PHP_URL_QUERY ), $embed_url_params ); 
  47.  
  48. if ( isset( $embed_url_params['eid'] ) ) { 
  49. $id = $embed_url_params['eid']; 
  50. // let's cache this response indefinitely. 
  51. set_transient( $transient_key, $id ); 
  52. } else { 
  53. $id = $transient_value; 
  54.  
  55. if ( ! $id ) { 
  56. return '<!-- Hulu Error: Not a Hulu video. -->'; 
  57.  
  58. $width = intval( $attr['width'] ); 
  59. $height = round( ( $width / 640 ) * 360 ); 
  60.  
  61. $iframe_url = 'http://www.hulu.com/embed.html'; 
  62. if ( is_ssl() ) { 
  63. $iframe_url = 'https://secure.hulu.com/embed.html'; 
  64.  
  65. $query_args = array(); 
  66. $query_args['eid'] = esc_attr( $id ); 
  67. if ( isset( $start_time ) ) { 
  68. $query_args['st'] = intval( $start_time ); 
  69. if ( isset( $end_time ) ) { 
  70. $query_args['et'] = intval( $end_time ); 
  71. if ( isset( $thumbnail_frame ) ) { 
  72. $query_args['it'] = 'i' . intval( $thumbnail_frame ); 
  73.  
  74. $iframe_url = add_query_arg( $query_args, $iframe_url ); 
  75.  
  76. $html = sprintf( 
  77. '<div class="embed-hulu" style="text-align: center;"><iframe src="%s" width="%s" height="%s" style="border:0;" scrolling="no" webkitAllowFullScreen 
  78. mozallowfullscreen allowfullscreen></iframe></div>',  
  79. esc_url( $iframe_url ),  
  80. esc_attr( $width ),  
  81. esc_attr( $height ) 
  82. ); 
  83. $html = apply_filters( 'video_embed_html', $html ); 
  84.  
  85. return $html;