PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Notification / Manager.php
matomo / app / core / Notification Last commit date
Manager.php 6 years ago
Manager.php
204 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Notification;
10
11 use Piwik\Notification;
12 use Piwik\Session;
13 use Piwik\Session\SessionNamespace;
14
15 /**
16 * Posts and removes UI notifications (see {@link Piwik\Notification} to learn more).
17 *
18 */
19 class Manager
20 {
21 /**
22 * @var SessionNamespace
23 */
24 private static $session = null;
25
26 /**
27 * @var Notification[]
28 */
29 private static $notifications = array();
30
31 /**
32 * Posts a notification that will be shown in Piwik's status bar. If a notification with the same ID
33 * has been posted and has not been closed/removed, it will be replaced with `$notification`.
34 *
35 * @param string $id A unique identifier for this notification. The ID must be a valid HTML
36 * element ID. It can only contain alphanumeric characters (underscores can
37 * be used).
38 * @param Notification $notification The notification to post.
39 * @api
40 */
41 public static function notify($id, Notification $notification)
42 {
43 self::checkId($id);
44
45 self::removeOldestNotificationsIfThereAreTooMany();
46 self::addNotification($id, $notification);
47 }
48
49 /**
50 * Removes a posted notification by ID.
51 *
52 * @param string $id The notification ID, see {@link notify()}.
53 */
54 public static function cancel($id)
55 {
56 self::checkId($id);
57
58 self::removeNotification($id);
59 }
60
61 /**
62 * Removes all temporary notifications.
63 *
64 * Call this method after the notifications have been
65 * displayed to make sure temporary notifications won't be displayed twice.
66 */
67 public static function cancelAllNonPersistent()
68 {
69 foreach (static::getAllNotifications() as $id => $notification) {
70 if (Notification::TYPE_PERSISTENT != $notification->type) {
71 static::removeNotification($id);
72 }
73 }
74 }
75
76 /**
77 * Determine all notifications that needs to be displayed. They are sorted by priority. Highest priorities first.
78 * @return \ArrayObject
79 */
80 public static function getAllNotificationsToDisplay()
81 {
82 $notifications = static::getAllNotifications();
83
84 uasort($notifications, function ($n1, $n2) {
85 /** @var Notification $n1 */ /** @var Notification $n2 */
86 if ($n1->getPriority() == $n2->getPriority()) {
87 return 0;
88 }
89
90 return $n1->getPriority() > $n2->getPriority() ? -1 : 1;
91 });
92
93 return $notifications;
94 }
95
96 /**
97 * @param $id
98 * @throws \Exception In case id is empty or if id contains non word characters
99 */
100 private static function checkId($id)
101 {
102 if (empty($id)) {
103 throw new \Exception('Notification ID is empty.');
104 }
105
106 if (!preg_match('/^(\w)*$/', $id)) {
107 throw new \Exception('Invalid Notification ID given. Only word characters (AlNum + underscore) allowed.');
108 }
109 }
110
111 private static function addNotification($id, Notification $notification)
112 {
113 self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification);
114
115 // we store all kinda notifications here so in case the session is not enabled or disabled later there is still
116 // a chance it gets delivered to the UI during the same request.
117 self::$notifications[$id] = $notification;
118 }
119
120 private static function saveNotificationAcrossUiRequestsIfNeeded($id, Notification $notification)
121 {
122 if (self::isSessionEnabled()) {
123 // we need to save even non persistent notifications if possible. Otherwise if there's a redirect
124 // a notification is not shown on the next page view
125 $session = static::getSession();
126 $session->notifications[$id] = $notification;
127 }
128 }
129
130 private static function removeOldestNotificationsIfThereAreTooMany()
131 {
132 if (!self::isSessionEnabled()) {
133 return;
134 }
135
136 $maxNotificationsInSession = 30;
137
138 $session = static::getSession();
139
140 while (count($session->notifications) >= $maxNotificationsInSession) {
141 array_shift($session->notifications);
142 }
143 }
144
145 private static function getAllNotifications()
146 {
147 if (!self::isSessionEnabled()) {
148 return array();
149 }
150
151 $notifications = self::$notifications;
152
153 foreach ($notifications as $id => $notification) {
154 // we copy them over to the session if possible and persist it in case the session was not yet
155 // writable / enabled at the time the notification was added.
156 self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification);
157 }
158
159 if (self::isSessionEnabled()) {
160 $session = static::getSession();
161 foreach ($session->notifications as $id => $notification) {
162 $notifications[$id] = $notification;
163 }
164 }
165
166 return $notifications;
167 }
168
169 private static function removeNotification($id)
170 {
171 if (array_key_exists($id, self::$notifications)) {
172 unset(self::$notifications[$id]);
173 }
174
175 if (self::isSessionEnabled()) {
176 $session = static::getSession();
177 if (array_key_exists($id, $session->notifications)) {
178 unset($session->notifications[$id]);
179 }
180 }
181 }
182
183 private static function isSessionEnabled()
184 {
185 return Session::isWritable() && Session::isReadable();
186 }
187
188 /**
189 * @return SessionNamespace
190 */
191 private static function getSession()
192 {
193 if (!isset(static::$session)) {
194 static::$session = new SessionNamespace('notification');
195 }
196
197 if (empty(static::$session->notifications) && self::isSessionEnabled()) {
198 static::$session->notifications = array();
199 }
200
201 return static::$session;
202 }
203 }
204