| @@ -1,63 +1,84 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | class berqNotifications { |
| 4 | - // public $notices = []; | |
| 4 | + private $transient_key = 'berqwp_user_notice'; // Define a unique key for the transient | |
| 5 | 5 | |
| 6 | 6 | function __construct() { |
| 7 | - add_action('admin_notices', [$this, 'notification']); | |
| 8 | - add_action('init', [$this, 'session']); | |
| 7 | + add_action('berqwp_notices', [$this, 'notification']); | |
| 8 | + add_action('shutdown', [$this, 'maybe_clear_transient']); // Clear transient if empty after rendering notices | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | - function session() { | |
| 12 | - if (is_admin()) { | |
| 13 | - session_start(); | |
| 14 | - } | |
| 15 | - } | |
| 16 | - | |
| 17 | 11 | function notification() { |
| 12 | + // Get the notices from the transient | |
| 13 | + $notices = get_transient($this->transient_key); | |
| 18 | 14 | |
| 19 | - if (empty($_SESSION['berqwp_user_notice'])) { | |
| 15 | + if (empty($notices)) { | |
| 20 | 16 | return; |
| 21 | 17 | } |
| 22 | 18 | |
| 23 | - $notices = $_SESSION['berqwp_user_notice']; | |
| 24 | - | |
| 25 | - | |
| 26 | 19 | if (!empty($notices)) { |
| 27 | - foreach($notices as $notice) { | |
| 20 | + foreach ($notices as $notice) { | |
| 28 | 21 | $msg = $notice[1]; |
| 29 | 22 | $class = $notice[0]; |
| 30 | 23 | |
| 31 | - $notice = '<div class="notice notice-'.$class.' is-dismissible">'; | |
| 32 | - $notice .= '<p>'; | |
| 33 | - $notice .= __($msg, 'searchpro'); | |
| 34 | - $notice .= '</p>'; | |
| 35 | - $notice .= '</div>'; | |
| 36 | - echo wp_kses_post($notice); | |
| 37 | - | |
| 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); | |
| 38 | 32 | } |
| 39 | 33 | |
| 40 | - $_SESSION['berqwp_user_notice'] = []; | |
| 34 | + // Clear the notices after displaying them by removing the transient | |
| 35 | + set_transient($this->transient_key, []); | |
| 41 | 36 | } |
| 42 | - | |
| 43 | 37 | } |
| 44 | 38 | |
| 39 | + // Store a new notice in the transient | |
| 45 | 40 | function notice($text) { |
| 46 | - $_SESSION['berqwp_user_notice'][] = ['info', $text]; | |
| 41 | + $this->add_notice('info', $text); | |
| 47 | 42 | } |
| 48 | 43 | |
| 49 | 44 | function error($text) { |
| 50 | - $_SESSION['berqwp_user_notice'][] = ['error', $text]; | |
| 45 | + $this->add_notice('error', $text); | |
| 51 | 46 | } |
| 52 | 47 | |
| 53 | 48 | function warning($text) { |
| 54 | - $_SESSION['berqwp_user_notice'][] = ['warning', $text]; | |
| 49 | + $this->add_notice('warning', $text); | |
| 55 | 50 | } |
| 56 | 51 | |
| 57 | 52 | function success($text) { |
| 58 | - $_SESSION['berqwp_user_notice'][] = ['success', $text]; | |
| 53 | + $this->add_notice('success', $text); | |
| 59 | 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 | + } | |
| 60 | 79 | } |
| 61 | 80 | |
| 62 | -global $berqNotifications; | |
| 63 | -$berqNotifications = new berqNotifications(); | |
| 81 | +if (is_admin()) { | |
| 82 | + global $berqNotifications; | |
| 83 | + $berqNotifications = new berqNotifications(); | |
| 84 | +} | |