Completed
Push — add/vr-shortcode ( 271979...022ee9 )
by
unknown
26:28 queued 20:24
created

vr.php ➔ vr_viewer_shortcode()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 6
nop 1
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
function jetpack_vr_viewer_get_viewer_url_params( $params ) {
4
	$url_params = array();
5
6
	if ( isset( $params['rotation'] ) ) {
7
		$url_params['rotation'] = intval( $params['rotation'], 10 );
8
	}
9
10
	if ( isset( $params['view'] ) && in_array( $params['view'], array( 'cinema', '360' ), true ) ) {
11
		$url_params['view'] = $params['view'];
12
	}
13
14
	if ( $params['preview'] ) {
15
		$url_params['preview'] = 1;
16
	}
17
18
	if ( isset( $params['url'] ) ) {
19
		return array_merge( $url_params, array( 'url' => $params['url'] ) );
20
	} else if ( isset( $params['guid'] ) ) {
21
		return array_merge( $url_params, array( 'guid' => $params['guid'] ) );
22
	}
23
24
	return false;
25
}
26
27
function jetpack_vr_viewer_iframe_padding( $view ) {
28
	if ( $view === '360' ) {
29
		return '100%'; // 1:1 square aspect for 360
30
	}
31
32
	return '50%'; // 2:1 panorama aspect
33
}
34
35
function jetpack_vr_viewer_get_html( $url_params ) {
36
	$iframe = add_query_arg( $url_params, 'https://vr.me.sh/view/' );
37
38
	$rtn  = '<div style="position: relative; max-width: 720px; margin-left: auto; margin-right: auto; overflow: hidden;">';
39
	$rtn .= '<div style="padding-top: '. jetpack_vr_viewer_iframe_padding( $url_params['view'] ).';"></div>';
40
	$rtn .= '<iframe style="position: absolute; top: 0; right: 0; bottom: 0; left: 0; height: 100%" allowfullscreen="true" frameborder="0" width="100%" height="300" src="'.esc_url( $iframe ).'">';
41
	$rtn .= '</iframe>';
42
	$rtn .= '</div>';
43
44
	return $rtn;
45
}
46
47
function jetpack_vr_viewer_shortcode( $atts ) {
48
49
	$params = shortcode_atts( array(
50
		0          => null,
51
		'url'      => null,
52
		'src'      => null,
53
		'guid'     => null,
54
		'rotation' => null,
55
		'view'     => null,
56
		'preview'  => false,
57
	), $atts );
58
59
	// We offer a few ways to specify the URL
60
	if ( $params[0] ) {
61
		$params['url'] = $params[0];
62
	} else if ( $params['src'] ) {
63
		$params['url'] = $params['src'];
64
	}
65
66
	$url_params = jetpack_vr_viewer_get_viewer_url_params( $params );
67
	if ( $url_params ) {
68
		return jetpack_vr_viewer_get_html( $url_params );
69
	}
70
71
	return '[vr] shortcode requires a data source to be given';
72
}
73
74
add_shortcode( 'vr', 'jetpack_vr_viewer_shortcode' );
75