NoticeController.php
9.33 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace EnableMediaReplace\Notices;
use EnableMediaReplace\ShortPixelLogger\ShortPixelLogger as Log;
class NoticeController //extends ShortPixelController
{
protected static $notices = array();
protected static $instance = null;
protected static $cssHookLoaded = false; // prevent css output more than once.
protected $notice_displayed = array();
public $notice_count = 0;
protected $has_stored = false;
protected $notice_option = ''; // The wp_options name for notices here.
/** For backward compat. Never call constructor directly. */
public function __construct()
{
$ns = __NAMESPACE__;
$ns = substr($ns, 0, strpos($ns, '\\')); // try to get first part of namespace
$this->notice_option = $ns . '-notices';
add_action('wp_ajax_' . $this->notice_option, array($this, 'ajax_action'));
$this->loadNotices();
//$this->loadConfig();
}
public static function getInstance()
{
if ( self::$instance === null)
{
self::$instance = new NoticeController();
}
return self::$instance;
}
/** Reset all notices, before loading them, to ensure on updates / activations one starts fresh */
public static function resetNotices()
{
$ns = __NAMESPACE__;
$ns = substr($ns, 0, strpos($ns, '\\')); // try to get first part of namespace
$result = delete_option($ns . '-notices');
}
/** Load Notices Config File, if any
*
* [ Future Use ]
*/
public function loadConfig()
{
return;
if (file_exists('../notice_config.json'))
{
$config = file_get_contents('../notice_config.json');
$json_config = json_decode($config);
}
}
public function loadIcons($icons)
{
foreach($icons as $name => $icon)
NoticeModel::setIcon($name, $icon);
}
protected function loadNotices()
{
$notices = get_option($this->notice_option, false);
$cnotice = (is_array($notices)) ? count($notices) : 0;
if ($notices !== false && is_array($notices))
{
$checked = array();
foreach($notices as $noticeObj)
{
if (is_object($noticeObj) && $noticeObj instanceOf NoticeModel)
{
$checked[] = $noticeObj;
}
}
self::$notices = $checked;
$this->has_stored = true;
}
else {
self::$notices = array();
$this->has_stored = false;
}
$this->countNotices();
}
protected function addNotice($message, $code, $unique)
{
$notice = new NoticeModel($message, $code);
if ($unique)
{
foreach(self::$notices as $nitem)
{
if ($nitem->message == $notice->message && $nitem->code == $notice->code) // same message.
return $nitem; // return the notice with the same message.
}
}
self::$notices[] = $notice;
$this->countNotices();
$this->update();
return $notice;
}
/** Update the notices to store, check what to remove, returns count. */
public function update()
{
if (! is_array(self::$notices) || count(self::$notices) == 0)
{
if ($this->has_stored)
delete_option($this->notice_option);
return 0;
}
$new_notices = array();
foreach(self::$notices as $item)
{
if (! $item->isDone() )
{
$new_notices[] = $item;
}
}
update_option($this->notice_option, $new_notices);
self::$notices = $new_notices;
return $this->countNotices();
}
public function countNotices()
{
$this->notice_count = count(self::$notices);
return $this->notice_count;
}
public function getNotices()
{
return self::$notices;
}
public function getNoticesForDisplay()
{
$newNotices = array();
foreach(self::$notices as $notice)
{
if ($notice->isDismissed()) // dismissed never displays.
continue;
if ($notice->isPersistent())
{
$id = $notice->getID();
if (! is_null($id) && ! in_array($id, $this->notice_displayed))
{
$notice->notice_action = $this->notice_option;
$newNotices[] = $notice;
$this->notice_displayed[] = $id;
}
}
else
$newNotices[] = $notice;
}
return $newNotices;
}
public function getNoticeByID($id)
{
foreach(self::$notices as $notice)
{
if ($notice->getID() == $id)
return $notice;
}
return false;
}
public static function removeNoticeByID($id)
{
$noticeController = self::getInstance();
for($i = 0; $i < count(self::$notices); $i++)
{
$item = self::$notices[$i];
if (is_object($item) && $item->getID() == $id)
{
Log::addDebug('Removing notice with ID ' . $id);
unset(self::$notices[$i]);
}
//if ($notice_item )
}
$noticeController->update();
}
public function ajax_action()
{
$response = array('result' => false, 'reason' => '');
if (isset($_POST['nonce']) && wp_verify_nonce( sanitize_key($_POST['nonce']), 'dismiss') )
{
if (isset($_POST['plugin_action']) && 'dismiss' == $_POST['plugin_action'] )
{
$id = (isset($_POST['id'])) ? sanitize_text_field( wp_unslash($_POST['id'])) : null;
if (! is_null($id))
{
$notice = $this->getNoticeByID($id);
}
else
{
$notice = false;
}
if(false !== $notice)
{
$notice->dismiss();
$this->update();
$response['result'] = true;
}
else
{
Log::addError('Notice not found when dismissing -> ' . $id, self::$notices);
$response['result'] = false;
$response['reason'] = ' Notice ' . $id . ' not found. ';
}
}
}
else
{
Log::addError('Wrong Nonce when dismissed notice. ');
$response['reason'] = 'wrong nonce';
}
wp_send_json($response);
}
/** Adds a notice, quick and fast method
* @param String $message The Message you want to notify
* @param Boolean $unique If unique, check to not repeat notice exact same text in notices. Discard if so
* @param int $code A value of messageType as defined in model
* @returm Object Instance of noticeModel
*/
public static function addNormal($message, $unique = false)
{
$noticeController = self::getInstance();
$notice = $noticeController->addNotice($message, NoticeModel::NOTICE_NORMAL, $unique);
return $notice;
}
public static function addError($message, $unique = false)
{
$noticeController = self::getInstance();
$notice = $noticeController->addNotice($message, NoticeModel::NOTICE_ERROR, $unique);
return $notice;
}
public static function addWarning($message, $unique = false)
{
$noticeController = self::getInstance();
$notice = $noticeController->addNotice($message, NoticeModel::NOTICE_WARNING, $unique);
return $notice;
}
public static function addSuccess($message, $unique = false)
{
$noticeController = self::getInstance();
$notice = $noticeController->addNotice($message, NoticeModel::NOTICE_SUCCESS, $unique);
return $notice;
}
public static function addDetail($notice, $detail)
{
$noticeController = self::getInstance();
$notice->addDetail($detail);
// $notice_id = spl_object_id($notice);
$noticeController->update();
}
/** Make a regular notice persistent across multiple page loads
* @param $notice NoticeModel The Notice to make Persistent
* @param $key String Identifier of the persistent notice.
* @param $suppress Int When dismissed, time to stay dismissed
* @param $callback Function Callable function
*/
public static function makePersistent($notice, $key, $suppress = -1, $callback = null)
{
$noticeController = self::getInstance();
$existing = $noticeController->getNoticeByID($key);
// if this key already exists, don't allow the new notice to be entered into the array. Remove it since it's already created.
if ($existing)
{
for($i = 0; $i < count(self::$notices); $i++)
{
$item = self::$notices[$i];
if ($item->message == $notice->message && $item->getID() == null)
{
if ($item->message != $existing->message) // allow the persistent message to be updated, if something else is served on this ID
{
$existing->message = $item->message;
}
unset(self::$notices[$i]);
}
//if ($notice_item )
}
}
else
{
$notice->setPersistent($key, $suppress, $callback); // set this notice persistent.
}
$noticeController->update();
}
public function admin_notices()
{
if ($this->countNotices() > 0)
{
if (! self::$cssHookLoaded)
{
add_action('admin_print_footer_scripts', array($this, 'printNoticeStyle'));
self::$cssHookLoaded = true;
}
foreach($this->getNoticesForDisplay() as $notice)
{
echo $notice->getForDisplay();
}
}
$this->update(); // puts views, and updates
}
public function printNoticeStyle()
{
if (file_exists(__DIR__ . '/css/notices.css'))
{
echo '<style>' . esc_html(file_get_contents(__DIR__ . '/css/notices.css')) . '</style>';
}
else {
Log::addDebug('Notices : css/notices.css could not be loaded');
}
}
}