説明なし

bandcamp.php 7.7KB

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