-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAdminNotices.php
More file actions
549 lines (460 loc) · 19.4 KB
/
AdminNotices.php
File metadata and controls
549 lines (460 loc) · 19.4 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
namespace Cleantalk\ApbctWP;
use Cleantalk\ApbctWP\Variables\Get;
use Cleantalk\ApbctWP\Variables\Post;
use Cleantalk\Common\UniversalBanner\BannerDataDto;
use Cleantalk\ApbctWP\ServerRequirementsChecker\ServerRequirementsChecker;
use Cleantalk\Common\TT;
use Cleantalk\ApbctWP\LinkConstructor;
class AdminNotices
{
/*
* The time interval in which notifications hidden by the user are not displayed
*/
const DAYS_INTERVAL_HIDING_NOTICE = 14;
/*
* The time interval in which the review notification will be hidden for user
*/
const DAYS_INTERVAL_HIDING_REVIEW_NOTICE = 90;
/**
* @var null|AdminNotices
*/
private static $instance;
/**
* @var array
*/
const NOTICES = array(
'notice_key_is_incorrect',
'notice_key_is_empty',
'notice_get_key_error',
'notice_trial',
'notice_renew',
'notice_incompatibility',
'notice_review',
'notice_email_decoder_changed',
'notice_server_requirements',
);
/**
* @var State
*/
private $apbct;
/**
* @var bool
*/
private $is_cleantalk_page;
/**
* @var string
*/
private $settings_link;
/**
* @var string
*/
private $user_token;
/**
* AdminNotices constructor.
*/
private function __construct()
{
global $apbct;
$this->apbct = $apbct;
$this->is_cleantalk_page = Get::get('page') &&
in_array(Get::get('page'), array('cleantalk', 'ct_check_spam', 'ct_check_users'));
$this->user_token = $this->apbct->user_token ?: '';
$self_owned_key = $this->apbct->moderate_ip == 0 && !$this->apbct->constants->self_owned_access_key->isDefinedAndTypeOK();
$is_dashboard = is_network_admin() || is_admin();
$is_admin = current_user_can('activate_plugins');
$uid = get_current_user_id();
if ( $self_owned_key && $is_dashboard && $is_admin ) {
if ( defined('DOING_AJAX') ) {
add_action('wp_ajax_cleantalk_dismiss_notice', array($this, 'setNoticeDismissed'));
} else {
foreach ( self::NOTICES as $notice ) {
$notice_uid = $notice . '_' . $uid;
// Notice "review" not need to show everytime in the plugin settings page
if ( $notice === 'notice_review' && $this->isDismissedNotice($notice_uid) ) {
continue;
}
if ( $this->is_cleantalk_page || ! $this->isDismissedNotice($notice_uid) ) {
add_action('admin_notices', array($this, $notice));
add_action('network_admin_notices', array($this, $notice));
}
}
add_filter('cleantalk_admin_bar__parent_node__after', array($this, 'addAttentionMark'), 20, 1);
}
}
}
/**
* Get singleton instance of AdminNotices
*
* @return AdminNotices
*/
private static function getInstance()
{
if ( is_null(self::$instance) ) {
self::$instance = new static();
}
return self::$instance;
}
/**
* Initialize method
*/
public static function showAdminNotices()
{
$admin_notices = self::getInstance();
if ( is_network_admin() ) {
$site_url = get_site_option('siteurl');
$site_url = preg_match('/\/$/', $site_url) ? $site_url : $site_url . '/';
$admin_notices->settings_link = $site_url . 'wp-admin/options-general.php?page=cleantalk';
} else {
$admin_notices->settings_link = 'options-general.php?page=cleantalk';
}
}
/**
* Callback for the notice hook
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_get_key_error() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if (isset($this->apbct->errors['key_get']) &&
!empty($this->apbct->errors['key_get']) && !$this->apbct->white_label
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'get_key_error';
$banner_data->text = __("Unable to get Access key automatically:", 'cleantalk-spam-protect');
$banner_data->secondary_text = end($this->apbct->errors['key_get'])['error'];
$banner_data->button_url = LinkConstructor::buildCleanTalkLink('get_access_key_link', 'wordpress-anti-spam-plugin') .
'&platform=wordpress&email=' . urlencode(ct_get_admin_email()) .
'&website=' . urlencode(get_option('home'));
$banner_data->button_text = __('Get the Access key', 'cleantalk-spam-protect');
$banner_data->level = 'error';
$banner_data->is_dismissible = ! $this->is_cleantalk_page;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
}
}
/**
* Callback for the notice hook
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_key_is_empty() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ( ! $this->apbct->white_label &&
empty($this->apbct->api_key) &&
$this->apbct->moderate_ip == 0
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'key_is_empty';
$banner_data->text = sprintf(
__("Please enter the Access Key in %s plugin to enable spam protection!", 'cleantalk-spam-protect'),
$this->apbct->plugin_name
);
$banner_data->button_url = $this->settings_link;
$banner_data->button_text = __('Settings', 'cleantalk-spam-protect');
$banner_data->level = 'error';
$banner_data->is_dismissible = ! $this->is_cleantalk_page;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
$this->apbct->notice_show = false;
}
}
/**
* Callback for the notice hook
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_key_is_incorrect() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ( ! $this->apbct->white_label &&
! empty($this->apbct->api_key) &&
apbct_api_key__is_correct() &&
! $this->apbct->key_is_ok &&
$this->apbct->moderate_ip == 0
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'key_is_incorrect';
$banner_data->text = sprintf(
__("Access key is not valid, update the plugin settings. %s.", 'cleantalk-spam-protect'),
$this->apbct->plugin_name
);
$banner_data->button_url = $this->settings_link;
$banner_data->button_text = __('Settings', 'cleantalk-spam-protect');
$banner_data->level = 'error';
$banner_data->is_dismissible = ! $this->is_cleantalk_page;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
$this->apbct->notice_show = false;
}
}
/**
* Callback for the notice hook
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_trial() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ($this->apbct->notice_show &&
$this->apbct->notice_trial == 1 &&
$this->apbct->moderate_ip == 0 &&
! $this->apbct->white_label
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'trial';
$banner_data->text = sprintf(
__("%s trial period ends, please upgrade to next year!", 'cleantalk-spam-protect'),
$this->apbct->plugin_name
);
$banner_data->button_url = LinkConstructor::buildRenewalLink($this->user_token, 'renew_notice_trial');
$banner_data->button_text = __('Upgrade', 'cleantalk-spam-protect');
$banner_data->additional_text = sprintf(
__('Account status updates every 24 hours or click Settings -> %s -> Synchronize with Cloud.', 'cleantalk-spam-protect'),
$this->apbct->data['wl_brandname']
);
$banner_data->level = isset($this->apbct->data['notice_trial_level']) ? $this->apbct->data['notice_trial_level'] : 'error';
$banner_data->is_dismissible = ! $this->is_cleantalk_page;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
$this->apbct->notice_show = false;
}
}
/**
* Callback for the notice hook
* @deprecated
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_renew() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ($this->apbct->notice_show &&
$this->apbct->notice_renew == 1 &&
$this->apbct->moderate_ip == 0 &&
! $this->apbct->white_label
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'renew';
$banner_data->text = __("Please renew your Anti-Spam license for next year!", 'cleantalk-spam-protect');
$banner_data->button_url = LinkConstructor::buildRenewalLink($this->user_token, 'renew_notice_renew');
$banner_data->button_text = __('Upgrade', 'cleantalk-spam-protect');
$banner_data->additional_text = sprintf(
__('Account status updates every 24 hours or click Settings -> %s -> Synchronize with Cloud.', 'cleantalk-spam-protect'),
$this->apbct->data['wl_brandname']
);
$banner_data->level = isset($this->apbct->data['notice_renew_level']) ? $this->apbct->data['notice_renew_level'] : 'error';
$banner_data->is_dismissible = ! $this->is_cleantalk_page;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
$this->apbct->notice_show = false;
}
}
/**
* Callback for the notice hook
* @deprecated
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_review() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ($this->apbct->notice_review == 1 &&
! $this->apbct->white_label
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'review';
$banner_data->text = sprintf(
__("Help others to fight spam with %s – leave your feedback!", 'cleantalk-spam-protect'),
$this->apbct->data['wl_brandname']
);
$banner_data->button_url = 'https://wordpress.org/support/plugin/cleantalk-spam-protect/reviews/?filter=5';
$banner_data->button_text = __('SHARE YOUR FEEDBACK', 'cleantalk-spam-protect');
$support_link = '<a href="https://wordpress.org/support/plugin/cleantalk-spam-protect/"
style="display:inline-block; margin-top: 10px;" target="_blank">'
. __('Still have spam?', 'cleantalk-spam-protect')
. '</a>';
$close_link = '<a href="#" class="notice-dismiss-link" onclick="return false;">'
. __('Already posted the review', 'cleantalk-spam-protect')
. '</a>';
$banner_data->additional_text = $support_link . ' ' . $close_link;
$banner_data->level = isset($this->apbct->data['notice_review_level']) ? $this->apbct->data['notice_review_level'] : 'success';
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
}
}
/**
* Callback for the notice hook
* @psalm-suppress PossiblyUnusedMethod, PossiblyUndefinedStringArrayOffset
*/
public function notice_incompatibility() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
global $apbct;
if ( ! empty($this->apbct->data['notice_incompatibility']) &&
$this->is_cleantalk_page &&
$this->apbct->settings['sfw__enabled']
) {
foreach ( $this->apbct->data['notice_incompatibility'] as $notice ) {
$banner_data = new BannerDataDto();
$banner_data->type = 'incompatibility';
$banner_data->text = sprintf(
__("Some plugin is not compatible with %s – instruction in the article.", 'cleantalk-spam-protect'),
$this->apbct->data['wl_brandname']
);
switch ($notice) {
case (strpos($notice, 'W3 Total Cache') !== false):
$banner_data->button_url = 'https://cleantalk.org/help/cleantalk-and-w3-total-cache';
break;
default:
$banner_data->button_url = '';
break;
}
$banner_data->button_text = __('Read the article', 'cleantalk-spam-protect');
$banner_data->additional_text = $notice;
$banner_data->level = 'error';
$banner_data->is_dismissible = ! $this->is_cleantalk_page;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
}
} else {
$apbct->data['notice_incompatibility'] = array();
$apbct->saveData();
}
}
/**
* Callback for the notice hook
* @psalm-suppress PossiblyUnusedMethod
*/
public function notice_email_decoder_changed() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
global $apbct;
if ($apbct->data['notice_email_decoder_changed'] &&
$this->is_cleantalk_page && apbct_is_cache_plugins_exists()
) {
$banner_data = new BannerDataDto();
$banner_data->type = 'email_decoder_changed';
$banner_data->text = __("Need to clear the cache", 'cleantalk-spam-protect');
$banner_data->additional_text = __('You have changed the "Encode contact data" setting. If you use caching plugins, then you need to clear the cache.', 'cleantalk-spam-protect');
$banner_data->level = 'info';
$banner_data->is_show_button = false;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
}
}
/**
* Show a banner if server requirements are not met
* @psalm-suppress PossiblyUnusedMethod
* @return void
*/
public function notice_server_requirements() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ( ! $this->is_cleantalk_page ) {
return;
}
$uid = get_current_user_id();
$notice_uid = 'notice_server_requirements_' . $uid;
if ($this->isDismissedNotice($notice_uid)) {
return;
}
$server_checker = new ServerRequirementsChecker();
$warnings = $server_checker->checkRequirements();
if (!empty($warnings)) {
$link = LinkConstructor::buildCleanTalkLink(
'notice_server_requirements',
'help/system-requirements-for-anti-spam-and-security '
);
$body = __('Some server settings are incompatible', 'cleantalk-spam-protect') . ': ';
$body .= '<ul style="margin-left:20px;list-style:disc inside;">';
foreach ($warnings as $msg) {
$body .= '<li style="margin-bottom:8px;">' . esc_html($msg) . '</li>';
}
$body .= '</ul>';
$body .= sprintf(
'<a href="%s">%s</a>',
$link,
__('Instructions for solving the compatibility issue', 'cleantalk-spam-protect')
);
$banner_data = new BannerDataDto();
$banner_data->type = 'server_requirements';
$banner_data->level = 'error';
$banner_data->text = __('CleanTalk Anti-Spam: Compatibility Issue Detected', 'cleantalk-spam-protect');
$banner_data->additional_text = $body;
$banner_data->is_dismissible = true;
$banner = new ApbctUniversalBanner($banner_data);
$banner->echoBannerBody();
}
}
/**
* Check dismiss status of the notice
*
* @param string $notice_uid
*
* @return bool
*/
private function isDismissedNotice($notice_uid)
{
$option_name = 'cleantalk_' . $notice_uid . '_dismissed';
// Special for notice_review
if (is_string($notice_uid) && strpos($notice_uid, 'notice_review')) {
return $this->checkOptionExpired($option_name, self::DAYS_INTERVAL_HIDING_REVIEW_NOTICE);
}
return $this->checkOptionExpired($option_name, self::DAYS_INTERVAL_HIDING_NOTICE);
}
/**
* Check option not expired
*
* @param string $option_name
* @param int $expired_date
*
* @return bool
*/
private function checkOptionExpired($option_name, $expired_date)
{
$notice_date_option = get_option($option_name);
if ( $notice_date_option !== false && \Cleantalk\Common\Helper::dateValidate($notice_date_option) ) {
$current_date = date_create();
$notice_date = date_create($notice_date_option);
$diff = date_diff($current_date, $notice_date);
if ( $diff->days <= $expired_date ) {
return true;
}
}
return false;
}
public function setNoticeDismissed()
{
AJAXService::checkAdminNonce();
if ( ! Post::get('notice_id') ) {
wp_send_json_error(esc_html__('Wrong request.', 'cleantalk-spam-protect'));
}
global $apbct;
$notice = sanitize_text_field(TT::toString(Post::get('notice_id')));
$uid = get_current_user_id();
$notice_uid = $notice . '_' . $uid;
$current_date = current_time('Y-m-d H:i:s');
if ( in_array(str_replace('cleantalk_', '', $notice), self::NOTICES, true) ) {
if ( update_option($notice_uid . '_dismissed', $current_date) ) {
if ($notice === 'cleantalk_notice_email_decoder_changed') {
$apbct->data['notice_email_decoder_changed'] = 0;
$apbct->save('data');
}
if ( strpos($notice, 'cleantalk_notice_review') !== false ) {
$api_update = API::methodUserDataUpdate($apbct->data['user_token'], json_encode(['show_review' => 0]));
if ( isset($api_update['error']) ) {
wp_send_json_error($api_update['error']);
}
}
wp_send_json_success();
} else {
wp_send_json_error(esc_html__('Notice status not updated.', 'cleantalk-spam-protect'));
}
} else {
wp_send_json_error(esc_html__('Notice name is not allowed.', 'cleantalk-spam-protect'));
}
}
/**
* Callback for the admin-bar filtering hook
*
* @param string $after
*
* @return string
* @psalm-suppress PossiblyUnusedReturnValue
*/
public function addAttentionMark($after)
{
if ( $this->apbct->notice_show ) {
return $after . '<i class="apbct-icon-attention-alt"></i>';
}
return $after;
}
}