Completed
Push — add/jetpack-plan-support ( c36d23...95100b )
by
unknown
14:33 queued 05:53
created

twitchtv.php ➔ wpcom_twitchtv_shortcode()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 65
nop 1
dl 0
loc 50
rs 6.3636
c 0
b 0
f 0
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&#038;muted=false&#038;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&#038;muted=false&#038;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
		), $atts
31
	);
32
33
	if ( empty( $attr['url'] ) ) {
34
		return '<!-- Invalid twitchtv URL -->';
35
	}
36
37
	preg_match( '|^http://www.twitch.tv/([^/?]+)(/v/(\d+))?|i', $attr['url'], $match );
38
39
	$url_args = array(
40
		'autoplay' => ( false !== $attr['autoplay'] && 'false' !== $attr['autoplay'] ) ? 'true' : 'false',
41
		'muted'    => ( false !== $attr['muted'] && 'false' !== $attr['muted'] ) ? 'true' : 'false',
42
		'time'     => $attr['time']
43
	);
44
45
	$width    = intval( $attr['width'] );
46
	$height   = intval( $attr['height'] );
47
48
	$user_id  = $match[1];
49
	$video_id = 0;
50
	if ( ! empty( $match[3] ) ) {
51
		$video_id = (int) $match[3];
52
	}
53
54
	do_action( 'jetpack_bump_stats_extras', 'twitchtv', 'shortcode' );
55
56
	if ( $video_id > 0 ) {
57
		$url_args['video'] = 'v' . $video_id;
58
	} else {
59
		$url_args['channel'] = $user_id;
60
	}
61
62
	$url = add_query_arg( $url_args, 'https://player.twitch.tv/' );
63
64
	return sprintf(
65
		'<iframe src="%s" width="%d" height="%d" frameborder="0" scrolling="no" allowfullscreen></iframe>',
66
		esc_url( $url ),
67
		esc_attr( $width ),
68
		esc_attr( $height )
69
	);
70
}
71
72
add_shortcode( 'twitch', 'wpcom_twitchtv_shortcode' );
73
add_shortcode( 'twitchtv', 'wpcom_twitchtv_shortcode' );
74