|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\notices; |
|
4
|
|
|
|
|
5
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AdminNotice |
|
11
|
|
|
* generates WordPress admin notices from EventEspresso\core\services\notices\Notice objects |
|
12
|
|
|
* |
|
13
|
|
|
* @package EventEspresso\core\services\notices |
|
14
|
|
|
* @author Brent Christensen |
|
15
|
|
|
* @since 4.9.47.rc.041 |
|
16
|
|
|
*/ |
|
17
|
|
|
class AdminNotice |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
const ERROR = 'notice-error'; |
|
21
|
|
|
|
|
22
|
|
|
const WARNING = 'notice-warning'; |
|
23
|
|
|
|
|
24
|
|
|
const SUCCESS = 'notice-success'; |
|
25
|
|
|
|
|
26
|
|
|
const INFORMATION = 'notice-info'; |
|
27
|
|
|
|
|
28
|
|
|
const DISMISSABLE = ' is-dismissible'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* generic system notice to be converted into a WP admin notice |
|
32
|
|
|
* |
|
33
|
|
|
* @var NoticeInterface $notice |
|
34
|
|
|
*/ |
|
35
|
|
|
private $notice; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* AdminNotice constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param NoticeInterface $notice |
|
42
|
|
|
* @param bool $display_now |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(NoticeInterface $notice, $display_now = true) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->notice = $notice; |
|
47
|
|
|
if (! did_action('admin_notices')) { |
|
48
|
|
|
add_action('admin_notices', array($this, 'displayNotice')); |
|
49
|
|
|
} elseif ($display_now) { |
|
50
|
|
|
$this->displayNotice(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function displayNotice() |
|
59
|
|
|
{ |
|
60
|
|
|
echo $this->getNotice(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* produces something like: |
|
66
|
|
|
* <div class="notice notice-success is-dismissible event-espresso-admin-notice"> |
|
67
|
|
|
* <p>YOU DID IT!</p> |
|
68
|
|
|
* <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this |
|
69
|
|
|
* notice.</span></button> |
|
70
|
|
|
* </div> |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getNotice() |
|
75
|
|
|
{ |
|
76
|
|
|
return sprintf( |
|
77
|
|
|
'<div class="notice %1$s%2$s event-espresso-admin-notice"><p>%3$s</p></div>', |
|
78
|
|
|
$this->getType(), |
|
79
|
|
|
$this->notice->isDismissible() ? AdminNotice::DISMISSABLE : '', |
|
80
|
|
|
$this->getMessage() |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getType() |
|
89
|
|
|
{ |
|
90
|
|
|
switch ($this->notice->type()) { |
|
91
|
|
|
case Notice::ERROR : |
|
92
|
|
|
return AdminNotice::ERROR; |
|
93
|
|
|
break; |
|
|
|
|
|
|
94
|
|
|
case Notice::ATTENTION : |
|
95
|
|
|
return AdminNotice::WARNING; |
|
96
|
|
|
break; |
|
|
|
|
|
|
97
|
|
|
case Notice::SUCCESS : |
|
98
|
|
|
return AdminNotice::SUCCESS; |
|
99
|
|
|
break; |
|
|
|
|
|
|
100
|
|
|
case Notice::INFORMATION : |
|
101
|
|
|
default: |
|
102
|
|
|
return AdminNotice::INFORMATION; |
|
103
|
|
|
break; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getMessage() |
|
112
|
|
|
{ |
|
113
|
|
|
$message = $this->notice->message(); |
|
114
|
|
|
if (WP_DEBUG && $this->getType() === AdminNotice::ERROR) { |
|
115
|
|
|
$message .= '<br/><span class="tiny-text">' . $this->generateErrorCode() . '</span>'; |
|
116
|
|
|
} |
|
117
|
|
|
return $message; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* create error code from filepath, function name, |
|
123
|
|
|
* and line number where notice was generated |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function generateErrorCode() |
|
128
|
|
|
{ |
|
129
|
|
|
$file = explode('.', basename($this->notice->file())); |
|
130
|
|
|
$error_code = ! empty($file[0]) ? $file[0] : ''; |
|
131
|
|
|
$error_code .= ! empty($error_code) ? ' - ' . $this->notice->func() : $this->notice->func(); |
|
132
|
|
|
$error_code .= ' - ' . $this->notice->line(); |
|
133
|
|
|
return $error_code; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.