PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.4
Jetpack – WP Security, Backup, Speed, & Growth v7.2.4
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 / youtube.php
youtube.php
397 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * youtube shortcode
5 *
6 * Contains shortcode + some improvements over the Embeds syntax @
7 * http://codex.wordpress.org/Embeds
8 *
9 * @example [youtube=http://www.youtube.com/watch?v=wq0rXGLs0YM&amp;fs=1&amp;hl=bg_BG]
10 */
11
12 /**
13 * Replaces YouTube embeds with YouTube shortcodes.
14 *
15 * @param string $content HTML content.
16 * @return string The content with YouTube embeds replaced with YouTube shortcodes.
17 */
18 // 2008-07-15:
19 // <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>
20 // around 2008-06-06 youtube changed their old embed code to this:
21 // <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>
22 // old style was:
23 // <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>
24 // 12-2010:
25 // <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>
26 // 01-2011:
27 // <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>
28 // <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0"></iframe>
29 function youtube_embed_to_short_code( $content ) {
30 if ( ! is_string( $content ) || false === strpos( $content, 'youtube.com' ) ) {
31 return $content;
32 }
33
34 // older codes
35 $regexp = '!<object(.*?)>.*?<param\s+name=[\'"]movie[\'"]\s+value=[\'"](https?:)?//www\.youtube\.com/v/([^\'"]+)[\'"].*?>.*?</object>!i';
36 $regexp_ent = htmlspecialchars( $regexp, ENT_NOQUOTES );
37 $old_regexp = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|&#0*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!';
38 $old_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) );
39
40 // new code
41 $ifr_regexp = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="(https?:)?//(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i';
42 $ifr_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) );
43
44 foreach ( array( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg ) {
45 if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
46 continue;
47 }
48
49 foreach ( $matches as $match ) {
50 // Hack, but '?' should only ever appear once, and
51 // it should be for the 1st field-value pair in query string,
52 // if it is present
53 // YouTube changed their embed code.
54 // Example of how it is now:
55 // <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>
56 // As shown at the start of function, previous YouTube didn't '?'
57 // the 1st field-value pair.
58 if ( in_array( $reg, array( 'ifr_regexp', 'ifr_regexp_ent', 'regexp', 'regexp_ent' ) ) ) {
59 $params = $match[1];
60
61 if ( in_array( $reg, array( 'ifr_regexp_ent', 'regexp_ent' ) ) ) {
62 $params = html_entity_decode( $params );
63 }
64
65 $params = wp_kses_hair( $params, array( 'http' ) );
66
67 $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
68 $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
69 $wh = '';
70
71 if ( $width && $height ) {
72 $wh = "&w=$width&h=$height";
73 }
74
75 $url = esc_url_raw( "https://www.youtube.com/watch?v={$match[3]}{$wh}" );
76 } else {
77 $match[1] = str_replace( '?', '&', $match[1] );
78
79 $url = esc_url_raw( 'https://www.youtube.com/watch?v=' . html_entity_decode( $match[1] ) );
80 }
81
82 $content = str_replace( $match[0], "[youtube $url]", $content );
83
84 /**
85 * Fires before the YouTube embed is transformed into a shortcode.
86 *
87 * @module shortcodes
88 *
89 * @since 1.2.0
90 *
91 * @param string youtube Shortcode name.
92 * @param string $url YouTube video URL.
93 */
94 do_action( 'jetpack_embed_to_shortcode', 'youtube', $url );
95 }
96 }
97
98 return $content;
99 }
100
101 add_filter( 'pre_kses', 'youtube_embed_to_short_code' );
102
103 /**
104 * Replaces plain-text links to YouTube videos with YouTube embeds.
105 *
106 * @param string $content HTML content
107 * @return string The content with embeds instead of URLs
108 */
109 function youtube_link( $content ) {
110 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/' );
111 }
112
113 /**
114 * Callback function for the regex that replaces YouTube URLs with
115 * YouTube embeds.
116 */
117 function youtube_link_callback( $matches ) {
118 return "\n" . youtube_id( $matches[0] ) . "\n";
119 }
120
121 /**
122 * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
123 *
124 * @param string $url
125 * @return string The normalized URL
126 */
127 if ( ! function_exists( 'youtube_sanitize_url' ) ) :
128 function youtube_sanitize_url( $url ) {
129 $url = trim( $url, ' "' );
130 $url = trim( $url );
131 $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
132
133 // 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.
134 $query_string_start = strpos( $url, '?' );
135
136 if ( false !== $query_string_start ) {
137 $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
138 }
139
140 return $url;
141 }
142 endif;
143
144 /*
145 * url can be:
146 * http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&amp;hl=en_US
147 * http://www.youtube.com/watch#!v=H2Ncxw1xfck
148 * http://www.youtube.com/watch?v=H2Ncxw1xfck
149 * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
150 * http://www.youtube.com/v/jF-kELmmvgA
151 * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
152 * http://youtu.be/Rrohlqeir5E
153 */
154
155 /**
156 * Converts a YouTube URL into an embedded YouTube video.
157 */
158 function youtube_id( $url ) {
159 if ( ! $id = jetpack_get_youtube_id( $url ) ) {
160 return '<!--YouTube Error: bad URL entered-->';
161 }
162
163 $url = youtube_sanitize_url( $url );
164 $url = parse_url( $url );
165
166 if ( ! isset( $url['query'] ) ) {
167 return false;
168 }
169
170 if ( isset( $url['fragment'] ) ) {
171 wp_parse_str( $url['fragment'], $fargs );
172 } else {
173 $fargs = array();
174 }
175 wp_parse_str( $url['query'], $qargs );
176
177 $qargs = array_merge( $fargs, $qargs );
178
179 // calculate the width and height, taking content_width into consideration
180 global $content_width;
181
182 $input_w = ( isset( $qargs['w'] ) && intval( $qargs['w'] ) ) ? intval( $qargs['w'] ) : 0;
183 $input_h = ( isset( $qargs['h'] ) && intval( $qargs['h'] ) ) ? intval( $qargs['h'] ) : 0;
184
185 // If we have $content_width, use it.
186 if ( ! empty( $content_width ) ) {
187 $default_width = $content_width;
188 } else {
189 // Otherwise get default width from the old, now deprecated embed_size_w option.
190 $default_width = get_option( 'embed_size_w' );
191 }
192
193 // If we don't know those 2 values use a hardcoded width.h
194 if ( empty( $default_width ) ) {
195 $default_width = 640;
196 }
197
198 if ( $input_w > 0 && $input_h > 0 ) {
199 $w = $input_w;
200 $h = $input_h;
201 } elseif ( 0 == $input_w && 0 == $input_h ) {
202 if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) {
203 $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
204 } else {
205 $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
206 $h = ceil( ( $w / 16 ) * 9 );
207 }
208 } elseif ( $input_w > 0 ) {
209 $w = $input_w;
210 $h = ceil( ( $w / 16 ) * 9 );
211 } else {
212 if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) {
213 $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
214 } else {
215 $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
216 $h = $input_h;
217 }
218 }
219
220 /**
221 * Filter the YouTube player width.
222 *
223 * @module shortcodes
224 *
225 * @since 1.1.0
226 *
227 * @param int $w Width of the YouTube player in pixels.
228 */
229 $w = (int) apply_filters( 'youtube_width', $w );
230
231 /**
232 * Filter the YouTube player height.
233 *
234 * @module shortcodes
235 *
236 * @since 1.1.0
237 *
238 * @param int $h Height of the YouTube player in pixels.
239 */
240 $h = (int) apply_filters( 'youtube_height', $h );
241
242 $rel = ( isset( $qargs['rel'] ) && 0 == $qargs['rel'] ) ? 0 : 1;
243 $search = ( isset( $qargs['showsearch'] ) && 1 == $qargs['showsearch'] ) ? 1 : 0;
244 $info = ( isset( $qargs['showinfo'] ) && 0 == $qargs['showinfo'] ) ? 0 : 1;
245 $iv = ( isset( $qargs['iv_load_policy'] ) && 3 == $qargs['iv_load_policy'] ) ? 3 : 1;
246
247 $fmt = ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) ? '&fmt=' . (int) $qargs['fmt'] : '';
248
249 if ( ! isset( $qargs['autohide'] ) || ( $qargs['autohide'] < 0 || 2 < $qargs['autohide'] ) ) {
250 $autohide = '&autohide=2';
251 } else {
252 $autohide = '&autohide=' . absint( $qargs['autohide'] );
253 }
254
255 $start = 0;
256 if ( isset( $qargs['start'] ) ) {
257 $start = intval( $qargs['start'] );
258 } elseif ( isset( $qargs['t'] ) ) {
259 $time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $qargs['t'] );
260
261 foreach ( $time_pieces as $time_piece ) {
262 $int = (int) $time_piece;
263 switch ( substr( $time_piece, -1 ) ) {
264 case 'h':
265 $start += $int * 3600;
266 break;
267 case 'm':
268 $start += $int * 60;
269 break;
270 case 's':
271 $start += $int;
272 break;
273 }
274 }
275 }
276
277 $start = $start ? '&start=' . $start : '';
278 $end = ( isset( $qargs['end'] ) && intval( $qargs['end'] ) ) ? '&end=' . (int) $qargs['end'] : '';
279 $hd = ( isset( $qargs['hd'] ) && intval( $qargs['hd'] ) ) ? '&hd=' . (int) $qargs['hd'] : '';
280
281 $vq = ( isset( $qargs['vq'] ) && in_array( $qargs['vq'], array( 'hd720', 'hd1080' ) ) ) ? '&vq=' . $qargs['vq'] : '';
282
283 $cc = ( isset( $qargs['cc_load_policy'] ) ) ? '&cc_load_policy=1' : '';
284 $cc_lang = ( isset( $qargs['cc_lang_pref'] ) ) ? '&cc_lang_pref=' . preg_replace( '/[^_a-z0-9-]/i', '', $qargs['cc_lang_pref'] ) : '';
285
286 $wmode = ( isset( $qargs['wmode'] ) && in_array( strtolower( $qargs['wmode'] ), array( 'opaque', 'window', 'transparent' ) ) ) ? $qargs['wmode'] : 'transparent';
287
288 $theme = ( isset( $qargs['theme'] ) && in_array( strtolower( $qargs['theme'] ), array( 'dark', 'light' ) ) ) ? '&theme=' . $qargs['theme'] : '';
289
290 $autoplay = '';
291 /**
292 * Allow YouTube videos to start playing automatically.
293 *
294 * @module shortcodes
295 *
296 * @since 2.2.2
297 *
298 * @param bool false Enable autoplay for YouTube videos.
299 */
300 if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $qargs['autoplay'] ) ) {
301 $autoplay = '&autoplay=' . (int) $qargs['autoplay'];
302 }
303
304 if ( ( isset( $url['path'] ) && '/videoseries' == $url['path'] ) || isset( $qargs['list'] ) ) {
305 $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>";
306 } else {
307 $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{$cc}{$cc_lang}" ) . "' allowfullscreen='true' style='border:0;'></iframe>";
308 }
309
310 // Let's do some alignment wonder in a span, unless we're producing a feed
311 if ( ! is_feed() ) {
312 $alignmentcss = 'text-align:center;';
313 if ( isset( $qargs['align'] ) ) {
314 switch ( $qargs['align'] ) {
315 case 'left':
316 $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;";
317 break;
318 case 'right':
319 $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;";
320 break;
321 }
322 }
323
324 $html = sprintf(
325 '<span class="embed-youtube" style="%s display: block;">%s</span>',
326 esc_attr( $alignmentcss ),
327 $html
328 );
329
330 }
331
332 /**
333 * Filter the YouTube video HTML output.
334 *
335 * @module shortcodes
336 *
337 * @since 1.2.3
338 *
339 * @param string $html YouTube video HTML output.
340 */
341 $html = apply_filters( 'video_embed_html', $html );
342
343 return $html;
344 }
345
346 function youtube_shortcode( $atts ) {
347 return youtube_id( ( isset( $atts[0] ) ) ? ltrim( $atts[0], '=' ) : shortcode_new_to_old_params( $atts ) );
348 }
349
350 add_shortcode( 'youtube', 'youtube_shortcode' );
351
352 /**
353 * For bare URLs on their own line of the form
354 * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
355 */
356 function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
357 return youtube_id( $url );
358 }
359
360 function wpcom_youtube_embed_crazy_url_init() {
361 wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
362 }
363
364 add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );
365
366 /**
367 * Allow oEmbeds in Jetpack's Comment form.
368 *
369 * @module shortcodes
370 *
371 * @since 2.8.0
372 *
373 * @param int get_option('embed_autourls') Option to automatically embed all plain text URLs.
374 */
375 if ( ! is_admin() && apply_filters( 'jetpack_comments_allow_oembed', true ) ) {
376 // We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out.
377 // Higher priority because we need it before auto-link and autop get to it
378 add_filter( 'comment_text', 'youtube_link', 1 );
379 }
380
381 /**
382 * Core changes to do_shortcode (https://core.trac.wordpress.org/changeset/34747) broke "improper" shortcodes
383 * with the format [shortcode=http://url.com].
384 *
385 * This removes the "=" from the shortcode so it can be parsed.
386 *
387 * @see https://github.com/Automattic/jetpack/issues/3121
388 */
389 function jetpack_fix_youtube_shortcode_display_filter( $content ) {
390 if ( strpos( $content, '[youtube=' ) !== false ) {
391 $content = preg_replace( '@\[youtube=(.*?)\]@', '[youtube $1]', $content );
392 }
393
394 return $content;
395 }
396 add_filter( 'the_content', 'jetpack_fix_youtube_shortcode_display_filter', 7 );
397