1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for managing notices |
7
|
|
|
* |
8
|
|
|
* Notices are messages along with an associated context, by default |
9
|
|
|
* they are stored in the db and thus persist between page loads. |
10
|
|
|
*/ |
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() { |
56
|
|
|
|
57
|
|
|
if ( ! ( self::$instance instanceof Notices ) ) { |
58
|
|
|
self::$instance = new Notices(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return self::$instance; |
62
|
|
|
|
63
|
|
|
} |
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 ) { |
73
|
|
|
|
74
|
|
|
// Clear any empty messages and avoid duplicates |
75
|
|
|
$messages = array_unique( array_filter( $messages ) ); |
76
|
|
|
|
77
|
|
|
if ( empty( $context ) || empty( $messages ) ) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->notices[ $context ] = array_merge( $this->get_notices( $context ), $messages ); |
82
|
|
|
|
83
|
|
|
if ( $persistent ) { |
84
|
|
|
|
85
|
|
|
$new_notices = $notices = $this->get_persistent_notices(); |
86
|
|
|
|
87
|
|
|
// Make sure we merge in any existing notices |
88
|
|
|
if ( ! empty( $notices[ $context ] ) ) { |
89
|
|
|
$new_notices[ $context ] = array_merge( $notices[ $context ], $messages ); |
90
|
|
|
} else { |
91
|
|
|
$new_notices[ $context ] = $messages; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Only update the database if the notice array has changed |
95
|
|
|
if ( $new_notices !== $notices ) { |
96
|
|
|
update_option( 'hmbkp_notices', $new_notices ); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
|
102
|
|
|
} |
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() { |
137
|
|
|
return array_map( 'array_unique', array_merge_recursive( $this->notices, $this->get_persistent_notices() ) ); |
138
|
|
|
} |
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() { |
146
|
|
|
$notices = get_option( 'hmbkp_notices' ); |
147
|
|
|
return ! empty( $notices ) ? $notices : array(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Clear all notices including persistent ones |
152
|
|
|
*/ |
153
|
|
|
public function clear_all_notices() { |
154
|
|
|
$this->notices = array(); |
155
|
|
|
delete_option( 'hmbkp_notices' ); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|