<?php
/**
 * Sitemap XML dynamique CreaMod3D
 * Ce fichier génère le sitemap à chaque visite
 */

// Charger WordPress
require_once dirname(__FILE__) . '/wp-load.php';

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex, follow');

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

<!-- Page d'accueil -->
<url>
    <loc><?php echo esc_url(home_url('/')); ?></loc>
    <lastmod><?php echo current_time('c'); ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>

<!-- Page Boutique -->
<?php $shop_id = wc_get_page_id('shop'); if ($shop_id > 0) : ?>
<url>
    <loc><?php echo esc_url(get_permalink($shop_id)); ?></loc>
    <lastmod><?php echo get_post_modified_time('c', true, $shop_id); ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
</url>
<?php endif; ?>

<!-- Catégories -->
<?php
$categories = get_terms(array('taxonomy' => 'product_cat', 'hide_empty' => true));
if (!is_wp_error($categories)) :
    foreach ($categories as $cat) :
        $cat_url = get_term_link($cat);
        if (is_wp_error($cat_url)) continue;
        $priority = $cat->count >= 10 ? '0.8' : '0.7';
?>
<url>
    <loc><?php echo esc_url($cat_url); ?></loc>
    <lastmod><?php echo current_time('c'); ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority><?php echo $priority; ?></priority>
<?php
        $thumb_id = get_term_meta($cat->term_id, 'thumbnail_id', true);
        if ($thumb_id) :
            $img = wp_get_attachment_url($thumb_id);
            if ($img) :
?>
    <image:image>
        <image:loc><?php echo esc_url($img); ?></image:loc>
        <image:caption><?php echo esc_html($cat->name); ?></image:caption>
    </image:image>
<?php endif; endif; ?>
</url>
<?php endforeach; endif; ?>

<!-- Produits -->
<?php
$products = get_posts(array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'post_status' => 'publish'
));
foreach ($products as $p) :
    $wc = wc_get_product($p->ID);
    if (!$wc) continue;
    $priority = '0.8';
    if ($wc->is_featured()) $priority = '1.0';
    elseif ($wc->is_on_sale()) $priority = '0.9';
?>
<url>
    <loc><?php echo esc_url(get_permalink($p->ID)); ?></loc>
    <lastmod><?php echo get_post_modified_time('c', true, $p); ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority><?php echo $priority; ?></priority>
<?php
    // Image principale
    $img_id = $wc->get_image_id();
    if ($img_id) :
        $img_url = wp_get_attachment_url($img_id);
        $img_alt = get_post_meta($img_id, '_wp_attachment_image_alt', true);
        if ($img_url) :
?>
    <image:image>
        <image:loc><?php echo esc_url($img_url); ?></image:loc>
<?php if ($img_alt) : ?>
        <image:caption><?php echo esc_html($img_alt); ?></image:caption>
<?php endif; ?>
    </image:image>
<?php endif; endif; ?>
<?php
    // Images galerie
    $gallery = $wc->get_gallery_image_ids();
    foreach ($gallery as $gid) :
        $gurl = wp_get_attachment_url($gid);
        $galt = get_post_meta($gid, '_wp_attachment_image_alt', true);
        if ($gurl) :
?>
    <image:image>
        <image:loc><?php echo esc_url($gurl); ?></image:loc>
<?php if ($galt) : ?>
        <image:caption><?php echo esc_html($galt); ?></image:caption>
<?php endif; ?>
    </image:image>
<?php endif; endforeach; ?>
</url>
<?php endforeach; ?>

<!-- Pages -->
<?php
$excluded = array(
    wc_get_page_id('cart'),
    wc_get_page_id('checkout'),
    wc_get_page_id('myaccount'),
    wc_get_page_id('shop'),
    get_option('page_on_front')
);
$pages = get_posts(array('post_type' => 'page', 'posts_per_page' => -1, 'post_status' => 'publish'));
foreach ($pages as $pg) :
    if (in_array($pg->ID, $excluded)) continue;
?>
<url>
    <loc><?php echo esc_url(get_permalink($pg->ID)); ?></loc>
    <lastmod><?php echo get_post_modified_time('c', true, $pg); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
</url>
<?php endforeach; ?>

</urlset>
