<?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    Settings Page
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - WP AJAX BLUE RAVEN LOAD SETTINGS
 * || 
 * =============================================================================
 */

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

/*
============================================================
    WP AJAX BLUE RAVEN LOAD SETTINGS
============================================================
*/

add_action('wp_ajax_blue_raven_load_sub_settings', 'blue_raven_load_sub_settings_ajax');

function blue_raven_load_sub_settings_ajax() {
    
    ## =========== Security check =========== ##
    
    if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'blue_raven_admin_settings_nonce')) {
        BlueRaven_SendError("ERROR", "blue-raven/includes/admin-settings-pages.php", "blue_raven_load_sub_settings_ajax", "Invalid nonce");
        wp_send_json_error('Invalid nonce');
    }
    
    ## =========== Get Page =========== ##
     
    $page = sanitize_text_field(wp_unslash($_POST['page'] ?? ''));
	$url  = sanitize_text_field(wp_unslash($_POST['pageURL'] ?? ''));
    parse_str($url, $params);
    
    ## =========== Settings =========== ##
        
    if ($page === 'settings') {
        if (!class_exists('BlueRaven_Admin_Settings')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/class-admin-settings.php';
    
        $dashboard = new BlueRaven_Admin_Settings();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- contains trusted, pre-rendered HTML
        echo $content;
        
    ## =========== User =========== ##
    
    }else if ($page === 'blue-raven-pro') {
        if (!class_exists('BlueRaven_Admin_BlueRavenPro')) require_once BLUERAVEN_PLUGIN_DIR . '/templates/class-admin-more-on-pro.php';
    
        $dashboard = new BlueRaven_Admin_BlueRavenPro();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- contains trusted, pre-rendered HTML
        echo $content;
        
    ## =========== User =========== ##
    
    }else if ($page === 'feather') { 
        echo esc_html(BlueRaven_FeatherCheck());
        
    }else {
        echo 'Page not found.';
    }

    wp_die(); 
}