1
|
|
|
<?php |
2
|
|
|
namespace WPDFI\Admin; |
3
|
|
|
/** |
4
|
|
|
* This class handle all notice stuffs in admin dashboard of this plugin |
5
|
|
|
* |
6
|
|
|
* @author Duc Bui Quang <[email protected]> |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
*/ |
9
|
|
|
use WPDFI\Traits\Singleton; |
10
|
|
|
|
11
|
|
|
final class Notice |
12
|
|
|
{ |
13
|
|
|
use Singleton; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @traitDoc |
17
|
|
|
*/ |
18
|
|
|
public function initializes() |
19
|
|
|
{ |
20
|
|
|
// |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Type of notice, it can be 'success', 'error', 'warning', 'info'. |
25
|
|
|
*/ |
26
|
|
|
private $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Message of the notice |
30
|
|
|
*/ |
31
|
|
|
private $message; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add new notice to the admin dashboard. |
35
|
|
|
* |
36
|
|
|
* @param string $noticeMessage |
37
|
|
|
* @param string $noticeType |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function add($noticeMessage, $noticeType = 'info') |
42
|
|
|
{ |
43
|
|
|
$this->message = $noticeMessage; |
44
|
|
|
$this->type = $noticeType; |
45
|
|
|
|
46
|
|
|
\add_action( 'admin_notices', [$this, 'display']); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Display notice template to the admin dashboard. |
51
|
|
|
* |
52
|
|
|
* @since 1.0.0 |
53
|
|
|
* @return \VA\Templater |
54
|
|
|
*/ |
55
|
|
|
public function display() |
56
|
|
|
{ |
57
|
|
|
if(!$this->message) return; |
|
|
|
|
58
|
|
|
|
59
|
|
|
echo \wpdfi()->templater->render('admin/notice', [ |
|
|
|
|
60
|
|
|
'classes' => $this->get_classes(), |
61
|
|
|
'message' => __($this->message, 'wpdfi') |
|
|
|
|
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get all the classes of the notice |
67
|
|
|
* |
68
|
|
|
* @since 1.0.0 |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
private function get_classes() |
72
|
|
|
{ |
73
|
|
|
return "notice notice-{$this->get_type()} is-dismissible"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the type of the notice |
78
|
|
|
* |
79
|
|
|
* @since 1.0.0 |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function get_type() |
83
|
|
|
{ |
84
|
|
|
return $this->type ? $this->type : $this->get_default_type(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get default type of the notice |
89
|
|
|
* |
90
|
|
|
* @since 1.0.0 |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
private function get_default_type() |
94
|
|
|
{ |
95
|
|
|
return 'info'; |
96
|
|
|
} |
97
|
|
|
} |