-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
90 lines (73 loc) · 2.4 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/*
Plugin name: WJCT Composer Now Playing Shortcode
Author: Matthew Forgette, Md. Sarwar-A-Kawsar, Ray Hollister
Version: 1.3
1.1 Working PHP API call
1.2 Changed API call to JavaScript, and set to run every half hour AND fixed the time format to AP Style!
1.3 Cleaned up code and moved UCSID
*/
wp_enqueue_style( "composer_now_playing", plugins_url('style.css', __FILE__) );
defined('ABSPATH') or die('You cannot access to this page');
add_shortcode( 'composer_now_playing', 'composer_now_playing_shortcode_callback' );
function composer_now_playing_shortcode_callback(){
?>
<div id="composer-now-playing" class="composer-now-playing">
</div>
<script type='text/JavaScript'>
let firstRun = true;
let minutes;
const element = document.getElementById(
"composer-now-playing"
);
// Change this to your station's UCSID number.
const UCSID = "5187f37be1c838d5f207363f";
const URL =
`https://api.composer.nprstations.org/v1/widget/${UCSID}/now?format=json&limit=20`;
const fetchComposerNowPlaying = async () => {
try {
const response = await fetch(URL);
const data = await response.json();
const name = data.onNow.program.name;
const start = data.onNow.start_utc;
let newStart = new Date(start);
newStart = newStart.toLocaleTimeString(('en-US'), {hour: 'numeric', minute:'2-digit'});
newStart = newStart.replace("AM", "a.m.").replace("PM", "p.m.");
const end = data.onNow.end_utc;
let newEnd = new Date(end);
newEnd = newEnd.toLocaleTimeString(('en-US'), {hour: 'numeric', minute:'2-digit'});
newEnd = newEnd.replace("AM", "a.m.").replace("PM", "p.m.");
const program_url = data.onNow.program.program_link;
element.innerHTML = `
<h3 class="current-show"><a href="${program_url}">${name}</a></h3>
<span class="cpw_program_time">${newStart} - ${newEnd}</span>
`;
} catch (error) {
console.error(error);
}
};
function AddShortcodeIntervalLogic() {
let d = new Date(); // initializes the exact time the the page loaded
let minutes = d.getMinutes();
let seconds = d.getSeconds();
if (firstRun) {
if (minutes > 30) {
minutes = 60 - minutes;
} else {
minutes = 30 - minutes;
}
firstRun = false;
setInterval(fetchComposerNowPlaying, minutes * 60000);
} else {
minutes = 30;
setInterval(fetchComposerNowPlaying, minutes * 60000);
}
}
fetchComposerNowPlaying();
AddShortcodeIntervalLogic();
</script>
<?php
return ob_get_clean();
}
// return ob_get_clean();
// }