-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-api.php
451 lines (342 loc) · 10.8 KB
/
class-api.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
<?php
/**
* Api helper.
*
* @package megaventory
* @since 1.0.0
*
* Author URI: https://github.com/Megaventory/WooCommerce
* Developer URI: https://megaventory.com/
* Developer e-mail: [email protected]
* Copyright: © 2009-2019 WooCommerce.
* License: GNU General Public License v3.0
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Megaventory;
require_once MEGAVENTORY__PLUGIN_DIR . 'helpers/class-address.php';
/**
* Megaventory API helper.
*/
class API {
/**
* Get the Megaventory API key
*/
public static function get_api_key() {
return get_option( 'megaventory_api_key' );
}
/**
* Get Megaventory URL
*
* @return string
*/
private static function get_megaventory_url() {
$url = self::get_api_host() . 'json/reply/';
return $url;
}
/**
* Get host
*/
public static function get_api_host() {
$host = get_option( 'megaventory_api_host', \Megaventory\Models\MV_Constants::MV_DEFAULT_HOST );
return $host;
}
/**
* Setting up Megaventory API host.
*
* @param string $host as Megaventory host.
* @return void
*/
public static function set_api_host( $host ) {
if ( '/' !== substr( $host, -1 ) ) {
$host .= '/';
}
update_option( 'megaventory_api_host', (string) $host );
}
/**
* Create URL.
*
* @param string $call as Megaventory API method.
*/
public static function get_url_for_call( $call ) {
$url = self::get_megaventory_url();
return $url . $call;
}
/**
* Send json.
*
* @param string $url as string.
* @param mixed $json_request as stdclass.
* @return mixed
*/
public static function send_json( $url, $json_request ) {
/*
TODO: Send directly the array and not an object.
*/
$body_to_send = self::create_array_from_object( $json_request );
$body_to_send['APIKEY'] = get_option( 'megaventory_api_key' );
$host_reachable = false;
$data_to_send = array(
'headers' => array(
'Content-Type' => 'application/json',
),
'method' => 'POST',
'timeout' => \Megaventory\Models\MV_Constants::REQUEST_TIMEOUT_LIMIT_IN_SECONDS,
'body' => wp_json_encode( $body_to_send ),
);
$data = array();
$response = array();
// Try multiple times until MAX_REQUEST_ATTEMPTS is reached before failing and registering an error when the API is timing out.
// After the maximum attempts have been exhausted with no success, return a WordPress request error.
for ( $attempt = 1; $attempt <= \Megaventory\Models\MV_Constants::MAX_REQUEST_ATTEMPTS; $attempt++ ) {
$response = wp_remote_post( $url, $data_to_send );
if ( ! is_wp_error( $response ) ) {
$data = json_decode( wp_remote_retrieve_body( $response ), true );
$host_reachable = true;
break;
} else {
// Exponential Backoff algorithm (Time = 2^c - 1, where c is the current number of attempts).
$time_sleep = ( pow( 2, $attempt ) - 1 ) * \Megaventory\Models\MV_Constants::SECONDS_TO_MICROSECONDS_CONVERSION_RATE;
usleep( $time_sleep );
}
}
if ( ! $host_reachable ) {
$data['InternalErrorCode'] = 'WordPress Request timeout';
$data['ResponseStatus']['Message'] = $response->get_error_message();
}
$data['json_object'] = wp_json_encode( $body_to_send );
return $data;
}
/**
* Send json.
*
* @param string $url as string.
* @param mixed $request as array.
* @return array
*/
public static function send_request_to_megaventory( $url, $request ) {
$body_to_send = $request;
$body_to_send['APIKEY'] = get_option( 'megaventory_api_key' );
$host_reachable = false;
$data_to_send = array(
'headers' => array(
'Content-Type' => 'application/json',
),
'method' => 'POST',
'timeout' => \Megaventory\Models\MV_Constants::REQUEST_TIMEOUT_LIMIT_IN_SECONDS,
'body' => wp_json_encode( $body_to_send ),
);
$data = array();
$response = array();
// Try multiple times until MAX_REQUEST_ATTEMPTS is reached before failing and registering an error when the API is timing out.
// Log the error only one time, after maximum attempts have been exhausted.
for ( $attempt = 1; $attempt <= \Megaventory\Models\MV_Constants::MAX_REQUEST_ATTEMPTS; $attempt++ ) {
$response = wp_remote_post( $url, $data_to_send );
if ( ! is_wp_error( $response ) ) {
$data = json_decode( wp_remote_retrieve_body( $response ), true );
$host_reachable = true;
break;
} else {
// Exponential Backoff algorithm (Time = 2^c - 1, where c is the current number of attempts).
$time_sleep = ( pow( 2, $attempt ) - 1 ) * \Megaventory\Models\MV_Constants::SECONDS_TO_MICROSECONDS_CONVERSION_RATE;
usleep( $time_sleep );
}
}
if ( ! $host_reachable ) {
$data['InternalErrorCode'] = 'WordPress Request timeout';
$data['ResponseStatus']['Message'] = $response->get_error_message();
}
$data['json_object'] = wp_json_encode( $body_to_send );
return $data;
}
/**
* Creates an array from dynamic object.
*
* @param mixed $my_object as a dynamic object.
* @return array
*/
private static function create_array_from_object( $my_object ) {
$array_to_return = array();
foreach ( $my_object as $key => $value ) {
if ( is_a( $value, 'stdClass' ) ) {
$array_to_return[ $key ] = self::create_array_from_object( $value );
} else {
$array_to_return[ $key ] = $value;
}
}
return $array_to_return;
}
/**
* Wrap api key to object.
*
* @param object $json_object as string.
*/
public static function wrap_json( $json_object ) {
$api_key = get_option( 'megaventory_api_key' );
$json_object->apikey = $api_key;
return $json_object;
}
/**
* Curl call.
*
* @param string $url as string.
* @return array
*/
public static function perform_call_to_megaventory( $url ) {
$body_to_send = array( 'APIKEY' => self::get_api_key() );
$data_to_send = array(
'headers' => array(
'Content-Type' => 'application/json',
),
'method' => 'POST',
'timeout' => \Megaventory\Models\MV_Constants::REQUEST_TIMEOUT_LIMIT_IN_SECONDS,
'body' => wp_json_encode( $body_to_send ),
);
$host_reachable = false;
$data = array();
$response = array();
for ( $attempt = 1; $attempt <= \Megaventory\Models\MV_Constants::MAX_REQUEST_ATTEMPTS; $attempt++ ) {
$response = wp_remote_post( $url, $data_to_send );
if ( ! is_wp_error( $response ) ) {
$data = json_decode( wp_remote_retrieve_body( $response ), true );
$host_reachable = true;
break;
} else {
// Exponential Backoff algorithm (Time = 2^c - 1, where c is the current number of attempts).
$time_sleep = ( pow( 2, $attempt ) - 1 ) * \Megaventory\Models\MV_Constants::SECONDS_TO_MICROSECONDS_CONVERSION_RATE;
usleep( $time_sleep );
}
}
if ( ! $host_reachable ) {
$data['InternalErrorCode'] = 'WordPress Request timeout';
$data['ResponseStatus']['Message'] = $response->get_error_message();
}
return $data;
}
/**
* Get Default Currency.
*/
public static function get_default_currency() {
$url = self::get_url_for_call( \Megaventory\Models\MV_Constants::CURRENCY_GET );
$data = array(
'Filters' => array(
'FieldName' => 'CurrencyIsDefault',
'SearchOperator' => 'Equals',
'SearchValue' => 'true',
),
);
$response = self::send_request_to_megaventory( $url, $data );
$megaventory_currency = $response['mvCurrencies'][0]['CurrencyCode'];
update_option( 'primary_megaventory_currency', $megaventory_currency );
return $megaventory_currency;
}
/**
* Check connectivity.
*
* @return bool
*/
private static function check_connectivity() {
$url = self::get_megaventory_url();
$result = wp_remote_get( $url );
if ( ! $result || ! is_array( $result ) || is_wp_error( $result ) ) {
return false;
}
return true;
}
/**
* Check API key.
*
* @return array|false with ApiKeyGet response.
*/
public static function check_key() {
$api_key = get_option( 'megaventory_api_key' );
if ( empty( $api_key ) ) {
update_option( 'api_key_is_set', 0 );
update_option( 'correct_megaventory_apikey', 0 );
update_option( 'correct_currency', 0 );
return false;
}
$connectivity = self::check_connectivity();
if ( ! $connectivity ) {
update_option( 'correct_connection', 0 );
$data = array();
$data['ResponseStatus']['ErrorCode'] = 500;
$data['ResponseStatus']['Message'] = 'Unable to reach host.';
} else {
update_option( 'correct_connection', 1 );
if ( empty( $api_key ) ) {
$data = array();
$data['ResponseStatus']['ErrorCode'] = 500;
$data['ResponseStatus']['Message'] = 'Empty Api Key, create an API key under My Profile.';
} else {
$url = self::get_url_for_call( 'APIKeyGet' );
$data = self::perform_call_to_megaventory( $url );
}
}
return $data;
}
/**
* Integration check.
*/
public static function check_if_integration_is_enabled() {
$url = self::get_url_for_call( 'AccountSettingsGet' );
$json_object = new \stdClass();
$integration_object = new \stdClass();
$integration_object->settingname = 'isWoocommerceEnabled';
$json_object->mvaccountsettings = $integration_object;
$json_object = self::wrap_json( $integration_object );
$data = self::send_json( $url, $json_object );
$is_woo_commerce_enabled_response = $data['mvAccountSettings'][0]['SettingValue'];
return $is_woo_commerce_enabled_response;
}
/**
* Get last inserted Megaventory API key.
*
* @return string
*/
public static function get_last_valid_api_key() {
global $wpdb;
/*
This is reference for quick search for query below: megaventory_api_keys .
*/
$apikeys_table_name = $wpdb->prefix . 'megaventory_api_keys';
$existing_table = $wpdb->get_results( $wpdb->prepare( 'show tables like %s', $apikeys_table_name ), ARRAY_A ); // phpcs:ignore
if ( count( $existing_table ) === 0 ) {
return '';
}
$last_valid_apikey_array = $wpdb->get_results( // phpcs:ignore
"
SELECT api_key
FROM {$wpdb->prefix}megaventory_api_keys
ORDER BY id
DESC LIMIT 1 "
);
if ( ! empty( $last_valid_apikey_array ) ) {
$last_valid_apikey = $last_valid_apikey_array[0]->api_key;
} else {
$last_valid_apikey = '';
}
return $last_valid_apikey;
}
/**
* Log API key.
*
* @param string $api_key as string.
*/
public static function log_apikey( $api_key ) {
global $wpdb;
$last_valid_api_key = self::get_last_valid_api_key();
if ( trim( $last_valid_api_key ) === trim( $api_key ) ) {
return;
}
$apikeys_table_name = $wpdb->prefix . 'megaventory_api_keys';
$charset_collate = $wpdb->get_charset_collate();
$return = $wpdb->insert( // phpcs:ignore
$apikeys_table_name,
array(
'created_at' => get_date_from_gmt( gmdate( 'Y-m-d H:i:s' ), 'Y-m-d H:i:s' ),
'api_key' => $api_key,
)
);
return $return;
}
}