Completed
Push — branch-22.3 ( 2d311f )
by Jeremy
06:36
created

youtube.php ➔ jetpack_shortcode_youtube_args()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Youtube shortcode
4
 *
5
 * Contains shortcode + some improvements over the Core Embeds syntax (see http://codex.wordpress.org/Embeds )
6
 *
7
 * Examples:
8
 * [youtube https://www.youtube.com/watch?v=WVbQ-oro7FQ]
9
 * [youtube=http://www.youtube.com/watch?v=wq0rXGLs0YM&fs=1&hl=bg_BG&autohide=1&rel=0]
10
 * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
11
 * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
12
 * https://www.youtube.com/playlist?list=PLP7HaNDU4Cifov7C2fQM8Ij6Ew_uPHEXW
13
 *
14
 * @package Jetpack
15
 */
16
17
/**
18
 * Replaces YouTube embeds with YouTube shortcodes.
19
 *
20
 * Covers the following formats:
21
 * 2008-07-15:
22
 * <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/bZBHZT3a-FA&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/bZBHZT3a-FA&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>
23
 * around 2008-06-06 youtube changed their old embed code to this:
24
 * <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/M1D30gS7Z8U&hl=en"></param><embed src="http://www.youtube.com/v/M1D30gS7Z8U&hl=en" type="application/x-shockwave-flash" width="425" height="344"></embed></object>
25
 * old style was:
26
 * <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/dGY28Qbj76A&rel=0"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/dGY28Qbj76A&rel=0" type="application/x-shockwave-flash" wmode="transparent" width="425" height="344"></embed></object>
27
 * 12-2010:
28
 * <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/3H8bnKdf654?fs=1&amp;hl=en_GB"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/3H8bnKdf654?fs=1&amp;hl=en_GB" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
29
 * 01-2011:
30
 * <iframe title="YouTube video player" class="youtube-player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/Qq9El3ki0_g" frameborder="0" allowFullScreen></iframe>
31
 * <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0"></iframe>
32
 *
33
 * @param string $content HTML content.
34
 * @return string The content with YouTube embeds replaced with YouTube shortcodes.
35
 */
36
function youtube_embed_to_short_code( $content ) {
37
	if ( ! is_string( $content ) || false === strpos( $content, 'youtube.com' ) ) {
38
		return $content;
39
	}
40
41
	// older codes.
42
	$regexp         = '!<object(.*?)>.*?<param\s+name=[\'"]movie[\'"]\s+value=[\'"](https?:)?//www\.youtube\.com/v/([^\'"]+)[\'"].*?>.*?</object>!i';
43
	$regexp_ent     = htmlspecialchars( $regexp, ENT_NOQUOTES );
44
	$old_regexp     = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|&#0*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!';
45
	$old_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) );
46
47
	// new code.
48
	$ifr_regexp     = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="(https?:)?//(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i';
49
	$ifr_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) );
50
51
	foreach ( compact( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg => $regexp ) {
52
		if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
53
			continue;
54
		}
55
56
		foreach ( $matches as $match ) {
57
			/*
58
			 * Hack, but '?' should only ever appear once, and
59
			 * it should be for the 1st field-value pair in query string,
60
			 * if it is present
61
			 * YouTube changed their embed code.
62
			 * Example of how it is now:
63
			 * <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
64
			 * As shown at the start of function, previous YouTube didn't '?'
65
			 * the 1st field-value pair.
66
			 */
67
			if ( in_array( $reg, array( 'ifr_regexp', 'ifr_regexp_ent', 'regexp', 'regexp_ent' ), true ) ) {
68
				$params = $match[1];
69
70
				if ( in_array( $reg, array( 'ifr_regexp_ent', 'regexp_ent' ), true ) ) {
71
					$params = html_entity_decode( $params );
72
				}
73
74
				$params = wp_kses_hair( $params, array( 'http' ) );
75
76
				$width  = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
77
				$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
78
				$wh     = '';
79
80
				if ( $width && $height ) {
81
					$wh = "&w=$width&h=$height";
82
				}
83
84
				$url = esc_url_raw( "https://www.youtube.com/watch?v={$match[3]}{$wh}" );
85
			} else {
86
				$match[1] = str_replace( '?', '&', $match[1] );
87
88
				$url = esc_url_raw( 'https://www.youtube.com/watch?v=' . html_entity_decode( $match[1] ) );
89
			}
90
91
			$content = str_replace( $match[0], "[youtube $url]", $content );
92
93
			/**
94
			 * Fires before the YouTube embed is transformed into a shortcode.
95
			 *
96
			 * @module shortcodes
97
			 *
98
			 * @since 1.2.0
99
			 *
100
			 * @param string youtube Shortcode name.
101
			 * @param string $url YouTube video URL.
102
			 */
103
			do_action( 'jetpack_embed_to_shortcode', 'youtube', $url );
104
		}
105
	}
106
107
	return $content;
108
}
109
add_filter( 'pre_kses', 'youtube_embed_to_short_code' );
110
111
/**
112
 * Replaces plain-text links to YouTube videos with YouTube embeds.
113
 *
114
 * @param string $content HTML content.
115
 *
116
 * @return string The content with embeds instead of URLs
117
 */
118
function youtube_link( $content ) {
119
	return jetpack_preg_replace_callback_outside_tags( '!(?:\n|\A)https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/)[^\s]+?(?:\n|\Z)!i', 'youtube_link_callback', $content, 'youtube.com/' );
120
}
121
122
/**
123
 * Callback function for the regex that replaces YouTube URLs with
124
 * YouTube embeds.
125
 *
126
 * @param array $matches An array containing a YouTube URL.
127
 */
128
function youtube_link_callback( $matches ) {
129
	return "\n" . youtube_id( $matches[0] ) . "\n";
130
}
131
132
/**
133
 * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
134
 *
135
 * @param string $url
136
 * @return string The normalized URL
137
 */
138 View Code Duplication
if ( ! function_exists( 'youtube_sanitize_url' ) ) :
139
	/**
140
	 * Clean up Youtube URL to match a single format.
141
	 *
142
	 * @param string $url Youtube URL.
143
	 */
144
	function youtube_sanitize_url( $url ) {
0 ignored issues
show
Best Practice introduced by
The function youtube_sanitize_url() has been defined more than once; this definition is ignored, only the first definition in functions.compat.php (L54-67) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
145
		$url = trim( $url, ' "' );
146
		$url = trim( $url );
147
		$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
148
149
		// Replace any extra question marks with ampersands - the result of a URL like "http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in.
150
		$query_string_start = strpos( $url, '?' );
151
152
		if ( false !== $query_string_start ) {
153
			$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
154
		}
155
156
		return $url;
157
	}
158
endif;
159
160
/**
161
 * Converts a YouTube URL into an embedded YouTube video.
162
 *
163
 * URL can be:
164
 *    http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&amp;hl=en_US
165
 *    http://www.youtube.com/watch#!v=H2Ncxw1xfck
166
 *    http://www.youtube.com/watch?v=H2Ncxw1xfck
167
 *    http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
168
 *    http://www.youtube.com/v/jF-kELmmvgA
169
 *    http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
170
 *    http://youtu.be/Rrohlqeir5E
171
 *
172
 * @param string $url Youtube URL.
173
 */
174
function youtube_id( $url ) {
175
	$id = jetpack_get_youtube_id( $url );
176
177
	if ( ! $id ) {
178
		return sprintf( '<!--%s-->', esc_html__( 'YouTube Error: bad URL entered', 'jetpack' ) );
179
	}
180
181
	$url = youtube_sanitize_url( $url );
182
	$url = wp_parse_url( $url );
183
184
	$args = jetpack_shortcode_youtube_args( $url );
185
	if ( empty( $args ) ) {
186
		return sprintf( '<!--%s-->', esc_html__( 'YouTube Error: empty URL args', 'jetpack' ) );
187
	}
188
189
	list( $w, $h ) = jetpack_shortcode_youtube_dimensions( $args );
190
	$rel           = ( isset( $args['rel'] ) && '0' === $args['rel'] ) ? 0 : 1;
191
	$search        = ( isset( $args['showsearch'] ) && '1' === $args['showsearch'] ) ? 1 : 0;
192
	$info          = ( isset( $args['showinfo'] ) && '0' === $args['showinfo'] ) ? 0 : 1;
193
	$iv            = ( isset( $args['iv_load_policy'] ) && '3' === $args['iv_load_policy'] ) ? 3 : 1;
194
195
	$fmt = ( isset( $args['fmt'] ) && intval( $args['fmt'] ) ) ? '&fmt=' . (int) $args['fmt'] : '';
196
197
	if ( ! isset( $args['autohide'] ) || ( $args['autohide'] < 0 || 2 < $args['autohide'] ) ) {
198
		$autohide = '&autohide=2';
199
	} else {
200
		$autohide = '&autohide=' . absint( $args['autohide'] );
201
	}
202
203
	$start = 0;
204
	if ( isset( $args['start'] ) ) {
205
		$start = intval( $args['start'] );
206
	} elseif ( isset( $args['t'] ) ) {
207
		$time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $args['t'] );
208
209
		foreach ( $time_pieces as $time_piece ) {
210
			$int = (int) $time_piece;
211
			switch ( substr( $time_piece, -1 ) ) {
212
				case 'h':
213
					$start += $int * 3600;
214
					break;
215
				case 'm':
216
					$start += $int * 60;
217
					break;
218
				case 's':
219
					$start += $int;
220
					break;
221
			}
222
		}
223
	}
224
225
	$start = $start ? '&start=' . $start : '';
226
	$end   = ( isset( $args['end'] ) && intval( $args['end'] ) ) ? '&end=' . (int) $args['end'] : '';
227
	$hd    = ( isset( $args['hd'] ) && intval( $args['hd'] ) ) ? '&hd=' . (int) $args['hd'] : '';
228
229
	$vq = ( isset( $args['vq'] ) && in_array( $args['vq'], array( 'hd720', 'hd1080' ), true ) ) ? '&vq=' . $args['vq'] : '';
230
231
	$cc      = ( isset( $args['cc_load_policy'] ) ) ? '&cc_load_policy=1' : '';
232
	$cc_lang = ( isset( $args['cc_lang_pref'] ) ) ? '&cc_lang_pref=' . preg_replace( '/[^_a-z0-9-]/i', '', $args['cc_lang_pref'] ) : '';
233
234
	$wmode = ( isset( $args['wmode'] ) && in_array( strtolower( $args['wmode'] ), array( 'opaque', 'window', 'transparent' ), true ) ) ? $args['wmode'] : 'transparent';
235
236
	$theme = ( isset( $args['theme'] ) && in_array( strtolower( $args['theme'] ), array( 'dark', 'light' ), true ) ) ? '&theme=' . $args['theme'] : '';
237
238
	$autoplay = '';
239
	/**
240
	 * Allow YouTube videos to start playing automatically.
241
	 *
242
	 * @module shortcodes
243
	 *
244
	 * @since 2.2.2
245
	 *
246
	 * @param bool false Enable autoplay for YouTube videos.
247
	 */
248
	if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $args['autoplay'] ) ) {
249
		$autoplay = '&autoplay=' . (int) $args['autoplay'];
250
	}
251
252
	if (
253
		( isset( $url['path'] ) && '/videoseries' === $url['path'] )
254
		|| isset( $args['list'] )
255
	) {
256
		$html = "<iframe class='youtube-player' type='text/html' width='$w' height='$h' src='" . esc_url( "https://www.youtube.com/embed/videoseries?list=$id&hl=en_US" ) . "' allowfullscreen='true' style='border:0;'></iframe>";
257
	} else {
258
		$html = "<iframe class='youtube-player' type='text/html' width='$w' height='$h' src='" . esc_url( "https://www.youtube.com/embed/$id?version=3&rel=$rel&fs=1$fmt$autohide&showsearch=$search&showinfo=$info&iv_load_policy=$iv$start$end$hd&wmode=$wmode$theme$autoplay$vq{$cc}{$cc_lang}" ) . "' allowfullscreen='true' style='border:0;'></iframe>";
259
	}
260
261
	// Let's do some alignment wonder in a span, unless we're producing a feed.
262
	if ( ! is_feed() ) {
263
		$alignmentcss = 'text-align:center;';
264
		if ( isset( $args['align'] ) ) {
265
			switch ( $args['align'] ) {
266
				case 'left':
267
					$alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;";
268
					break;
269
				case 'right':
270
					$alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;";
271
					break;
272
			}
273
		}
274
275
		$html = sprintf(
276
			'<span class="embed-youtube" style="%s display: block;">%s</span>',
277
			esc_attr( $alignmentcss ),
278
			$html
279
		);
280
281
	}
282
283
	/**
284
	 * Filter the YouTube video HTML output.
285
	 *
286
	 * @module shortcodes
287
	 *
288
	 * @since 1.2.3
289
	 *
290
	 * @param string $html YouTube video HTML output.
291
	 */
292
	$html = apply_filters( 'video_embed_html', $html );
293
294
	return $html;
295
}
296
297
/**
298
 * Gets the args present in the YouTube shortcode URL.
299
 *
300
 * @since 8.0.0
301
 *
302
 * @param string $url The URL of the shortcode.
303
 *
304
 * @return array|false The query args of the URL, or false.
305
 */
306
function jetpack_shortcode_youtube_args( $url ) {
307
	$qargs = array();
308
	if ( ! empty( $url['query'] ) ) {
309
		wp_parse_str( $url['query'], $qargs );
310
	} else {
311
		return false;
312
	}
313
314
	$fargs = array();
315
	if ( ! empty( $url['fragment'] ) ) {
316
		wp_parse_str( $url['fragment'], $fargs );
317
	}
318
319
	return array_merge( $fargs, $qargs );
320
}
321
322
/**
323
 * Display the Youtube shortcode.
324
 *
325
 * @param array $atts Shortcode attributes.
326
 *
327
 * @return string The rendered shortcode.
328
 */
329
function youtube_shortcode( $atts ) {
330
	$url = ( isset( $atts[0] ) ) ? ltrim( $atts[0], '=' ) : shortcode_new_to_old_params( $atts );
331
332
	if (
333
		class_exists( 'Jetpack_AMP_Support' )
334
		&& Jetpack_AMP_Support::is_amp_request()
335
	) {
336
		return jetpack_amp_youtube_shortcode( $url );
337
	} else {
338
		return youtube_id( $url );
339
	}
340
}
341
add_shortcode( 'youtube', 'youtube_shortcode' );
342
343
/**
344
 * Renders the [youtube] shortcode as an AMP component.
345
 *
346
 * @since 8.0.0
347
 *
348
 * @param string $url The YouTube URL.
349
 *
350
 * @return string The AMP-compatible rendered shortcode.
351
 */
352
function jetpack_amp_youtube_shortcode( $url ) {
353
	$video_id = jetpack_get_youtube_id( $url );
354
	if ( empty( $video_id ) ) {
355
		return sprintf(
356
			'<a href="%s" class="amp-wp-embed-fallback">%s</a>',
357
			esc_url( $url ),
358
			esc_url( $url )
359
		);
360
	}
361
362
	$sanitized_url          = youtube_sanitize_url( $url );
363
	$parsed_url             = wp_parse_url( $sanitized_url );
364
	$args                   = jetpack_shortcode_youtube_args( $parsed_url );
365
	list( $width, $height ) = jetpack_shortcode_youtube_dimensions( $args );
0 ignored issues
show
Security Bug introduced by
It seems like $args can also be of type false; however, jetpack_shortcode_youtube_dimensions() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
366
	return sprintf(
367
		'<amp-youtube data-videoid="%s" layout="responsive" width="%d" height="%d"></amp-youtube>',
368
		esc_attr( $video_id ),
369
		absint( $width ),
370
		absint( $height )
371
	);
372
}
373
374
/**
375
 * Gets the dimensions of the [youtube] shortcode.
376
 *
377
 * Calculates the width and height, taking $content_width into consideration.
378
 *
379
 * @since 8.0.0
380
 *
381
 * @param array $query_args The query args of the URL.
382
 *
383
 * @return array The width and height of the shortcode.
384
 */
385
function jetpack_shortcode_youtube_dimensions( $query_args ) {
386
	global $content_width;
387
388
	$input_w = ( isset( $query_args['w'] ) && intval( $query_args['w'] ) ) ? intval( $query_args['w'] ) : 0;
389
	$input_h = ( isset( $query_args['h'] ) && intval( $query_args['h'] ) ) ? intval( $query_args['h'] ) : 0;
390
391
	// If we have $content_width, use it.
392
	if ( ! empty( $content_width ) ) {
393
		$default_width = $content_width;
394
	} else {
395
		// Otherwise get default width from the old, now deprecated embed_size_w option.
396
		$default_width = get_option( 'embed_size_w' );
397
	}
398
399
	// If we don't know those 2 values use a hardcoded width.
400
	if ( empty( $default_width ) ) {
401
		$default_width = 640;
402
	}
403
404
	if ( $input_w > 0 && $input_h > 0 ) {
405
		$w = $input_w;
406
		$h = $input_h;
407
	} elseif ( 0 === $input_w && 0 === $input_h ) {
408 View Code Duplication
		if ( isset( $query_args['fmt'] ) && intval( $query_args['fmt'] ) ) {
409
			$w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
410
		} else {
411
			$w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
412
			$h = ceil( ( $w / 16 ) * 9 );
413
		}
414
	} elseif ( $input_w > 0 ) {
415
		$w = $input_w;
416
		$h = ceil( ( $w / 16 ) * 9 );
417 View Code Duplication
	} else {
418
		if ( isset( $query_args['fmt'] ) && intval( $query_args['fmt'] ) ) {
419
			$w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
420
		} else {
421
			$w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
422
			$h = $input_h;
423
		}
424
	}
425
426
	/**
427
	 * Filter the YouTube player width.
428
	 *
429
	 * @module shortcodes
430
	 *
431
	 * @since 1.1.0
432
	 *
433
	 * @param int $w Width of the YouTube player in pixels.
434
	 */
435
	$w = (int) apply_filters( 'youtube_width', $w );
436
437
	/**
438
	 * Filter the YouTube player height.
439
	 *
440
	 * @module shortcodes
441
	 *
442
	 * @since 1.1.0
443
	 *
444
	 * @param int $h Height of the YouTube player in pixels.
445
	 */
446
	$h = (int) apply_filters( 'youtube_height', $h );
0 ignored issues
show
Bug introduced by
The variable $h does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
447
448
	return array( $w, $h );
449
}
450
451
/**
452
 * For bare URLs on their own line of the form
453
 * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
454
 *
455
 * @param array $matches Regex partial matches against the URL passed.
456
 * @param array $attr    Attributes received in embed response.
457
 * @param array $url     Requested URL to be embedded.
458
 */
459
function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
460
	return youtube_id( $url );
0 ignored issues
show
Documentation introduced by
$url is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
461
}
462
463
/**
464
 * Add a new handler to automatically transform custom Youtube URLs (like playlists) into embeds.
465
 */
466
function wpcom_youtube_embed_crazy_url_init() {
467
	wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
468
}
469
add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );
470
471
/**
472
 * Allow oEmbeds in Jetpack's Comment form.
473
 *
474
 * @module shortcodes
475
 *
476
 * @since 2.8.0
477
 *
478
 * @param int get_option('embed_autourls') Option to automatically embed all plain text URLs.
479
 */
480
if ( ! is_admin() && apply_filters( 'jetpack_comments_allow_oembed', true ) ) {
481
	/*
482
	 * We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway,
483
	 * so the iframe gets filtered out.
484
	 * Higher priority because we need it before auto-link and autop get to it.
485
	 */
486
	add_filter( 'comment_text', 'youtube_link', 1 );
487
}
488
489
/**
490
 * Core changes to do_shortcode (https://core.trac.wordpress.org/changeset/34747) broke "improper" shortcodes
491
 * with the format [shortcode=http://url.com].
492
 *
493
 * This removes the "=" from the shortcode so it can be parsed.
494
 *
495
 * @see https://github.com/Automattic/jetpack/issues/3121
496
 *
497
 * @param string $content HTML content.
498
 */
499
function jetpack_fix_youtube_shortcode_display_filter( $content ) {
500
	if ( strpos( $content, '[youtube=' ) !== false ) {
501
		$content = preg_replace( '@\[youtube=(.*?)\]@', '[youtube $1]', $content );
502
	}
503
504
	return $content;
505
}
506
add_filter( 'the_content', 'jetpack_fix_youtube_shortcode_display_filter', 7 );
507