1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
if (!defined('ABSPATH')) { |
5
|
|
|
exit; // Exit if accessed directly |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Log helper for debugging |
10
|
|
|
*/ |
11
|
|
|
class pagantisLogger |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public static $logger; |
15
|
|
|
const PG_LOG_FILENAME = 'pagantis-woocommerce-gateway'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Utilize WC logger class |
19
|
|
|
* |
20
|
|
|
* @param $message |
21
|
|
|
* @param null $start_time |
|
|
|
|
22
|
|
|
* @param null $end_time |
|
|
|
|
23
|
|
|
* @version 1.0.0 |
24
|
|
|
* @since 8.6.9 |
25
|
|
|
*/ |
26
|
|
|
public static function log($message, $start_time = null, $end_time = null) |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
if (!class_exists('WC_Logger')) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
if (empty(self::$logger)) { |
33
|
|
|
if (version_compare(WC_VERSION, 3.0, '<')) { |
|
|
|
|
34
|
|
|
self::$logger = new WC_Logger(); |
|
|
|
|
35
|
|
|
} else { |
36
|
|
|
self::$logger = wc_get_logger(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
if (!defined('WP_DEBUG')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!is_null($start_time)) { |
|
|
|
|
45
|
|
|
$formatted_start_time = date_i18n(get_option('date_format') . ' g:ia', $start_time); |
|
|
|
|
46
|
|
|
$end_time = is_null($end_time) ? current_time('timestamp') : $end_time; |
|
|
|
|
47
|
|
|
$formatted_end_time = date_i18n(get_option('date_format') . ' g:ia', $end_time); |
48
|
|
|
$elapsed_time = round(abs($end_time - $start_time) / 60, 2); |
49
|
|
|
|
50
|
|
|
$log_entry = "\n" . '====Pagantis Version: ' . '====' . "\n"; |
51
|
|
|
$log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n"; |
52
|
|
|
$log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n"; |
53
|
|
|
} else { |
54
|
|
|
$log_entry = "\n" . '====Pagantis LOG ====' . PHP_EOL; |
55
|
|
|
$log_entry .= date_i18n('M j, Y @ G:i' , strtotime( 'now' ), true ) . PHP_EOL; |
56
|
|
|
$log_entry .= json_encode($message, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) . PHP_EOL; |
57
|
|
|
$log_entry .= PHP_EOL; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (version_compare(WC_VERSION, 3.0, '<')) { |
61
|
|
|
self::$logger->add(self::PG_LOG_FILENAME, json_encode($log_entry, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) ); |
62
|
|
|
} else { |
63
|
|
|
self::$logger->debug($log_entry, array('source' => self::PG_LOG_FILENAME)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function pg_debug_backtrace($return = true, $html = false, $show_first = true) |
69
|
|
|
{ |
70
|
|
|
|
71
|
|
|
$d = debug_backtrace(); |
72
|
|
|
$out = ''; |
73
|
|
|
if ($html) { |
74
|
|
|
$out .= "<pre>"; |
75
|
|
|
} |
76
|
|
|
foreach ($d as $i => $r) { |
77
|
|
|
if (!$show_first && $i == 0) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
// sometimes there is undefined index 'file' |
81
|
|
|
@$out .= "[$i] {$r['file']}:{$r['line']}\n"; |
82
|
|
|
} |
83
|
|
|
if ($html) { |
84
|
|
|
$out .= "</pre>"; |
85
|
|
|
} |
86
|
|
|
if ($return) { |
87
|
|
|
return $out; |
88
|
|
|
} else { |
89
|
|
|
echo $out; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
public static function pg_print_r($var) |
95
|
|
|
{ |
96
|
|
|
echo '<pre>'; |
97
|
|
|
print_r($var); |
98
|
|
|
echo '</pre>'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function toSnakeCase($str, $delimiter = '_') |
102
|
|
|
{ |
103
|
|
|
$str = lcfirst($str); |
104
|
|
|
$lowerCase = strtolower($str); |
105
|
|
|
$result = ''; |
106
|
|
|
$length = strlen($str); |
107
|
|
|
for ($i = 0; $i < $length; $i++) { |
108
|
|
|
$result .= ($str[$i] === $lowerCase[$i] ? '' : $delimiter) . $lowerCase[$i]; |
109
|
|
|
} |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function jsonSerialize($data) |
114
|
|
|
{ |
115
|
|
|
$arrayProperties = array(); |
116
|
|
|
|
117
|
|
|
foreach ($data as $key => $value) { |
118
|
|
|
$arrayProperties[self::toSnakeCase($key)] = $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $arrayProperties; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public static function toJson($initialResponse) |
125
|
|
|
{ |
126
|
|
|
if (!is_array($initialResponse)){ |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
$response = self::jsonSerialize($initialResponse); |
130
|
|
|
|
131
|
|
|
return json_encode($response, JSON_UNESCAPED_SLASHES); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|