PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.2.3
Jetpack – WP Security, Backup, Speed, & Growth v9.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 / bandcamp.php
bandcamp.php
244 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shortcode handler for [bandcamp], which inserts a bandcamp.com
4 * music player (iframe, html5)
5 *
6 * [bandcamp album=119385304]
7 * [bandcamp album=3462839126 bgcol=FFFFFF linkcol=4285BB size=venti]
8 * [bandcamp track=2446959313]
9 *
10 * @package Jetpack
11 */
12
13 /**
14 * Display the Bandcamp shortcode.
15 *
16 * @param array $atts Shortcode attributes.
17 */
18 function shortcode_handler_bandcamp( $atts ) {
19 // there are no default values, but specify here anyway to explicitly list supported atts.
20 $attributes = shortcode_atts(
21 array(
22 'album' => null, // integer album id.
23 'track' => null, // integer track id.
24 'video' => null, // integer track id for video player.
25 'size' => 'venti', // one of the supported sizes.
26 'bgcol' => 'FFFFFF', // hex, no '#' prefix.
27 'linkcol' => null, // hex, no '#' prefix.
28 'layout' => null, // encoded layout url.
29 'width' => null, // integer with optional "%".
30 'height' => null, // integer with optional "%".
31 'notracklist' => null, // may be string "true" (defaults false).
32 'tracklist' => null, // may be string "false" (defaults true).
33 'artwork' => null, // may be string "false" (alternately: "none") or "small" (default is large).
34 'minimal' => null, // may be string "true" (defaults false).
35 'theme' => null, // may be theme identifier string ("light"|"dark" so far).
36 'package' => null, // integer package id.
37 't' => null, // integer track number.
38 'tracks' => null, // comma separated list of allowed tracks.
39 'esig' => null, // hex, no '#' prefix.
40 ),
41 $atts,
42 'bandcamp'
43 );
44
45 $sizes = array(
46 'venti' => array(
47 'width' => 400,
48 'height' => 100,
49 ),
50 'grande' => array(
51 'width' => 300,
52 'height' => 100,
53 ),
54 'grande2' => array(
55 'width' => 300,
56 'height' => 355,
57 ),
58 'grande3' => array(
59 'width' => 300,
60 'height' => 415,
61 ),
62 'tall_album' => array(
63 'width' => 150,
64 'height' => 295,
65 ),
66 'tall_track' => array(
67 'width' => 150,
68 'height' => 270,
69 ),
70 'tall2' => array(
71 'width' => 150,
72 'height' => 450,
73 ),
74 'short' => array(
75 'width' => 46,
76 'height' => 23,
77 ),
78 'large' => array(
79 'width' => 350,
80 'height' => 470,
81 ),
82 'medium' => array(
83 'width' => 450,
84 'height' => 120,
85 ),
86 'small' => array(
87 'width' => 350,
88 'height' => 42,
89 ),
90 );
91
92 $sizekey = $attributes['size'];
93 $height = null;
94 $width = null;
95
96 $is_video = false;
97
98 /*
99 * Build iframe url. For audio players, args are appended as
100 * extra path segments for historical reasons having to
101 * do with an IE-only flash bug which required this URL
102 * to contain no querystring. Delay the actual joining
103 * of args into a string until after we decide if it's
104 * a video player or an audio player
105 */
106 $argparts = array();
107
108 if ( ! isset( $attributes['album'] ) && ! isset( $attributes['track'] ) && ! isset( $attributes['video'] ) ) {
109 return "[bandcamp: shortcode must include 'track', 'album', or 'video' param]";
110 }
111
112 if ( isset( $attributes['track'] ) && is_numeric( $attributes['track'] ) ) {
113 $track = esc_attr( $attributes['track'] );
114 array_push( $argparts, "track={$track}" );
115 } elseif ( isset( $attributes['video'] ) && is_numeric( $attributes['video'] ) ) {
116 $track = esc_attr( $attributes['video'] ); // videos are referenced by track id.
117 $url = '//bandcamp.com/EmbeddedPlayer/v=2';
118 $is_video = true;
119 array_push( $argparts, "track={$track}" );
120 }
121 if ( isset( $attributes['album'] ) && is_numeric( $attributes['album'] ) ) {
122 $album = esc_attr( $attributes['album'] );
123 array_push( $argparts, "album={$album}" );
124 }
125
126 if ( 'tall' === $sizekey ) {
127 if ( isset( $attributes['album'] ) ) {
128 $sizekey .= '_album';
129 } else {
130 $sizekey .= '_track';
131 }
132 }
133
134 // if size specified that we don't recognize, fall back on venti.
135 if ( empty( $sizes[ $sizekey ] ) ) {
136 $sizekey = 'venti';
137 $attributes['size'] = 'venti';
138 }
139
140 /*
141 * use strict regex for digits + optional % instead of absint for height/width
142 * 'width' and 'height' params in the iframe url get the exact string from the shortcode
143 * args, whereas the inline style attribute must have "px" added to it if it has no "%"
144 */
145 if ( isset( $attributes['width'] ) && preg_match( '|^([0-9]+)(%)?$|', $attributes['width'], $matches ) ) {
146 $width = $attributes['width'];
147 $csswidth = $attributes['width'];
148 if ( count( $matches ) < 3 ) {
149 $csswidth .= 'px';
150 }
151 }
152 if ( isset( $attributes['height'] ) && preg_match( '|^([0-9]+)(%)?$|', $attributes['height'], $matches ) ) {
153 $height = $attributes['height'];
154 $cssheight = $attributes['height'];
155 if ( count( $matches ) < 3 ) {
156 $cssheight .= 'px';
157 }
158 }
159
160 if ( ! $height ) {
161 $height = $sizes[ $sizekey ]['height'];
162 $cssheight = $height . 'px';
163 }
164
165 if ( ! $width ) {
166 $width = $sizes[ $sizekey ]['width'];
167 $csswidth = $width . 'px';
168 }
169
170 if ( isset( $attributes['layout'] ) ) {
171 array_push( $argparts, "layout={$attributes['layout']}" );
172 } elseif ( isset( $attributes['size'] ) && preg_match( '|^[a-zA-Z0-9]+$|', $attributes['size'] ) ) {
173 array_push( $argparts, "size={$attributes['size']}" );
174 }
175
176 if ( isset( $attributes['bgcol'] ) && preg_match( '|^[0-9A-Fa-f]+$|', $attributes['bgcol'] ) ) {
177 array_push( $argparts, "bgcol={$attributes['bgcol']}" );
178 }
179
180 if ( isset( $attributes['linkcol'] ) && preg_match( '|^[0-9A-Fa-f]+$|', $attributes['linkcol'] ) ) {
181 array_push( $argparts, "linkcol={$attributes['linkcol']}" );
182 }
183
184 if ( isset( $attributes['package'] ) && preg_match( '|^[0-9]+$|', $attributes['package'] ) ) {
185 array_push( $argparts, "package={$attributes['package']}" );
186 }
187
188 if ( isset( $attributes['t'] ) && preg_match( '|^[0-9]+$|', $attributes['t'] ) ) {
189 array_push( $argparts, "t={$attributes['t']}" );
190 }
191
192 if ( 'true' === $attributes['notracklist'] ) {
193 array_push( $argparts, 'notracklist=true' );
194 }
195
196 // 'tracklist' arg deprecates 'notracklist=true' to be less weird. note, behavior
197 // if both are specified is undefined
198 switch ( $attributes['tracklist'] ) {
199 case 'false':
200 case 'none':
201 array_push( $argparts, 'tracklist=false' );
202 break;
203 }
204
205 switch ( $attributes['artwork'] ) {
206 case 'false':
207 case 'none':
208 case 'small':
209 array_push( $argparts, 'artwork=' . $attributes['artwork'] );
210 break;
211 }
212
213 if ( 'true' === $attributes['minimal'] ) {
214 array_push( $argparts, 'minimal=true' );
215 }
216
217 if ( isset( $attributes['theme'] ) && preg_match( '|^[a-zA-Z_]+$|', $attributes['theme'] ) ) {
218 array_push( $argparts, "theme={$attributes['theme']}" );
219 }
220
221 // param 'tracks' is signed digest param 'esig'.
222 if ( isset( $attributes['tracks'] ) && preg_match( '|^[0-9\,]+$|', $attributes['tracks'] ) ) {
223 if ( isset( $attributes['esig'] ) && preg_match( '|^[0-9A-Fa-f]+$|', $attributes['esig'] ) ) {
224 array_push( $argparts, "tracks={$attributes['tracks']}" );
225 array_push( $argparts, "esig={$attributes['esig']}" );
226 }
227 }
228
229 if ( $is_video ) {
230 $url = '//bandcamp.com/VideoEmbed?' . join( '&', $argparts );
231 $extra_attrs = " mozallowfullscreen='1' webkitallowfullscreen='1' allowfullscreen='1'";
232 } else {
233 $url = '//bandcamp.com/EmbeddedPlayer/v=2/' . join( '/', $argparts ) . '/';
234 $extra_attrs = '';
235 }
236
237 $iframe = '<iframe width="%s" height="%s" style="position: relative; display: block; width: %s; height: %s;" src="%s" allowtransparency="true" frameborder="0"%s></iframe>';
238 $iframe = sprintf( $iframe, esc_attr( $width ), esc_attr( $height ), esc_attr( $csswidth ), esc_attr( $cssheight ), esc_url( $url ), $extra_attrs );
239
240 return $iframe;
241 }
242
243 add_shortcode( 'bandcamp', 'shortcode_handler_bandcamp' );
244