PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.0.1
Jetpack – WP Security, Backup, Speed, & Growth v14.0.1
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 14.4.2 All 500 releases
jetpack / modules / shortcodes / twitchtv.php
twitchtv.php
84 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Twitch.tv shortcode
4 *
5 * Examples:
6 * [twitchtv url='https://www.twitch.tv/paperbat' height='378' width='620' autoplay='false']
7 * [twitchtv url='https://www.twitch.tv/paperbat/b/323486192' height='378' width='620' autoplay='false']
8 *
9 * @package automattic/jetpack
10 */
11
12 /**
13 * (Live URL) https://www.twitch.tv/paperbat
14 *
15 * <iframe src="https://player.twitch.tv/?autoplay=false&#038;muted=false&#038;channel=paperbat" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe>
16 *
17 * (Archive URL) https://www.twitch.tv/paperbat/v/323486192
18 *
19 * <iframe src="https://player.twitch.tv/?autoplay=false&#038;muted=false&#038;video=v323486192" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe>
20 *
21 * @param array $atts User supplied shortcode arguments.
22 *
23 * @return string HTML output of the shortcode.
24 */
25 function wpcom_twitchtv_shortcode( $atts ) {
26 $attr = shortcode_atts(
27 array(
28 'height' => 378,
29 'width' => 620,
30 'url' => '',
31 'autoplay' => 'false',
32 'muted' => 'false',
33 'time' => null,
34 ),
35 $atts
36 );
37
38 if ( empty( $attr['url'] ) ) {
39 return '<!-- Invalid twitchtv URL -->';
40 }
41
42 preg_match( '|^https?://www.twitch.tv/([^/?]+)(/v/(\d+))?|i', $attr['url'], $match );
43
44 $url_args = array(
45 'autoplay' => ( false !== $attr['autoplay'] && 'false' !== $attr['autoplay'] ) ? 'true' : 'false',
46 'muted' => ( false !== $attr['muted'] && 'false' !== $attr['muted'] ) ? 'true' : 'false',
47 'time' => $attr['time'],
48 );
49
50 $width = (int) $attr['width'];
51 $height = (int) $attr['height'];
52
53 $user_id = $match[1];
54 $video_id = 0;
55 if ( ! empty( $match[3] ) ) {
56 $video_id = (int) $match[3];
57 }
58
59 do_action( 'jetpack_bump_stats_extras', 'twitchtv', 'shortcode' );
60
61 if ( $video_id > 0 ) {
62 $url_args['video'] = 'v' . $video_id;
63 } else {
64 $url_args['channel'] = $user_id;
65 }
66
67 // See https://discuss.dev.twitch.tv/t/twitch-embedded-player-updates-in-2020/23956.
68 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
69 $url_args['parent'] = isset( $_SERVER['HTTP_HOST'] )
70 ? rawurlencode( wp_unslash( $_SERVER['HTTP_HOST'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
71 : '';
72
73 $url = add_query_arg( $url_args, 'https://player.twitch.tv/' );
74
75 return sprintf(
76 '<iframe src="%s" width="%d" height="%d" frameborder="0" scrolling="no" allowfullscreen sandbox="allow-popups allow-scripts allow-same-origin allow-presentation"></iframe>',
77 esc_url( $url ),
78 esc_attr( $width ),
79 esc_attr( $height )
80 );
81 }
82 add_shortcode( 'twitch', 'wpcom_twitchtv_shortcode' );
83 add_shortcode( 'twitchtv', 'wpcom_twitchtv_shortcode' );
84