| @@ -1,128 +1,145 @@ | ||
| 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 } | |
| 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 | + | |