Skip to content

Commit

Permalink
fix: cache bug and notices (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianthulin authored Mar 5, 2024
1 parent 2fd26f5 commit e75bd2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
33 changes: 25 additions & 8 deletions source/php/Module/FireDangerLevels/FireDangerLevels.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class FireDangerLevels extends \Modularity\Module
public $icon = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IS0tIFVwbG9hZGVkIHRvOiBTVkcgUmVwbywgd3d3LnN2Z3JlcG8uY29tLCBHZW5lcmF0b3I6IFNWRyBSZXBvIE1peGVyIFRvb2xzIC0tPg0KPHN2ZyB3aWR0aD0iODAwcHgiIGhlaWdodD0iODAwcHgiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNNS45MjYgMjAuNTc0YTcuMjYgNy4yNiAwIDAgMCAzLjAzOSAxLjUxMWMuMTA3LjAzNS4xNzktLjEwNS4xMDctLjE3NS0yLjM5NS0yLjI4NS0xLjA3OS00Ljc1OC0uMTA3LTUuODczLjY5My0uNzk2IDEuNjgtMi4xMDcgMS42MDgtMy44NjUgMC0uMTc2LjE4LS4zMTcuMzIyLS4yMTEgMS4zNTkuNzAzIDIuMjg4IDIuMjUgMi41MzggMy41MTUuMzk0LS4zODYuNTM3LS45ODQuNTM3LTEuNTExIDAtLjE3Ni4yMTQtLjMxNy4zOTMtLjE3NiAxLjI4NyAxLjE2IDMuNTAzIDUuMDk3LS4wNzIgOC4xOS0uMDcxLjA3MSAwIC4yMTIuMDcyLjE3N2E4Ljc2MSA4Ljc2MSAwIDAgMCAzLjAwMy0xLjQ0MmM1LjgyNy00LjUgMi4wMzctMTIuNDgtLjQzLTE1LjExNi0uMzIxLS4zMTctLjg5My0uMTA2LS44OTMuMzUxLS4wMzYuOTUtLjMyMiAyLjAwNC0xLjA3MiAyLjcwNy0uNTcyLTIuMzktMi40NzgtNS4xMDUtNS4xOTUtNi40NDEtLjM1Ny0uMTc2LS43ODYuMTA1LS43NS40OTIuMDcgMy4yNy0yLjA2MyA1LjM1Mi0zLjkyMiA4LjA1OS0xLjY0NSAyLjQyNS0yLjcxNyA2Ljg5LjgyMiA5LjgwOHoiIGZpbGw9IiMwMDAwMDAiLz48L3N2Zz4=';
public $supports = array();
public $plugin = array();
public $cacheTtl = MINUTE_IN_SECONDS * 15;
public $cacheTtl = MINUTE_IN_SECONDS * 1;
public $refreshInterval = MINUTE_IN_SECONDS * 15;
public $hideTitle = false;
public $isDeprecated = false;
private $apiUrl = null;
Expand All @@ -28,11 +29,11 @@ public function data(): array
$apiDateTimeChanged = $this->getDateTimeChanged();
$dateTimeChanged = $this->formatApiDateTime($apiDateTimeChanged);

$data['refreshInterval'] = MINUTE_IN_SECONDS * 15;
$data['notices'] = $this->getNoticesData();
$data['dateTimeChangedLabel'] = sprintf(__('Updated at %s', 'api-alarm-integration'), $dateTimeChanged);
$data['isAjaxRequest'] = wp_doing_ajax() || defined('REST_REQUEST');
$data['ID'] = $this->ID;
$data['refreshInterval'] = $this->refreshInterval ?? 0;
$data['notices'] = $this->getNoticesData();
$data['dateTimeChangedLabel'] = sprintf(__('Updated at %s', 'api-alarm-integration'), $dateTimeChanged);
$data['isAjaxRequest'] = wp_doing_ajax() || defined('REST_REQUEST');
$data['ID'] = $this->ID;

return $data;
}
Expand Down Expand Up @@ -66,18 +67,34 @@ private function getNoticesData()
private function getDateTimeChanged(): string
{
$data = $this->getDataFromApi();
return $data['dateTimeChanged'];
return $data['dateTimeChanged'] ?? '';
}

private function getDataFromApi()
{
$response = wp_remote_get($this->apiUrl);
$response = wp_remote_get(
$this->appendCacheBustQueryParam(
$this->apiUrl,
$this->cacheTtl
)
);
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);

return $data;
}

private function getCacheBustKey($url, $ttl = 5) {
return md5($url . ceil(time() / $ttl));
}

private function appendCacheBustQueryParam($url, $ttl = 5) {
return add_query_arg(
'cache_bust', $this->getCacheBustKey($url, $ttl),
$url
);
}

private function getNoticeTypeFromLevel($level): string
{
return [
Expand Down
10 changes: 5 additions & 5 deletions templates/fire-danger-levels.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@if (!empty($notices))
<div data-module-refresh-interval="{!! $refreshInterval !!}" data-module-id="{!! $ID !!}">
@if (!$hideTitle && !empty($postTitle))
@if (isset($hideTitle) && !$hideTitle && !empty($postTitle))
@typography([
'id' => 'mod-fire-danger-levels-' . $ID . '-label',
'element' => 'h2',
Expand All @@ -16,10 +16,10 @@
<div class="o-grid-12@sm o-grid-6@md o-grid-4@lg o-grid-4@xl">
@notice([
'type' => $notice['type'],
'message' => [
'title' => $notice['title'],
'text' => $notice['text'],
'size' => 'sm',
'message' => (object) [
'title' => $notice['title'],
'text' => $notice['text'],
'size' => 'sm',
],
'classList' => [!$isAjaxRequest ? 'u-preloader' : ''],
'attributeList' => ['aria-hidden' => $isAjaxRequest ? 'false' : 'true'],
Expand Down

0 comments on commit e75bd2f

Please sign in to comment.