WpNotices.php
6.81 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
<?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WpNotices class.
*
* @since 4.2.3
*/
class WpNotices {
/**
* Notices array
*
* @since 4.2.3
*
* @var array
*/
private $notices;
/**
* The cache key.
*
* @since 4.2.3
*
* @var string
*/
private $cacheKey = 'wp_notices';
/**
* Class Constructor.
*
* @since 4.2.3
*/
public function __construct() {
add_action( 'rest_api_init', [ $this, 'registerApiField' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueueScripts' ] );
add_action( 'admin_notices', [ $this, 'adminNotices' ] );
}
/**
* Enqueue notices scripts.
*
* @since 4.2.3
*
* @return void
*/
public function enqueueScripts() {
aioseo()->core->assets->load( 'src/vue/standalone/wp-notices/main.js' );
}
/**
* Registers an API field with notices.
*
* @since 4.2.3
*
* @return void
*/
public function registerApiField() {
foreach ( aioseo()->helpers->getPublicPostTypes( true ) as $postType ) {
register_rest_field( $postType, 'aioseo_notices', [
'get_callback' => [ $this, 'apiGetNotices' ]
] );
}
}
/**
* API field callback.
*
* @since 4.2.3
*
* @return array Notices array
*/
public function apiGetNotices() {
$notices = $this->getNoticesInContext();
// Notices show only one time.
$this->removeNotices( $notices );
return $notices;
}
/**
* Get all notices.
*
* @since 4.2.3
*
* @return array Notices array
*/
public function getNotices() {
if ( empty( $this->notices ) ) {
$this->notices = aioseo()->core->cache->get( $this->cacheKey );
}
return ! empty( $this->notices ) ? $this->notices : [];
}
/**
* Get all notices in the current context.
*
* @since 4.2.6
*
* @return array Notices array
*/
public function getNoticesInContext() {
$contextNotices = $this->getNotices();
foreach ( $contextNotices as $key => $notice ) {
if ( empty( $notice['allowedContexts'] ) ) {
continue;
}
$allowed = false;
foreach ( $notice['allowedContexts'] as $allowedContext ) {
if ( $this->isAllowedContext( $allowedContext ) ) {
$allowed = true;
break;
}
}
if ( ! $allowed ) {
unset( $contextNotices[ $key ] );
}
}
return $contextNotices;
}
/**
* Test if we are in the current context.
*
* @since 4.2.6
*
* @param string $context The context to test. (posts)
* @return bool Is the required context.
*/
private function isAllowedContext( $context ) {
switch ( $context ) {
case 'posts':
return aioseo()->helpers->isScreenPostList() ||
aioseo()->helpers->isScreenPostEdit() ||
aioseo()->helpers->isAjaxCronRestRequest();
}
return false;
}
/**
* Finds a notice by message.
*
* @since 4.2.3
*
* @param string $message The message string.
* @param string $type The message type.
* @return void|array The found notice.
*/
public function getNotice( $message, $type = '' ) {
$notices = $this->getNotices();
foreach ( $notices as $notice ) {
if ( $notice['options']['id'] === $this->getNoticeId( $message, $type ) ) {
return $notice;
}
}
}
/**
* Generates a notice id.
*
* @since 4.2.3
*
* @param string $message The message string.
* @param string $type The message type.
* @return string The notice id.
*/
public function getNoticeId( $message, $type = '' ) {
return md5( $message . $type );
}
/**
* Clear notices.
*
* @since 4.2.3
*
* @return void
*/
public function clearNotices() {
$this->notices = [];
$this->updateCache();
}
/**
* Remove certain notices.
*
* @since 4.2.6
*
* @param array $notices A list of notices to remove.
* @return void
*/
public function removeNotices( $notices ) {
foreach ( array_keys( $notices ) as $noticeKey ) {
unset( $this->notices[ $noticeKey ] );
}
$this->updateCache();
}
/**
* Adds a notice.
*
* @since 4.2.3
*
* @param string $message The message.
* @param string $status The message status [success, info, warning, error]
* @param array $options Options for the message. https://developer.wordpress.org/block-editor/reference-guides/data/data-core-notices/#createnotice
* @param array $allowedContexts The contexts where this notice will show.
* @return void
*/
public function addNotice( $message, $status = 'warning', $options = [], $allowedContexts = [] ) {
$type = ! empty( $options['type'] ) ? $options['type'] : '';
$foundNotice = $this->getNotice( $message, $type );
if ( empty( $message ) || ! empty( $foundNotice ) ) {
return;
}
$notice = [
'message' => $message,
'status' => $status,
'options' => wp_parse_args( $options, [
'id' => $this->getNoticeId( $message, $type ),
'isDismissible' => true
] ),
'allowedContexts' => $allowedContexts
];
$this->notices[] = $notice;
$this->updateCache();
}
/**
* Show notices on classic editor.
*
* @since 4.2.3
*
* @return void
*/
public function adminNotices() {
// Double check we're actually in the admin before outputting anything.
if ( ! is_admin() ) {
return;
}
$notices = $this->getNoticesInContext();
foreach ( $notices as $notice ) {
// Hide snackbar notices on classic editor.
if ( ! empty( $notice['options']['type'] ) && 'snackbar' === $notice['options']['type'] ) {
continue;
}
$status = ! empty( $notice['status'] ) ? $notice['status'] : 'warning';
$class = ! empty( $notice['options']['class'] ) ? $notice['options']['class'] : '';
?>
<div
class="notice notice-<?php echo esc_attr( $status ) ?> <?php echo esc_attr( $class ) ?>">
<?php echo '<p>' . $notice['message'] . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php
if ( ! empty( $notice['options']['actions'] ) ) {
foreach ( $notice['options']['actions'] as $action ) {
echo '<p>';
if ( ! empty( $action['url'] ) ) {
$class = ! empty( $action['class'] ) ? $action['class'] : '';
$target = ! empty( $action['target'] ) ? $action['target'] : '';
echo '<a
href="' . esc_attr( $action['url'] ) . '"
class="' . esc_attr( $class ) . '"
target="' . esc_attr( $target ) . '"
>';
}
echo $action['label']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( ! empty( $action['url'] ) ) {
echo '</a>';
}
echo '</p>';
}
?>
<?php } ?>
</div>
<?php
}
// Notices show only one time.
$this->removeNotices( $notices );
}
/**
* Helper to update the cache with the current notices array.
*
* @since 4.2.6
*
* @return void
*/
private function updateCache() {
aioseo()->core->cache->update( $this->cacheKey, $this->notices );
}
}