Only check for updates every 24 hours

This commit is contained in:
Jay Trees 2022-02-22 14:40:25 +01:00
parent 777b7fe188
commit de2a551cc4
2 changed files with 34 additions and 13 deletions

View file

@ -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());
}
}

View file

@ -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 . '")
;');
}
}
}