AbstractCache.php
3 years ago
FlashMessage.php
10 months ago
QuizAttempts.php
11 months ago
TutorCache.php
3 years ago
FlashMessage.php
130 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handle flash message |
| 4 | * |
| 5 | * @package Tutor\FlashMessage |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 2.1.9 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\Cache; |
| 12 | |
| 13 | use Tutor\Cache\AbstractCache; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * User data caching |
| 21 | * Get Set & check |
| 22 | * |
| 23 | * @since 2.1.9 |
| 24 | */ |
| 25 | class FlashMessage extends AbstractCache { |
| 26 | |
| 27 | const SUCCESS = 'success'; |
| 28 | const WARNING = 'warning'; |
| 29 | const DANGER = 'danger'; |
| 30 | const INFO = 'info'; |
| 31 | |
| 32 | /** |
| 33 | * Set data property |
| 34 | * |
| 35 | * @since 2.1.9 |
| 36 | * |
| 37 | * @param string $msg alert message. |
| 38 | * @param string $alert_type alert type. |
| 39 | */ |
| 40 | public function __construct( $msg = '', $alert_type = self::SUCCESS ) { |
| 41 | if ( '' !== $msg ) { |
| 42 | $this->data = array( |
| 43 | 'alert' => $alert_type, |
| 44 | 'message' => $msg, |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Key for cache identifier |
| 51 | * |
| 52 | * @var string |
| 53 | * |
| 54 | * @since 2.1.9 |
| 55 | */ |
| 56 | private const KEY = 'tutor_flash_message'; |
| 57 | |
| 58 | /** |
| 59 | * Cache expire time |
| 60 | * |
| 61 | * @var string |
| 62 | * |
| 63 | * @since 2.1.9 |
| 64 | */ |
| 65 | private const HOUR_IN_SECONDS = 0; |
| 66 | |
| 67 | /** |
| 68 | * Data for caching |
| 69 | * |
| 70 | * @var string |
| 71 | * |
| 72 | * @since 2.1.9 |
| 73 | */ |
| 74 | public $data; |
| 75 | |
| 76 | /** |
| 77 | * Key |
| 78 | * |
| 79 | * @since 2.1.9 |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function key(): string { |
| 84 | return self::KEY; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Cache data |
| 89 | * |
| 90 | * @since 2.1.9 |
| 91 | * |
| 92 | * @return object |
| 93 | */ |
| 94 | public function cache_data() { |
| 95 | return $this->data; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Cache time |
| 100 | * |
| 101 | * @since 2.1.9 |
| 102 | * |
| 103 | * @return int |
| 104 | */ |
| 105 | public function cache_time(): int { |
| 106 | $cache_time = self::HOUR_IN_SECONDS; |
| 107 | return $cache_time; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Helper method to print & remove message |
| 112 | * |
| 113 | * This helper method assume that $this->data has single dimensional |
| 114 | * array. For ex: [alert => warning, message => operation failed ] |
| 115 | * |
| 116 | * @since 2.1.9 |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function show() { |
| 121 | $data = $this->get_cache(); |
| 122 | if ( is_array( $data ) && count( $data ) ) { |
| 123 | tutor_closeable_alert_msg( $data['message'], $data['alert'], array(), $data['css_class'] ?? '' ); |
| 124 | |
| 125 | // Delete flash message. |
| 126 | $this->delete_cache(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 |