Completed
Push — update/editor-blocks-icon-colo... ( 093ab2...3cfb5e )
by
unknown
08:47
created

hulu.php ➔ jetpack_hulu_link()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
 * @package Jetpack
18
 */
19
20
add_shortcode( 'hulu', 'jetpack_hulu_shortcode' );
21
22 View Code Duplication
if (
23
	! is_admin()
24
	/** This filter is documented in modules/shortcodes/youtube.php */
25
	&& apply_filters( 'jetpack_comments_allow_oembed', true )
26
	// No need for this on WordPress.com, this is done for multiple shortcodes at a time there.
27
	&& ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
28
) {
29
	add_filter( 'comment_text', 'jetpack_hulu_link', 1 );
30
}
31
32
/**
33
 * Return a Hulu video ID from a given set to attributes.
34
 *
35
 * @since 4.5.0
36
 *
37
 * @param array $atts Shortcode parameters.
38
 *
39
 * @return string $id  Hulu video ID.
40
 */
41
function jetpack_shortcode_get_hulu_id( $atts ) {
42
	// This will catch an id explicitly defined as such, or assume any param without a label is the id.  First found is used.
43
	if ( isset( $atts['id'] ) ) {
44
		// First we check to see if [hulu id=369061] or [hulu id=gQ6Z0I990IWv_VFQI2J7Eg] was used.
45
		$id = esc_attr( $atts['id'] );
46
	} elseif ( isset( $atts[0] ) && preg_match( '|www\.hulu\.com/watch/(\d+)|i', $atts[0], $match ) ) {
47
		// this checks for [hulu http://www.hulu.com/watch/369061].
48
		$id = (int) $match[1];
49
	} elseif ( isset( $atts[0] ) ) {
50
		// This checks for [hulu 369061] or [hulu 65yppv6xqa45s5n7_m1wng].
51
		$id = esc_attr( $atts[0] );
52
	} else {
53
		$id = 0;
54
	}
55
56
	return $id;
57
}
58
59
/**
60
 * Convert a Hulu shortcode into an embed code.
61
 *
62
 * @since 4.5.0
63
 *
64
 * @param array $atts An array of shortcode attributes.
65
 *
66
 * @return string The embed code for the Hulu video.
67
 */
68
function jetpack_hulu_shortcode( $atts ) {
69
	global $content_width;
70
71
	// Set a default content width, if it's not specified.
72
	$attr = shortcode_atts(
73
		array(
74
			'id'              => '',
75
			'width'           => $content_width ? $content_width : 640,
76
			'start_time'      => '',
77
			'end_time'        => '',
78
			'thumbnail_frame' => '',
79
		),
80
		$atts
81
	);
82
83
	$id = jetpack_shortcode_get_hulu_id( $atts );
84
	if ( ! $id ) {
85
		return '<!-- Hulu Error: Hulu shortcode syntax invalid. -->';
86
	}
87
88
	$start_time = 0;
89
	if ( is_numeric( $attr['start_time'] ) ) {
90
		$start_time = intval( $attr['start_time'] );
91
	}
92
	if ( is_numeric( $attr['end_time'] ) && intval( $attr['end_time'] ) > $start_time ) {
93
		$end_time = intval( $attr['end_time'] );
94
	}
95
	if ( is_numeric( $attr['thumbnail_frame'] ) ) {
96
		$thumbnail_frame = intval( $attr['thumbnail_frame'] );
97
	}
98
99
	// check to see if $id is 76560 else we assume it's gQ6Z0I990IWv_VFQI2J7Eg
100
	// If id is numeric, we'll send it off to the hulu oembed api to get the embed URL (and non-numeric id).
101
	if ( is_numeric( $id ) ) {
102
		$transient_key   = "hulu-$id";
103
		$transient_value = get_transient( $transient_key );
104
105
		if ( false === $transient_value ) {
106
			// let's make a cross-site http request out to the hulu oembed api.
107
			$oembed_url       = sprintf(
108
				'https://www.hulu.com/api/oembed.json?url=%s',
109
				rawurlencode( 'https://www.hulu.com/watch/' . esc_attr( $id ) )
110
			);
111
			$response         = wp_remote_get( $oembed_url );
112
			$response_code    = wp_remote_retrieve_response_code( $response );
113
			$response_message = wp_remote_retrieve_response_message( $response );
114
			if ( 200 !== $response_code && ! empty( $response_message ) ) {
115
				return "<!-- Hulu Error: Hulu shortcode http error $response_message -->";
116
			} elseif ( 200 !== $response_code ) {
117
				return "<!-- Hulu Error: Hulu shortcode unknown error occurred, $response_code -->";
118
			} else {
119
				$response_body = wp_remote_retrieve_body( $response );
120
				$json          = json_decode( $response_body );
121
122
				// Pull out id from embed url (from oembed API).
123
				$embed_url_params = array();
124
				parse_str( wp_parse_url( $json->embed_url, PHP_URL_QUERY ), $embed_url_params );
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_QUERY.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
125
126
				if ( isset( $embed_url_params['eid'] ) ) {
127
					$id = $embed_url_params['eid'];
128
				}
129
				// let's cache this response indefinitely.
130
				set_transient( $transient_key, $id );
131
			}
132
		} else {
133
			$id = $transient_value;
134
		}
135
	}
136
137
	if ( ! $id ) {
138
		return '<!-- Hulu Error: Not a Hulu video. -->';
139
	}
140
141
	$query_args        = array();
142
	$query_args['eid'] = esc_attr( $id );
143
	if ( isset( $start_time ) ) {
144
		$query_args['st'] = intval( $start_time );
145
	}
146
	if ( isset( $end_time ) ) {
147
		$query_args['et'] = intval( $end_time );
148
	}
149
	if ( isset( $thumbnail_frame ) ) {
150
		$query_args['it'] = 'i' . intval( $thumbnail_frame );
151
	}
152
153
	$iframe_url = add_query_arg( $query_args, 'https://www.hulu.com/embed.html' );
154
	$width      = intval( $attr['width'] );
155
	$height     = round( ( $width / 640 ) * 360 );
156
157
	$html = sprintf(
158
		'<div class="embed-hulu" style="text-align: center;"><iframe src="%s" width="%s" height="%s" style="border:0;" scrolling="no" webkitAllowFullScreen
159
mozallowfullscreen allowfullscreen></iframe></div>',
160
		esc_url( $iframe_url ),
161
		esc_attr( $width ),
162
		esc_attr( $height )
163
	);
164
	$html = apply_filters( 'video_embed_html', $html );
165
166
	return $html;
167
}
168
169
/**
170
 * Callback to convert Hulu links in comments into a embed src.
171
 *
172
 * @since 4.5.0
173
 *
174
 * @param array $matches Array of matches from regex.
175
 *
176
 * @return string
177
 */
178
function jetpack_hulu_link_callback( $matches ) {
179
	$video_id = $matches[4];
180
181
	// Make up an embed src to pass to the shortcode reversal function.
182
	$attrs = array(
183
		'src' => 'https://www.hulu.com/embed.html?eid=' . esc_attr( $video_id ),
184
	);
185
186
	return wpcom_shortcodereverse_huluhelper( $attrs );
187
}
188
189
/**
190
 * Convert Hulu links in comments into a Hulu shortcode.
191
 *
192
 * @since 4.5.0
193
 *
194
 * @param string $content Post content.
195
 *
196
 * @return string
197
 */
198
function jetpack_hulu_link( $content ) {
199
	$content = preg_replace_callback( '!^(http(s)?://)?(www\.)?hulu\.com\/watch\/([0-9]+)$!im', 'jetpack_hulu_link_callback', $content );
200
201
	return $content;
202
}
203
204
/**
205
 * Makes a Hulu shortcode from $attrs and $pattern
206
 *
207
 * @since 4.5.0
208
 *
209
 * @param array $attrs Shortcode attributes.
210
 *
211
 * @return string
212
 */
213
function wpcom_shortcodereverse_huluhelper( $attrs ) {
214
	$attrs = wpcom_shortcodereverse_parseattr( $attrs );
215
216
	$src_attributes = array();
217
	parse_str( wp_parse_url( $attrs['src'], PHP_URL_QUERY ), $src_attributes );
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_QUERY.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
218
219
	$attrs = array_merge( $attrs, $src_attributes );
220
221
	// If we don't have an eid, we can't do anything.  Just send back the src string.
222
	if ( ! isset( $attrs['eid'] ) ) {
223
		return $attrs['src'];
224
	}
225
226
	$shortcode = '[hulu id=' . esc_attr( $attrs['eid'] );
227
228
	if ( $attrs['width'] ) {
229
		$shortcode .= ' width=' . intval( $attrs['width'] );
230
	}
231
232
	if ( $attrs['height'] ) {
233
		$shortcode .= ' height=' . intval( $attrs['height'] );
234
	}
235
236
	if ( $attrs['st'] ) {
237
		$shortcode .= ' start_time=' . intval( $attrs['st'] );
238
	}
239
240
	if ( $attrs['et'] ) {
241
		$shortcode .= ' end_time=' . intval( $attrs['et'] );
242
	}
243
244
	if ( $attrs['it'] ) {
245
		// the thumbnail frame attribute comes with an i in front of the value, so we've got to remove that.
246
		$shortcode .= ' thumbnail_frame=' . intval( ltrim( $attrs['it'], 'i' ) );
247
	}
248
	$shortcode .= ']';
249
250
	return $shortcode;
251
}
252
253
/**
254
 * Initiates process to convert iframe HTML into a Hulu shortcode.
255
 *
256
 * Example:
257
 * <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>
258
 *
259
 * Converts to:
260
 * [hulu id=nlg_ios3tutcfrhatkiaow width=512 height=288 start_time=10 end_time=20 thumbnail_frame=11]
261
 *
262
 * @since 4.5.0
263
 *
264
 * @param array $attrs Shortcode attributes.
265
 *
266
 * @return string
267
 */
268
function wpcom_shortcodereverse_huluembed( $attrs ) {
269
270
	$shortcode = wpcom_shortcodereverse_huluhelper( $attrs );
271
272
	if ( '[' === substr( $shortcode, 0, 1 ) ) {
273
		/** This action is documented in modules/widgets/social-media-icons.php */
274
		do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'hulu-embed' );
275
	}
276
277
	return $shortcode;
278
}
279
Filter_Embedded_HTML_Objects::register( '#^https?://www.hulu.com/embed.html#i', 'wpcom_shortcodereverse_huluembed', true );
280