PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Features/Notifications/NotificationsRepository.php +130 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,130 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Features\Notifications;
6 +
7 +use Metricool\Interfaces\NoticeInterface;
8 +
9 +class NotificationsRepository
10 +{
11 + public const OPTION_NAME = 'metricool_notices';
12 +
13 + /** @var NoticeInterface[] */
14 + private array $notices = [];
15 +
16 + public function __construct()
17 + {
18 + $this->loadNoticesFromDatabase();
19 + }
20 +
21 + /**
22 + * Retrieve a single Notice by its ID
23 + */
24 + public function getNotice(string $noticeId): ?NoticeInterface
25 + {
26 + return $this->notices[$noticeId] ?? null;
27 + }
28 +
29 + /**
30 + * Retrieve all registered notices
31 + * @return NoticeInterface[]
32 + */
33 + public function getAllNotices(): array
34 + {
35 + return $this->notices;
36 + }
37 +
38 + /**
39 + * Add a single Notice to the repository
40 + */
41 + public function addNotice(NoticeInterface $notice, bool $save = true): void
42 + {
43 + $this->notices[$notice->getId()] = $notice;
44 +
45 + if ($save) {
46 + $this->saveNoticesToDatabase();
47 + }
48 + }
49 +
50 + /**
51 + * Upgrade a Notice in the repository. Only replace existing Notices with
52 + * same identifier if the version is lower than the new Notice version.
53 + */
54 + public function upgradeNotice(NoticeInterface $notice, bool $save = true): void
55 + {
56 + $existingNotice = $this->getNotice($notice->getId());
57 + $noticeExists = !empty($existingNotice);
58 +
59 + $noticeIsUpdatable = (
60 + !$noticeExists
61 + || (version_compare($existingNotice->getVersion(), $notice->getVersion(), '<'))
62 + );
63 +
64 + if ($noticeIsUpdatable === false) {
65 + return;
66 + }
67 +
68 + // Upgrades existing Notices and add new Notices
69 + $this->addNotice($notice, $save);
70 + }
71 +
72 + /**
73 + * Remove a Notice from the repository
74 + */
75 + public function removeNotice(NoticeInterface $notice, bool $save = true): void
76 + {
77 + unset($this->notices[$notice->getId()]);
78 +
79 + if ($save) {
80 + $this->saveNoticesToDatabase();
81 + }
82 + }
83 +
84 + /**
85 + * Remove a Notice by its ID from the repository
86 + */
87 + public function removeNoticeById(string $noticeId, bool $save = true): void
88 + {
89 + if (isset($this->notices[$noticeId])) {
90 + unset($this->notices[$noticeId]);
91 + }
92 +
93 + if ($save) {
94 + $this->saveNoticesToDatabase();
95 + }
96 + }
97 +
98 + /**
99 + * Update the status of a Notice if the Notice exists. If the Notice is
100 + * required and the status is set to 'dismissed', the status will not be
101 + * updated.
102 + */
103 + public function toggleNoticeServerSide(string $noticeId, bool $active): void
104 + {
105 + $notice = $this->getNotice($noticeId);
106 + if ($notice === null) {
107 + return;
108 + }
109 +
110 + $notice->setActive($active);
111 + $this->addNotice($notice);
112 + }
113 +
114 + /**
115 + * Load notices from the WordPress database
116 + */
117 + private function loadNoticesFromDatabase(): void
118 + {
119 + $storedNotices = get_option(self::OPTION_NAME, []);
120 + $this->notices = is_array($storedNotices) ? $storedNotices : [];
121 + }
122 +
123 + /**
124 + * Save notices to the WordPress database
125 + */
126 + public function saveNoticesToDatabase(): void
127 + {
128 + update_option(self::OPTION_NAME, $this->notices, false);
129 + }
130 +}