PluginProbe ʕ •ᴥ•ʔ
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings / 1.0.255
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings v1.0.255
1.0.272 1.0.271 1.0.271.1 1.0.270 1.0.269 trunk 1.0.216 1.0.217 1.0.218 1.0.219 1.0.220 1.0.221 1.0.222 1.0.223 1.0.224 1.0.225 1.0.226 1.0.227 1.0.227.1 1.0.228 1.0.229 1.0.230 1.0.231 1.0.232 1.0.233 1.0.234 1.0.234.1 1.0.235 1.0.236 1.0.237 1.0.238 1.0.239 1.0.240 1.0.241 1.0.242 1.0.243 1.0.244 1.0.245 1.0.246 1.0.247 1.0.248 1.0.249 1.0.250 1.0.251 1.0.251.1 1.0.252 1.0.252.1 1.0.253 1.0.254 1.0.255 1.0.256 1.0.257 1.0.258 1.0.259 1.0.259.1 1.0.260 1.0.261 1.0.262 1.0.263 1.0.264 1.0.264.1 1.0.265 1.0.266 1.0.266.1 1.0.267 1.0.268
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src / class-notification-center.php
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src Last commit date
admin 2 years ago database 2 years ago helpers 2 years ago class-json-manager.php 2 years ago class-notification-center.php 2 years ago class-notification.php 2 years ago index.php 2 years ago
class-notification-center.php
358 lines
1 <?php
2 /**
3 * The Notification center handles notifications storage and display.
4 *
5 * @since 1.0.0
6 * @package MyThemeShop
7 * @subpackage MyThemeShop
8 * @author MyThemeShop <admin@mythemeshop.com>
9 */
10
11 namespace MyThemeShop;
12
13 /**
14 * Notification_Center class.
15 */
16 class Notification_Center {
17
18 /**
19 * Option name to store notifications in.
20 *
21 * @var string
22 */
23 private $storage_key = '';
24
25 /**
26 * Notifications.
27 *
28 * @var array
29 */
30 private $notifications = [];
31
32 /**
33 * Stores whether we need to clear storage or not.
34 *
35 * @var array
36 */
37 private $should_clear_storage = true;
38
39 /**
40 * Stores already displayed notice texts to avoid duplication.
41 *
42 * @var array
43 */
44 private $displayed_notifications = [];
45
46 /**
47 * Internal flag for whether notifications have been retrieved from storage.
48 *
49 * @var bool
50 */
51 private $retrieved = false;
52
53 /**
54 * Construct
55 *
56 * @param string $storage_key Option name to store notification in.
57 */
58 public function __construct( $storage_key = 'mythemeshop_notifications' ) {
59 $this->storage_key = $storage_key;
60 add_action( 'plugins_loaded', array( $this, 'get_from_storage' ), 5 );
61 add_action( 'all_admin_notices', array( $this, 'display' ) );
62 add_action( 'shutdown', array( $this, 'update_storage' ) );
63 add_action( 'admin_footer', array( $this, 'print_javascript' ) );
64
65 add_action( 'wp_ajax_wp_helpers_notice_dismissible', array( $this, 'notice_dismissible' ) );
66 }
67
68 /**
69 * Retrieve the notifications from storage
70 *
71 * @codeCoverageIgnore
72 *
73 * @return array Notification[] Notifications
74 */
75 public function get_from_storage() {
76 if ( $this->retrieved ) {
77 return;
78 }
79
80 $this->retrieved = true;
81 $notifications = get_option( $this->storage_key );
82
83 // Check if notifications are stored.
84 if ( empty( $notifications ) ) {
85 $this->should_clear_storage = false;
86 return;
87 }
88
89 if ( is_array( $notifications ) ) {
90 foreach ( $notifications as $notification ) {
91 $this->notifications[] = new Notification(
92 $notification['message'],
93 $notification['options']
94 );
95 }
96 }
97 }
98
99 /**
100 * Display the notifications.
101 *
102 * @codeCoverageIgnore
103 */
104 public function display() {
105
106 // Never display notifications for network admin.
107 if ( $this->is_network_admin() ) {
108 return;
109 }
110
111 $notifications = $this->get_sorted_notifications();
112 if ( empty( $notifications ) ) {
113 return;
114 }
115
116 foreach ( $notifications as $notification ) {
117 if ( $notification->can_display() && ! in_array( (string) $notification, $this->displayed_notifications, true ) ) {
118 echo $notification;
119 $this->displayed_notifications[] = (string) $notification;
120 }
121 }
122 }
123
124 /**
125 * Print JS for dismissile.
126 *
127 * @codeCoverageIgnore
128 */
129 public function print_javascript() {
130 ?>
131 <script>
132 ;(function($) {
133 $( '.wp-helpers-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {
134 var notice = $( this ).parent()
135
136 $.ajax({
137 url: ajaxurl,
138 type: 'POST',
139 dataType: 'json',
140 data: {
141 action: 'wp_helpers_notice_dismissible',
142 security: notice.data( 'security' ),
143 notificationId: notice.attr( 'id' )
144 }
145 });
146 });
147 })(jQuery);
148 </script>
149 <?php
150 }
151
152 /**
153 * Save persistent or transactional notifications to storage.
154 *
155 * We need to be able to retrieve these so they can be dismissed at any time during the execution.
156 *
157 * @codeCoverageIgnore
158 */
159 public function update_storage() {
160 $notifications = $this->get_notifications();
161 $notifications = array_filter( $notifications, [ $this, 'remove_notification' ] );
162
163 /**
164 * Filter: 'wp_helpers_notifications_before_storage' - Allows developer to filter notifications before saving them.
165 *
166 * @param Notification[] $notifications
167 */
168 $notifications = apply_filters( 'wp_helpers_notifications_before_storage', $notifications );
169
170 // No notifications to store, clear storage.
171 if ( empty( $notifications ) && $this->should_clear_storage ) {
172 delete_option( $this->storage_key );
173 return;
174 }
175
176 $notifications = array_map( [ $this, 'notification_to_array' ], $notifications );
177
178 // Save the notifications to the storage.
179 update_option( $this->storage_key, $notifications );
180 }
181
182 /**
183 * Dismiss persistent notice.
184 *
185 * @codeCoverageIgnore
186 */
187 public function notice_dismissible() {
188 $notification_id = filter_input( INPUT_POST, 'notificationId' );
189 check_ajax_referer( $notification_id, 'security' );
190
191 $notification = $this->remove_by_id( $notification_id );
192
193 /**
194 * Filter: 'wp_helpers_notification_dismissed' - Allows developer to perform action after dismissed.
195 *
196 * @param Notification[] $notifications
197 */
198 do_action( 'wp_helpers_notification_dismissed', $notification_id, $notification );
199 }
200
201 /**
202 * Add notification
203 *
204 * @param string $message Message string.
205 * @param array $options Set of options.
206 */
207 public function add( $message, $options = [] ) {
208 if ( isset( $options['id'] ) && ! is_null( $this->get_notification_by_id( $options['id'] ) ) ) {
209 return;
210 }
211
212 $this->notifications[] = new Notification(
213 $message,
214 $options
215 );
216 }
217
218 /**
219 * Provide a way to verify present notifications
220 *
221 * @return array|Notification[] Registered notifications.
222 */
223 public function get_notifications() {
224 return $this->notifications;
225 }
226
227 /**
228 * Get the notification by ID
229 *
230 * @param string $notification_id The ID of the notification to search for.
231 * @return null|Notification
232 */
233 public function get_notification_by_id( $notification_id ) {
234 foreach ( $this->notifications as $notification ) {
235 if ( $notification_id === $notification->args( 'id' ) ) {
236 return $notification;
237 }
238 }
239 return null;
240 }
241
242 /**
243 * Remove the notification by ID
244 *
245 * @codeCoverageIgnore
246 *
247 * @param string $notification_id The ID of the notification to search for.
248 * @return Notification Instance of delete notification.
249 */
250 public function remove_by_id( $notification_id ) {
251 $notification = $this->get_notification_by_id( $notification_id );
252 if ( ! is_null( $notification ) ) {
253 $notification->dismiss();
254 }
255
256 return $notification;
257 }
258
259 /**
260 * Remove notification after it has been displayed.
261 *
262 * @codeCoverageIgnore
263 *
264 * @param Notification $notification Notification to remove.
265 */
266 public function remove_notification( Notification $notification ) {
267 if ( ! $notification->is_displayed() ) {
268 return true;
269 }
270
271 if ( $notification->is_persistent() ) {
272 return true;
273 }
274
275 return false;
276 }
277
278 /**
279 * Return the notifications sorted on type and priority
280 *
281 * @codeCoverageIgnore
282 *
283 * @return array|Notification[] Sorted Notifications
284 */
285 private function get_sorted_notifications() {
286 $notifications = $this->get_notifications();
287 if ( empty( $notifications ) ) {
288 return [];
289 }
290
291 // Sort by severity, error first.
292 usort( $notifications, [ $this, 'sort_notifications' ] );
293
294 return $notifications;
295 }
296
297 /**
298 * Sort on type then priority
299 *
300 * @codeCoverageIgnore
301 *
302 * @param Notification $first Compare with B.
303 * @param Notification $second Compare with A.
304 * @return int 1, 0 or -1 for sorting offset.
305 */
306 private function sort_notifications( Notification $first, Notification $second ) {
307
308 if ( 'error' === $first->args( 'type' ) ) {
309 return -1;
310 }
311
312 if ( 'error' === $second->args( 'type' ) ) {
313 return 1;
314 }
315
316 return 0;
317 }
318
319 /**
320 * Convert Notification to array representation
321 *
322 * @codeCoverageIgnore
323 *
324 * @param Notification $notification Notification to convert.
325 * @return array
326 */
327 private function notification_to_array( Notification $notification ) {
328 return $notification->to_array();
329 }
330
331 /**
332 * Check if is network admin.
333 *
334 * @codeCoverageIgnore
335 *
336 * @return bool
337 */
338 private function is_network_admin() {
339 return function_exists( 'is_network_admin' ) && is_network_admin();
340 }
341
342 /**
343 * Check if a notification with the given ID exists.
344 *
345 * @param string $id Notification ID.
346 * @return boolean
347 */
348 public function has_notification( $id ) {
349 $notifications = $this->get_notifications();
350 foreach ( $notifications as $notification ) {
351 if ( isset( $notification->options['id'] ) && $notification->options['id'] === $id ) {
352 return true;
353 }
354 }
355 return false;
356 }
357 }
358