Widget:UPDEvents: Difference between revisions
Appearance
Created page with " <?php →* * UP Diliman Events feed for Iskomunidad. * Retrieves up to five upcoming events from upd.edu.ph.: header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: public, max-age=900'); date_default_timezone_set('Asia/Manila'); $sourceUrl = 'https://upd.edu.ph/events-list/'; function respond($events, $status = 200) { http_response_code($status); echo json_encode( $events, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_..." |
No edit summary |
||
| (12 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<includeonly> | |||
<div id="upd-events-widget" class="upd-events-widget"> | |||
<div class="upd-events-loading">Loading upcoming events...</div> | |||
<ul class="upd-events-list" style="display:none;"></ul> | |||
</div> | |||
< | <style> | ||
.upd-events-widget { | |||
color: #000; | |||
font-size: 15px; | |||
} | |||
.upd-events-list { | |||
list-style: none; | |||
padding: 0; | |||
margin: 0; | |||
} | |||
.upd-events-list li { | |||
margin: 0; | |||
padding: 10px 0; | |||
border-bottom: 1px solid #e5e5e5; | |||
} | |||
.upd-events-list li:last-child { | |||
border-bottom: none; | |||
} | |||
.upd-events-list a { | |||
color: #0E6021 !important; | |||
text-decoration: none; | |||
font-weight: 600; | |||
} | |||
.upd-events-list a:hover { | |||
text-decoration: underline; | |||
} | |||
.upd-events-loading, | |||
.upd-events-empty { | |||
color: #555; | |||
font-size: 14px; | |||
} | |||
</style> | |||
<script> | |||
(function () { | |||
var container = document.getElementById('upd-events-widget'); | |||
if (!container || container.dataset.loaded === '1') { | |||
return; | |||
} | |||
container.dataset.loaded = '1'; | |||
var loading = container.querySelector('.upd-events-loading'); | |||
var list = container.querySelector('.upd-events-list'); | |||
loading.textContent = | |||
'Widget JavaScript is running. Requesting UP Diliman...'; | |||
) | |||
var endpoint = new URL( | |||
} | 'https://upd.edu.ph/wp-admin/admin-ajax.php' | ||
); | |||
var params = { | |||
action: 'search_events', | |||
id: '1', | |||
scope: 'future', | |||
orderby: 'event_start_date,event_start_time,event_name', | |||
view: 'list-grouped', | |||
pno: '1' | |||
}; | |||
Object.keys(params).forEach(function (key) { | |||
endpoint.searchParams.set(key, params[key]); | |||
}); | |||
var finished = false; | |||
var timeout = setTimeout(function () { | |||
if (finished) return; | |||
finished = true; | |||
loading.textContent = | |||
'The UP Diliman request timed out or is blocked.'; | |||
}, 10000); | |||
fetch(endpoint.toString(), { | |||
method: 'GET', | |||
mode: 'cors', | |||
credentials: 'omit' | |||
}) | |||
.then(function (response) { | |||
if (finished) return null; | |||
if (!response.ok) { | |||
throw new Error('HTTP ' + response.status); | |||
} | |||
return response.text(); | |||
}) | |||
.then(function (html) { | |||
if (finished || html === null) return; | |||
finished = true; | |||
clearTimeout(timeout); | |||
var doc = new DOMParser().parseFromString(html, 'text/html'); | |||
var anchors = doc.querySelectorAll( | |||
'#em-events-list-grouped-1 a[href*="/events/"], ' + | |||
'.em-events-list a[href*="/events/"]' | |||
); | |||
var seen = new Set(); | |||
var events = []; | |||
anchors.forEach(function (anchor) { | |||
var heading = anchor.querySelector('h4'); | |||
if (!heading) return; | |||
var title = heading.textContent.trim(); | |||
var url; | |||
try { | |||
url = new URL( | |||
anchor.getAttribute('href'), | |||
'https://upd.edu.ph' | |||
).href; | |||
} catch (e) { | |||
return; | |||
} | |||
if (!title || seen.has(url)) return; | |||
if (!/^https:\/\/upd\.edu\.ph\/events\//i.test(url)) { | |||
return; | |||
} | |||
seen.add(url); | |||
events.push({ | |||
title: title, | |||
} | url: url | ||
}); | |||
}); | |||
list.replaceChildren(); | |||
if (events.length === 0) { | |||
' | loading.textContent = | ||
'The request succeeded, but no event titles were found.'; | |||
return; | |||
} | } | ||
events.slice(0, 5).forEach(function (event) { | |||
var li = document.createElement('li'); | |||
var a = document.createElement('a'); | |||
a.href = event.url; | |||
a.textContent = event.title; | |||
a.target = '_blank'; | |||
a.rel = 'noopener noreferrer'; | |||
li.appendChild(a); | |||
list.appendChild(li); | |||
} | }); | ||
loading.style.display = 'none'; | |||
list.style.display = 'block'; | |||
}) | |||
.catch(function (error) { | |||
if (finished) return; | |||
finished = true; | |||
clearTimeout(timeout); | |||
console.error('UPEvents widget error:', error); | |||
loading.textContent = | |||
'Request failed: ' + | |||
error.name + ' — ' + error.message; | |||
}); | |||
})(); | |||
</script> | |||
</includeonly> | |||