From e75bd2f6cfe087a8983a90155ced1e5921ced130 Mon Sep 17 00:00:00 2001 From: Sebastian Thulin Date: Tue, 5 Mar 2024 14:29:53 +0100 Subject: [PATCH] fix: cache bug and notices (#16) --- .../FireDangerLevels/FireDangerLevels.php | 33 ++++++++++++++----- templates/fire-danger-levels.blade.php | 10 +++--- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/source/php/Module/FireDangerLevels/FireDangerLevels.php b/source/php/Module/FireDangerLevels/FireDangerLevels.php index debff9c..a0e29d5 100644 --- a/source/php/Module/FireDangerLevels/FireDangerLevels.php +++ b/source/php/Module/FireDangerLevels/FireDangerLevels.php @@ -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; @@ -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; } @@ -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 [ diff --git a/templates/fire-danger-levels.blade.php b/templates/fire-danger-levels.blade.php index cc4a9ff..872ae18 100644 --- a/templates/fire-danger-levels.blade.php +++ b/templates/fire-danger-levels.blade.php @@ -1,6 +1,6 @@ @if (!empty($notices))
- @if (!$hideTitle && !empty($postTitle)) + @if (isset($hideTitle) && !$hideTitle && !empty($postTitle)) @typography([ 'id' => 'mod-fire-danger-levels-' . $ID . '-label', 'element' => 'h2', @@ -16,10 +16,10 @@
@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'],