|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift\Cache\Ttl_Cache; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @since 3.28.0 |
|
9
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Key_Validation_Notice { |
|
12
|
|
|
|
|
13
|
|
|
const CACHE_KEY = 'is_key_valid'; |
|
14
|
|
|
|
|
15
|
|
|
const KEY_VALIDATION_NONCE_ACTION = 'wl_key_validation_notice_nonce'; |
|
16
|
|
|
|
|
17
|
|
|
const KEY_VALIDATION_NONCE_PARAM = '_wl_key_validation_notice_nonce'; |
|
18
|
|
|
|
|
19
|
|
|
const KEY_VALIDATION_NOTICE_PARAM = 'wl_key_validation_notice'; |
|
20
|
|
|
|
|
21
|
|
|
const NOTIFICATION_OPTION_KEY = 'wordlift_key_validation_notification_shown'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Wordlift_Key_Validation_Service |
|
25
|
|
|
*/ |
|
26
|
|
|
private $key_validation_service; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Wordlift_Configuration_Service |
|
30
|
|
|
*/ |
|
31
|
|
|
private $configuration_service; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Ttl_Cache |
|
34
|
|
|
*/ |
|
35
|
|
|
private $ttl_cache_service; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Key_Validation_Notice constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Wordlift_Key_Validation_Service $key_validation_service |
|
41
|
|
|
* @param \Wordlift_Configuration_Service $configuration_service |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( $key_validation_service, $configuration_service ) { |
|
44
|
|
|
|
|
45
|
|
|
$this->key_validation_service = $key_validation_service; |
|
46
|
|
|
|
|
47
|
|
|
$this->configuration_service = $configuration_service; |
|
48
|
|
|
|
|
49
|
|
|
$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification', 60 * 60 * 8 ); |
|
50
|
|
|
|
|
51
|
|
|
if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
52
|
|
|
$this->display_key_validation_notice(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$that = $this; |
|
56
|
|
|
add_action( 'plugins_loaded', function () use ( $that ) { |
|
57
|
|
|
$that->notification_close_handler(); |
|
58
|
|
|
} ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function show_notification() { |
|
63
|
|
|
$settings_url = admin_url( 'admin.php?page=wl_configuration_admin_menu' ); |
|
64
|
|
|
?> |
|
65
|
|
|
<div class="error"> |
|
66
|
|
|
<p> |
|
67
|
|
|
<?php echo __( "Your WordLift key is not valid, please update the key in <a href='$settings_url'>WordLift Settings</a> or contact our support at [email protected].", 'wordlift' ); ?> |
|
68
|
|
|
</p> |
|
69
|
|
|
<p class="submit"> |
|
70
|
|
|
|
|
71
|
|
|
<a class="button-secondary skip" |
|
72
|
|
|
href="<?php echo esc_url( wp_nonce_url( add_query_arg( self::KEY_VALIDATION_NOTICE_PARAM, self::KEY_VALIDATION_NOTICE_PARAM ) |
|
73
|
|
|
, self::KEY_VALIDATION_NONCE_ACTION, self::KEY_VALIDATION_NONCE_PARAM ) ); ?>"> |
|
74
|
|
|
<?php esc_html_e( 'Close', 'wordlift' ); ?></a> |
|
75
|
|
|
</p> |
|
76
|
|
|
</div> |
|
77
|
|
|
<?php |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function is_key_valid() { |
|
81
|
|
|
|
|
82
|
|
|
$key = $this->configuration_service->get_key(); |
|
83
|
|
|
|
|
84
|
|
|
// Check cache if the result is present, if not get the results |
|
85
|
|
|
// save it and return the data. |
|
86
|
|
|
if ( $this->ttl_cache_service->get( self::CACHE_KEY ) !== null ) { |
|
87
|
|
|
return $this->ttl_cache_service->get( self::CACHE_KEY ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$is_valid = $this->key_validation_service->is_key_valid( $key ); |
|
91
|
|
|
|
|
92
|
|
|
// when the cache is set, clear the notification flag. |
|
93
|
|
|
delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
94
|
|
|
|
|
95
|
|
|
$this->ttl_cache_service->put( self::CACHE_KEY, $is_valid ); |
|
96
|
|
|
|
|
97
|
|
|
return $is_valid; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function display_key_validation_notice() { |
|
101
|
|
|
$that = $this; |
|
102
|
|
|
add_action( 'admin_notices', function () use ( $that ) { |
|
103
|
|
|
|
|
104
|
|
|
$is_notification_shown = get_option( Key_Validation_Notice::NOTIFICATION_OPTION_KEY, false ); |
|
105
|
|
|
|
|
106
|
|
|
$key = $that->configuration_service->get_key(); |
|
107
|
|
|
|
|
108
|
|
|
if ( ! $key ) { |
|
109
|
|
|
// Dont show warning or make API call, return early. |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ( $that->is_key_valid() ) { |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ( $is_notification_shown ) { |
|
118
|
|
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$that->show_notification(); |
|
122
|
|
|
|
|
123
|
|
|
} ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function notification_close_handler() { |
|
127
|
|
|
if ( ! isset( $_GET['wl_key_validation_notice'] ) |
|
128
|
|
|
|| ! isset( $_GET['_wl_key_validation_notice_nonce'] ) ) { |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
$type = (string) $_GET['wl_key_validation_notice']; |
|
|
|
|
|
|
132
|
|
|
$nonce = (string) $_GET['_wl_key_validation_notice_nonce']; |
|
133
|
|
|
|
|
134
|
|
|
if ( wp_verify_nonce( $nonce, self::KEY_VALIDATION_NONCE_ACTION ) |
|
135
|
|
|
&& current_user_can( 'manage_options' ) ) { |
|
136
|
|
|
// close the notification. |
|
137
|
|
|
update_option( self::NOTIFICATION_OPTION_KEY, true ); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function remove_notification_flag() { |
|
142
|
|
|
delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.