<?php
/**
 * Layout Renderers for Global Header and Footer
 * 
 * Provides functions to render global header and footer HTML based on
 * settings configured in the admin settings page.
 * 
 * @package Author_Website_Templates
 * @since 1.0.9
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Get Global Header HTML
 * 
 * Renders the global header using a selected Reusable Block from settings.
 * 
 * @return string Header HTML or empty string if no block selected
 */
function rswpthemes_awt_get_global_header()
{
    // 1. Get global settings
    $settings = get_option('awt_global_settings', []);

    // 2. Get the Slug (Text)
    $block_slug = isset($settings['awt_global_header_block']) ? $settings['awt_global_header_block'] : '';

    // Return empty if no slug is saved
    if (empty($block_slug)) {
        return '';
    }

    // 3. Convert Slug to ID using helper function
    // Check if helper exists to prevent fatal errors
    if (!function_exists('rswpthemes_awt_get_block_id_by_slug')) {
        return '';
    }

    $block_id = rswpthemes_awt_get_block_id_by_slug($block_slug);

    // 4. Validate ID
    if (empty($block_id)) {
        return '';
    }

    // 5. Get the block post object
    $block_post = get_post($block_id);

    // 6. Verify the post exists, is a wp_block, and is published
    if (!$block_post || $block_post->post_type !== 'wp_block' || $block_post->post_status !== 'publish') {
        return '';
    }

    // 7. Get the raw content from the post
    $content = $block_post->post_content;

    // 8. Process blocks (YOUR LOGIC)
    $rendered_content = do_blocks($content);

    return $rendered_content;
}

/**
 * Get Global Footer HTML
 * 
 * Renders the global footer using a selected Reusable Block from settings.
 * 
 * @return string Footer HTML or empty string if no block selected
 */
function rswpthemes_awt_get_global_footer()
{
    // 1. Get global settings
    $settings = get_option('awt_global_settings', []);

    // 2. Get the Slug
    $block_slug = isset($settings['awt_global_footer_block']) ? $settings['awt_global_footer_block'] : '';

    if (empty($block_slug)) {
        return '';
    }

    // 3. Convert Slug to ID
    if (!function_exists('rswpthemes_awt_get_block_id_by_slug')) {
        return '';
    }

    $block_id = rswpthemes_awt_get_block_id_by_slug($block_slug);

    if (empty($block_id)) {
        return '';
    }

    // 4. Get the block post
    $block_post = get_post($block_id);

    // 5. Verify post
    if (!$block_post || $block_post->post_type !== 'wp_block' || $block_post->post_status !== 'publish') {
        return '';
    }

    // 6. Get content and render
    $content = $block_post->post_content;
    $rendered_content = do_blocks($content);

    return $rendered_content;
}