aboutsummaryrefslogtreecommitdiffstats
path: root/partials/site_status.php
blob: c758186e84bc45747c4c399eecf496c6ef4205a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

function site_latest_update_timestamp(): int
{
    static $latest = null;
    if ($latest !== null) {
        return $latest;
    }

    $root = dirname(__DIR__);
    $latest = 0;
    $extensions = ['php' => true, 'html' => true, 'css' => true, 'js' => true, 'txt' => true];
    $skipDirs = ['.git' => true, '.agents' => true, 'node_modules' => true, 'vendor' => true];

    $iterator = new RecursiveIteratorIterator(
        new RecursiveCallbackFilterIterator(
            new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS),
            static function (SplFileInfo $current) use ($skipDirs): bool {
                if ($current->isDir()) {
                    return !isset($skipDirs[$current->getFilename()]);
                }
                return true;
            }
        )
    );

    foreach ($iterator as $file) {
        if (!$file->isFile()) {
            continue;
        }
        $extension = strtolower($file->getExtension());
        if (!isset($extensions[$extension])) {
            continue;
        }
        $mtime = @filemtime($file->getPathname());
        if ($mtime && $mtime > $latest) {
            $latest = $mtime;
        }
    }

    return $latest;
}

function site_update_status(): array
{
    $timezone = new DateTimeZone('America/New_York');
    $latest = site_latest_update_timestamp();
    $date = $latest
        ? (new DateTimeImmutable('@' . $latest))->setTimezone($timezone)->format('Y-m-d')
        : (new DateTimeImmutable('now', $timezone))->format('Y-m-d');
    $today = (new DateTimeImmutable('now', $timezone))->format('Y-m-d');

    return [
        'date' => $date,
        'updated_today' => $date === $today,
    ];
}

function render_site_update_status(string $class = 'site-status-row'): void
{
    $status = site_update_status();
    $date = htmlspecialchars($status['date'], ENT_QUOTES, 'UTF-8');
    $label = $status['updated_today'] ? 'Updated today: check the changelog' : 'View changelog';
    ?>
    <div class="<?= htmlspecialchars($class, ENT_QUOTES, 'UTF-8') ?>" aria-label="Site status">
      <span>Last updated: <time datetime="<?= $date ?>"><?= $date ?></time></span>
      <a href="https://www.sillylaird.ca/changelog/"><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></a>
    </div>
    <?php
}