PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.24
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.24
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / class-berqnotifications.php

class-berqnotifications.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.24, at inc/class-berqnotifications.php

85 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class berqNotifications {
4 private $transient_key = 'berqwp_user_notice'; // Define a unique key for the transient
5
6 function __construct() {
7 add_action('berqwp_notices', [$this, 'notification']);
8 add_action('shutdown', [$this, 'maybe_clear_transient']); // Clear transient if empty after rendering notices
9 }
10
11 function notification() {
12 // Get the notices from the transient
13 $notices = get_transient($this->transient_key);
14
15 if (empty($notices)) {
16 return;
17 }
18
19 if (!empty($notices)) {
20 foreach ($notices as $notice) {
21 $msg = $notice[1];
22 $class = $notice[0];
23
24 bwp_notice($class, '', $msg);
25
26 // $notice_html = '<div class="notice notice-'.$class.' is-dismissible">';
27 // $notice_html .= '<p>';
28 // $notice_html .= esc_html__($msg, 'searchpro');
29 // $notice_html .= '</p>';
30 // $notice_html .= '</div>';
31 // echo wp_kses_post($notice_html);
32 }
33
34 // Clear the notices after displaying them by removing the transient
35 set_transient($this->transient_key, []);
36 }
37 }
38
39 // Store a new notice in the transient
40 function notice($text) {
41 $this->add_notice('info', $text);
42 }
43
44 function error($text) {
45 $this->add_notice('error', $text);
46 }
47
48 function warning($text) {
49 $this->add_notice('warning', $text);
50 }
51
52 function success($text) {
53 $this->add_notice('success', $text);
54 }
55
56 // Add a notice to the transient
57 private function add_notice($type, $text) {
58 // Get current notices
59 $notices = get_transient($this->transient_key);
60
61 if (!$notices) {
62 $notices = [];
63 }
64
65 // Add the new notice
66 $notices[] = [$type, $text];
67
68 // Store the notices back in the transient (with no expiration, so it persists until explicitly cleared)
69 set_transient($this->transient_key, $notices);
70 }
71
72 // Optionally clear transient if it's empty after rendering notices
73 function maybe_clear_transient() {
74 $notices = get_transient($this->transient_key);
75 if (empty($notices)) {
76 delete_transient($this->transient_key);
77 }
78 }
79 }
80
81 if (is_admin()) {
82 global $berqNotifications;
83 $berqNotifications = new berqNotifications();
84 }
85