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