forked from awesomemotive/WP-Mail-SMTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wpms-am-notification.php
455 lines (400 loc) · 12.8 KB
/
class-wpms-am-notification.php
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* Awesome Motive Notifications.
*
* This creates a custom post type (if it doesn't exist) and calls the API to
* retrieve notifications for this product.
*
* @package AwesomeMotive
* @author AwesomeMotive Team
* @license GPL-2.0+
* @copyright Copyright (c) 2018, Awesome Motive LLC
* @version 1.0.7
*/
class WPMS_AM_Notification {
/**
* The api url we are calling.
*
* @since 1.0.0
*
* @var string
*/
public $api_url = 'https://api.awesomemotive.com/v1/notification/';
/**
* A unique slug for this plugin.
* (Not the WordPress plugin slug)
*
* @since 1.0.0
*
* @var string
*/
public $plugin;
/**
* The current plugin version.
*
* @since 1.0.0
*
* @var string
*/
public $plugin_version;
/**
* Flag if a notice has been registered.
*
* @since 1.0.0
*
* @var bool
*/
public static $registered = false;
/**
* Construct.
*
* @since 1.0.0
*
* @param string $plugin The plugin slug.
* @param mixed $version The version of the plugin.
*/
public function __construct( $plugin = '', $version = 0 ) {
$this->plugin = $plugin;
$this->plugin_version = $version;
add_action( 'init', array( $this, 'custom_post_type' ) );
add_action( 'admin_init', array( $this, 'get_remote_notifications' ), 100 );
add_action( 'admin_notices', array( $this, 'display_notifications' ) );
add_action( 'wp_ajax_am_notification_dismiss', array( $this, 'dismiss_notification' ) );
}
/**
* Registers a custom post type.
*
* @since 1.0.0
*/
public function custom_post_type() {
register_post_type( 'amn_' . $this->plugin, array(
'label' => $this->plugin . ' Announcements',
'can_export' => false,
'supports' => false,
'capability_type' => 'manage_options',
) );
}
/**
* Retrieve the remote notifications if the time has expired.
*
* @since 1.0.0
*/
public function get_remote_notifications() {
if ( ! apply_filters( 'am_notifications_display', is_super_admin() ) ) {
return;
}
$last_checked = get_option( '_amn_' . $this->plugin . '_last_checked', strtotime( '-1 week' ) );
if ( $last_checked < strtotime( 'today midnight' ) ) {
$plugin_notifications = $this->get_plugin_notifications( 1 );
$notification_id = null;
if ( ! empty( $plugin_notifications ) ) {
// Unset it from the array.
$notification = $plugin_notifications[0];
$notification_id = get_post_meta( $notification->ID, 'notification_id', true );
}
$response = wp_remote_retrieve_body( wp_remote_post( $this->api_url, array(
'body' => array(
'slug' => $this->plugin,
'version' => $this->plugin_version,
'last_notification' => $notification_id,
),
) ) );
$data = json_decode( $response );
if ( ! empty( $data->id ) ) {
$notifications = array();
foreach ( (array) $data->slugs as $slug ) {
$notifications = array_merge(
$notifications,
(array) get_posts(
array(
'post_type' => 'amn_' . $slug,
'post_status' => 'all',
'meta_key' => 'notification_id',
'meta_value' => $data->id,
)
)
);
}
if ( empty( $notifications ) ) {
$new_notification_id = wp_insert_post(
array(
'post_content' => wp_kses_post( $data->content ),
'post_type' => 'amn_' . $this->plugin,
)
);
update_post_meta( $new_notification_id, 'notification_id', absint( $data->id ) );
update_post_meta( $new_notification_id, 'type', sanitize_text_field( trim( $data->type ) ) );
update_post_meta( $new_notification_id, 'dismissable', (bool) $data->dismissible ? 1 : 0 );
update_post_meta( $new_notification_id, 'location', function_exists( 'wp_json_encode' ) ? wp_json_encode( $data->location ) : json_encode( $data->location ) );
update_post_meta( $new_notification_id, 'version', sanitize_text_field( trim( $data->version ) ) );
update_post_meta( $new_notification_id, 'viewed', 0 );
update_post_meta( $new_notification_id, 'expiration', $data->expiration ? absint( $data->expiration ) : false );
update_post_meta( $new_notification_id, 'plans', function_exists( 'wp_json_encode' ) ? wp_json_encode( $data->plans ) : json_encode( $data->plans ) );
}
}
// Possibly revoke notifications.
if ( ! empty( $data->revoked ) ) {
$this->revoke_notifications( $data->revoked );
}
// Set the option now so we can't run this again until after 24 hours.
update_option( '_amn_' . $this->plugin . '_last_checked', strtotime( 'today midnight' ) );
}
}
/**
* Get local plugin notifications that have already been set.
*
* @since 1.0.0
*
* @param integer $limit Set the limit for how many posts to retrieve.
* @param array $args Any top-level arguments to add to the array.
*
* @return WP_Post[] WP_Post that match the query.
*/
public function get_plugin_notifications( $limit = - 1, $args = array() ) {
return get_posts(
array(
'posts_per_page' => $limit,
'post_type' => 'amn_' . $this->plugin,
) + $args
);
}
/**
* Display any notifications that should be displayed.
*
* @since 1.0.0
*/
public function display_notifications() {
if ( ! apply_filters( 'am_notifications_display', is_super_admin() ) ) {
return;
}
$plugin_notifications = $this->get_plugin_notifications( - 1, array(
'post_status' => 'all',
'meta_key' => 'viewed',
'meta_value' => '0',
) );
$plugin_notifications = $this->validate_notifications( $plugin_notifications );
if ( ! empty( $plugin_notifications ) && ! self::$registered ) {
foreach ( $plugin_notifications as $notification ) {
$dismissable = get_post_meta( $notification->ID, 'dismissable', true );
$type = get_post_meta( $notification->ID, 'type', true );
?>
<div class="am-notification am-notification-<?php echo absint( $notification->ID ); ?> notice notice-<?php echo esc_attr( $type ); ?><?php echo $dismissable ? ' is-dismissible' : ''; ?>">
<?php echo wp_kses_post( $notification->post_content ); ?>
</div>
<script type="text/javascript">
jQuery( document ).ready( function ( $ ) {
$( document ).on( 'click', '.am-notification-<?php echo absint( $notification->ID ); ?> button.notice-dismiss', function ( event ) {
$.post( ajaxurl, {
action: 'am_notification_dismiss',
notification_id: '<?php echo absint( $notification->ID ); ?>'
} );
} );
} );
</script>
<?php
}
self::$registered = true;
}
}
/**
* Validate the notifications before displaying them.
*
* @since 1.0.0
*
* @param array $plugin_notifications An array of plugin notifications.
*
* @return array A filtered array of plugin notifications.
*/
public function validate_notifications( $plugin_notifications ) {
global $pagenow;
foreach ( $plugin_notifications as $key => $notification ) {
// Location validation.
$location = (array) json_decode( get_post_meta( $notification->ID, 'location', true ) );
$continue = false;
if ( ! in_array( 'everywhere', $location, true ) ) {
if ( in_array( 'index.php', $location, true ) && 'index.php' === $pagenow ) {
$continue = true;
}
if ( in_array( 'plugins.php', $location, true ) && 'plugins.php' === $pagenow ) {
$continue = true;
}
if ( ! $continue ) {
unset( $plugin_notifications[ $key ] );
}
}
// Plugin validation (OR conditional).
$plugins = (array) json_decode( get_post_meta( $notification->ID, 'plugins', true ) );
$continue = false;
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $plugin ) {
if ( is_plugin_active( $plugin ) ) {
$continue = true;
}
}
if ( ! $continue ) {
unset( $plugin_notifications[ $key ] );
}
}
// Theme validation.
$theme = get_post_meta( $notification->ID, 'theme', true );
$continue = (string) wp_get_theme() === $theme;
if ( ! empty( $theme ) && ! $continue ) {
unset( $plugin_notifications[ $key ] );
}
// Version validation.
$version = get_post_meta( $notification->ID, 'version', true );
$continue = false;
if ( ! empty( $version ) ) {
if ( version_compare( $this->plugin_version, $version, '<=' ) ) {
$continue = true;
}
if ( ! $continue ) {
unset( $plugin_notifications[ $key ] );
}
}
// Expiration validation.
$expiration = get_post_meta( $notification->ID, 'expiration', true );
$continue = false;
if ( ! empty( $expiration ) ) {
if ( $expiration > time() ) {
$continue = true;
}
if ( ! $continue ) {
unset( $plugin_notifications[ $key ] );
}
}
// Plan validation.
$plans = (array) json_decode( get_post_meta( $notification->ID, 'plans', true ) );
$continue = false;
if ( ! empty( $plans ) ) {
$level = $this->get_plan_level();
if ( in_array( $level, $plans, true ) ) {
$continue = true;
}
if ( ! $continue ) {
unset( $plugin_notifications[ $key ] );
}
}
}
return $plugin_notifications;
}
/**
* Grab the current plan level.
*
* @since 1.0.0
*
* @return string The current plan level.
*/
public function get_plan_level() {
// Prepare variables.
$key = '';
$level = '';
switch ( $this->plugin ) {
case 'wpforms':
$option = get_option( 'wpforms_license' );
$key = is_array( $option ) && isset( $option['key'] ) ? $option['key'] : '';
$level = is_array( $option ) && isset( $option['type'] ) ? $option['type'] : '';
// Possibly check for a constant.
if ( empty( $key ) && defined( 'WPFORMS_LICENSE_KEY' ) ) {
$key = WPFORMS_LICENSE_KEY;
}
break;
case 'mi-lite':
case 'mi':
if ( version_compare( MONSTERINSIGHTS_VERSION, '6.9.0', '>=' ) ) {
if ( MonsterInsights()->license->get_site_license_type() ) {
$key = MonsterInsights()->license->get_site_license_key();
$type = MonsterInsights()->license->get_site_license_type();
} else if ( MonsterInsights()->license->get_network_license_type() ) {
$key = MonsterInsights()->license->get_network_license_key();
$type = MonsterInsights()->license->get_network_license_type();
}
// Check key fallbacks
if ( empty( $key ) ) {
$key = MonsterInsights()->license->get_license_key();
}
} else {
$option = get_option( 'monsterinsights_license' );
$key = is_array( $option ) && isset( $option['key'] ) ? $option['key'] : '';
$level = is_array( $option ) && isset( $option['type'] ) ? $option['type'] : '';
// Possibly check for a constant.
if ( empty( $key ) && defined( 'MONSTERINSIGHTS_LICENSE_KEY' ) && is_string( MONSTERINSIGHTS_LICENSE_KEY ) && strlen( MONSTERINSIGHTS_LICENSE_KEY ) > 10 ) {
$key = MONSTERINSIGHTS_LICENSE_KEY;
}
}
break;
case 'om':
$option = get_option( 'optin_monster_api' );
$key = is_array( $option ) && isset( $option['api']['apikey'] ) ? $option['api']['apikey'] : '';
// Possibly check for a constant.
if ( empty( $key ) && defined( 'OPTINMONSTER_REST_API_LICENSE_KEY' ) ) {
$key = OPTINMONSTER_REST_API_LICENSE_KEY;
}
// If the key is still empty, check for the old legacy key.
if ( empty( $key ) ) {
$key = is_array( $option ) && isset( $option['api']['key'] ) ? $option['api']['key'] : '';
}
break;
}
// Possibly set the level to 'none' if the key is empty and no level has been set.
if ( empty( $key ) && empty( $level ) ) {
$level = 'none';
}
// Possibly set the level to 'unknown' if a key is entered, but no level can be determined (such as manually entered key)
if ( ! empty( $key ) && empty( $level ) ) {
$level = 'unknown';
}
// Normalize the level.
switch ( $level ) {
case 'bronze':
case 'personal':
$level = 'basic';
break;
case 'silver':
case 'multi':
$level = 'plus';
break;
case 'gold':
case 'developer':
$level = 'pro';
break;
case 'platinum':
case 'master':
$level = 'ultimate';
break;
}
// Return the plan level.
return $level;
}
/**
* Dismiss the notification via AJAX.
*
* @since 1.0.0
*/
public function dismiss_notification() {
if ( ! apply_filters( 'am_notifications_display', is_super_admin() ) ) {
die;
}
$notification_id = intval( $_POST['notification_id'] );
update_post_meta( $notification_id, 'viewed', 1 );
die;
}
/**
* Revokes notifications.
*
* @since 1.0.0
*
* @param array $ids An array of notification IDs to revoke.
*/
public function revoke_notifications( $ids ) {
// Loop through each of the IDs and find the post that has it as meta.
foreach ( (array) $ids as $id ) {
$notifications = $this->get_plugin_notifications( - 1, array( 'post_status' => 'all', 'meta_key' => 'notification_id', 'meta_value' => $id ) );
if ( $notifications ) {
foreach ( $notifications as $notification ) {
update_post_meta( $notification->ID, 'viewed', 1 );
}
}
}
}
}