<?php

use ThemeAtelier\DomainForSale\Frontend\Templates\Template_01;

$options = get_option('dfs-opt');
$single_page_header_footer = isset($options['single_page_header_footer']) ? $options['single_page_header_footer'] : '';

if ($single_page_header_footer) {
    global $wp_version;

    $is_block_theme = version_compare($wp_version, '5.9', '>=') && function_exists('wp_is_block_theme') && wp_is_block_theme();

    if ($is_block_theme) {
        $theme = wp_get_theme();
        $theme_slug = $theme->get('TextDomain');
?>
        <!doctype html>
        <html <?php language_attributes(); ?>>

        <head>
            <meta charset="<?php bloginfo('charset'); ?>">
            <?php wp_head(); ?>
        </head>

        <body <?php body_class(); ?>>
            <?php wp_body_open(); ?>
            <div class="wp-site-blocks">
            <?php
            echo do_blocks('<!-- wp:template-part {"slug":"header","theme":"' . $theme_slug . '","tagName":"header","className":"site-header","layout":{"inherit":true}} /-->');
        } else {
            get_header();
        }
    } else {
            ?>

            <!DOCTYPE html>
            <html <?php language_attributes(); ?>>

            <head>
                <meta charset="<?php bloginfo('charset'); ?>">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <?php wp_head(); ?>
            </head>

            <body>
            <?php
        }


        while (have_posts()) : the_post();

            $dfs_id  = get_the_ID();
            $domain_options = get_post_meta($dfs_id, 'dfs_template', true);
            $layout = get_post_meta($dfs_id, 'dfs_layouts', true);
            $template_options  = get_post_meta($dfs_id, 'dfs_template_options', true);
            $dfs_layout_preset = isset($layout['dfs_layout_preset']) ? $layout['dfs_layout_preset'] : '';

            if ('layout_01' === $dfs_layout_preset) :
                Template_01::content($template_options);
            endif;

        endwhile;

        if ($single_page_header_footer) {
            if ($is_block_theme) {
                echo do_blocks('<!-- wp:template-part {"slug":"footer","theme":"' . $theme_slug . '","tagName":"footer","className":"site-footer","layout":{"inherit":true}} /-->');
                echo '</div>';
                wp_footer();
                echo '</body>';
                echo '</html>';
            } else {
                get_footer();
            }
        } else {
            wp_footer();
            ?>
            </body>

            </html>
        <?php
        }
