PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.0.13
Admin Columns v7.0.13
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 3 months ago
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