Notification.php
8.89 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 AIOSEO\Plugin\Common\Models;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Notification DB Model.
*
* @since 4.0.0
*/
class Notification extends Model {
/**
* The name of the table in the database, without the prefix.
*
* @since 4.0.0
*
* @var string
*/
protected $table = 'aioseo_notifications';
/**
* An array of fields to set to null if already empty when saving to the database.
*
* @since 4.0.0
*
* @var array
*/
protected $nullFields = [
'start',
'end',
'notification_id',
'notification_name',
'button1_label',
'button1_action',
'button2_label',
'button2_action'
];
/**
* Fields that should be json encoded on save and decoded on get.
*
* @since 4.0.0
*
* @var array
*/
protected $jsonFields = [ 'level' ];
/**
* Fields that should be boolean values.
*
* @since 4.0.0
*
* @var array
*/
protected $booleanFields = [ 'dismissed' ];
/**
* Fields that should be hidden when serialized.
*
* @var array
*/
protected $hidden = [ 'id' ];
/**
* An array of fields attached to this resource.
*
* @since 4.0.0
*
* @var array
*/
protected $columns = [
'id',
'slug',
'addon',
'title',
'content',
'type',
'level',
'notification_id',
'notification_name',
'start',
'end',
'button1_label',
'button1_action',
'button2_label',
'button2_action',
'dismissed',
'new',
'created',
'updated'
];
/**
* Get the list of notifications.
*
* @since 4.1.3
*
* @param boolean $reset Whether or not to reset the new notifications.
* @return array An array of notifications.
*/
public static function getNotifications( $reset = true ) {
return [
'active' => self::getAllActiveNotifications(),
'new' => self::getNewNotifications( $reset ),
'dismissed' => self::getAllDismissedNotifications()
];
}
/**
* Get an array of active notifications.
*
* @since 4.0.0
*
* @return array An array of active notifications.
*/
public static function getAllActiveNotifications() {
$staticNotifications = self::getStaticNotifications();
$notifications = array_values( json_decode( wp_json_encode( self::getActiveNotifications() ), true ) );
return ! empty( $staticNotifications ) ? array_merge( $staticNotifications, $notifications ) : $notifications;
}
/**
* Get all new notifications. After retrieving them, this will reset them.
* This means that calling this method twice will result in no results
* the second time. The only exception is to pass false as a reset variable to prevent it.
*
* @since 4.1.3
*
* @param boolean $reset Whether or not to reset the new notifications.
* @return array An array of new notifications if any exist.
*/
public static function getNewNotifications( $reset = true ) {
$notifications = self::filterNotifications(
aioseo()->core->db
->start( 'aioseo_notifications' )
->where( 'dismissed', 0 )
->where( 'new', 1 )
->whereRaw( "(start <= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR start IS NULL)" )
->whereRaw( "(end >= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR end IS NULL)" )
->orderBy( 'start DESC, created DESC' )
->run()
->models( 'AIOSEO\\Plugin\\Common\\Models\\Notification' )
);
if ( $reset ) {
self::resetNewNotifications();
}
return $notifications;
}
/**
* Resets all new notifications.
*
* @since 4.1.3
*
* @return void
*/
public static function resetNewNotifications() {
aioseo()->core->db
->update( 'aioseo_notifications' )
->where( 'new', 1 )
->set( 'new', 0 )
->run();
}
/**
* Returns all static notifications.
*
* @since 4.1.2
*
* @return array An array of static notifications.
*/
public static function getStaticNotifications() {
$staticNotifications = [];
$notifications = [
'unlicensed-addons',
'review'
];
foreach ( $notifications as $notification ) {
switch ( $notification ) {
case 'review':
// If they intentionally dismissed the main notification, we don't show the repeat one.
$originalDismissed = get_user_meta( get_current_user_id(), '_aioseo_plugin_review_dismissed', true );
if ( '4' !== $originalDismissed ) {
break;
}
$dismissed = get_user_meta( get_current_user_id(), '_aioseo_notification_plugin_review_dismissed', true );
if ( '3' === $dismissed ) {
break;
}
if ( ! empty( $dismissed ) && $dismissed > time() ) {
break;
}
$activated = aioseo()->internalOptions->internal->firstActivated( time() );
if ( $activated > strtotime( '-20 days' ) ) {
break;
}
$isV3 = get_option( 'aioseop_options' ) || get_option( 'aioseo_options_v3' );
$staticNotifications[] = [
'slug' => 'notification-' . $notification,
'component' => 'notifications-' . $notification . ( $isV3 ? '' : '2' )
];
break;
case 'unlicensed-addons':
$unlicensedAddons = aioseo()->addons->unlicensedAddons();
if ( empty( $unlicensedAddons['addons'] ) ) {
break;
}
$staticNotifications[] = [
'slug' => 'notification-' . $notification,
'component' => 'notifications-' . $notification,
'addons' => $unlicensedAddons['addons'],
'message' => $unlicensedAddons['message']
];
break;
}
}
return $staticNotifications;
}
/**
* Retrieve active notifications.
*
* @since 4.0.0
*
* @return array An array of active notifications or empty.
*/
public static function getActiveNotifications() {
return self::filterNotifications(
aioseo()->core->db
->start( 'aioseo_notifications' )
->where( 'dismissed', 0 )
->whereRaw( "(start <= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR start IS NULL)" )
->whereRaw( "(end >= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR end IS NULL)" )
->orderBy( 'start DESC, created DESC' )
->run()
->models( 'AIOSEO\\Plugin\\Common\\Models\\Notification' )
);
}
/**
* Get an array of dismissed notifications.
*
* @since 4.0.0
*
* @return array An array of dismissed notifications.
*/
public static function getAllDismissedNotifications() {
return array_values( json_decode( wp_json_encode( self::getDismissedNotifications() ), true ) );
}
/**
* Retrieve dismissed notifications.
*
* @since 4.0.0
*
* @return array An array of dismissed notifications or empty.
*/
public static function getDismissedNotifications() {
return self::filterNotifications(
aioseo()->core->db
->start( 'aioseo_notifications' )
->where( 'dismissed', 1 )
->orderBy( 'updated DESC' )
->run()
->models( 'AIOSEO\\Plugin\\Common\\Models\\Notification' )
);
}
/**
* Returns a notification by its name.
*
* @since 4.0.0
*
* @param string $name The notification name.
* @return Notification The notification.
*/
public static function getNotificationByName( $name ) {
return aioseo()->core->db
->start( 'aioseo_notifications' )
->where( 'notification_name', $name )
->run()
->model( 'AIOSEO\\Plugin\\Common\\Models\\Notification' );
}
/**
* Stores a new notification in the DB.
*
* @since 4.0.0
*
* @param array $fields The fields.
* @return Notification $notification The notification.
*/
public static function addNotification( $fields ) {
// Set the dismissed status to false.
$fields['dismissed'] = 0;
$notification = new self();
$notification->set( $fields );
$notification->save();
return $notification;
}
/**
* Deletes a notification by its name.
*
* @since 4.0.0
*
* @param string $name The notification name.
* @return void
*/
public static function deleteNotificationByName( $name ) {
aioseo()->core->db
->delete( 'aioseo_notifications' )
->where( 'notification_name', $name )
->run();
}
/**
* Filters the notifications based on the targeted plan levels.
*
* @since 4.0.0
*
* @param array $notifications The notifications
* @return array $remainingNotifications The remaining notifications.
*/
public static function filterNotifications( $notifications ) {
$remainingNotifications = [];
foreach ( $notifications as $notification ) {
// If announcements are disabled and this is an announcement, skip adding it and move on.
if (
! aioseo()->options->advanced->announcements &&
'success' === $notification->type
) {
continue;
}
// If this is an addon notification and the addon is disabled, skip adding it and move on.
if ( ! empty( $notification->addon ) && ! aioseo()->addons->getLoadedAddon( $notification->addon ) ) {
continue;
}
$levels = $notification->level;
if ( ! is_array( $levels ) ) {
$levels = empty( $notification->level ) ? [ 'all' ] : [ $notification->level ];
}
foreach ( $levels as $level ) {
if ( ! aioseo()->notices->validateType( $level ) ) {
continue 2;
}
}
$remainingNotifications[] = $notification;
}
return $remainingNotifications;
}
}