<?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
 * @Contents        The Page control for main page
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - WP AJAX BLUE RAVEN LOAD SUB MENUS
 * || 
 * =============================================================================
 */

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

/*
============================================================
    WP AJAX BLUE RAVEN LOAD SUB MENUS
============================================================
*/

add_action('wp_ajax_blue_raven_load_sub_menus', 'blue_raven_load_sub_menus_ajax');

function blue_raven_load_sub_menus_ajax() {
    
    ## =========== Security check =========== ##
    
    $nonce = wp_unslash( sanitize_text_field( filter_input(INPUT_POST, 'nonce') ?? '' ) );

    if (empty($nonce) || !wp_verify_nonce($nonce, 'blue_raven_admin_nonce')) {
        BlueRaven_SendError("ERROR", "blue-raven/includes/admin-pages.php", "blue_raven_load_sub_menus_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);
    
    ## =========== Home =========== ##
        
    if ($page === 'home') {

        if (!class_exists('BlueRaven_Admin_Dashboard')) require_once BLUERAVEN_PLUGIN_DIR . 'templates/class-admin-dashboard.php';
    
        $dashboard = new BlueRaven_Admin_Dashboard();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- contains trusted, pre-rendered HTML
        echo $content;
        
    ## =========== my Products =========== ##
    
    }else if ($page === 'my-products') {
        $product = isset($params['product']) && $params['product'] !== '' ? sanitize_text_field($params['product']) : '';
        
        if (!class_exists('Blue_Raven_Admin_My_Products')) require_once BLUERAVEN_PLUGIN_DIR . 'templates/class-admin-my-products.php';
        
        $dashboard = new Blue_Raven_Admin_My_Products();
        ob_start();
        $dashboard->render($product);
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- contains trusted, pre-rendered HTML
        echo $content;
        
    ## =========== Tools =========== ##
    
    }else if ($page === 'tools') {

        if (!class_exists('Blue_Raven_Admin_Tools')) require_once BLUERAVEN_PLUGIN_DIR . 'templates/class-admin-tools.php';

        $dashboard = new Blue_Raven_Admin_Tools();
        ob_start();
        $dashboard->render();
        $content = ob_get_clean();
        
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- contains trusted, pre-rendered HTML
        echo $content;
        
    }else if ($page === 'feather') { 
        echo esc_html(BlueRaven_FeatherCheck());
        
    }else {
        echo 'Page not found.';
    }

    wp_die(); 
}