<?php
/**
 * Dynamic XML Sitemap for deela.se
 *
 * Upload to: /STRATO-apps/wordpress_02/app/sitemap.xml.php
 * Then rename to sitemap.xml — or keep as sitemap.xml.php and add
 * a rewrite rule in .htaccess:
 *   RewriteRule ^sitemap\.xml$ sitemap.xml.php [L]
 *
 * Outputs proper XML with dynamic lastmod dates.
 * City/Lan pages use today's date since listings change hourly.
 * Static pages use their last WordPress modified date.
 */

// Prevent any output before headers
if ( ! defined( 'ABSPATH' ) ) {
    // Load WordPress if called directly
    $wp_load = dirname( __FILE__ ) . '/wp-load.php';
    if ( file_exists( $wp_load ) ) {
        require_once $wp_load;
    }
}

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

$today    = date( 'Y-m-d' );
$thisweek = date( 'Y-m-d', strtotime( '-7 days' ) );

// Get last modified dates for static pages from WordPress
function deela_page_lastmod( $slug ) {
    $page = get_page_by_path( $slug );
    if ( $page ) {
        return date( 'Y-m-d', strtotime( $page->post_modified ) );
    }
    return date( 'Y-m-d' );
}

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

  <!-- Core pages -->
  <url>
    <loc>https://deela.se/</loc>
    <lastmod><?php echo deela_page_lastmod( '' ); ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://deela.se/partners/</loc>
    <lastmod><?php echo deela_page_lastmod( 'partners' ); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://deela.se/blog/</loc>
    <lastmod><?php echo $thisweek; ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc>https://deela.se/privacy-policy/</loc>
    <lastmod><?php echo deela_page_lastmod( 'privacy-policy' ); ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.3</priority>
  </url>
  <url>
    <loc>https://deela.se/why-sharing-is-the-new-saving-how-swedes-are-reducing-waste-in-2026/</loc>
    <lastmod><?php echo deela_page_lastmod( 'why-sharing-is-the-new-saving-how-swedes-are-reducing-waste-in-2026' ); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://deela.se/why-swedes-dont-like-free-but-love-sharing/</loc>
    <lastmod><?php echo deela_page_lastmod( 'why-swedes-dont-like-free-but-love-sharing' ); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
  </url>

  <!-- Sverige — changes hourly -->
  <url>
    <loc>https://deela.se/sverige/</loc>
    <lastmod><?php echo $today; ?></lastmod>
    <changefreq>hourly</changefreq>
    <priority>0.9</priority>
  </url>

  <!-- City pages — listings change hourly so lastmod = today -->
<?php
$cities = [
    'stockholm'   => 0.9,
    'goteborg'    => 0.9,
    'malmo'       => 0.8,
    'uppsala'     => 0.8,
    'vasteras'    => 0.8,
    'orebro'      => 0.8,
    'linkoping'   => 0.8,
    'helsingborg' => 0.8,
    'jonkoping'   => 0.8,
    'norrkoping'  => 0.8,
];
foreach ( $cities as $slug => $priority ) : ?>
  <url>
    <loc>https://deela.se/<?php echo $slug; ?>/</loc>
    <lastmod><?php echo $today; ?></lastmod>
    <changefreq>hourly</changefreq>
    <priority><?php echo $priority; ?></priority>
  </url>
<?php endforeach; ?>

  <!-- Lan pages — listings change hourly -->
<?php
$lans = [
    'stockholms-lan'      => 0.8,
    'vastra-gotalands-lan'=> 0.8,
    'skane-lan'           => 0.8,
    'ostergotlands-lan'   => 0.7,
    'jonkopings-lan'      => 0.7,
    'vastmanlands-lan'    => 0.7,
    'orebro-lan'          => 0.7,
    'uppland'             => 0.7,
    'dalarna'             => 0.7,
    'halland'             => 0.7,
];
foreach ( $lans as $slug => $priority ) : ?>
  <url>
    <loc>https://deela.se/<?php echo $slug; ?>/</loc>
    <lastmod><?php echo $today; ?></lastmod>
    <changefreq>hourly</changefreq>
    <priority><?php echo $priority; ?></priority>
  </url>
<?php endforeach; ?>

</urlset>
