Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
wpcom_twitchtv_shortcode
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
90
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
12if ( ! 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 */
29function 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    // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
73    $url_args['parent'] = isset( $_SERVER['HTTP_HOST'] )
74        ? rawurlencode( wp_unslash( $_SERVER['HTTP_HOST'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
75        : '';
76
77    $url = add_query_arg( $url_args, 'https://player.twitch.tv/' );
78
79    return sprintf(
80        '<iframe src="%s" width="%d" height="%d" frameborder="0" scrolling="no" allowfullscreen sandbox="allow-popups allow-scripts allow-same-origin allow-presentation"></iframe>',
81        esc_url( $url ),
82        esc_attr( $width ),
83        esc_attr( $height )
84    );
85}
86add_shortcode( 'twitch', 'wpcom_twitchtv_shortcode' );
87add_shortcode( 'twitchtv', 'wpcom_twitchtv_shortcode' );