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