| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways\App; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
use Hyperpay\Gateways\Main; |
| 8 |
|
| 9 |
class Log |
| 10 |
{ |
| 11 |
|
| 12 |
public static function load() |
| 13 |
{ |
| 14 |
|
| 15 |
$files = get_option('hyperpay_payments_logs' , "[]"); |
| 16 |
$files = json_decode($files, true); |
| 17 |
$names = \array_keys($files); |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
$current_file = isset($_GET['file']) ? sanitize_text_field(wp_unslash($_GET['file'])) : array_key_last($files); |
| 22 |
|
| 23 |
|
| 24 |
if (empty($files)) { |
| 25 |
self::print_error("There is no log "); |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
if ($current_file && \array_key_exists($current_file , $files)) { |
| 30 |
$content = $files[$current_file]; |
| 31 |
} else { |
| 32 |
self::print_error("Current log dose not exists"); |
| 33 |
$content = ''; |
| 34 |
} |
| 35 |
|
| 36 |
wp_enqueue_script('code-editor'); |
| 37 |
wp_enqueue_style('code-editor'); |
| 38 |
|
| 39 |
$content_label = esc_html__('Selected file content:', 'hyperpay-gateways'); |
| 40 |
$url = isset($_SERVER["REQUEST_URI"]) ? sanitize_url(wp_unslash($_SERVER["REQUEST_URI"])) : ''; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
return View::render('logs.html', \compact('content_label', 'content', 'current_file', 'names', 'url')); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
private static function print_error($error) |
| 49 |
{ |
| 50 |
?> |
| 51 |
<div class="notice notice-error"> |
| 52 |
<p> |
| 53 |
<?php echo esc_html($error); ?> |
| 54 |
</p> |
| 55 |
</div> |
| 56 |
<?php |
| 57 |
} |
| 58 |
|
| 59 |
private static function fileName() |
| 60 |
{ |
| 61 |
$today = gmdate("Y-m-d"); |
| 62 |
return "hyperpay-log-$today"; |
| 63 |
} |
| 64 |
|
| 65 |
public static function write($msg) |
| 66 |
{ |
| 67 |
$log_file = static::fileName(); |
| 68 |
$time = gmdate("H:i:s"); |
| 69 |
$message = print_r($msg, true); |
| 70 |
$bt = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3); |
| 71 |
if (isset($bt[2])) { |
| 72 |
$caller = $bt[2]; |
| 73 |
$file = $caller['file']; |
| 74 |
$line = $caller['line']; |
| 75 |
$title = "[$time $file:$line]"; |
| 76 |
$footer = print_r($bt[2]['args'], true); |
| 77 |
} else { |
| 78 |
$title = "[$time]"; |
| 79 |
$footer = print_r($bt, true); |
| 80 |
} |
| 81 |
|
| 82 |
$selected = get_option('hyperpay_payments_logs' , "[]"); |
| 83 |
$selected = json_decode($selected, true); |
| 84 |
|
| 85 |
$selected[$log_file] = ($selected[$log_file] ?? '') . "$title\n$message\n$footer\n"; |
| 86 |
|
| 87 |
|
| 88 |
update_option('hyperpay_payments_logs', json_encode($selected)); |
| 89 |
} |
| 90 |
|
| 91 |
public static function removeExpiredLogs() |
| 92 |
{ |
| 93 |
$selected = get_option('hyperpay_payments_logs' , "[]"); |
| 94 |
$selected = json_decode($selected, true); |
| 95 |
|
| 96 |
$selected = array_filter($selected, function ($key) { |
| 97 |
return strtotime($key) >= strtotime('-14 days'); |
| 98 |
}, ARRAY_FILTER_USE_KEY); |
| 99 |
|
| 100 |
|
| 101 |
update_option('hyperpay_payments_logs', json_encode($selected)); |
| 102 |
} |
| 103 |
} |
| 104 |
|