PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.2 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 All 502 releases
jetpack / modules / shortcodes / youtube.php

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

434 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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 ) {
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 '<!--YouTube Error: bad URL entered-->';
179 }
180
181 $url = youtube_sanitize_url( $url );
182 $url = wp_parse_url( $url );
183
184 $qargs = array();
185 if ( ! empty( $url['query'] ) ) {
186 wp_parse_str( $url['query'], $qargs );
187 } else {
188 return false;
189 }
190
191 $fargs = array();
192 if ( ! empty( $url['fragment'] ) ) {
193 wp_parse_str( $url['fragment'], $fargs );
194 }
195
196 $qargs = array_merge( $fargs, $qargs );
197
198 // calculate the width and height, taking content_width into consideration.
199 global $content_width;
200
201 $input_w = ( isset( $qargs['w'] ) && intval( $qargs['w'] ) ) ? intval( $qargs['w'] ) : 0;
202 $input_h = ( isset( $qargs['h'] ) && intval( $qargs['h'] ) ) ? intval( $qargs['h'] ) : 0;
203
204 // If we have $content_width, use it.
205 if ( ! empty( $content_width ) ) {
206 $default_width = $content_width;
207 } else {
208 // Otherwise get default width from the old, now deprecated embed_size_w option.
209 $default_width = get_option( 'embed_size_w' );
210 }
211
212 // If we don't know those 2 values use a hardcoded width.
213 if ( empty( $default_width ) ) {
214 $default_width = 640;
215 }
216
217 if ( $input_w > 0 && $input_h > 0 ) {
218 $w = $input_w;
219 $h = $input_h;
220 } elseif ( 0 === $input_w && 0 === $input_h ) {
221 if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) {
222 $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
223 } else {
224 $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
225 $h = ceil( ( $w / 16 ) * 9 );
226 }
227 } elseif ( $input_w > 0 ) {
228 $w = $input_w;
229 $h = ceil( ( $w / 16 ) * 9 );
230 } else {
231 if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) {
232 $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
233 } else {
234 $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
235 $h = $input_h;
236 }
237 }
238
239 /**
240 * Filter the YouTube player width.
241 *
242 * @module shortcodes
243 *
244 * @since 1.1.0
245 *
246 * @param int $w Width of the YouTube player in pixels.
247 */
248 $w = (int) apply_filters( 'youtube_width', $w );
249
250 /**
251 * Filter the YouTube player height.
252 *
253 * @module shortcodes
254 *
255 * @since 1.1.0
256 *
257 * @param int $h Height of the YouTube player in pixels.
258 */
259 $h = (int) apply_filters( 'youtube_height', $h );
260
261 $rel = ( isset( $qargs['rel'] ) && '0' === $qargs['rel'] ) ? 0 : 1;
262 $search = ( isset( $qargs['showsearch'] ) && '1' === $qargs['showsearch'] ) ? 1 : 0;
263 $info = ( isset( $qargs['showinfo'] ) && '0' === $qargs['showinfo'] ) ? 0 : 1;
264 $iv = ( isset( $qargs['iv_load_policy'] ) && '3' === $qargs['iv_load_policy'] ) ? 3 : 1;
265
266 $fmt = ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) ? '&fmt=' . (int) $qargs['fmt'] : '';
267
268 if ( ! isset( $qargs['autohide'] ) || ( $qargs['autohide'] < 0 || 2 < $qargs['autohide'] ) ) {
269 $autohide = '&autohide=2';
270 } else {
271 $autohide = '&autohide=' . absint( $qargs['autohide'] );
272 }
273
274 $start = 0;
275 if ( isset( $qargs['start'] ) ) {
276 $start = intval( $qargs['start'] );
277 } elseif ( isset( $qargs['t'] ) ) {
278 $time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $qargs['t'] );
279
280 foreach ( $time_pieces as $time_piece ) {
281 $int = (int) $time_piece;
282 switch ( substr( $time_piece, -1 ) ) {
283 case 'h':
284 $start += $int * 3600;
285 break;
286 case 'm':
287 $start += $int * 60;
288 break;
289 case 's':
290 $start += $int;
291 break;
292 }
293 }
294 }
295
296 $start = $start ? '&start=' . $start : '';
297 $end = ( isset( $qargs['end'] ) && intval( $qargs['end'] ) ) ? '&end=' . (int) $qargs['end'] : '';
298 $hd = ( isset( $qargs['hd'] ) && intval( $qargs['hd'] ) ) ? '&hd=' . (int) $qargs['hd'] : '';
299
300 $vq = ( isset( $qargs['vq'] ) && in_array( $qargs['vq'], array( 'hd720', 'hd1080' ), true ) ) ? '&vq=' . $qargs['vq'] : '';
301
302 $cc = ( isset( $qargs['cc_load_policy'] ) ) ? '&cc_load_policy=1' : '';
303 $cc_lang = ( isset( $qargs['cc_lang_pref'] ) ) ? '&cc_lang_pref=' . preg_replace( '/[^_a-z0-9-]/i', '', $qargs['cc_lang_pref'] ) : '';
304
305 $wmode = ( isset( $qargs['wmode'] ) && in_array( strtolower( $qargs['wmode'] ), array( 'opaque', 'window', 'transparent' ), true ) ) ? $qargs['wmode'] : 'transparent';
306
307 $theme = ( isset( $qargs['theme'] ) && in_array( strtolower( $qargs['theme'] ), array( 'dark', 'light' ), true ) ) ? '&theme=' . $qargs['theme'] : '';
308
309 $autoplay = '';
310 /**
311 * Allow YouTube videos to start playing automatically.
312 *
313 * @module shortcodes
314 *
315 * @since 2.2.2
316 *
317 * @param bool false Enable autoplay for YouTube videos.
318 */
319 if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $qargs['autoplay'] ) ) {
320 $autoplay = '&autoplay=' . (int) $qargs['autoplay'];
321 }
322
323 if (
324 ( isset( $url['path'] ) && '/videoseries' === $url['path'] )
325 || isset( $qargs['list'] )
326 ) {
327 $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>";
328 } else {
329 $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>";
330 }
331
332 // Let's do some alignment wonder in a span, unless we're producing a feed.
333 if ( ! is_feed() ) {
334 $alignmentcss = 'text-align:center;';
335 if ( isset( $qargs['align'] ) ) {
336 switch ( $qargs['align'] ) {
337 case 'left':
338 $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;";
339 break;
340 case 'right':
341 $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;";
342 break;
343 }
344 }
345
346 $html = sprintf(
347 '<span class="embed-youtube" style="%s display: block;">%s</span>',
348 esc_attr( $alignmentcss ),
349 $html
350 );
351
352 }
353
354 /**
355 * Filter the YouTube video HTML output.
356 *
357 * @module shortcodes
358 *
359 * @since 1.2.3
360 *
361 * @param string $html YouTube video HTML output.
362 */
363 $html = apply_filters( 'video_embed_html', $html );
364
365 return $html;
366 }
367
368 /**
369 * Display the Youtube shortcode.
370 *
371 * @param array $atts Shortcode attributes.
372 */
373 function youtube_shortcode( $atts ) {
374 return youtube_id( ( isset( $atts[0] ) ) ? ltrim( $atts[0], '=' ) : shortcode_new_to_old_params( $atts ) );
375 }
376 add_shortcode( 'youtube', 'youtube_shortcode' );
377
378 /**
379 * For bare URLs on their own line of the form
380 * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
381 *
382 * @param array $matches Regex partial matches against the URL passed.
383 * @param array $attr Attributes received in embed response.
384 * @param array $url Requested URL to be embedded.
385 */
386 function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
387 return youtube_id( $url );
388 }
389
390 /**
391 * Add a new handler to automatically transform custom Youtube URLs (like playlists) into embeds.
392 */
393 function wpcom_youtube_embed_crazy_url_init() {
394 wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
395 }
396 add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );
397
398 /**
399 * Allow oEmbeds in Jetpack's Comment form.
400 *
401 * @module shortcodes
402 *
403 * @since 2.8.0
404 *
405 * @param int get_option('embed_autourls') Option to automatically embed all plain text URLs.
406 */
407 if ( ! is_admin() && apply_filters( 'jetpack_comments_allow_oembed', true ) ) {
408 /*
409 * We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway,
410 * so the iframe gets filtered out.
411 * Higher priority because we need it before auto-link and autop get to it.
412 */
413 add_filter( 'comment_text', 'youtube_link', 1 );
414 }
415
416 /**
417 * Core changes to do_shortcode (https://core.trac.wordpress.org/changeset/34747) broke "improper" shortcodes
418 * with the format [shortcode=http://url.com].
419 *
420 * This removes the "=" from the shortcode so it can be parsed.
421 *
422 * @see https://github.com/Automattic/jetpack/issues/3121
423 *
424 * @param string $content HTML content.
425 */
426 function jetpack_fix_youtube_shortcode_display_filter( $content ) {
427 if ( strpos( $content, '[youtube=' ) !== false ) {
428 $content = preg_replace( '@\[youtube=(.*?)\]@', '[youtube $1]', $content );
429 }
430
431 return $content;
432 }
433 add_filter( 'the_content', 'jetpack_fix_youtube_shortcode_display_filter', 7 );
434