NoticeState.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Notice; |
| 6 | |
| 7 | use AC\Storage\UserMeta; |
| 8 | |
| 9 | class NoticeState |
| 10 | { |
| 11 | |
| 12 | private const META_KEY = '_ac_notices'; |
| 13 | |
| 14 | private ?array $data = null; |
| 15 | |
| 16 | public function get(string $slug, string $key) |
| 17 | { |
| 18 | $data = $this->get_data(); |
| 19 | |
| 20 | return $data[$slug][$key] ?? null; |
| 21 | } |
| 22 | |
| 23 | public function set(string $slug, string $key, $value): void |
| 24 | { |
| 25 | $data = $this->get_data(); |
| 26 | $data[$slug][$key] = $value; |
| 27 | |
| 28 | $this->save_data($data); |
| 29 | } |
| 30 | |
| 31 | public function is_dismissed(string $slug): bool |
| 32 | { |
| 33 | return (bool)$this->get($slug, 'dismissed'); |
| 34 | } |
| 35 | |
| 36 | public function dismiss(string $slug): void |
| 37 | { |
| 38 | $this->set($slug, 'dismissed', true); |
| 39 | } |
| 40 | |
| 41 | public function track_first_seen(string $slug): void |
| 42 | { |
| 43 | if ( ! $this->get($slug, 'first-seen')) { |
| 44 | $this->set($slug, 'first-seen', time()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check if enough time has passed since the notice was first seen. |
| 50 | * Each notice defines its own delay via get_delay_days(). This prevents |
| 51 | * notices from showing immediately on new installs. |
| 52 | */ |
| 53 | public function is_delay_met(string $slug, int $days): bool |
| 54 | { |
| 55 | $first_seen = $this->get($slug, 'first-seen'); |
| 56 | |
| 57 | return $first_seen && (time() - (int)$first_seen) >= $days * DAY_IN_SECONDS; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Record that a notice was just dismissed. Used together with is_cooldown_active() |
| 62 | * to suppress all integration notices for a period after any dismissal, preventing |
| 63 | * the user from seeing back-to-back notices. |
| 64 | */ |
| 65 | public function track_dismissal(): void |
| 66 | { |
| 67 | $data = $this->get_data(); |
| 68 | $data['_last_dismissed'] = time(); |
| 69 | |
| 70 | $this->save_data($data); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check if a recent dismissal should suppress all notices. |
| 75 | * Returns true if any notice was dismissed less than $days ago. |
| 76 | */ |
| 77 | public function is_cooldown_active(int $days): bool |
| 78 | { |
| 79 | $last = $this->get_data()['_last_dismissed'] ?? null; |
| 80 | |
| 81 | return $last && (time() - (int)$last) < $days * DAY_IN_SECONDS; |
| 82 | } |
| 83 | |
| 84 | private function get_data(): array |
| 85 | { |
| 86 | if (null === $this->data) { |
| 87 | $this->data = $this->create_storage()->get() ?: []; |
| 88 | } |
| 89 | |
| 90 | return $this->data; |
| 91 | } |
| 92 | |
| 93 | private function save_data(array $data): void |
| 94 | { |
| 95 | $this->data = $data; |
| 96 | $this->create_storage()->save($data); |
| 97 | } |
| 98 | |
| 99 | private function create_storage(): UserMeta |
| 100 | { |
| 101 | return new UserMeta(self::META_KEY); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |