|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flynt\Utils\AdminNotices; |
|
4
|
|
|
|
|
5
|
|
|
// # TODO make strings translatable |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* AdminNoticeManager is to be used in other features to enable an easy-to-use interface for the display of admin notices. |
|
9
|
|
|
* |
|
10
|
|
|
* Example usage: |
|
11
|
|
|
* use Flynt\Utils\AdminNotices\AdminNoticeManager; |
|
12
|
|
|
* // get the singleton instance of the manager |
|
13
|
|
|
* $manager = AdminNoticeManager::getInstance(); |
|
14
|
|
|
* |
|
15
|
|
|
* // Prepare the admin notice |
|
16
|
|
|
* $message = ['This notice will show up in the admin backend.']; |
|
17
|
|
|
* $options = [ |
|
18
|
|
|
* 'type' => 'info', |
|
19
|
|
|
* 'title' => 'Flynt Notice', |
|
20
|
|
|
* 'dismissible' => true, |
|
21
|
|
|
* 'filenames' => 'related-file.php, another.json' |
|
22
|
|
|
* ]; |
|
23
|
|
|
*/ |
|
24
|
|
|
class AdminNoticeManager |
|
25
|
|
|
{ |
|
26
|
|
|
protected static $instance = null; |
|
27
|
|
|
protected static $notices = []; |
|
28
|
|
|
|
|
29
|
|
|
const DEFAULT_OPTIONS = [ |
|
30
|
|
|
'type' => 'info', |
|
31
|
|
|
'title' => 'Flynt - Oops, something went wrong', |
|
32
|
|
|
'dismissible' => true, |
|
33
|
|
|
'filenames' => '' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public static function getInstance() |
|
37
|
|
|
{ |
|
38
|
|
|
if (null === self::$instance) { |
|
39
|
|
|
self::$instance = new self; |
|
40
|
|
|
} |
|
41
|
|
|
return self::$instance; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* clone |
|
46
|
|
|
* |
|
47
|
|
|
* Prevent cloning with 'protected' keyword |
|
48
|
|
|
**/ |
|
49
|
|
|
protected function __clone() |
|
50
|
|
|
{ |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* constructor |
|
55
|
|
|
* |
|
56
|
|
|
* Prevent instantiation with 'protected' keyword |
|
57
|
|
|
**/ |
|
58
|
|
|
protected function __construct() |
|
59
|
|
|
{ |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function addNotice($messages = [], $options = []) |
|
63
|
|
|
{ |
|
64
|
|
|
if (empty($messages)) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$options = array_merge(self::DEFAULT_OPTIONS, $options); |
|
69
|
|
|
|
|
70
|
|
|
$cssClasses = 'notice'; |
|
71
|
|
|
$cssClasses .= $options['dismissible'] ? ' is-dismissible' : ''; |
|
72
|
|
|
$cssClasses .= !empty($options['type']) ? " notice-{$options['type']}" : ''; |
|
73
|
|
|
|
|
74
|
|
|
$files = !empty($options['filenames']) ? " ({$options['filenames']}) " : ' '; |
|
75
|
|
|
|
|
76
|
|
|
$msg = ''; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($messages as $message) { |
|
79
|
|
|
$msg .= '<p>' . $message . '</p>'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$msg .= '<p><i>To resolve this issue either follow the steps above' |
|
83
|
|
|
. " or remove the Feature{$files}requiring this functionality in your theme.</i></p>"; |
|
84
|
|
|
$msg = "<div class=\"{$cssClasses}\">" |
|
85
|
|
|
. "<p><strong>{$options['title']}</strong></p>" |
|
86
|
|
|
. $msg . '</div>'; |
|
87
|
|
|
|
|
88
|
|
|
add_action('admin_notices', function () use ($msg) { |
|
89
|
|
|
echo $msg; |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
array_push(self::$notices, [ |
|
93
|
|
|
'options' => $options, |
|
94
|
|
|
'message' => $msg |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getAll() |
|
99
|
|
|
{ |
|
100
|
|
|
return self::$notices; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|