PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.7
Jetpack – WP Security, Backup, Speed, & Growth v6.7
16.2-beta 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 All 501 releases
jetpack / modules / shortcodes / hulu.php

hulu.php in Jetpack – WP Security, Backup, Speed, & Growth 6.7, at modules/shortcodes/hulu.php

274 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 } elseif ( 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 } elseif ( 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 ),
74 $atts
75 );
76
77 $id = jetpack_shortcode_get_hulu_id( $atts );
78 if ( ! $id ) {
79 return '<!-- Hulu Error: Hulu shortcode syntax invalid. -->';
80 }
81
82 $start_time = 0;
83 if ( is_numeric( $attr['start_time'] ) ) {
84 $start_time = intval( $attr['start_time'] );
85 }
86 if ( is_numeric( $attr['end_time'] ) && intval( $attr['end_time'] ) > $start_time ) {
87 $end_time = intval( $attr['end_time'] );
88 }
89 if ( is_numeric( $attr['thumbnail_frame'] ) ) {
90 $thumbnail_frame = intval( $attr['thumbnail_frame'] );
91 }
92
93 // check to see if $id is 76560 else we assume it's gQ6Z0I990IWv_VFQI2J7Eg
94 // If id is numeric, we'll send it off to the hulu oembed api to get the embed URL (and non-numeric id)
95 if ( is_numeric( $id ) ) {
96 $transient_key = "hulu-$id";
97 if ( false === ( $transient_value = get_transient( $transient_key ) ) ) {
98 // let's make a cross-site http request out to the hulu oembed api
99 $response = wp_remote_get( 'http://www.hulu.com/api/oembed.json?url=' . urlencode( 'http://www.hulu.com/watch/' . esc_attr( $id ) ) );
100 $response_code = wp_remote_retrieve_response_code( $response );
101 $response_message = wp_remote_retrieve_response_message( $response );
102 if ( 200 !== $response_code && ! empty( $response_message ) ) {
103 return "<!-- Hulu Error: Hulu shortcode http error $response_message -->";
104 } elseif ( 200 !== $response_code ) {
105 return "<!-- Hulu Error: Hulu shortcode unknown error occurred, $response_code -->";
106 } else {
107 $response_body = wp_remote_retrieve_body( $response );
108 $json = json_decode( $response_body );
109
110 // Pull out id from embed url (from oembed API)
111 $embed_url_params = array();
112 parse_str( parse_url( $json->embed_url, PHP_URL_QUERY ), $embed_url_params );
113
114 if ( isset( $embed_url_params['eid'] ) ) {
115 $id = $embed_url_params['eid'];
116 }
117 // let's cache this response indefinitely.
118 set_transient( $transient_key, $id );
119 }
120 } else {
121 $id = $transient_value;
122 }
123 }
124
125 if ( ! $id ) {
126 return '<!-- Hulu Error: Not a Hulu video. -->';
127 }
128
129 $width = intval( $attr['width'] );
130 $height = round( ( $width / 640 ) * 360 );
131
132 $iframe_url = 'http://www.hulu.com/embed.html';
133 if ( is_ssl() ) {
134 $iframe_url = 'https://secure.hulu.com/embed.html';
135 }
136
137 $query_args = array();
138 $query_args['eid'] = esc_attr( $id );
139 if ( isset( $start_time ) ) {
140 $query_args['st'] = intval( $start_time );
141 }
142 if ( isset( $end_time ) ) {
143 $query_args['et'] = intval( $end_time );
144 }
145 if ( isset( $thumbnail_frame ) ) {
146 $query_args['it'] = 'i' . intval( $thumbnail_frame );
147 }
148
149 $iframe_url = add_query_arg( $query_args, $iframe_url );
150
151 $html = sprintf(
152 '<div class="embed-hulu" style="text-align: center;"><iframe src="%s" width="%s" height="%s" style="border:0;" scrolling="no" webkitAllowFullScreen
153 mozallowfullscreen allowfullscreen></iframe></div>',
154 esc_url( $iframe_url ),
155 esc_attr( $width ),
156 esc_attr( $height )
157 );
158 $html = apply_filters( 'video_embed_html', $html );
159
160 return $html;
161 }
162
163 /**
164 * Callback to convert Hulu links in comments into a embed src.
165 *
166 * @since 4.5.0
167 *
168 * @param array $matches
169 *
170 * @return string
171 */
172 function jetpack_hulu_link_callback( $matches ) {
173 $video_id = $matches[4];
174 $src = is_ssl()
175 ? 'https://secure.hulu.com'
176 : 'http://www.hulu.com';
177
178 // Make up an embed src to pass to the shortcode reversal function
179 $attrs['src'] = $src . '/embed.html?eid=' . esc_attr( $video_id );
180
181 return wpcom_shortcodereverse_huluhelper( $attrs );
182 }
183
184 /**
185 * Convert Hulu links in comments into a Hulu shortcode.
186 *
187 * @since 4.5.0
188 *
189 * @param string $content
190 *
191 * @return string
192 */
193 function jetpack_hulu_link( $content ) {
194 $content = preg_replace_callback( '!^(http(s)?://)?(www\.)?hulu\.com\/watch\/([0-9]+)$!im', 'jetpack_hulu_link_callback', $content );
195
196 return $content;
197 }
198
199 /**
200 * Makes a Hulu shortcode from $attrs and $pattern
201 *
202 * @since 4.5.0
203 *
204 * @param array $attrs
205 *
206 * @return string
207 */
208 function wpcom_shortcodereverse_huluhelper( $attrs ) {
209 $attrs = wpcom_shortcodereverse_parseattr( $attrs );
210
211 $src_attributes = array();
212 parse_str( parse_url( $attrs['src'], PHP_URL_QUERY ), $src_attributes );
213
214 $attrs = array_merge( $attrs, $src_attributes );
215
216 // If we don't have an eid, we can't do anything. Just send back the src string.
217 if ( ! isset( $attrs['eid'] ) ) {
218 return $attrs['src'];
219 }
220
221 $shortcode = '[hulu id=' . esc_attr( $attrs['eid'] );
222
223 if ( $attrs['width'] ) {
224 $shortcode .= ' width=' . intval( $attrs['width'] );
225 }
226
227 if ( $attrs['height'] ) {
228 $shortcode .= ' height=' . intval( $attrs['height'] );
229 }
230
231 if ( $attrs['st'] ) {
232 $shortcode .= ' start_time=' . intval( $attrs['st'] );
233 }
234
235 if ( $attrs['et'] ) {
236 $shortcode .= ' end_time=' . intval( $attrs['et'] );
237 }
238
239 if ( $attrs['it'] ) {
240 // the thumbnail frame attribute comes with an i in front of the value, so we've got to remove that
241 $shortcode .= ' thumbnail_frame=' . intval( ltrim( $attrs['it'], 'i' ) );
242 }
243 $shortcode .= ']';
244
245 return $shortcode;
246 }
247
248 /**
249 * Initiates process to convert iframe HTML into a Hulu shortcode.
250 *
251 * Example:
252 * <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>
253 *
254 * Converts to:
255 * [hulu id=nlg_ios3tutcfrhatkiaow width=512 height=288 start_time=10 end_time=20 thumbnail_frame=11]
256 *
257 * @since 4.5.0
258 *
259 * @param array $attrs
260 *
261 * @return string
262 */
263 function wpcom_shortcodereverse_huluembed( $attrs ) {
264
265 $shortcode = wpcom_shortcodereverse_huluhelper( $attrs );
266 if ( substr( $shortcode, 0, 1 ) == '[' ) {
267 /** This action is documented in modules/widgets/social-media-icons.php */
268 do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'hulu-embed' );
269 }
270
271 return $shortcode;
272 }
273 Filter_Embedded_HTML_Objects::register( '#^http://www.hulu.com/embed.html#i', 'wpcom_shortcodereverse_huluembed', true );
274