<?php
 
 /**
 * =============================================================================
 * @package			    Blue Raven (Free Version)
 * @Plugin URI			https://enterrahost.com/plugins/blue-raven
 * 
 * @Author			    EnterraHost
 * @Author URI			https://enterrahost.com
 * @License			    GPL-2.0-or-later
 *  
 * @Copyright			(c) 2025 EnterraHost
 * @File Description	Scheduling for Sitemap
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - SITE MAP CRON
 * ||   - CORE FUNCTIONS
 * || 
 * =============================================================================
 */

if ( ! defined( 'ABSPATH' ) ) exit; 

/*
============================================================
   SITE MAP CRON
============================================================
*/

class BlueRaven_Site_Map_Cron {

    ## =========== Hook name for our scheduled event =========== ##
    
    const CRON_HOOK = 'blue_raven_cron_site_map';

    public function __construct() {
    add_action(self::CRON_HOOK, array($this, 'execute_cron_task'));
    add_filter('cron_schedules', array($this, 'add_custom_cron_interval'));
}

public function add_custom_cron_interval($schedules) {
    $schedules['daily'] = array('interval' => 86400, 'display'  => __('Once Daily' , 'blue-raven'));
    return $schedules;
}

public function activate() { if (!wp_next_scheduled(self::CRON_HOOK)) { wp_schedule_event(time(), 'daily', self::CRON_HOOK);}}
public function deactivate() { wp_clear_scheduled_hook(self::CRON_HOOK);}
public function execute_cron_task() {
    BlueRaven_SendError("CRON", "blue-raven/includes/routines/admin-cron-site-map.php", "execute_cron_task", "Working");
         
/*
============================================================
    CORE FUNCTIONS
============================================================
*/  

    ## =========== Start the sitemap generation =========== ##
        
        ob_start();
        
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" 
                      xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" 
                      xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\">\n";
    
    ## =========== Start the sitemap generation =========== ##
    
        function BlueRaven_get_all_content($post_types = ['post', 'page']) {
            $args = ['post_type' => $post_types,'post_status' => 'publish','numberposts' => -1,'has_password' => false];
            return get_posts($args);
        }
    
    ## =========== Include standard post types plus any public custom post types =========== ##
    
        $post_types = array_merge(['post', 'page'], get_post_types(['public' => true, '_builtin' => false]));
    
        foreach (BlueRaven_get_all_content($post_types) as $item) {
            echo "<url>\n";
            echo "<loc>" . esc_url(get_permalink($item->ID)) . "</loc>\n";
            echo '<lastmod>' . esc_html(get_the_modified_time('c', $item->ID)) . "</lastmod>\n";
            echo "<priority>" . ($item->post_type == 'page' ? '0.8' : '0.6') . "</priority>\n";
            echo "<changefreq>weekly</changefreq>\n";

    ## =========== Add images =========== ##
    
            $images = get_attached_media('image', $item->ID);
            if ($images) {
                foreach ($images as $image) {
                    echo "<image:image>\n";
                    echo "<image:loc>" . esc_url(wp_get_attachment_url($image->ID)) . "</image:loc>\n";
                    $image_title = !empty($image->post_title) ? $image->post_title : 'Image';
                    echo '<image:title>' . esc_xml($image_title) . "</image:title>\n";
                    if (!empty($image->post_excerpt)) {echo '<image:caption>' . esc_xml($image->post_excerpt) . "</image:caption>\n";}
                    echo "</image:image>\n";
                }
            }
            
            echo "</url>\n";
        }
    
        echo "</urlset>\n";
    
        $sitemap_content = ob_get_clean();

		$document_root = isset($_SERVER['DOCUMENT_ROOT']) ? sanitize_text_field(wp_unslash($_SERVER['DOCUMENT_ROOT'])) : ABSPATH;
		$sitemap_file = rtrim($document_root, '/') . '/br-sitemap.xml';
	
    ## =========== Save the sitemap file =========== ##
    
        file_put_contents($sitemap_file, $sitemap_content);
    }
}

new BlueRaven_Site_Map_Cron();
