| 1 |
<?php |
| 2 |
/** |
| 3 |
* twitch.tv shortcode |
| 4 |
* [twitchtv url='http://www.twitch.tv/paperbat' height='378' width='620' autoplay='false'] |
| 5 |
* [twitchtv url='http://www.twitch.tv/paperbat/b/323486192' height='378' width='620' autoplay='false'] |
| 6 |
**/ |
| 7 |
|
| 8 |
/** |
| 9 |
* (Live URL) http://www.twitch.tv/paperbat |
| 10 |
* |
| 11 |
* <iframe src="https://player.twitch.tv/?autoplay=false&muted=false&channel=paperbat" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe> |
| 12 |
* |
| 13 |
* (Archive URL) http://www.twitch.tv/paperbat/v/323486192 |
| 14 |
* |
| 15 |
* <iframe src="https://player.twitch.tv/?autoplay=false&muted=false&video=v323486192" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe> |
| 16 |
* |
| 17 |
* @param $atts array User supplied shortcode arguments. |
| 18 |
* |
| 19 |
* @return string HTML output of the shortcode. |
| 20 |
*/ |
| 21 |
function wpcom_twitchtv_shortcode( $atts ) { |
| 22 |
$attr = shortcode_atts( |
| 23 |
array( |
| 24 |
'height' => 378, |
| 25 |
'width' => 620, |
| 26 |
'url' => '', |
| 27 |
'autoplay' => 'false', |
| 28 |
'muted' => 'false', |
| 29 |
'time' => null, |
| 30 |
), |
| 31 |
$atts |
| 32 |
); |
| 33 |
|
| 34 |
if ( empty( $attr['url'] ) ) { |
| 35 |
return '<!-- Invalid twitchtv URL -->'; |
| 36 |
} |
| 37 |
|
| 38 |
preg_match( '|^http://www.twitch.tv/([^/?]+)(/v/(\d+))?|i', $attr['url'], $match ); |
| 39 |
|
| 40 |
$url_args = array( |
| 41 |
'autoplay' => ( false !== $attr['autoplay'] && 'false' !== $attr['autoplay'] ) ? 'true' : 'false', |
| 42 |
'muted' => ( false !== $attr['muted'] && 'false' !== $attr['muted'] ) ? 'true' : 'false', |
| 43 |
'time' => $attr['time'], |
| 44 |
); |
| 45 |
|
| 46 |
$width = intval( $attr['width'] ); |
| 47 |
$height = intval( $attr['height'] ); |
| 48 |
|
| 49 |
$user_id = $match[1]; |
| 50 |
$video_id = 0; |
| 51 |
if ( ! empty( $match[3] ) ) { |
| 52 |
$video_id = (int) $match[3]; |
| 53 |
} |
| 54 |
|
| 55 |
do_action( 'jetpack_bump_stats_extras', 'twitchtv', 'shortcode' ); |
| 56 |
|
| 57 |
if ( $video_id > 0 ) { |
| 58 |
$url_args['video'] = 'v' . $video_id; |
| 59 |
} else { |
| 60 |
$url_args['channel'] = $user_id; |
| 61 |
} |
| 62 |
|
| 63 |
$url = add_query_arg( $url_args, 'https://player.twitch.tv/' ); |
| 64 |
|
| 65 |
return sprintf( |
| 66 |
'<iframe src="%s" width="%d" height="%d" frameborder="0" scrolling="no" allowfullscreen></iframe>', |
| 67 |
esc_url( $url ), |
| 68 |
esc_attr( $width ), |
| 69 |
esc_attr( $height ) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
add_shortcode( 'twitch', 'wpcom_twitchtv_shortcode' ); |
| 74 |
add_shortcode( 'twitchtv', 'wpcom_twitchtv_shortcode' ); |
| 75 |
|