PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.3
Jetpack – WP Security, Backup, Speed, & Growth v7.2.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / vr.php
vr.php
137 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // VR Viewer Shortcode
4 // converts [vr] shortcode to an iframe viewer hosted on vr.me.sh
5
6
7 /**
8 * Scrub URL paramaters for VR viewer
9 *
10 * @param url_params - parameter array which is passed to the jetpack_vr_viewer
11 * @param url_params['url'] - url of 360 media
12 * @param url_params['guid'] - guid for videopress
13 * @param url_params['view'] - cinema, 360 - controls if panaroma view, or 360
14 * @param url_params['rotation'] - number for rotating media
15 * @param url_params['preview'] - show preview image or not
16 * @return url_params array or false
17 */
18 function jetpack_vr_viewer_get_viewer_url_params( $params ) {
19 $url_params = array();
20
21 if ( isset( $params['rotation'] ) ) {
22 $url_params['rotation'] = intval( $params['rotation'], 10 );
23 }
24
25 if ( isset( $params['view'] ) && in_array( $params['view'], array( 'cinema', '360' ), true ) ) {
26 $url_params['view'] = $params['view'];
27 }
28
29 if ( isset( $params['preview'] ) && $params['preview'] ) {
30 $url_params['preview'] = 1;
31 }
32
33 if ( isset( $params['url'] ) ) {
34 return array_merge( $url_params, array( 'url' => $params['url'] ) );
35 } elseif ( isset( $params['guid'] ) ) {
36 return array_merge( $url_params, array( 'guid' => $params['guid'] ) );
37 }
38
39 return false;
40 }
41
42 /**
43 * Get padding for IFRAME depending on view type
44 *
45 * @param view - string cinema, 360 - default cinema
46 * @return css padding
47 */
48 function jetpack_vr_viewer_iframe_padding( $view ) {
49 if ( $view === '360' ) {
50 return '100%'; // 1:1 square aspect for 360
51 }
52
53 return '50%'; // 2:1 panorama aspect
54 }
55
56 /**
57 * Create HTML for VR Viewer IFRAME and wrapper
58 * The viewer code is hosted on vr.me.sh site which is then displayed
59 * within posts via an IFRAME. This function returns the IFRAME html.
60 *
61 * @param url_params - parameter array which is passed to the jetpack_vr_viewer
62 * @param url_params['url'] - url of 360 media
63 * @param url_params['guid'] - guid for videopress
64 * @param url_params['view'] - cinema, 360 - controls if panaroma view, or 360
65 * @param url_params['rotation'] - number for rotating media
66 * @param url_params['preview'] - show preview image or not
67 * @return html - an iframe for viewer
68 */
69 function jetpack_vr_viewer_get_html( $url_params ) {
70 global $content_width;
71
72 $iframe = add_query_arg( $url_params, 'https://vr.me.sh/view/' );
73
74 // set some defaults
75 $maxwidth = ( isset( $content_width ) ) ? $content_width : 720;
76 $view = ( isset( $url_params['view'] ) ) ? $url_params['view'] : 'cinema';
77
78 $rtn = '<div style="position: relative; max-width: ' . $maxwidth . 'px; margin-left: auto; margin-right: auto; overflow: hidden;">';
79 $rtn .= '<div style="padding-top: ' . jetpack_vr_viewer_iframe_padding( $view ) . ';"></div>';
80 $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 ) . '">';
81 $rtn .= '</iframe>';
82 $rtn .= '</div>';
83
84 return $rtn;
85 }
86
87 /**
88 * Convert [vr] shortcode to viewer
89 *
90 * Shortcode example:
91 * [vr url="https://en-blog.files.wordpress.com/2016/12/regents_park.jpg" view="360"]
92 *
93 * VR Viewer embed code:
94 * <div style="position: relative; max-width: 720px; margin-left: auto; margin-right: auto; overflow: hidden;">
95 * <div style="padding-top: 100%;"></div>
96 * <iframe style="position: absolute; top: 0; right: 0; bottom: 0; left: 0; height: 100%" allowfullscreen="true" frameborder="0" width="100%" height="400" src="https://vr.me.sh/view/?view=360&amp;url=https://en-blog.files.wordpress.com/2016/12/regents_park.jpg">
97 * </iframe>
98 * </div>
99 *
100 * @return html - complete vr viewer html
101 */
102 function jetpack_vr_viewer_shortcode( $atts ) {
103 $params = shortcode_atts(
104 array(
105 0 => null,
106 'url' => null,
107 'src' => null,
108 'guid' => null,
109 'rotation' => null,
110 'view' => null,
111 'preview' => false,
112 ),
113 $atts
114 );
115
116 // We offer a few ways to specify the URL
117 if ( $params[0] ) {
118 $params['url'] = $params[0];
119 } elseif ( $params['src'] ) {
120 $params['url'] = $params['src'];
121 }
122
123 $url_params = jetpack_vr_viewer_get_viewer_url_params( $params );
124 if ( $url_params ) {
125 return jetpack_vr_viewer_get_html( $url_params );
126 }
127
128 // add check for user
129 if ( current_user_can( 'edit_posts' ) ) {
130 return '[vr] shortcode requires a data source to be given';
131 } else {
132 return '';
133 }
134 }
135
136 add_shortcode( 'vr', 'jetpack_vr_viewer_shortcode' );
137