-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.php
520 lines (445 loc) · 12.9 KB
/
Utils.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
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
<?php
namespace Woocommerce\Pagarme\Helper;
if (!function_exists('add_action')) {
exit(0);
}
use Woocommerce\Pagarme\Core;
use Woocommerce\Pagarme\Model\Config;
use Woocommerce\Pagarme\Model\Order;
use WC_Order;
class Utils
{
/**
* Sanitize value from custom method
*
* @since 1.0
* @param string $name
* @param mixed $default
* @param string|array $sanitize
* @return mixed
*/
public static function request($type, $name, $default, $sanitize = 'rm_tags')
{
$request = filter_input_array($type, FILTER_SANITIZE_SPECIAL_CHARS);
if (!isset($request[$name]) || empty($request[$name])) {
return $default;
}
return self::sanitize($request[$name], $sanitize);
}
/**
* Sanitize value from methods post
*
* @since 1.0
* @param string $name
* @param mixed $default
* @param string|array $sanitize
* @return mixed
*/
public static function post($name, $default = '', $sanitize = 'rm_tags')
{
return self::request(INPUT_POST, $name, $default, $sanitize);
}
/**
* Sanitize value from methods get
*
* @since 1.0
* @param string $name
* @param mixed $default
* @param string|array $sanitize
* @return mixed
*/
public static function get($name, $default = '', $sanitize = 'rm_tags')
{
return self::request(INPUT_GET, $name, $default, $sanitize);
}
/**
* Get filtered super global server by key
*
* @since 1.0
* @param string $key
* @return string
*/
public static function server($key)
{
$value = self::get_value_by($_SERVER, strtoupper($key));
return self::rm_tags($value, true);
}
/**
* Sanitize requests
*
* @since 1.0
* @param string $value
* @param string|array $sanitize
* @return string
*/
public static function sanitize($value, $sanitize)
{
if (!is_callable($sanitize)) {
return (false === $sanitize) ? $value : self::rm_tags($value);
}
if (is_array($value)) {
return array_map($sanitize, $value);
}
return call_user_func($sanitize, $value);
}
/**
* Properly strip all HTML tags including script and style
*
* @since 1.0
* @param mixed string|array $value
* @param bool $remove_breaks
* @return mixed string|array
*/
public static function rm_tags($value, $remove_breaks = false)
{
if (empty($value) || is_object($value)) {
return $value;
}
if (is_array($value)) {
return array_map(__METHOD__, $value);
}
return wp_strip_all_tags($value, $remove_breaks);
}
/**
* Verify request ajax
*
* @since 1.0
* @return bool
*/
public static function is_request_ajax()
{
return ( strtolower(self::server('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'
|| (0 === strpos(self::server('QUERY_STRING'), 'wc-ajax')));
}
/**
* Verify if request is from checkout
*
* @since 1.0
* @return boolean
*/
public static function isCheckoutRequest()
{
return strpos(strtolower(self::server('REQUEST_URI')), 'checkout') !== false;
}
/**
* Get value by array index
*
* @since 1.0
* @param array $args
* @param string|int $index
* @return string
*/
public static function get_value_by($args, $index, $default = '')
{
if (!array_key_exists($index, $args) || empty($args[$index])) {
return $default;
}
return $args[$index];
}
/**
* Admin sanitize url
*
* @since 1.0
* @param string $path
* @return string
*/
public static function get_admin_url($path = '')
{
return esc_url(get_admin_url(null, $path));
}
/**
* Site URL
*
* @since 1.0
* @param string $path
* @return string
*/
public static function get_site_url($path = '')
{
return esc_url(get_site_url(null, $path));
}
/**
* Add prefix in string
*
* @since 1.0
* @param string $after
* @param string $before
* @return string
*/
public static function add_prefix($after, $before = '')
{
return $before . Core::PREFIX . $after;
}
/**
* Component attribute with prefix
*
* @since 1.0
* @param string $name
* @return string
*/
public static function get_component($name)
{
return self::add_prefix(sprintf('-component="%s"', $name), 'data-');
}
/**
* Format and validate phone number with DDD
*
* @since 1.0
* @param string $phone
* @return string
*/
public static function format_phone_number($phone)
{
$phone = preg_replace(array('/[^\d]+/', '/^(?![1-9])0/'), '', $phone);
if (strlen($phone) < 10) {
return '';
}
return array(substr($phone, 0, 2), substr($phone, 2));
}
/**
* Format order price with amount
*
* @since 1.0
* @param mixed string|float|int $price
* @return int
*/
public static function format_order_price($price)
{
if (empty($price)) {
return;
}
return @(int)number_format($price, 2, '', '');
}
/**
* Format order price with a currency symbol
*
* @since 1.0
* @param mixed string|float|int $price
* @return string
*/
public static function format_order_price_with_currency_symbol($price)
{
if (empty($price)) {
return;
}
return 'R$' . (string)number_format($price, 2, ',', '.');
}
/**
* Format desnormalized order price with amount
*
* @since 1.0
* @param mixed string|float|int $price
* @return int
*/
public static function format_desnormalized_order_price($price)
{
if (empty($price)) {
return;
}
$price = self::normalize_price($price);
return self::format_order_price($price);
}
public static function normalize_price($price)
{
if (empty($price)) {
return;
}
$price = str_replace('.', '', $price);
$price = str_replace(',', '.', $price);
return $price;
}
/**
* Format order price to current currency
*
* @since 1.0
* @param int $price
* @return string
*/
public static function format_order_price_to_view($price)
{
if (empty($price)) {
return;
}
$value = $price / 100;
$value = wc_price($value);
return $value;
}
/**
* Format document number
*
* @since 1.0
* @param string $document
* @return string
*/
public static function format_document($document)
{
return preg_replace('/[^0-9]+/', '', $document);
}
/**
* Get the order id by meta value
*
* @since 1.0
* @param string $meta_value
* @return int
*/
public static function get_order_by_meta_value($meta_value)
{
global $wpdb;
$query = $wpdb->prepare(
"SELECT
`post_id`
FROM
`{$wpdb->postmeta}`
WHERE
`meta_value` = %s
LIMIT 1
",
$meta_value
);
return (int) $wpdb->get_var($query);
}
/**
* Conversion of date
*
* @param string $date
* @param string $format
* @param string $search
* @param string $replace
* @return string
*/
public static function convert_date($date, $format = 'Y-m-d', $search = '/', $replace = '-')
{
if ($search && $replace) {
$date = str_replace($search, $replace, $date);
}
return date_i18n($format, strtotime($date));
}
public static function str_to_float($string)
{
return floatval(str_replace(',', '.', $string));
}
public static function calc_percentage($percentage, $total)
{
if (!$percentage) {
return 0;
}
$percentage = self::str_to_float($percentage);
return ($percentage / 100) * $total;
}
public static function get_json_post_data()
{
$post_data = file_get_contents('php://input');
return empty($post_data) ? false : json_decode($post_data);
}
public static function error_server_json($code, $message = 'Generic Message Error', $echo = true)
{
$response = json_encode(
array(
'status' => 'error',
'code' => $code,
'message' => $message,
)
);
if (!$echo) {
return $response;
}
echo esc_attr($response);
}
public static function get_phone_country_code($country)
{
$list = array(
'BR' => 55,
'US' => 1,
'UK' => 44,
'ES' => 34,
);
return isset($list[$country]) ? $list[$country] : 55;
}
public static function build_customer_address_from_order(Order $order)
{
return array(
'street' => substr($order->getWcOrder()->get_billing_address_1(), 0, 64),
'number' => substr($order->get_meta('billing_number'), 0, 15),
'complement' => substr($order->getWcOrder()->get_billing_address_2(), 0, 64),
'zip_code' => preg_replace('/[^\d]+/', '', $order->getWcOrder()->get_billing_postcode()),
'neighborhood' => substr($order->get_meta('billing_neighborhood'), 0, 64),
'city' => substr($order->get_meta('billing_city'), 0, 64),
'state' => substr($order->get_meta('billing_state'), 0, 2),
'country' => 'BR'
);
}
public static function build_document_from_order(Order $order)
{
if (!empty($order->get_meta('billing_cpf'))) {
return array(
'type' => 'individual',
'value' => $order->get_meta('billing_cpf'),
);
}
if (!empty($order->get_meta('billing_cnpj'))) {
return array(
'type' => 'company',
'value' => $order->get_meta('billing_cnpj'),
);
}
return array(
'type' => '',
'value' => '',
);
}
public static function build_customer_phones_from_order(Order $order)
{
$phones = array();
$phone = $order->get_meta('billing_phone');
$cellphone = $order->get_meta('billing_cellphone');
if ($phone) {
$phoneParts = self::format_phone_number($phone);
$phones['home_phone'] = array(
'country_code' => self::get_phone_country_code($order->getWcOrder()->get_billing_country()),
'area_code' => isset($phoneParts[0]) ? $phoneParts[0] : '',
'number' => isset($phoneParts[1]) ? $phoneParts[1] : '',
);
$phones["home_phone"]['complete_phone'] =
$phones['home_phone']['area_code'] .
$phones['home_phone']['number'];
}
if ($cellphone) {
$phoneParts = self::format_phone_number($cellphone);
$phones['mobile_phone'] = array(
'country_code' => self::get_phone_country_code($order->getWcOrder()->get_billing_country()),
'area_code' => isset($phoneParts[0]) ? $phoneParts[0] : '',
'number' => isset($phoneParts[1]) ? $phoneParts[1] : '',
);
$phones["mobile_phone"]['complete_phone'] =
$phones['mobile_phone']['area_code'] .
$phones['mobile_phone']['number'];
} else {
$phones['mobile_phone'] = $phones['home_phone'];
}
return $phones;
}
public static function build_customer_shipping_from_wc_order(WC_Order $wc_order)
{
$method = $wc_order->get_shipping_method();
$order = new Order($wc_order->get_id());
if (!$method) {
$method = 'Não informado';
}
$total = self::format_order_price($wc_order->get_shipping_total());
$shipping = $order->get_shipping_info();
return array(
'amount' => $total,
'description' => $method,
'address' => array(
'street' => substr($shipping['address_1'], 0, 64),
'number' => substr($shipping['number'], 0, 15),
'complement' => substr($shipping['address_2'], 0, 64),
'zip_code' => substr(preg_replace('/[^\d]+/', '', $shipping['postcode']), 0, 16),
'neighborhood' => substr($shipping['neighborhood'], 0, 64),
'city' => substr($shipping['city'], 0, 64),
'state' => substr($shipping['state'], 0, 2),
'country' => 'BR',
),
);
}
}