PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Notice / NoticeState.php
codepress-admin-columns / classes / Notice Last commit date
NoticeState.php 1 day ago
NoticeState.php
104 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 private const META_KEY = '_ac_notices';
12
13 private ?array $data = null;
14
15 public function get(string $slug, string $key)
16 {
17 $data = $this->get_data();
18
19 return $data[$slug][$key] ?? null;
20 }
21
22 public function set(string $slug, string $key, $value): void
23 {
24 $data = $this->get_data();
25 $data[$slug][$key] = $value;
26
27 $this->save_data($data);
28 }
29
30 public function is_dismissed(string $slug): bool
31 {
32 return (bool)$this->get($slug, 'dismissed');
33 }
34
35 public function dismiss(string $slug): void
36 {
37 $this->set($slug, 'dismissed', true);
38 }
39
40 public function track_first_seen(string $slug): void
41 {
42 if (! $this->get($slug, 'first-seen')) {
43 $this->set($slug, 'first-seen', time());
44 }
45 }
46
47 /**
48 * Check if enough time has passed since the notice was first seen.
49 * Each notice defines its own delay via get_delay_days(). This prevents
50 * notices from showing immediately on new installs.
51 */
52 public function is_delay_met(string $slug, int $days): bool
53 {
54 $first_seen = $this->get($slug, 'first-seen');
55
56 return $first_seen && (time() - (int)$first_seen) >= $days * DAY_IN_SECONDS;
57 }
58
59 /**
60 * Record that a notice was just dismissed. Used together with is_cooldown_active()
61 * to suppress all integration notices for a period after any dismissal, preventing
62 * the user from seeing back-to-back notices.
63 */
64 public function track_dismissal(): void
65 {
66 $data = $this->get_data();
67 $data['_last_dismissed'] = time();
68
69 $this->save_data($data);
70 }
71
72 /**
73 * Check if a recent dismissal should suppress all notices.
74 * Returns true if any notice was dismissed less than $days ago.
75 */
76 public function is_cooldown_active(int $days): bool
77 {
78 $last = $this->get_data()['_last_dismissed'] ?? null;
79
80 return $last && (time() - (int)$last) < $days * DAY_IN_SECONDS;
81 }
82
83 private function get_data(): array
84 {
85 if (null === $this->data) {
86 $this->data = $this->create_storage()->get() ?: [];
87 }
88
89 return $this->data;
90 }
91
92 private function save_data(array $data): void
93 {
94 $this->data = $data;
95 $this->create_storage()->save($data);
96 }
97
98 private function create_storage(): UserMeta
99 {
100 return new UserMeta(self::META_KEY);
101 }
102
103 }
104