<?php
/**
 * 维护模式定时功能测试文件
 * 用于测试和调试定时维护模式功能
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

/**
 * 测试定时维护模式功能
 */
function test_scheduled_maintenance() {
    echo "<h2>定时维护模式功能测试</h2>";
    
    // 获取当前设置
    $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 "<h3>当前设置：</h3>";
    echo "<p><strong>维护模式状态：</strong> " . ($maintenance_mode === '1' ? '已启用' : '已禁用') . "</p>";
    echo "<p><strong>开始时间：</strong> " . ($start_time ? $start_time : '未设置') . "</p>";
    echo "<p><strong>结束时间：</strong> " . ($end_time ? $end_time : '未设置') . "</p>";
    echo "<p><strong>时区：</strong> " . $timezone . "</p>";
    
    // 测试时区和时间处理
    echo "<h3>时间处理测试：</h3>";
    try {
        $tz = new DateTimeZone($timezone);
        $current_time = new DateTime('now', $tz);
        echo "<p><strong>当前时间（" . $timezone . "）：</strong> " . $current_time->format('Y-m-d H:i:s') . "</p>";
        
        if (!empty($start_time)) {
            $start_datetime = new DateTime($start_time, $tz);
            echo "<p><strong>开始时间解析：</strong> " . $start_datetime->format('Y-m-d H:i:s') . "</p>";
            echo "<p><strong>距离开始：</strong> " . ($start_datetime > $current_time ? '未开始' : '已开始') . "</p>";
        }
        
        if (!empty($end_time)) {
            $end_datetime = new DateTime($end_time, $tz);
            echo "<p><strong>结束时间解析：</strong> " . $end_datetime->format('Y-m-d H:i:s') . "</p>";
            echo "<p><strong>距离结束：</strong> " . ($end_datetime > $current_time ? '未结束' : '已结束') . "</p>";
        }
        
    } catch (Exception $e) {
        echo "<p style='color: red;'><strong>时间处理错误：</strong> " . $e->getMessage() . "</p>";
    }
    
    // 测试定时维护检查
    echo "<h3>定时维护检查测试：</h3>";
    $is_scheduled = check_scheduled_maintenance();
    echo "<p><strong>定时维护状态：</strong> " . ($is_scheduled ? '处于维护期间' : '不在维护期间') . "</p>";
    
    // 测试WordPress定时任务
    echo "<h3>WordPress定时任务测试：</h3>";
    $start_scheduled = wp_next_scheduled('maintenance_mode_start');
    $end_scheduled = wp_next_scheduled('maintenance_mode_end');
    
    echo "<p><strong>开始维护任务：</strong> ";
    if ($start_scheduled) {
        echo date('Y-m-d H:i:s', $start_scheduled) . " (时间戳: " . $start_scheduled . ")";
    } else {
        echo "未安排";
    }
    echo "</p>";
    
    echo "<p><strong>结束维护任务：</strong> ";
    if ($end_scheduled) {
        echo date('Y-m-d H:i:s', $end_scheduled) . " (时间戳: " . $end_scheduled . ")";
    } else {
        echo "未安排";
    }
    echo "</p>";
    
    // 显示所有已安排的定时任务
    echo "<h3>所有定时任务：</h3>";
    $cron_jobs = _get_cron_array();
    if (!empty($cron_jobs)) {
        echo "<ul>";
        foreach ($cron_jobs as $timestamp => $jobs) {
            foreach ($jobs as $hook => $job_array) {
                if (strpos($hook, 'maintenance_mode') !== false) {
                    echo "<li><strong>" . $hook . ":</strong> " . date('Y-m-d H:i:s', $timestamp) . "</li>";
                }
            }
        }
        echo "</ul>";
    } else {
        echo "<p>没有找到相关的定时任务</p>";
    }
    
    // 提供测试操作
    echo "<h3>测试操作：</h3>";
    echo "<p><a href='?test_action=schedule_tasks' class='button'>重新安排定时任务</a></p>";
    echo "<p><a href='?test_action=clear_tasks' class='button'>清除所有定时任务</a></p>";
    echo "<p><a href='?test_action=force_start' class='button'>强制开启维护模式</a></p>";
    echo "<p><a href='?test_action=force_end' class='button'>强制关闭维护模式</a></p>";
    echo "<p><a href='?test_action=trigger_cron' class='button'>手动触发Cron</a></p>";
}

/**
 * 处理测试操作
 */
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 "<div class='notice notice-success'><p>定时任务已重新安排</p></div>";
            break;
            
        case 'clear_tasks':
            wp_clear_scheduled_hook('maintenance_mode_start');
            wp_clear_scheduled_hook('maintenance_mode_end');
            echo "<div class='notice notice-success'><p>所有维护模式定时任务已清除</p></div>";
            break;
            
        case 'force_start':
            auto_start_maintenance_mode();
            echo "<div class='notice notice-success'><p>维护模式已强制开启</p></div>";
            break;
            
        case 'force_end':
            auto_end_maintenance_mode();
            echo "<div class='notice notice-success'><p>维护模式已强制关闭</p></div>";
            break;
            
        case 'trigger_cron':
            // 手动触发WordPress Cron
            wp_cron();
            echo "<div class='notice notice-success'><p>WordPress Cron已手动触发</p></div>";
            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 "<div class='wrap'>";
        test_scheduled_maintenance();
        echo "</div>";
    }
    
    // 添加测试页面到管理菜单
    add_action('admin_menu', function() {
        add_submenu_page(
            'themes.php',
            '维护模式测试',
            '维护模式测试',
            'manage_options',
            'maintenance-test',
            'maintenance_test_page'
        );
    });
}
?>