plugin attached
This commit is contained in:
147
igny8-wp-plugin/includes/class-igny8-webhook-logs.php
Normal file
147
igny8-wp-plugin/includes/class-igny8-webhook-logs.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
/**
|
||||
* Webhook Activity Logs
|
||||
*
|
||||
* Logs webhook activity for auditing and debugging
|
||||
*
|
||||
* @package Igny8Bridge
|
||||
*/
|
||||
|
||||
// Prevent direct access
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log webhook activity
|
||||
*
|
||||
* @param array $data Log data
|
||||
* @return string|false Log ID or false on failure
|
||||
*/
|
||||
function igny8_log_webhook_activity($data) {
|
||||
$logs = get_option('igny8_webhook_logs', array());
|
||||
|
||||
$log_entry = array(
|
||||
'id' => uniqid('webhook_', true),
|
||||
'event' => sanitize_text_field($data['event'] ?? 'unknown'),
|
||||
'data' => $data['data'] ?? null,
|
||||
'ip' => sanitize_text_field($data['ip'] ?? ''),
|
||||
'user_agent' => sanitize_text_field($data['user_agent'] ?? ''),
|
||||
'status' => sanitize_text_field($data['status'] ?? 'received'),
|
||||
'response' => $data['response'] ?? null,
|
||||
'error' => sanitize_text_field($data['error'] ?? ''),
|
||||
'received_at' => current_time('mysql'),
|
||||
'processed_at' => $data['processed_at'] ?? null
|
||||
);
|
||||
|
||||
$logs[] = $log_entry;
|
||||
|
||||
// Keep only last 500 logs
|
||||
if (count($logs) > 500) {
|
||||
$logs = array_slice($logs, -500);
|
||||
}
|
||||
|
||||
update_option('igny8_webhook_logs', $logs);
|
||||
|
||||
return $log_entry['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update webhook log entry
|
||||
*
|
||||
* @param string $log_id Log ID
|
||||
* @param array $updates Updates to apply
|
||||
* @return bool Success
|
||||
*/
|
||||
function igny8_update_webhook_log($log_id, $updates) {
|
||||
$logs = get_option('igny8_webhook_logs', array());
|
||||
|
||||
foreach ($logs as $key => $log) {
|
||||
if ($log['id'] === $log_id) {
|
||||
foreach ($updates as $field => $value) {
|
||||
if ($field === 'status') {
|
||||
$logs[$key][$field] = sanitize_text_field($value);
|
||||
} elseif ($field === 'response') {
|
||||
$logs[$key][$field] = $value;
|
||||
} elseif ($field === 'processed_at') {
|
||||
$logs[$key][$field] = sanitize_text_field($value);
|
||||
} else {
|
||||
$logs[$key][$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
update_option('igny8_webhook_logs', $logs);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get webhook logs
|
||||
*
|
||||
* @param array $args Query arguments
|
||||
* @return array Logs
|
||||
*/
|
||||
function igny8_get_webhook_logs($args = array()) {
|
||||
$defaults = array(
|
||||
'limit' => 50,
|
||||
'event' => null,
|
||||
'status' => null
|
||||
);
|
||||
|
||||
$args = wp_parse_args($args, $defaults);
|
||||
$logs = get_option('igny8_webhook_logs', array());
|
||||
|
||||
// Reverse to get newest first
|
||||
$logs = array_reverse($logs);
|
||||
|
||||
// Filter by event
|
||||
if ($args['event']) {
|
||||
$logs = array_filter($logs, function($log) use ($args) {
|
||||
return $log['event'] === $args['event'];
|
||||
});
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if ($args['status']) {
|
||||
$logs = array_filter($logs, function($log) use ($args) {
|
||||
return $log['status'] === $args['status'];
|
||||
});
|
||||
}
|
||||
|
||||
// Limit results
|
||||
if ($args['limit'] > 0) {
|
||||
$logs = array_slice($logs, 0, $args['limit']);
|
||||
}
|
||||
|
||||
return array_values($logs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear old webhook logs
|
||||
*
|
||||
* @param int $days_old Delete logs older than this many days
|
||||
* @return int Number of logs deleted
|
||||
*/
|
||||
function igny8_clear_old_webhook_logs($days_old = 30) {
|
||||
$logs = get_option('igny8_webhook_logs', array());
|
||||
$cutoff = strtotime("-{$days_old} days");
|
||||
$deleted = 0;
|
||||
|
||||
foreach ($logs as $key => $log) {
|
||||
$log_time = strtotime($log['received_at']);
|
||||
if ($log_time < $cutoff) {
|
||||
unset($logs[$key]);
|
||||
$deleted++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($deleted > 0) {
|
||||
update_option('igny8_webhook_logs', array_values($logs));
|
||||
}
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user