| 1 |
<?php |
| 2 |
$id = wp_unique_id('streamcast-'); |
| 3 |
|
| 4 |
$streamUrl = $attributes['radioPlayer']['streamURL']; |
| 5 |
$streamPort = $attributes['radioPlayer']['streamPort']; |
| 6 |
$playerType = $attributes['radioPlayer']['playerType']; |
| 7 |
$welcomeMessage = $attributes['radioPlayer']['welcomeMessage']; |
| 8 |
$skin = $attributes['radioPlayer']['skin']['name']; |
| 9 |
$width = $attributes['radioPlayer']['skin']['width']; |
| 10 |
$height = $attributes['radioPlayer']['skin']['height']; |
| 11 |
$autoPlay = $attributes['radioPlayer']['autoPlay']; |
| 12 |
$volume = $attributes['radioPlayer']['initialVolume']; |
| 13 |
$playerPosition = $attributes['radioPlayer']['playerPosition']; |
| 14 |
|
| 15 |
if (!function_exists('getPlayerPositionStyles')) { |
| 16 |
function getPlayerPositionStyles($id, $playerPosition) |
| 17 |
{ |
| 18 |
$styles = ""; |
| 19 |
|
| 20 |
if ($playerPosition === "center") { |
| 21 |
$styles = "margin: auto; display: block;"; |
| 22 |
} elseif ($playerPosition === "left") { |
| 23 |
$styles = "margin-left: 0; margin-right: auto;"; |
| 24 |
} elseif ($playerPosition === "right") { |
| 25 |
$styles = "margin-left: auto; margin-right: 0;"; |
| 26 |
} |
| 27 |
|
| 28 |
return "#" . esc_attr($id) . " .musesStyleReset { $styles }"; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
$dynamicPlayerStyles = getPlayerPositionStyles($id, $playerPosition); |
| 33 |
|
| 34 |
if ($skin !== 'b_circle' && $playerType === "standard") { |
| 35 |
$stationName = $attributes['radioPlayer']['stationName']; |
| 36 |
$fetchNameFromUrl = $attributes['radioPlayer']['fetchNameFromUrl']; |
| 37 |
|
| 38 |
?> |
| 39 |
<div id='<?php echo esc_attr($id); ?>'> |
| 40 |
<style> |
| 41 |
<?php echo $dynamicPlayerStyles; ?> |
| 42 |
</style> |
| 43 |
<script type="text/javascript" src="https://hosted.muses.org/mrp.js"></script> |
| 44 |
<script type="text/javascript"> |
| 45 |
window.MRP.insert({ |
| 46 |
url: "<?php echo esc_attr($streamUrl); ?>", |
| 47 |
lang: "en", |
| 48 |
codec: "mp3", |
| 49 |
volume: <?php echo esc_attr($volume); ?>, |
| 50 |
autoplay: <?php echo $autoPlay ? 'true' : 'false'; ?>, |
| 51 |
forceHTML5: true, |
| 52 |
welcome: "<?php echo esc_attr($welcomeMessage); ?>", |
| 53 |
jsevents: true, |
| 54 |
buffering: 0, |
| 55 |
wmode: "transparent", |
| 56 |
skin: "<?php echo esc_attr($skin); ?>", |
| 57 |
width: <?php echo esc_attr($width); ?>, |
| 58 |
height: <?php echo esc_attr($height); ?>, |
| 59 |
metadataMode: "shoutcast", |
| 60 |
metadataInterval: 15 |
| 61 |
}); |
| 62 |
|
| 63 |
var title = "<?php echo esc_attr($stationName); ?>"; |
| 64 |
|
| 65 |
async function fetchData() { |
| 66 |
try { |
| 67 |
const fetchUrl = "<?php echo esc_url($streamUrl); ?>/currentsong?sid=1"; |
| 68 |
const formData = new FormData(); |
| 69 |
formData.append('action', 'my_user_vote'); |
| 70 |
formData.append("url", fetchUrl); |
| 71 |
formData.append("nonce", "wp_rest"); |
| 72 |
|
| 73 |
const response = await fetch("<?php echo esc_url(admin_url('admin-ajax.php')) ?>", { |
| 74 |
method: "POST", |
| 75 |
body: formData, |
| 76 |
}); |
| 77 |
|
| 78 |
if (!response.ok) { |
| 79 |
throw new Error('Network response was not ok'); |
| 80 |
} |
| 81 |
|
| 82 |
const data = await response.json(); |
| 83 |
return data?.data || null; |
| 84 |
} catch (error) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
async function fetchIceCastData() { |
| 90 |
try { |
| 91 |
const response = await fetch("<?php echo esc_url($streamUrl); ?>/status-json.xsl"); |
| 92 |
const data = await response.json(); |
| 93 |
|
| 94 |
const stream = data.icestats?.source || null; |
| 95 |
if (stream) { |
| 96 |
return stream.title || stream.song || stream.server_name || "No Title Available"; |
| 97 |
} |
| 98 |
return null; |
| 99 |
} catch (error) { |
| 100 |
return null; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
async function updateTitle() { |
| 105 |
let title = "<?php echo esc_attr($stationName); ?>"; |
| 106 |
|
| 107 |
<?php if ($fetchNameFromUrl) { ?> |
| 108 |
let fetchedTitle = await fetchData(); |
| 109 |
if (!fetchedTitle) { |
| 110 |
fetchedTitle = await fetchIceCastData(); |
| 111 |
} |
| 112 |
if (fetchedTitle) { |
| 113 |
title = fetchedTitle; |
| 114 |
} |
| 115 |
<?php } ?> |
| 116 |
|
| 117 |
MRP.setTitle(title); |
| 118 |
} |
| 119 |
|
| 120 |
updateTitle(); |
| 121 |
</script> |
| 122 |
|
| 123 |
</div> |
| 124 |
<?php |
| 125 |
} else { |
| 126 |
?> |
| 127 |
<div <?php echo get_block_wrapper_attributes(); ?> id='<?php echo esc_attr($id); ?>' data-attributes='<?php echo esc_attr(wp_json_encode($attributes)); ?>'></div> |
| 128 |
<?php } |
| 129 |
|