From de2a551cc45a93463c49a6de414506ab08651bd3 Mon Sep 17 00:00:00 2001 From: Jay Trees Date: Tue, 22 Feb 2022 14:40:25 +0100 Subject: [PATCH] Only check for updates every 24 hours --- index.php | 26 +++++++++++++++++--------- src/classes/options.php | 21 +++++++++++++++++---- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/index.php b/index.php index 0377b449..4ecdcce4 100644 --- a/index.php +++ b/index.php @@ -79,21 +79,29 @@ if ($options) { /** * Update + * + * Check for update every 24 hours. */ use Github\Client; if ($options) { - try { - $client = new Client(); - $release = $client->api('repo')->releases()->latest('grandeljay', 'wishthis'); - $tag = $release['tag_name']; - $version = str_replace('v', '', $tag); + $updateLastChecked = $options->getOption('updateLastChecked'); - if (-1 === version_compare($options->version, $version)) { - $options->updateAvailable = true; + if (!$updateLastChecked || time() - $updateLastChecked >= 86400) { + try { + $client = new Client(); + $release = $client->api('repo')->releases()->latest('grandeljay', 'wishthis'); + $tag = $release['tag_name']; + $version = str_replace('v', '', $tag); + + if (-1 === version_compare($options->version, $version)) { + $options->updateAvailable = true; + } + } catch (\Github\Exception\RuntimeException $th) { + echo wishthis\Page::warning($th->getMessage()); } - } catch (\Github\Exception\RuntimeException $th) { - echo $th->getMessage(); + + $options->setOption('updateLastChecked', time()); } } diff --git a/src/classes/options.php b/src/classes/options.php index a20761f6..27b5cf9d 100644 --- a/src/classes/options.php +++ b/src/classes/options.php @@ -43,9 +43,22 @@ class Options public function setOption(string $key, string $value): void { - $this->database->query('UPDATE `options` - SET `value` = "' . $value . '" - WHERE `key` = "' . $key . '" - ;'); + $optionExists = 0 !== $this->database + ->query('SELECT * + FROM `options` + WHERE `key` = "' . $key . '";') + ->rowCount(); + + if ($optionExists) { + $this->database->query('UPDATE `options` + SET `value` = "' . $value . '" + WHERE `key` = "' . $key . '" + ;'); + } else { + $this->database->query('INSERT INTO `options` + (`key`, `value`) VALUES + ("' . $key . '", "' . $value . '") + ;'); + } } }