fix: cache state detection - add --allow-root fallback, capture error detail for diagnostics

main
Anthony 2026-02-28 16:14:16 +08:00
parent df460c8b96
commit 0d40aa4f4b
1 changed files with 14 additions and 4 deletions

View File

@ -78,14 +78,24 @@ function get_cache_state() {
return ['enabled' => null, 'note' => 'WordPress or WP-CLI not found'];
}
$result = run_command("sudo -u litespeed " . escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " litespeed-option get cache 2>/dev/null");
if ($result['return_var'] !== 0 || empty($result['output'][0])) {
return ['enabled' => null, 'note' => 'LiteSpeed Cache plugin not active or unavailable'];
// Try as litespeed user first (same pattern as manage_litespeed.php)
$cmd = "sudo -u litespeed " . escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " litespeed-option get cache 2>&1";
$result = run_command($cmd);
// Fallback: run as root with --allow-root (read-only operation, safe)
if ($result['return_var'] !== 0 || empty($result['output'])) {
$cmd = escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " litespeed-option get cache --allow-root 2>&1";
$result = run_command($cmd);
}
if ($result['return_var'] !== 0 || empty($result['output'])) {
$detail = !empty($result['output']) ? implode(' ', $result['output']) : 'no output';
return ['enabled' => null, 'note' => 'LiteSpeed Cache plugin not active or unavailable: ' . $detail];
}
$value = trim($result['output'][0]);
return [
'enabled' => $value === '1',
'enabled' => ($value === '1'),
'raw' => $value
];
}