| 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 |
|