<?php
/**
 * TySoLive - XML Sitemap Generator
 * sitemap-vnfootball.xml
 *
 * 生成符合 Google Sitemap Protocol 2.0 的 XML sitemap，
 * 包含所有静态页面 + WordPress 博客文章/分类。
 * WordPress 5.5+ 内置 sitemap 使用 wp-sitemap.xml（Yoast 覆盖为 sitemap_index.xml）。
 * 本文件覆盖 /sitemap-vnfootball.xml，提供完整 sitemap（含自定义路由页面）。
 *
 * @site TySoLive / f.zxdu.net
 * @lastmod 2026-06-01
 */

// 引导 WordPress（从网站根目录相对路径）
$wp_load_path = dirname(__FILE__) . '/wp-load.php';
if (!file_exists($wp_load_path)) {
    $wp_load_path = dirname(__FILE__) . '/../wp-load.php';
}
if (!file_exists($wp_load_path)) {
    $wp_load_path = dirname(dirname(__FILE__)) . '/wp-load.php';
}
if (file_exists($wp_load_path)) {
    require_once $wp_load_path;
} else {
    // 无法加载 WordPress，输出 minimal header
    header('HTTP/1.1 503 Service Unavailable');
    exit;
}

// ── 设置 ──
header('Content-Type: application/xml; charset=' . get_bloginfo('charset'), true);
header('X-Robots-Tag: noindex, follow', true);
echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?>' . "\n";

// ── 站点基 URL ──
$base = home_url('/');
$site_name = get_bloginfo('name');

// ═══════════════════════════════════════════════════════════════════
//  静态页面 URL（自定义路由 + WordPress 页面）
// ═══════════════════════════════════════════════════════════════════
$static_urls = array(
    // 主导航页面（优先级高，更新频繁）
    array('loc' => $base,                                          'priority' => '1.0', 'changefreq' => 'hourly'),
    array('loc' => $base . 'truot-tiep/',                           'priority' => '0.9', 'changefreq' => '5min'),
    array('loc' => $base . 'lich-thi-dau/',                        'priority' => '0.9', 'changefreq' => 'hourly'),
    array('loc' => $base . 'ket-qua-hoan-thanh/',                  'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'bang-xep-hang/',                        'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'vua-pha-luoi/',                         'priority' => '0.8', 'changefreq' => 'weekly'),
    array('loc' => $base . 'video/',                                'priority' => '0.8', 'changefreq' => 'daily'),
    array('loc' => $base . 'tin-tuc/',                             'priority' => '0.8', 'changefreq' => 'hourly'),
    array('loc' => $base . 'tim-kiem/',                             'priority' => '0.6', 'changefreq' => 'weekly'),
    array('loc' => $base . 'giai-dau-list/',                       'priority' => '0.8', 'changefreq' => 'daily'),
    array('loc' => $base . 'cau-thu/',                              'priority' => '0.7', 'changefreq' => 'weekly'),

    // 各联赛主页（按 slug 友好 URL）
    array('loc' => $base . 'v-league-1/',                          'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'v-league-2/',                          'priority' => '0.7', 'changefreq' => 'weekly'),
    array('loc' => $base . 'cup-quoc-gia/',                         'priority' => '0.7', 'changefreq' => 'weekly'),
    array('loc' => $base . 'premier-league/',                       'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'la-liga/',                              'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'serie-a/',                              'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'bundesliga/',                          'priority' => '0.8', 'changefreq' => 'daily'),
    array('loc' => $base . 'ligue-1/',                              'priority' => '0.8', 'changefreq' => 'daily'),
    array('loc' => $base . 'champions-league/',                     'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => $base . 'europa-league/',                       'priority' => '0.8', 'changefreq' => 'daily'),

    // 信息页面（低频更新，固定内容）
    array('loc' => $base . 'gioi-thieu/',                          'priority' => '0.5', 'changefreq' => 'monthly'),
    array('loc' => $base . 'chinh-sach/',                           'priority' => '0.4', 'changefreq' => 'monthly'),
    array('loc' => $base . 'dieu-khoan/',                           'priority' => '0.4', 'changefreq' => 'monthly'),
    array('loc' => $base . 'lien-he/',                              'priority' => '0.5', 'changefreq' => 'monthly'),
    array('loc' => $base . 'sitemap-vnfootball/',                   'priority' => '0.3', 'changefreq' => 'weekly'),
);

// ═══════════════════════════════════════════════════════════════════
//  WordPress 文章（博客新闻）
// ═══════════════════════════════════════════════════════════════════
$post_urls = array();
$posts = get_posts(array(
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => 500,
    'orderby'        => 'modified',
    'order'          => 'DESC',
    'no_found_rows'  => true,
    'update_post_meta_cache' => false,
    'update_post_term_cache' => false,
));
foreach ($posts as $p) {
    $post_urls[] = array(
        'loc'        => get_permalink($p),
        'priority'   => '0.7',
        'changefreq' => 'weekly',
        'lastmod'    => get_the_modified_date('c', $p),
    );
}

// ═══════════════════════════════════════════════════════════════════
//  WordPress 分类页
// ═══════════════════════════════════════════════════════════════════
$term_urls = array();
$categories = get_terms(array(
    'taxonomy'   => 'category',
    'hide_empty' => true,
    'number'     => 100,
));
foreach ($categories as $cat) {
    $cat_url = get_term_link($cat);
    if (!is_wp_error($cat_url)) {
        $term_urls[] = array(
            'loc'        => $cat_url,
            'priority'   => '0.6',
            'changefreq' => 'daily',
        );
    }
}

// ═══════════════════════════════════════════════════════════════════
//  联赛专属页面（按 API league ID，动态生成 URL）
// ═══════════════════════════════════════════════════════════════════
$league_id_map = array(
    271 => 'v-league-1',
    340 => 'v-league-1',
    292 => 'v-league-2',
    2002 => 'cup-quoc-gia',
    39  => 'premier-league',
    140 => 'la-liga',
    135 => 'serie-a',
    78  => 'bundesliga',
    61  => 'ligue-1',
    2   => 'champions-league',
    3   => 'europa-league',
);
$league_urls = array();
foreach ($league_id_map as $id => $slug) {
    $league_urls[] = array(
        'loc'        => $base . $slug . '/',
        'priority'   => '0.9',
        'changefreq' => 'daily',
    );
    // 每个联赛的 BXH 页面
    $league_urls[] = array(
        'loc'        => $base . 'bang-xep-hang/' . $slug . '/',
        'priority'   => '0.8',
        'changefreq' => 'daily',
    );
    // 每个联赛的 VPL 页面
    $league_urls[] = array(
        'loc'        => $base . 'vua-pha-luoi/' . $slug . '/',
        'priority'   => '0.7',
        'changefreq' => 'weekly',
    );
}

// ═══════════════════════════════════════════════════════════════════
//  输出 XML
// ═══════════════════════════════════════════════════════════════════
$now = date('c');
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    <?php foreach ($static_urls as $u): ?>
    <url>
        <loc><?php echo esc_url($u['loc']); ?></loc>
        <priority><?php echo $u['priority']; ?></priority>
        <changefreq><?php echo $u['changefreq']; ?></changefreq>
        <lastmod><?php echo $now; ?></lastmod>
    </url>
    <?php endforeach; ?>

    <?php foreach ($league_urls as $u): ?>
    <url>
        <loc><?php echo esc_url($u['loc']); ?></loc>
        <priority><?php echo $u['priority']; ?></priority>
        <changefreq><?php echo $u['changefreq']; ?></changefreq>
        <lastmod><?php echo $now; ?></lastmod>
    </url>
    <?php endforeach; ?>

    <?php foreach ($post_urls as $u): ?>
    <url>
        <loc><?php echo esc_url($u['loc']); ?></loc>
        <priority><?php echo $u['priority']; ?></priority>
        <changefreq><?php echo $u['changefreq']; ?></changefreq>
        <?php if (!empty($u['lastmod'])): ?>
        <lastmod><?php echo esc_html($u['lastmod']); ?></lastmod>
        <?php endif; ?>
    </url>
    <?php endforeach; ?>

    <?php foreach ($term_urls as $u): ?>
    <url>
        <loc><?php echo esc_url($u['loc']); ?></loc>
        <priority><?php echo $u['priority']; ?></priority>
        <changefreq><?php echo $u['changefreq']; ?></changefreq>
        <lastmod><?php echo $now; ?></lastmod>
    </url>
    <?php endforeach; ?>

</urlset>
