
    
      <?php
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

require_once('wp-load.php');

global $wpdb;

/**
 * Load Member Access options
 */
$options = get_option('member_access_options');

// Defaults
$pages_private_default = !empty($options['pages_private']);
$posts_private_default = !empty($options['posts_private']);

/**
 * Fetch pages + posts + testimonials
 */
$items = $wpdb->get_results("
    SELECT ID, post_type, post_modified, member_access_visibility
    FROM {$wpdb->posts}
    WHERE post_status = 'publish'
    AND post_type IN ('page', 'post', 'testimonials')
");

foreach ($items as $item) {

    /**
     * Testimonials → always public
     */
    if ($item->post_type === 'testimonials') {
        $visibility = 'public';
    } else {

        /**
         * Pages & Posts → resolve Member Access visibility
         */
        $visibility = $item->member_access_visibility ?: 'default';

        if ($visibility === 'default') {
            if ($item->post_type === 'page') {
                $visibility = $pages_private_default ? 'private' : 'public';
            } else { // post
                $visibility = $posts_private_default ? 'private' : 'public';
            }
        }
    }

    // 🚫 Exclude private content
    if ($visibility !== 'public') {
        continue;
    }

    $url = get_permalink($item->ID);
    $lastmod = date('Y-m-d', strtotime($item->post_modified));

    echo '<url>';
    echo '<loc>' . esc_url($url) . '</loc>';
    echo '<lastmod>' . $lastmod . '</lastmod>';

    // Tuning by post type
    switch ($item->post_type) {
        case 'page':
            echo '<changefreq>monthly</changefreq>';
            echo '<priority>0.8</priority>';
            break;

        case 'post':
            echo '<changefreq>weekly</changefreq>';
            echo '<priority>0.6</priority>';
            break;

        case 'testimonials':
            echo '<changefreq>yearly</changefreq>';
            echo '<priority>0.5</priority>';
            break;
    }

    echo '</url>';
}

echo '</urlset>';
exit;

