<?php
defined( 'ABSPATH' ) || exit;

if ( wp_is_block_theme() ) {
	?><!DOCTYPE html>
	<html <?php language_attributes(); ?>>
	<head>
		<meta charset="<?php bloginfo( 'charset' ); ?>" />
		<?php
		wp_head();
		?>
	</head>
	<body <?php body_class(); ?>>
	<?php
	if ( ! $sp_smart_header_id ) {
		ob_start();
		block_template_part( 'header' );
		$header = ob_get_clean();
		echo '<header class="' . esc_attr( 'wp-block-template-part' ) . '">' . $header . '</header>'; // phpcs:ignore.
	}
	wp_body_open();
} else {
	get_header();
}

	$current_post_id = get_the_ID();
	$nav_menu_page   = get_post_meta( $current_post_id, '_sp_demo_import_page', true );

if ( ! empty( $nav_menu_page ) ) {
	$sp_page_builder_id = $current_post_id;
}

	$content_post = get_post( $sp_page_builder_id );
	$content      = $content_post->post_content;

	echo '<div class="sp-smart-post-page-builder">';

	global $wp_embed;
	$content = $wp_embed->autoembed( $content );
	$content = do_blocks( $content );
	$content = do_shortcode( $content );
	$content = str_replace( ']]>', ']]&gt;', $content );
	$content = preg_replace( '%<p>&nbsp;\s*</p>%', '', $content );
	$content = preg_replace( '/^(?:<br\s*\/?>\s*)+/', '', $content );
	echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
	echo '</div>';

if ( wp_is_block_theme() ) {
	?>
	</body>
	</html>
	<?php
	if ( ! $sp_smart_header_id ) {
		ob_start();
		block_template_part( 'footer' );
		$footer = ob_get_clean();
		echo '<footer class="' . esc_attr( 'wp-block-template-part' ) . '">' . $footer . '</footer>'; // phpcs:ignore 
	}
	wp_head();
	wp_footer();
} else {
	get_footer();
}
