-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdata.php
More file actions
358 lines (324 loc) · 8.74 KB
/
data.php
File metadata and controls
358 lines (324 loc) · 8.74 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
<?php
namespace EA11y\Modules\Connect\Classes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Data
*/
class Data {
const CLIENT_ID = '_client_id';
const CLIENT_SECRET = '_client_secret';
const ACCESS_TOKEN = '_access_token';
const REFRESH_TOKEN = '_refresh_token';
const TOKEN_ID = '_token_id';
const SUBSCRIPTION_ID = '_subscription_id';
const OPTION_OWNER_USER_ID = '_owner_user_id';
const HOME_URL = '_home_url';
/**
* get_option
* @param $option_name
* @param $default
*
* @return false|mixed|null
*/
public static function get_option( $option_name, $default ) {
return get_option( Config::APP_PREFIX . $option_name, $default );
}
/**
* set_option
* @param $option_name
* @param $option_value
* @param bool $auto_load
*
* @return bool
*/
public static function set_option( $option_name, $option_value, $auto_load = false ) : bool {
return update_option( Config::APP_PREFIX . $option_name, $option_value, $auto_load );
}
/**
* delete_option
* @param $option_name
*
* @return bool
*/
public static function delete_option( $option_name ) : bool {
return delete_option( Config::APP_PREFIX . $option_name );
}
/**
* get_user_data
* @param $user_id
* @param $data_name
* @param mixed|bool $default
*
* @return false|mixed
*/
public static function get_user_data( $user_id, $data_name, $default = false ) {
$data = get_user_meta( $user_id, Config::APP_PREFIX . $data_name, true );
return empty( $data ) ? $default : $data;
}
/**
* set_user_data
* @param $user_id
* @param $data_name
* @param $value
*
* @return bool|int
*/
public static function set_user_data( $user_id, $data_name, $value ) {
return update_user_meta( $user_id, Config::APP_PREFIX . $data_name, $value );
}
/**
* delete_user_data
* @param $user_id
* @param $data_name
*
* @return bool
*/
public static function delete_user_data( $user_id, $data_name ) : bool {
return delete_user_meta( $user_id, Config::APP_PREFIX . $data_name );
}
/**
* get_connect_mode_data
* @param ...$data
*
* @return false|mixed|null|string
*/
public static function get_connect_mode_data( ...$data ) {
if ( Config::CONNECT_MODE === 'site' ) {
return self::get_option( ...$data );
}
$user_id = get_current_user_id();
return self::get_user_data( ...( [ $user_id, ...$data ] ) );
}
/**
* set_connect_mode_data
* @param ...$data
*
* @return bool|int
*/
public static function set_connect_mode_data( ...$data ) {
if ( Config::CONNECT_MODE === 'site' ) {
return self::set_option( ...$data );
}
$user_id = get_current_user_id();
return self::set_user_data( ...( [ $user_id, ...$data ] ) );
}
/**
* get_client_id
*
* @return string|bool|mixed
*/
public static function get_client_id() {
return self::get_connect_mode_data( self::CLIENT_ID, false );
}
/**
* get_client_secret
* @return false|mixed|string|null
*/
public static function get_client_secret() {
return self::get_connect_mode_data( self::CLIENT_SECRET, false );
}
/**
* set_client_id
* @param $value
*
* @return bool
*/
public static function set_client_id( $value ) : bool {
return self::set_connect_mode_data( self::CLIENT_ID, $value );
}
public static function get_subscription_id() {
return self::get_connect_mode_data( self::SUBSCRIPTION_ID, false );
}
public static function set_subscription_id( $value ) : bool {
return self::set_connect_mode_data( self::SUBSCRIPTION_ID, $value );
}
/**
* set_client_secret
* @param $value
*
* @return bool
*/
public static function set_client_secret( $value ) : bool {
return self::set_connect_mode_data( self::CLIENT_SECRET, $value );
}
/**
* get_access_token
* @return false|mixed|string|null
*/
public static function get_access_token() {
return self::get_connect_mode_data( self::ACCESS_TOKEN, false );
}
public static function get_token_id() {
return self::get_connect_mode_data( self::TOKEN_ID, false );
}
/**
* get_refresh_token
* @return false|mixed|string|null
*/
public static function get_refresh_token() {
return self::get_connect_mode_data( self::REFRESH_TOKEN, false );
}
/**
* get_home_url
*
* Return a plain text version of a based64 encoded home URL if it's stored as base64 encoded
* @return string|null
*/
public static function get_home_url() {
$raw = self::get_connect_mode_data( self::HOME_URL, false );
$is_base64 = base64_encode( base64_decode( $raw, true ) ) === $raw;
$url = $is_base64 ? base64_decode( $raw ) : $raw;
return apply_filters( 'ally_connect_home_url', $url );
}
/**
* set_home_url
*
* Stores home URL as a base64 string to avoid migration/stg tools from overriding value
*/
public static function set_home_url( ?string $home_url = null ) : bool {
$home_url = $home_url ?? home_url();
return self::set_connect_mode_data( self::HOME_URL, base64_encode( $home_url ) );
}
/**
* set_user_is_owner_option
*/
public static function set_user_is_owner_option( $value ) {
return self::set_connect_mode_data( self::OPTION_OWNER_USER_ID, $value );
}
/**
* get_user_is_owner_option
*/
public static function get_user_is_owner_option() {
return self::get_connect_mode_data( self::OPTION_OWNER_USER_ID, false );
}
/**
* fetch_option
* direct query to avoid cache and race condition issues
*
* @param $option_name
* @param $default
*
* @return mixed|null
*/
public static function fetch_option( $option_name, $default = null ) {
global $wpdb;
if ( ! self::is_option_whitelisted_for_direct_access( $option_name ) ) {
return $default;
}
$cache_buster = wp_generate_uuid4();
$option = $wpdb->get_col(
$wpdb->prepare(
"SELECT option_value
FROM $wpdb->options
WHERE option_name = %s
AND %s = %s
LIMIT 1",
$option_name,
$cache_buster,
$cache_buster
)
);
if ( ! empty( $option ) ) {
return $option[0];
}
return $default;
}
/**
* insert_option_uniquely
*
* used to insert option if not there already
* direct query to avoid cache and race condition issues
*
* @param $option_name
* @param $option_value
*
* @return bool
*/
public static function insert_option_uniquely( $option_name, $option_value ) : bool {
global $wpdb;
if ( ! self::is_option_whitelisted_for_direct_access( $option_name ) ) {
return false;
}
$cache_buster = wp_generate_uuid4();
$result = $wpdb->query(
$wpdb->prepare(
"INSERT INTO $wpdb->options (option_name, option_value, autoload)
SELECT * FROM (SELECT %s, %s, 'no') AS tmp
WHERE NOT EXISTS (
SELECT option_name
FROM $wpdb->options
WHERE option_name = %s
AND option_value = %s
AND %s = %s
) LIMIT 1",
$option_name,
$option_value,
$option_name,
$option_value,
$cache_buster,
$cache_buster
)
);
if ( false === $result || 0 === $result ) {
// false means query failed, 0 means no row inserted because it exists
return false;
}
return true;
}
/**
* is_option_whitelisted_for_direct_access
* allowed only list of option names
*
* @param string $option_name
*
* @return boolean
*/
public static function is_option_whitelisted_for_direct_access( string $option_name ) : bool {
$options_whitelist = [
Config::APP_NAME . Service::REFRESH_TOKEN_LOCK,
];
return in_array( $option_name, $options_whitelist, true );
}
/**
* User is subscription owner.
*
* Check if current user is subscription owner.
*
* @return boolean
*/
public static function user_is_subscription_owner(): bool {
$owner_id = (int) self::get_connect_mode_data( self::OPTION_OWNER_USER_ID, false );
return get_current_user_id() === $owner_id;
}
/**
* clear_session
*/
public static function clear_session( $with_client = false ) {
if ( Config::CONNECT_MODE === 'site' ) {
if ( $with_client ) {
self::delete_option( self::CLIENT_ID );
self::delete_option( self::CLIENT_SECRET );
}
self::delete_option( self::ACCESS_TOKEN );
self::delete_option( self::REFRESH_TOKEN );
self::delete_option( self::TOKEN_ID );
self::delete_option( self::SUBSCRIPTION_ID );
self::delete_option( self::OPTION_OWNER_USER_ID );
self::delete_option( self::HOME_URL );
} else {
$user_id = get_current_user_id();
if ( $with_client ) {
self::delete_user_data( $user_id, self::CLIENT_ID );
self::delete_user_data( $user_id, self::CLIENT_SECRET );
}
self::delete_user_data( $user_id, self::ACCESS_TOKEN );
self::delete_user_data( $user_id, self::REFRESH_TOKEN );
self::delete_user_data( $user_id, self::TOKEN_ID );
self::delete_user_data( $user_id, self::SUBSCRIPTION_ID );
self::delete_user_data( $user_id, self::OPTION_OWNER_USER_ID );
self::delete_user_data( $user_id, self::HOME_URL );
}
}
}