bandcamp.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // shortcode handler for [bandcamp], which inserts a bandcamp.com
  3. // music player (iframe, html5)
  4. //
  5. // [bandcamp album=119385304]
  6. // [bandcamp album=3462839126 bgcol=FFFFFF linkcol=4285BB size=venti]
  7. // [bandcamp track=2446959313]
  8. //
  9. function shortcode_handler_bandcamp( $atts ) {
  10. // there are no default values, but specify here anyway
  11. // to explicitly list supported atts
  12. $attributes = shortcode_atts( array(
  13. 'album' => null, // integer album id
  14. 'track' => null, // integer track id
  15. 'video' => null, // integer track id for video player
  16. 'size' => 'venti', // one of the supported sizes
  17. 'bgcol' => 'FFFFFF', // hex, no '#' prefix
  18. 'linkcol' => null, // hex, no '#' prefix
  19. 'layout' => null, // encoded layout url
  20. 'width' => null, // integer with optional "%"
  21. 'height' => null, // integer with optional "%"
  22. 'notracklist' => null, // may be string "true" (defaults false)
  23. 'tracklist' => null, // may be string "false" (defaults true)
  24. 'artwork' => null, // may be string "false" (alternately: "none") or "small" (default is large)
  25. 'minimal' => null, // may be string "true" (defaults false)
  26. 'theme' => null, // may be theme identifier string ("light"|"dark" so far)
  27. 'package' => null, // integer package id
  28. 't' => null, // integer track number
  29. 'tracks' => null, // comma separated list of allowed tracks
  30. 'esig' => null // hex, no '#' prefix
  31. ), $atts, 'bandcamp' );
  32. $sizes = array(
  33. 'venti' => array( 'width' => 400, 'height' => 100 ),
  34. 'grande' => array( 'width' => 300, 'height' => 100 ),
  35. 'grande2' => array( 'width' => 300, 'height' => 355 ),
  36. 'grande3' => array( 'width' => 300, 'height' => 415 ),
  37. 'tall_album' => array( 'width' => 150, 'height' => 295 ),
  38. 'tall_track' => array( 'width' => 150, 'height' => 270 ),
  39. 'tall2' => array( 'width' => 150, 'height' => 450 ),
  40. 'short' => array( 'width' => 46, 'height' => 23 ),
  41. 'large' => array( 'width' => 350, 'height' => 470 ),
  42. 'medium' => array( 'width' => 450, 'height' => 120 ),
  43. 'small' => array( 'width' => 350, 'height' => 42 )
  44. );
  45. $sizekey = $attributes['size'];
  46. $height = null;
  47. $width = null;
  48. $isVideo = false;
  49. // Build iframe url. For audio players, args are appended as
  50. // extra path segments for historical reasons having to
  51. // do with an IE-only flash bug which required this URL
  52. // to contain no querystring. Delay the actual joining
  53. // of args into a string until after we decide if it's
  54. // a video player or an audio player
  55. $argparts = array();
  56. if ( ! isset( $attributes['album'] ) && ! isset( $attributes['track'] ) && ! isset( $attributes['video'] ) ) {
  57. return "[bandcamp: shortcode must include 'track', 'album', or 'video' param]";
  58. }
  59. if ( isset( $attributes['track'] ) && is_numeric( $attributes['track'] ) ) {
  60. $track = esc_attr( $attributes['track'] );
  61. array_push( $argparts, "track={$track}" );
  62. } elseif ( isset( $attributes['video'] ) && is_numeric( $attributes['video'] ) ) {
  63. $track = esc_attr( $attributes['video'] ); // videos are referenced by track id
  64. $urlbase = "//bandcamp.com/EmbeddedPlayer/v=2";
  65. $isVideo = true;
  66. array_push( $argparts, "track={$track}" );
  67. }
  68. if ( isset( $attributes['album'] ) && is_numeric( $attributes['album'] ) ) {
  69. $album = esc_attr( $attributes['album'] );
  70. array_push( $argparts, "album={$album}" );
  71. }
  72. if ( $sizekey == 'tall' ) {
  73. if ( isset( $attributes['album'] ) ) {
  74. $sizekey .= '_album';
  75. } else {
  76. $sizekey .= '_track';
  77. }
  78. }
  79. // if size specified that we don't recognize, fall back on venti
  80. if ( empty( $sizes[ $sizekey ] ) ) {
  81. $sizekey = 'venti';
  82. $attributes['size'] = 'venti';
  83. }
  84. // use strict regex for digits + optional % instead of absint for height/width
  85. // 'width' and 'height' params in the iframe url get the exact string from the shortcode
  86. // args, whereas the inline style attribute must have "px" added to it if it has no "%"
  87. if ( isset( $attributes['width'] ) && preg_match( "|^([0-9]+)(%)?$|", $attributes['width'], $matches ) ) {
  88. $width = $csswidth = $attributes['width'];
  89. if ( sizeof( $matches ) < 3 ) {
  90. $csswidth .= "px";
  91. }
  92. }
  93. if ( isset( $attributes['height'] ) && preg_match( "|^([0-9]+)(%)?$|", $attributes['height'], $matches ) ) {
  94. $height = $cssheight = $attributes['height'];
  95. if ( sizeof( $matches ) < 3 ) {
  96. $cssheight .= "px";
  97. }
  98. }
  99. if ( ! $height ) {
  100. $height = $sizes[ $sizekey ]['height'];
  101. $cssheight = $height . "px";
  102. }
  103. if ( ! $width ) {
  104. $width = $sizes[ $sizekey ]['width'];
  105. $csswidth = $width . "px";
  106. }
  107. if ( isset( $attributes['layout'] ) ) {
  108. array_push( $argparts, "layout={$attributes['layout']}" );
  109. } elseif ( isset( $attributes['size'] ) && preg_match( "|^[a-zA-Z0-9]+$|", $attributes['size'] ) ) {
  110. array_push( $argparts, "size={$attributes['size']}" );
  111. }
  112. if ( isset( $attributes['bgcol'] ) && preg_match( "|^[0-9A-Fa-f]+$|", $attributes['bgcol'] ) ) {
  113. array_push( $argparts, "bgcol={$attributes['bgcol']}" );
  114. }
  115. if ( isset( $attributes['linkcol'] ) && preg_match( "|^[0-9A-Fa-f]+$|", $attributes['linkcol'] ) ) {
  116. array_push( $argparts, "linkcol={$attributes['linkcol']}" );
  117. }
  118. if ( isset( $attributes['package'] ) && preg_match( "|^[0-9]+$|", $attributes['package'] ) ) {
  119. array_push( $argparts, "package={$attributes['package']}" );
  120. }
  121. if ( isset( $attributes['t'] ) && preg_match( "|^[0-9]+$|", $attributes['t'] ) ) {
  122. array_push( $argparts, "t={$attributes['t']}" );
  123. }
  124. if ( $attributes['notracklist'] == "true" ) {
  125. array_push( $argparts, "notracklist=true" );
  126. }
  127. // 'tracklist' arg deprecates 'notracklist=true' to be less weird. note, behavior
  128. // if both are specified is undefined
  129. switch ( $attributes['tracklist'] ) {
  130. case "false":
  131. case "none":
  132. array_push( $argparts, "tracklist=false" );
  133. break;
  134. }
  135. switch ( $attributes['artwork'] ) {
  136. case "false":
  137. case "none":
  138. case "small":
  139. array_push( $argparts, "artwork=" . $attributes['artwork'] );
  140. break;
  141. }
  142. if ( $attributes['minimal'] == "true" ) {
  143. array_push( $argparts, "minimal=true" );
  144. }
  145. if ( isset( $attributes['theme'] ) && preg_match( "|^[a-zA-Z_]+$|", $attributes['theme'] ) ) {
  146. array_push( $argparts, "theme={$attributes['theme']}" );
  147. }
  148. // param 'tracks' is signed digest param 'esig'
  149. if ( isset( $attributes['tracks'] ) && preg_match( "|^[0-9\,]+$|", $attributes['tracks'] ) ) {
  150. if ( isset( $attributes['esig'] ) && preg_match( "|^[0-9A-Fa-f]+$|", $attributes['esig'] ) ) {
  151. array_push( $argparts, "tracks={$attributes['tracks']}" );
  152. array_push( $argparts, "esig={$attributes['esig']}" );
  153. }
  154. }
  155. if ( $isVideo ) {
  156. $url = "//bandcamp.com/VideoEmbed?" . join( '&', $argparts );
  157. $extraAttrs = " mozallowfullscreen='1' webkitallowfullscreen='1' allowfullscreen='1'";
  158. } else {
  159. $url = "//bandcamp.com/EmbeddedPlayer/v=2/" . join( '/', $argparts ) . '/';
  160. $extraAttrs = '';
  161. }
  162. return "<iframe width='" . esc_attr( $width ) . "' height='" . esc_attr( $height ) . "' style='position: relative; display: block; width: " . esc_attr( $csswidth ) . "; height: " . esc_attr( $cssheight ) . ";' src='" . esc_url( $url ) . "' allowtransparency='true' frameborder='0'" . $extraAttrs . "></iframe>";
  163. }
  164. add_shortcode( 'bandcamp', 'shortcode_handler_bandcamp' );