| 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&muted=false&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&muted=false&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 |
|