<?php

/**
 * Customizer Layout options
 * @package Glaciers
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly.
}

if (!function_exists('glaciers_customize_register_layout')) :
	function glaciers_customize_register_layout($wp_customize)
	{

		$wp_customize->add_section('glaciers_layout_settings', array(
			'title'      => esc_html__('Layout Options', 'glaciers'),
			'priority'   => 21,
			'capability' => 'edit_theme_options',
		));

		/** Boxed Layout */
		$wp_customize->add_setting(
			'glaciers_boxed',
			array(
				'default'  => false,
				'sanitize_callback' => 'glaciers_sanitize_checkbox',
			)
		);

		$wp_customize->add_control(
			new Glaciers_Toggle_Control(
				$wp_customize,
				'glaciers_boxed',
				array(
					'section'     => 'glaciers_layout_settings',
					'label'	      => esc_html__('Boxed Layout', 'glaciers'),
					'description' => esc_html__('Enable for a boxed layout.', 'glaciers'),
				)
			)
		);

		// Header Layout
		$wp_customize->add_setting(
			'glaciers_header_layout',
			array(
				'default' => 'header1',
				'sanitize_callback' => 'glaciers_sanitize_select',
			)
		);

		$wp_customize->add_control(
			new Glaciers_Select_Control(
				$wp_customize,
				'glaciers_header_layout',
				array(

					'label'   => esc_html__('Header Style', 'glaciers'),
					'section' => 'glaciers_layout_settings',
					'choices' => glaciers_header_style_choices()
				)
			)
		);

		// Blog Layout
		$wp_customize->add_setting(
			'glaciers_blog_style',
			array(
				'default' => 'classic-right',
				'sanitize_callback' => 'glaciers_sanitize_select',
			)
		);

		$wp_customize->add_control(
			new Glaciers_Select_Control(
				$wp_customize,
				'glaciers_blog_style',
				array(
					'label'   => esc_html__('Blog Style', 'glaciers'),
					'section' => 'glaciers_layout_settings',
					'choices' => glaciers_blog_style_choices()
				)
			)
		);


		// Post Layout
		$wp_customize->add_setting(
			'glaciers_post_style',
			array(
				'default' => 'classic-right',
				'sanitize_callback' => 'glaciers_sanitize_select',
			)
		);

		$wp_customize->add_control(
			new Glaciers_Select_Control(
				$wp_customize,
				'glaciers_post_style',
				array(
					'label'   => esc_html__('Post Style', 'glaciers'),
					'section' => 'glaciers_layout_settings',
					'choices' => glaciers_post_style_choices()
				)
			)
		);

		// Footer Layout
		$wp_customize->add_setting(
			'glaciers_footer_style',
			array(
				'default' => 'footer1',
				'sanitize_callback' => 'glaciers_sanitize_select',
			)
		);

		$wp_customize->add_control(
			new Glaciers_Select_Control(
				$wp_customize,
				'glaciers_footer_style',
				array(

					'label'   => esc_html__('Footer Style', 'glaciers'),
					'section' => 'glaciers_layout_settings',
					'choices' => glaciers_footer_style_choices()
				)
			)
		);

		// Add Partial for single Layout
		$wp_customize->selective_refresh->add_partial('glaciers_customize_partial_single_post', array(
			'selector'         => '.site-content',
			'settings'         => array(
				'glaciers_single_layout',
			),
			'render_callback'  => 'glaciers_customize_partial_single_content',
			'fallback_refresh' => false,
		));
	}

endif;
add_action('customize_register', 'glaciers_customize_register_layout');


// Render the blog content for the selective refresh partial.
function glaciers_customize_partial_blog_content()
{
	while (have_posts()) {
		the_post();
		get_template_part('template-parts/content', get_post_format());
	}
}

// Render single posts partial
function glaciers_customize_partial_single_post()
{
	while (have_posts()) {
		the_post();
		get_template_part('template-parts/content', 'single');
	}
}
