<?php
/*
Plugin Name: 财神组定制功能
Description: 水印、TG图标、版权声明、字体、LOGO及高级样式定制
*/

add_action('wp_head', function() {
    ?>
    <style>
        /* 强制中文字体 */
        body, h1, h2, h3, h4, h5, h6, p, a, span, div, li {
            font-family: "Microsoft YaHei", "PingFang SC", "Heiti SC", "Source Han Sans CN", sans-serif !important;
        }

        /* 全屏多端颜色水印 */
        .watermark-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 9998;
            pointer-events: none;
            overflow: hidden;
        }

        .watermark-text {
            position: absolute;
            width: 200%;
            height: 200%;
            top: -50%;
            left: -50%;
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200"><text x="20" y="100" font-family="Arial" font-size="20" font-weight="bold" fill="rgba(212,175,55,0.15)" transform="rotate(-25)">财神组专用</text></svg>');
            background-repeat: repeat;
            animation: color-shift 15s infinite linear;
        }

        @keyframes color-shift {
            0% { filter: hue-rotate(0deg); opacity: 0.4; }
            50% { filter: hue-rotate(180deg); opacity: 0.7; }
            100% { filter: hue-rotate(360deg); opacity: 0.4; }
        }

        /* 隐藏WordPress痕迹 */
        .wp-block-post-template__footer, .wp-block-footer, .powered-by-wordpress, .site-info, footer.wp-block-template-part, .wp-block-header {
            display: none !important;
        }

        /* 底部版权 */
        .custom-footer {
            text-align: center;
            padding: 40px;
            background: #fdfdfd;
            border-top: 1px solid #eee;
            color: #888;
            font-size: 14px;
            position: relative;
            z-index: 10;
        }

        /* 整体布局调整 */
        body {
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
            color: #333;
        }

        .site-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏布局 */
        .custom-sidebar {
            width: 320px;
            background: #0a0a0a;
            color: #fff;
            height: 100vh;
            position: fixed;
            left: 0;
            top: 0;
            overflow-y: auto;
            z-index: 1000;
            box-shadow: 4px 0 20px rgba(0,0,0,0.5);
            display: flex;
            flex-direction: column;
            border-right: 1px solid #222;
        }

        .sidebar-header {
            padding: 40px 20px;
            text-align: center;
            background: linear-gradient(180deg, #1a1a1a 0%, #0a0a0a 100%);
            border-bottom: 1px solid #d4af37;
        }

        .sidebar-logo {
            width: 120px;
            height: 120px;
            margin: 0 auto 15px;
            border-radius: 50%;
            border: 3px solid #d4af37;
            padding: 5px;
            background: #fff;
            object-fit: cover;
            box-shadow: 0 0 20px rgba(212, 175, 55, 0.4);
        }

        .custom-sidebar h1 {
            font-size: 22px;
            color: #d4af37;
            margin: 0;
            letter-spacing: 1px;
            font-weight: 900;
            text-shadow: 0 2px 4px rgba(0,0,0,0.5);
        }

        .custom-sidebar ul {
            list-style: none;
            padding: 20px 0;
            margin: 0;
            flex: 1;
        }

        .custom-sidebar a {
            color: #ccc;
            text-decoration: none;
            display: block;
            padding: 18px 25px;
            transition: all 0.3s;
            font-size: 15px;
            border-left: 4px solid transparent;
            position: relative;
            font-weight: 500;
        }

        .custom-sidebar li.active a, .custom-sidebar a:hover {
            background: rgba(212, 175, 55, 0.15);
            color: #fff;
            border-left-color: #d4af37;
        }

        /* 内容区布局 */
        .custom-content-wrapper {
            flex: 1;
            margin-left: 320px;
            background: #fff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            position: relative;
        }

        .custom-content {
            padding: 60px 80px;
            flex: 1;
            max-width: 900px;
            margin: 0 auto;
            width: 100%;
        }

        .entry-title {
            font-size: 32px;
            color: #111;
            margin-bottom: 40px;
            padding-bottom: 25px;
            border-bottom: 1px solid #eee;
            position: relative;
        }

        .entry-title::after {
            content: '';
            position: absolute;
            bottom: -1px;
            left: 0;
            width: 80px;
            height: 3px;
            background: #d4af37;
        }

        .entry-content {
            line-height: 1.8;
            color: #333;
            font-size: 18px;
            white-space: pre-wrap;
        }

        /* 重点标记 */
        b, strong {
            color: #d4af37;
            background: rgba(212, 175, 55, 0.05);
            padding: 0 4px;
            border-radius: 3px;
        }

        .highlight-box {
            border-left: 4px solid #d4af37;
            background: #fdfaf0;
            padding: 25px;
            margin: 25px 0;
            border-radius: 0 8px 8px 0;
            box-shadow: 2px 2px 10px rgba(0,0,0,0.02);
        }

        /* TG 闪动图标 */
        .tg-float {
            position: fixed;
            bottom: 40px;
            right: 40px;
            width: 60px;
            height: 60px;
            background: #229ED9;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            box-shadow: 0 4px 15px rgba(34, 158, 217, 0.4);
            z-index: 10000;
            animation: pulse 2s infinite;
            text-decoration: none;
        }
        .tg-float svg {
            width: 30px;
            height: 30px;
            fill: white;
        }

        @keyframes pulse {
            0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(34, 158, 217, 0.7); }
            70% { transform: scale(1.1); box-shadow: 0 0 0 15px rgba(34, 158, 217, 0); }
            100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(34, 158, 217, 0); }
        }

        .back-to-top {
            position: fixed;
            bottom: 120px;
            right: 45px;
            background: #fff;
            color: #333;
            width: 50px;
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 50%;
            cursor: pointer;
            z-index: 9999;
            font-size: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            transition: all 0.3s;
            border: 1px solid #eee;
        }
        .back-to-top:hover { 
            background: #d4af37;
            color: #fff;
            border-color: #d4af37;
        }

        @media (max-width: 1024px) {
            .custom-sidebar { width: 280px; }
            .custom-content-wrapper { margin-left: 280px; }
            .custom-content { padding: 40px 40px; }
        }

        @media (max-width: 782px) {
            .site-container { flex-direction: column; }
            .custom-sidebar { width: 100%; height: auto; position: static; }
            .custom-content-wrapper { margin-left: 0; }
            .custom-content { padding: 30px 20px; }
            .sidebar-header { padding: 20px; }
            .sidebar-logo { width: 60px; height: 60px; }
        }
    </style>
    <?php
});

// 重写整个前端页面结构
add_action('template_redirect', function() {
    if (is_admin() || is_login()) return;
    
    // 拦截所有前端页面请求
    if (is_feed() || is_trackback() || is_favicon()) return;

    global $post;
    
    // 处理首页跳转
    if (is_front_page()) {
        $front_page_id = get_option('page_on_front');
        if ($front_page_id) {
            $post = get_post($front_page_id);
        }
    }

    if (!$post || is_404()) {
        // 如果找不到页面，重定向到 ID 为 52 的页面（第1天）
        wp_redirect(get_permalink(52));
        exit;
    }
    
    setup_postdata($post);
    $current_id = $post->ID;
    $title = get_the_title($current_id);
    $content = apply_filters('the_content', $post->post_content);
    
    // 获取所有发布状态的页面，按菜单顺序排列
    $pages = get_posts(array(
        'post_type' => 'page',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC'
    ));
    
    // Logo URL
    $logo_url = '/wp-content/uploads/custom/logo.jpg';
    if (file_exists(ABSPATH . 'wp-content/uploads/custom/logo.jpg')) {
        $logo_url .= '?v=' . filemtime(ABSPATH . 'wp-content/uploads/custom/logo.jpg');
    }
    
    // 图标映射
    $icons = [
        1 => '👣', 
        2 => '🗣️', 
        3 => '❤️', 
        4 => '🎯', 
        5 => '✨', 
        6 => '📜',
    ];
    
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href="<?php echo $logo_url; ?>" type="image/jpeg">
        <link rel="shortcut icon" href="<?php echo $logo_url; ?>" type="image/jpeg">
        <?php wp_head(); ?>
        <title><?php echo $title; ?> - 财神组聊天构造</title>
    </head>
    <body <?php body_class(); ?>>
        <?php wp_body_open(); ?>
        <div class="watermark-overlay"><div class="watermark-text"></div></div>
        
        <div class="site-container">
            <div class="custom-sidebar">
                <div class="sidebar-header">
                    <img src="<?php echo $logo_url; ?>" alt="Logo" class="sidebar-logo">
                    <h1>财神组聊天构造</h1>
                </div>
                <ul>
                    <?php foreach ($pages as $index => $p): ?>
                        <?php 
                            $url = get_permalink($p->ID);
                            $icon = isset($icons[$index + 1]) ? $icons[$index + 1] : '💎';
                        ?>
                        <li class="<?php echo ($p->ID == $current_id) ? 'active' : ''; ?>">
                            <a href="<?php echo esc_url($url); ?>">
                                <span style="margin-right: 10px;"><?php echo $icon; ?></span>
                                <?php echo esc_html($p->post_title); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            
            <div class="custom-content-wrapper">
                <main class="custom-content">
                    <div class="content-card">
                        <h1 class="entry-title"><?php echo esc_html($title); ?></h1>
                        <div class="entry-content">
                            <?php echo $content; ?>
                        </div>
                    </div>
                </main>
                
                <footer class="custom-footer">
                    财神组内部资料，翻版必究
                </footer>
            </div>
        </div>

        <a href="https://t.me/zhangshihao818" class="tg-float" target="_blank" title="联系我们">
            <svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm4.64 6.8c-.15 1.58-.8 5.42-1.13 7.19-.14.75-.42 1-.68 1.03-.58.05-1.02-.38-1.58-.75-.88-.58-1.38-.94-2.23-1.5-.99-.65-.35-1.01.22-1.59.15-.15 2.71-2.48 2.76-2.69a.2.2 0 00-.05-.18c-.06-.05-.14-.03-.21-.02-.09.02-1.49.95-4.22 2.79-.4.27-.76.41-1.08.4-.36-.01-1.04-.2-1.55-.37-.63-.2-1.12-.31-1.08-.66.02-.18.27-.36.74-.55 2.92-1.27 4.86-2.11 5.83-2.51 2.78-1.16 3.35-1.36 3.73-1.36.08 0 .27.02.39.12.1.08.13.19.14.27-.01.06.01.24 0 .38z"/></svg>
        </a>
        <div class="back-to-top" onclick="window.scrollTo({top: 0, behavior: 'smooth'})" title="返回顶部">↑</div>
        
        <?php wp_footer(); ?>
    </body>
    </html>
    <?php
    exit;
});
