Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

hulu.php ➔ jetpack_hulu_shortcode()   F

Complexity

Conditions 17
Paths 305

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
nc 305
nop 1
dl 0
loc 100
rs 2.4566
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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