<?php
/**
* Plugin Name: WPSD Log Display
* Description: Display WPSD JSON logs in a table via shortcode.
* Version: 1.3.8
* Author: Copilot
*/
if (!defined('ABSPATH')) exit;
function wpsd_log_display_shortcode() {
$upload_dir = wp_upload_dir();
$dir = trailingslashit($upload_dir['basedir']) . 'wpsd';
if (!file_exists($dir)) return "<p>No log directory found.</p>";
$files = glob($dir . "/inbox-*.json");
if (!$files) return "<p>No log files found.</p>";
$logs = [];
foreach ($files as $file) {
$json = json_decode(file_get_contents($file), true);
if (!$json || !isset($json['timestamp'])) continue;
$dmr = isset($json['dmr']) ? $json['dmr'] : [];
$callsign = isset($dmr['callsign']) ? $dmr['callsign'] : (isset($json['callsign']) ? $json['callsign'] : '(未設定)');
$dmr_id = isset($json['dmr_id']) ? $json['dmr_id'] : '(未設定)';
$src = isset($dmr['src']) ? ($dmr['src'] === 'NETWORK' ? 'NW' : $dmr['src']) : '(未設定)';
$name = isset($json['name']) ? $json['name'] : '(未設定)';
$logs[] = [
'timestamp' => $json['timestamp'],
'callsign' => $callsign,
'dmr_id' => $dmr_id,
'src' => $src,
'name' => $name
];
}
usort($logs, function($a, $b) {
return strtotime($b['timestamp']) - strtotime($a['timestamp']);
});
$output = '<style>
.wpsd-log-table { border-collapse: collapse; width: 100%; table-layout: fixed; }
.wpsd-log-table th, .wpsd-log-table td { border: 1px solid #ccc; padding: 8px; text-align: left; }
.wpsd-log-table th:nth-child(1) { width: 20%; }
.wpsd-log-table th:nth-child(2) { width: 15%; }
.wpsd-log-table th:nth-child(3) { width: 15%; }
.wpsd-log-table th:nth-child(4) { width: 10%; }
.wpsd-log-table th:nth-child(5) { width: 40%; }
.wpsd-log-table td { word-wrap: break-word; }
</style>';
$output .= '<table class="wpsd-log-table">';
$output .= '<tr><th>Timestamp</th><th>Callsign</th><th>DMR ID</th><th>Source</th><th>Name</th></tr>';
foreach ($logs as $log) {
$output .= "<tr><td>{$log['timestamp']}</td><td>{$log['callsign']}</td><td>{$log['dmr_id']}</td><td>{$log['src']}</td><td>{$log['name']}</td></tr>";
}
$output .= '</table>';
return $output;
}
add_shortcode('wpsd_log_display', 'wpsd_log_display_shortcode');
?>