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