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