| 1 |
<?php |
| 2 |
// shortcode handler for [bandcamp], which inserts a bandcamp.com |
| 3 |
// music player (embedded flash object) |
| 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 |
'size' => 'venti', // one of the supported sizes |
| 16 |
'bgcol' => 'FFFFFF', // hex, no '#' prefix |
| 17 |
'linkcol' => null, // hex, no '#' prefix |
| 18 |
'layout' => null, // encoded layout url |
| 19 |
'width' => null, // integer with optional "%" |
| 20 |
'height' => null, // integer with optional "%" |
| 21 |
'notracklist' => null, // may be string "true" (defaults false) |
| 22 |
'artwork' => null, // may be string "false" (defaults true) |
| 23 |
'theme' => null, // may be theme identifier string ("light"|"dark" so far) |
| 24 |
'package' => null, // integer package id |
| 25 |
't' => null // integer track number |
| 26 |
), $atts ); |
| 27 |
|
| 28 |
$sizes = array( |
| 29 |
'venti' => array( 'width' => 400, 'height' => 100 ), |
| 30 |
'grande' => array( 'width' => 300, 'height' => 100 ), |
| 31 |
'grande2' => array( 'width' => 300, 'height' => 355 ), |
| 32 |
'grande3' => array( 'width' => 300, 'height' => 415 ), |
| 33 |
'tall_album' => array( 'width' => 150, 'height' => 295 ), |
| 34 |
'tall_track' => array( 'width' => 150, 'height' => 270 ), |
| 35 |
'tall2' => array( 'width' => 150, 'height' => 450 ), |
| 36 |
'short' => array( 'width' => 46, 'height' => 23 ), |
| 37 |
'large' => array( 'width' => 350, 'height' => 470 ), |
| 38 |
'medium' => array( 'width' => 450, 'height' => 120 ), |
| 39 |
'small' => array( 'width' => 350, 'height' => 42 ) |
| 40 |
); |
| 41 |
|
| 42 |
$sizekey = $attributes['size']; |
| 43 |
$height = null; |
| 44 |
$width = null; |
| 45 |
|
| 46 |
// Build iframe url. Args are appended as |
| 47 |
// extra path segments for historical reasons having to |
| 48 |
// do with an IE-only flash bug which required this URL |
| 49 |
// to contain no querystring |
| 50 |
|
| 51 |
$url = "http://bandcamp.com/EmbeddedPlayer/v=2/"; |
| 52 |
if ( isset( $attributes['track'] ) ) { |
| 53 |
$track = (int) $attributes['track']; |
| 54 |
$url .= "track={$track}"; |
| 55 |
|
| 56 |
if ( $sizekey == 'tall' ) { |
| 57 |
$sizekey .= '_track'; |
| 58 |
} |
| 59 |
} elseif ( isset( $attributes['album'] ) ) { |
| 60 |
$album = (int) $attributes['album']; |
| 61 |
$url .= "album={$album}"; |
| 62 |
$type = 'album'; |
| 63 |
|
| 64 |
if ( $sizekey == 'tall' ) { |
| 65 |
$sizekey .= '_album'; |
| 66 |
} |
| 67 |
} else { |
| 68 |
return "[bandcamp: shortcode must include track or album id]"; |
| 69 |
} |
| 70 |
|
| 71 |
// if size specified that we don't recognize, fall back on venti |
| 72 |
if ( empty( $sizes[$sizekey] ) ) { |
| 73 |
$sizekey = 'venti'; |
| 74 |
$attributes['size'] = 'venti'; |
| 75 |
} |
| 76 |
|
| 77 |
// use strict regex for digits + optional % instead of absint for height/width |
| 78 |
// 'width' and 'height' params in the iframe url get the exact string from the shortcode |
| 79 |
// args, whereas the inline style attribute must have "px" added to it if it has no "%" |
| 80 |
if ( isset( $attributes['width'] ) && preg_match( "|^([0-9]+)(%)?$|", $attributes['width'], $matches ) ) { |
| 81 |
$width = $csswidth = $attributes['width']; |
| 82 |
if ( sizeof( $matches ) < 3 ) { |
| 83 |
$csswidth .= "px"; |
| 84 |
} |
| 85 |
} |
| 86 |
if ( isset( $attributes['height'] ) && preg_match( "|^([0-9]+)(%)?$|", $attributes['height'], $matches ) ) { |
| 87 |
$height = $cssheight = $attributes['height']; |
| 88 |
if ( sizeof( $matches ) < 3 ) { |
| 89 |
$cssheight .= "px"; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ( !$height ) { |
| 94 |
$height = $sizes[$sizekey]['height']; |
| 95 |
$cssheight = $height . "px"; |
| 96 |
} |
| 97 |
|
| 98 |
if ( !$width ) { |
| 99 |
$width = $sizes[$sizekey]['width']; |
| 100 |
$csswidth = $width . "px"; |
| 101 |
} |
| 102 |
|
| 103 |
if ( isset( $attributes['layout'] ) ) { |
| 104 |
$url .= "/layout={$attributes['layout']}"; |
| 105 |
} elseif ( isset( $attributes['size'] ) && preg_match( "|^[a-zA-Z0-9]+$|", $attributes['size'] ) ) { |
| 106 |
$url .= "/size={$attributes['size']}"; |
| 107 |
} |
| 108 |
|
| 109 |
if ( isset( $attributes['bgcol'] ) && preg_match( "|^[0-9A-Fa-f]+$|", $attributes['bgcol'] ) ) { |
| 110 |
$url .= "/bgcol={$attributes['bgcol']}"; |
| 111 |
} |
| 112 |
|
| 113 |
if ( isset( $attributes['linkcol'] ) && preg_match( "|^[0-9A-Fa-f]+$|", $attributes['linkcol'] ) ) { |
| 114 |
$url .= "/linkcol={$attributes['linkcol']}"; |
| 115 |
} |
| 116 |
|
| 117 |
if ( isset( $attributes['package'] ) && preg_match( "|^[0-9]+$|", $attributes['package'] ) ) { |
| 118 |
$url .= "/package={$attributes['package']}"; |
| 119 |
} |
| 120 |
|
| 121 |
if ( isset( $attributes['t'] ) && preg_match( "|^[0-9]+$|", $attributes['t'] ) ) { |
| 122 |
$url .= "/t={$attributes['t']}"; |
| 123 |
} |
| 124 |
|
| 125 |
if ( $attributes['notracklist'] == "true" ) { |
| 126 |
$url .= "/notracklist=true"; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $attributes['artwork'] == "false" ) { |
| 130 |
$url .= "/artwork=false"; |
| 131 |
} |
| 132 |
|
| 133 |
if ( isset( $attributes['theme'] ) && preg_match( "|^[a-zA-Z_]+$|", $attributes['theme'] ) ) { |
| 134 |
$url .= "/theme={$attributes['theme']}"; |
| 135 |
} |
| 136 |
|
| 137 |
$url .= '/'; |
| 138 |
|
| 139 |
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'></iframe>"; |
| 140 |
} |
| 141 |
|
| 142 |
add_shortcode( 'bandcamp', 'shortcode_handler_bandcamp' ); |
| 143 |
|