定时维护模式功能测试"; // 获取当前设置 $options = get_option('themes_demo'); $start_time = isset($options['maintenance_start_time']) ? $options['maintenance_start_time'] : ''; $end_time = isset($options['maintenance_end_time']) ? $options['maintenance_end_time'] : ''; $timezone = isset($options['maintenance_timezone']) ? $options['maintenance_timezone'] : 'Asia/Shanghai'; $maintenance_mode = isset($options['maintenance_mode']) ? $options['maintenance_mode'] : '0'; echo "

当前设置:

"; echo "

维护模式状态: " . ($maintenance_mode === '1' ? '已启用' : '已禁用') . "

"; echo "

开始时间: " . ($start_time ? $start_time : '未设置') . "

"; echo "

结束时间: " . ($end_time ? $end_time : '未设置') . "

"; echo "

时区: " . $timezone . "

"; // 测试时区和时间处理 echo "

时间处理测试:

"; try { $tz = new DateTimeZone($timezone); $current_time = new DateTime('now', $tz); echo "

当前时间(" . $timezone . "): " . $current_time->format('Y-m-d H:i:s') . "

"; if (!empty($start_time)) { $start_datetime = new DateTime($start_time, $tz); echo "

开始时间解析: " . $start_datetime->format('Y-m-d H:i:s') . "

"; echo "

距离开始: " . ($start_datetime > $current_time ? '未开始' : '已开始') . "

"; } if (!empty($end_time)) { $end_datetime = new DateTime($end_time, $tz); echo "

结束时间解析: " . $end_datetime->format('Y-m-d H:i:s') . "

"; echo "

距离结束: " . ($end_datetime > $current_time ? '未结束' : '已结束') . "

"; } } catch (Exception $e) { echo "

时间处理错误: " . $e->getMessage() . "

"; } // 测试定时维护检查 echo "

定时维护检查测试:

"; $is_scheduled = check_scheduled_maintenance(); echo "

定时维护状态: " . ($is_scheduled ? '处于维护期间' : '不在维护期间') . "

"; // 测试WordPress定时任务 echo "

WordPress定时任务测试:

"; $start_scheduled = wp_next_scheduled('maintenance_mode_start'); $end_scheduled = wp_next_scheduled('maintenance_mode_end'); echo "

开始维护任务: "; if ($start_scheduled) { echo date('Y-m-d H:i:s', $start_scheduled) . " (时间戳: " . $start_scheduled . ")"; } else { echo "未安排"; } echo "

"; echo "

结束维护任务: "; if ($end_scheduled) { echo date('Y-m-d H:i:s', $end_scheduled) . " (时间戳: " . $end_scheduled . ")"; } else { echo "未安排"; } echo "

"; // 显示所有已安排的定时任务 echo "

所有定时任务:

"; $cron_jobs = _get_cron_array(); if (!empty($cron_jobs)) { echo ""; } else { echo "

没有找到相关的定时任务

"; } // 提供测试操作 echo "

测试操作:

"; echo "

重新安排定时任务

"; echo "

清除所有定时任务

"; echo "

强制开启维护模式

"; echo "

强制关闭维护模式

"; echo "

手动触发Cron

"; } /** * 处理测试操作 */ function handle_test_actions() { if (!isset($_GET['test_action']) || !current_user_can('manage_options')) { return; } $action = $_GET['test_action']; switch ($action) { case 'schedule_tasks': schedule_maintenance_mode_tasks(); echo "

定时任务已重新安排

"; break; case 'clear_tasks': wp_clear_scheduled_hook('maintenance_mode_start'); wp_clear_scheduled_hook('maintenance_mode_end'); echo "

所有维护模式定时任务已清除

"; break; case 'force_start': auto_start_maintenance_mode(); echo "

维护模式已强制开启

"; break; case 'force_end': auto_end_maintenance_mode(); echo "

维护模式已强制关闭

"; break; case 'trigger_cron': // 手动触发WordPress Cron wp_cron(); echo "

WordPress Cron已手动触发

"; break; } } // 如果是管理员且在测试页面,显示测试界面 if (is_admin() && current_user_can('manage_options') && isset($_GET['page']) && $_GET['page'] === 'maintenance-test') { add_action('admin_notices', 'handle_test_actions'); function maintenance_test_page() { echo "
"; test_scheduled_maintenance(); echo "
"; } // 添加测试页面到管理菜单 add_action('admin_menu', function() { add_submenu_page( 'themes.php', '维护模式测试', '维护模式测试', 'manage_options', 'maintenance-test', 'maintenance_test_page' ); }); } ?>