<?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	Blue Raven Tools and Tool Settings
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - WP AJAX BLUE RAVEN LOAD TOOLS SUB CONTAINER 
 * ||   - MAIL FORM BUILDER TOOL
 * ||   - SEACHABILITY TOOLS
 * ||   - COOKIES
 * ||   - ALT TEXT
 * || 
 * =============================================================================
 */

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

/*
============================================================
    WP AJAX BLUE RAVEN LOAD TOOLS SUB CONTAINER 
============================================================
*/

add_action('wp_ajax_blue_raven_load_tools_sub_container', 'blue_raven_load_tools_sub_container');

function blue_raven_load_tools_sub_container() {
    
    ## =========== Security check =========== ##
    
    $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';

	if (!wp_verify_nonce($nonce, 'blue_raven_load_nonce')) {
		BlueRaven_SendError("ERROR", "blue-raven/includes/admin-tools-subpages.php", "blue_raven_load_tools_sub_container", "Invalid nonce");
		wp_send_json_error('Invalid nonce');
	}

    ## =========== Get Page =========== ##
        
	$page = sanitize_text_field(wp_unslash($_POST['page'] ?? ''));

    
/*
============================================================
    MAIL FORM BUILDER TOOL
============================================================
*/

    ## =========== Mail =========== ##
    
    if ($page === 'mail') {

        if (!class_exists('Blue_Raven_Admin_Tools_Mail')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-mail.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Mail();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
/*
============================================================
    SEACHABILITY TOOLS
============================================================
*/  

    ## =========== Robots =========== ##
    
    }else if ($page === 'robots') {

        if (!class_exists('Blue_Raven_Admin_Tools_Robots')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-robots.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Robots();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
        
    ## =========== Site Map =========== ##    
        
    }else if ($page === 'sitemap') {

        if (!class_exists('Blue_Raven_Admin_Tools_SiteMap')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-sitemap.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_SiteMap();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
        
    ## =========== Analytics =========== ##        
        
    }else if ($page === 'atags') {

        if (!class_exists('Blue_Raven_Admin_Tools_Analytics')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-analytics.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Analytics();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
        
    ## =========== Main Schema =========== ##             
        
    }else if ($page === 'mschema') {

        if (!class_exists('Blue_Raven_Admin_Tools_Main_Schema')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-main-schema.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Main_Schema();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
        
    ## =========== Main Meta =========== ##   
        
    }else if ($page === 'mmeta') {

        if (!class_exists('Blue_Raven_Admin_Tools_Main_Meta')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-meta.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Main_Meta();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
        
    ## =========== Verification =========== ##   
        
    }else if ($page === 'verification') {

        if (!class_exists('Blue_Raven_Admin_Tools_Verification')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-verification.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Verification();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;
        
/*
============================================================
    COOKIES
============================================================
*/     
    
    ## =========== Cookies =========== ##
    
    }else if ($page === 'cookies') {
        
/*
============================================================
    ALT TEXT
============================================================
*/     
    
    ## =========== Alt =========== ##
        
    }else if ($page === 'alt') {

        if (!class_exists('Blue_Raven_Admin_Tools_Alt')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/tools/class-tools-alt.php';
        
        $dashboard = new Blue_Raven_Admin_Tools_Alt();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is fully escaped or trusted template output
        echo $content;        
        
    }else {
        BlueRaven_SendError("ERROR", "blue-raven/includes/admin-tools-subpages.php", "blue_raven_load_tools_sub_container", "Tool not found");
        echo 'Tool not found.';
    }

    wp_die(); 
}