register_hook('TPL_METAHEADERS_OUTPUT', 'BEFORE', $this, 'switch_theme'); $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'switch_theme'); // Break cache payloads so themes toggle instantly on every page hit $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'bypass_cache'); } /** * Prevent page caching so the server can switch themes dynamically */ public function bypass_cache(Doku_Event $event, $param) { $event->preventDefault(); $event->stopPropagation(); $event->result = false; } /** * Switch theme based on system preference or browser time calculation */ public function switch_theme(Doku_Event $event, $param) { global $conf; // FIXED: Explicitly mapping config values to DokuWiki's native nested plugin structure $dayTheme = isset($conf['plugin']['timeswitcher']['day_template']) ? $conf['plugin']['timeswitcher']['day_template'] : 'was'; $nightTheme = isset($conf['plugin']['timeswitcher']['night_template']) ? $conf['plugin']['timeswitcher']['night_template'] : 'wasdark'; $startStr = isset($conf['plugin']['timeswitcher']['night_start']) ? $conf['plugin']['timeswitcher']['night_start'] : '20:00'; $endStr = isset($conf['plugin']['timeswitcher']['night_end']) ? $conf['plugin']['timeswitcher']['night_end'] : '06:00'; // 1. Direct System Theme Preference Detection $themePref = null; if (isset($_GET['theme_init'])) { $themePref = $_GET['theme_init']; } elseif (isset($_COOKIE['user_theme_pref'])) { $themePref = $_COOKIE['user_theme_pref']; } if ($themePref === 'dark') { $conf['template'] = $nightTheme; return; } elseif ($themePref === 'light') { $conf['template'] = $dayTheme; return; } // 2. Fallback Time-Based Logic (Runs only if theme detection data is unavailable) $browserOffsetMinutes = null; if (isset($_GET['tz_init'])) { $browserOffsetMinutes = (int)$_GET['tz_init']; } elseif (isset($_COOKIE['user_tz_offset'])) { $browserOffsetMinutes = (int)$_COOKIE['user_tz_offset']; } if ($browserOffsetMinutes !== null) { $userTimestamp = time() - ($browserOffsetMinutes * 60); $currentHour = (int)gmdate('G', $userTimestamp); $currentMinute = (int)gmdate('i', $userTimestamp); } else { $currentHour = (int)date('G'); $currentMinute = (int)date('i'); } list($sH, $sM) = explode(':', $startStr); list($eH, $eM) = explode(':', $endStr); $start = ($sH * 60) + $sM; $end = ($eH * 60) + $eM; $current = ($currentHour * 60) + $currentMinute; $isNight = false; if ($start < $end) { if ($current >= $start && $current < $end) $isNight = true; } else { if ($current >= $start || $current < $end) $isNight = true; } // Apply theme assignment cleanly to the core rendering template property $conf['template'] = $isNight ? $nightTheme : $dayTheme; } }