1 | <?php |
||
11 | class Notices { |
||
12 | |||
13 | /** |
||
14 | * The array of notice messages |
||
15 | * |
||
16 | * This is a multidimensional array of |
||
17 | * `array( context => array( 'message' ) );`` |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $notices = array(); |
||
22 | |||
23 | /** |
||
24 | * Contains the instantiated Notices instance |
||
25 | * |
||
26 | * @var Notices $this->get_instance |
||
27 | */ |
||
28 | private static $instance; |
||
29 | |||
30 | /** |
||
31 | * Protected constructor to prevent creating a new instance of the |
||
32 | * *Singleton* via the `new` operator from outside of this class. |
||
33 | */ |
||
34 | protected function __construct() {} |
||
35 | |||
36 | /** |
||
37 | * Private clone method to prevent cloning of the instance of the |
||
38 | * *Singleton* instance. |
||
39 | */ |
||
40 | private function __clone() {} |
||
41 | |||
42 | /** |
||
43 | * Private unserialize method to prevent unserializing of the *Singleton* |
||
44 | * instance. |
||
45 | */ |
||
46 | private function __wakeup() {} |
||
47 | |||
48 | /** |
||
49 | * Returns the *Singleton* instance of this class. |
||
50 | * |
||
51 | * @staticvar Notices $instance The *Singleton* instances of this class. |
||
52 | * |
||
53 | * @return Notices The *Singleton* instance. |
||
54 | */ |
||
55 | public static function get_instance() { |
||
64 | |||
65 | /** |
||
66 | * Set an array of notice messages for a specific context |
||
67 | * |
||
68 | * @param string $context The context of the notice message |
||
69 | * @param array $messages The array of messages |
||
70 | * @param boolean $persistent Whether the notice should persist via the database. Defaults to true. |
||
71 | */ |
||
72 | public function set_notices( $context, array $messages, $persistent = true ) { |
||
103 | |||
104 | /** |
||
105 | * Fetch an array of notices messages |
||
106 | * |
||
107 | * If you specify a context then you'll just get messages for that context otherwise |
||
108 | * you get multidimensional array of all contexts and their messages. |
||
109 | * |
||
110 | * @param string $context The context that you'd like the messages for |
||
111 | * |
||
112 | * @return array The array of notice messages |
||
113 | */ |
||
114 | public function get_notices( $context = '' ) { |
||
115 | |||
116 | $notices = $this->get_all_notices(); |
||
117 | |||
118 | if ( ! empty( $notices ) ) { |
||
119 | |||
120 | if ( ! empty( $context ) ) { |
||
121 | return isset( $notices[ $context ] ) ? $notices[ $context ] : array(); |
||
122 | } |
||
123 | |||
124 | return $notices; |
||
125 | } |
||
126 | |||
127 | return array(); |
||
128 | |||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get both standard and persistent notices |
||
133 | * |
||
134 | * @return array The array of contexts and notices |
||
135 | */ |
||
136 | private function get_all_notices() { |
||
139 | |||
140 | /** |
||
141 | * Load the persistent notices from the database |
||
142 | * |
||
143 | * @return array The array of notices |
||
144 | */ |
||
145 | private function get_persistent_notices() { |
||
149 | |||
150 | /** |
||
151 | * Clear all notices including persistent ones |
||
152 | */ |
||
153 | public function clear_all_notices() { |
||
157 | } |
||
158 |