class-notices.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace HM\BackUpWordPress;
/**
* Class for managing notices
*
* Notices are messages along with an associated context, by default
* they are stored in the db and thus persist between page loads.
*/
class Notices {
/**
* The array of notice messages
*
* This is a multidimensional array of
* `array( context => array( 'message' ) );``
*
* @var array
*/
private $notices = array();
/**
* Contains the instantiated Notices instance
*
* @var Notices $this->get_instance
*/
private static $instance;
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct() {}
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*/
private function __clone() {}
/**
* Private unserialize method to prevent unserializing of the *Singleton*
* instance.
*/
private function __wakeup() {}
/**
* Returns the *Singleton* instance of this class.
*
* @staticvar Notices $instance The *Singleton* instances of this class.
*
* @return Notices The *Singleton* instance.
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof Notices ) ) {
self::$instance = new Notices();
}
return self::$instance;
}
/**
* Set an array of notice messages for a specific context
*
* @param string $context The context of the notice message
* @param array $messages The array of messages
* @param boolean $persistent Whether the notice should persist via the database. Defaults to true.
*/
public function set_notices( $context, array $messages, $persistent = true ) {
// Clear any empty messages and avoid duplicates
$messages = array_unique( array_filter( $messages ) );
if ( empty( $context ) || empty( $messages ) ) {
return false;
}
$this->notices[ $context ] = array_merge( $this->get_notices( $context ), $messages );
if ( $persistent ) {
$new_notices = $notices = $this->get_persistent_notices();
// Make sure we merge in any existing notices
if ( ! empty( $notices[ $context ] ) ) {
$new_notices[ $context ] = array_merge( $notices[ $context ], $messages );
} else {
$new_notices[ $context ] = $messages;
}
// Only update the database if the notice array has changed
if ( $new_notices !== $notices ) {
update_option( 'hmbkp_notices', $new_notices );
}
}
return true;
}
/**
* Fetch an array of notices messages
*
* If you specify a context then you'll just get messages for that context otherwise
* you get multidimensional array of all contexts and their messages.
*
* @param string $context The context that you'd like the messages for
*
* @return array The array of notice messages
*/
public function get_notices( $context = '' ) {
$notices = $this->get_all_notices();
if ( ! empty( $notices ) ) {
if ( ! empty( $context ) ) {
return isset( $notices[ $context ] ) ? $notices[ $context ] : array();
}
return $notices;
}
return array();
}
/**
* Get both standard and persistent notices
*
* @return array The array of contexts and notices
*/
private function get_all_notices() {
return array_map( 'array_unique', array_merge_recursive( $this->notices, $this->get_persistent_notices() ) );
}
/**
* Load the persistent notices from the database
*
* @return array The array of notices
*/
private function get_persistent_notices() {
$notices = get_option( 'hmbkp_notices' );
return ! empty( $notices ) ? $notices : array();
}
/**
* Clear all notices including persistent ones
*/
public function clear_all_notices() {
$this->notices = array();
delete_option( 'hmbkp_notices' );
}
}