database = $database;
}
/**
* Ensure database table exists
*
* @return void
*/
private function ensure_table_exists() {
// Create tables if they don't exist
$this->database->create_tables();
}
/**
* Render logs page
*
* @return void
*/
public function render() {
// Ensure table exists before rendering
$this->ensure_table_exists();
// Handle actions
$this->handle_actions();
// Get current filters (nonce verification not required for read-only GET parameters in admin context)
$current_page = isset($_GET['paged']) ? max(1, intval(wp_unslash($_GET['paged']))) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$per_page = 10;
$action_filter = isset($_GET['action_filter']) ? sanitize_text_field(wp_unslash($_GET['action_filter'])) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// Get logs
$result = $this->database->get_logs([
'page' => $current_page,
'per_page' => $per_page,
'action' => $action_filter
]);
$logs = $result['logs'];
$total_pages = $result['pages'];
$total_items = $result['total'];
// Show statistics
$this->render_statistics();
?>
' . esc_html__('Security check failed. Please try again.', 'ai-comment-guard') . '
';
return;
}
if (!current_user_can('manage_options')) {
echo '' . esc_html__('You are not allowed to perform this action.', 'ai-comment-guard') . '
';
return;
}
$this->database->clear_logs();
echo '' . esc_html__('Logs deleted successfully.', 'ai-comment-guard') . '
';
}
}
/**
* Render statistics
*
* @return void
*/
private function render_statistics() {
// Get retention days from config
$config = new \AICOG\Utils\Config();
$retention_days = $config->get('log_retention_days', 30);
// If retention is 0 (indefinite), show last 30 days for stats
$stats_days = $retention_days > 0 ? $retention_days : 30;
$stats = $this->database->get_statistics($stats_days);
if ($stats['total'] === 0) {
return;
}
?>
>
>
>
>
ai_response, true);
?>
created_at)); ?>
comment_content): ?>
comment_author ?: __('Unknown', 'ai-comment-guard')); ?>
comment_author_email): ?>
comment_author_email); ?>
get_action_label($log->action)); ?>
comment_author_ip ?: __('Unknown', 'ai-comment-guard')); ?>
__('Approved', 'ai-comment-guard'),
'spam' => __('Spam', 'ai-comment-guard'),
'reject' => __('Rejected', 'ai-comment-guard'),
'hold' => __('Held', 'ai-comment-guard')
];
return $labels[$action] ?? ucfirst($action);
}
/**
* Handle delete logs AJAX request
*
* @return void
*/
public function handle_delete_logs() {
check_ajax_referer('aicog_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_die('Unauthorized');
}
$days = isset($_POST['days']) ? intval($_POST['days']) : 0;
$deleted = $this->database->clear_logs($days);
wp_send_json_success([
'message' => sprintf(
/* translators: %d is the number of logs deleted */
_n('%d log deleted', '%d logs deleted', $deleted, 'ai-comment-guard'),
$deleted
)
]);
}
}