| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Utilities; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Carries one ticket-flow message across an admin redirect. |
| 9 |
*/ |
| 10 |
class TicketFlashNotice |
| 11 |
{ |
| 12 |
private const TRANSIENT_PREFIX = 'sync_basalam_ticket_flash_'; |
| 13 |
private const ALLOWED_TYPES = ['success', 'error', 'warning', 'info']; |
| 14 |
|
| 15 |
public static function push(string $message, string $type = 'error'): void |
| 16 |
{ |
| 17 |
$userId = get_current_user_id(); |
| 18 |
if ($userId <= 0) return; |
| 19 |
|
| 20 |
$type = in_array($type, self::ALLOWED_TYPES, true) ? $type : 'info'; |
| 21 |
set_transient( |
| 22 |
self::TRANSIENT_PREFIX . $userId, |
| 23 |
[ |
| 24 |
'message' => wp_strip_all_tags($message), |
| 25 |
'type' => $type, |
| 26 |
], |
| 27 |
2 * MINUTE_IN_SECONDS |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
public static function pull(): ?array |
| 32 |
{ |
| 33 |
$userId = get_current_user_id(); |
| 34 |
if ($userId <= 0) return null; |
| 35 |
|
| 36 |
$key = self::TRANSIENT_PREFIX . $userId; |
| 37 |
$notice = get_transient($key); |
| 38 |
if ($notice === false) return null; |
| 39 |
|
| 40 |
delete_transient($key); |
| 41 |
if (!is_array($notice) || empty($notice['message'])) return null; |
| 42 |
|
| 43 |
$type = isset($notice['type']) && in_array($notice['type'], self::ALLOWED_TYPES, true) |
| 44 |
? $notice['type'] |
| 45 |
: 'info'; |
| 46 |
|
| 47 |
return [ |
| 48 |
'message' => (string) $notice['message'], |
| 49 |
'type' => $type, |
| 50 |
]; |
| 51 |
} |
| 52 |
} |
| 53 |
|