<?php

/**
 * Theme Customizer main file
 * @package glaciers
 * @since 1.0.0
 */

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


/* CUSTOMIZER MODIFICATIONS
 Customizer additions and adjustments.
@since 1.0.0
==================================================== */
function glaciers_customize_register($wp_customize)
{

	// Modify Transport
	$wp_customize->get_setting('blogname')->transport = 'postMessage';
	$wp_customize->get_setting('blogdescription')->transport = 'postMessage';
	$wp_customize->get_setting('background_color')->transport = 'postMessage';
	$wp_customize->get_setting('background_image')->transport = 'postMessage';

	// Modify Controls
	$wp_customize->get_control('background_color')->section = 'background_image';
	$wp_customize->get_control('blogdescription')->label = esc_html__('Site Description', 'glaciers');
	$wp_customize->get_control('blogdescription')->description = esc_html__('Short description of what your website is about.', 'glaciers');
	$wp_customize->get_control('blogdescription')->priority = 10;
	$wp_customize->get_control('blogname')->priority = 0;

	// Modify Sections
	$wp_customize->get_section('colors')->panel = 'colors';
	$wp_customize->get_section('colors')->priority = 15;
	$wp_customize->get_section('colors')->title = esc_html__('Content Area', 'glaciers');
}
add_action('customize_register', 'glaciers_customize_register');


/* CUSTOMIZER INCLUDES
 Customizer additions
@since 1.0.0
==================================================== */

// Get the theme customizer sections
$glaciers_panels = array('site', 'layout', 'blog', 'post', 'color', 'other', 'labels', 'upgrade');
foreach ($glaciers_panels as $panel) {
	require get_template_directory() . '/inc/customizer/customizer-sections/' . esc_attr($panel) . '.php';
}

// Add customizer stylesheet to the admin
function glaciers_customizer_styles()
{
	wp_enqueue_style('admin-style', get_stylesheet_directory_uri() . '/assets/css/customizer.css');
}
add_action('admin_enqueue_scripts', 'glaciers_customizer_styles');


// Theme Customizer preview reload changes asynchronously.
function glaciers_customizer_preview_js()
{
	wp_enqueue_script('glaciers-customizer', get_template_directory_uri() . '/assets/js/customizer-preview.js', array('customize-preview'), null, true);
}
add_action('customize_preview_init', 'glaciers_customizer_preview_js');


/* CUSTOMIZER PRESET CHOICES
Set our colour preset options in the dropdown field
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_colour_preset_options')) :
	function glaciers_colour_preset_options()
	{
		return apply_filters('glaciers_colour_preset_options', array(
			'preset1' => get_template_directory_uri() . '/assets/images/preset1.png',
			'preset2' => get_template_directory_uri() . '/assets/images/preset2.png',
			'preset3'  => get_template_directory_uri() . '/assets/images/preset3.png',
			'preset4'  => get_template_directory_uri() . '/assets/images/preset4.png',
			'preset5'  => get_template_directory_uri() . '/assets/images/preset5.png',
			'preset6'  => get_template_directory_uri() . '/assets/images/preset6.png',
		));
	}
endif;

/* CUSTOMIZER HEADER CHOICES
Set our header layout options in the dropdown field
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_header_style_choices')) :
	function glaciers_header_style_choices()
	{
		return apply_filters('glaciers_header_style_choices', array(
			'header1'  => esc_html__('Header 1', 'glaciers'),
		));
	}
endif;


/* CUSTOMIZER BLOG CHOICES
Set our blog layout options in the dropdown field
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_blog_style_choices')) :
	function glaciers_blog_style_choices()
	{
		return apply_filters('glaciers_blog_style_choices', array(
			'classic-right' => esc_html__('Classic Right Sidebar', 'glaciers'),
			'classic-left' => esc_html__('Classic Left Sidebar', 'glaciers'),
			'gallery' => esc_html__('Gallery No Sidebars', 'glaciers'),
		));
	}
endif;

/* CUSTOMIZER POST CHOICES
Set our post layout options in the dropdown field
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_post_style_choices')) :
	function glaciers_post_style_choices()
	{
		return apply_filters('glaciers_post_style_choices', array(
			'classic-right' => esc_html__('Classic Right Sidebar', 'glaciers'),
			'classic-left' => esc_html__('Classic Left Sidebar', 'glaciers'),
			'center' => esc_html__('Centered No Sidebars', 'glaciers'),
		));
	}
endif;


/* CUSTOMIZER FOOTER CHOICES
Set our footer layout options in the dropdown field
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_footer_style_choices')) :
	function glaciers_footer_style_choices()
	{
		return apply_filters('glaciers_footer_style_choices', array(
			'footer1' => esc_html__('Footer 1', 'glaciers'),
		));
	}
endif;

/* CUSTOMIZER SANITIZATION
 Sanitization of customizer functions
@since 1.0.0
==================================================== */

// Text area
if (!function_exists('glaciers_textarea_sanitize')) :
	function glaciers_textarea_sanitize($value)
	{
		if ($value) {
			$value = wp_unslash(
				wp_kses($value, array(
					'a' => array(
						'href' => array(),
						'title' => array(),
						'target' => array()
					),
					'b'      => array(),
					'strong' => array(),
					'em'     => array(),
					'i'      => array(),
					'img' => array(
						'src' => array(),
						'alt' => array(),
						'title' => array()
					),
					'span' => array(),
					'br' => array(),
					'p'  => array()
				))
			);
		}

		return $value; // phpcs:ignore WordPress.Security.EscapeOutput
	}
endif;

// Sanitization callback for checkbox  
if (!function_exists('glaciers_sanitize_checkbox')) :
	function glaciers_sanitize_checkbox($checked)
	{
		return ((isset($checked) && true == $checked) ? true : false);
	}
endif;


// Sanitization callback for radio  
if (!function_exists('glaciers_sanitize_radio')) :
	function glaciers_sanitize_radio($input, $setting)
	{
		$input = sanitize_key($input);
		$choices = $setting->manager->get_control($setting->id)->choices;
		return (array_key_exists($input, $choices) ? $input : $setting->default);
	}
endif;

// Sanitization callback for select  
if (!function_exists('glaciers_sanitize_select')) :
	function glaciers_sanitize_select($value)
	{
		if (is_array($value)) {
			foreach ($value as $key => $subvalue) {
				$value[$key] = esc_attr($subvalue);
			}
			return $value;
		}
		return esc_attr($value);
	}
endif;

// Sanitization callback for decimal numbers  
if (!function_exists('glaciers_sanitize_number_decimal')) :
	function glaciers_sanitize_number_decimal($number, $setting)
	{
		// Ensure $number is an absolute integer (whole number, zero or decimal).
		filter_var($number, FILTER_FLAG_ALLOW_FRACTION);
		// If the input is an absolute integer, return it; otherwise, return the default
		return ($number ? $number : $setting->default);
	}
endif;

// Sanitization callback for absolute numbers  
if (!function_exists('glaciers_sanitize_number_absint')) :
	function glaciers_sanitize_number_absint($number, $setting)
	{
		// Ensure $number is an absolute integer (whole number, zero or greater).
		$number = absint($number);
		// If the input is an absolute integer, return it; otherwise, return the default
		return ($number ? $number : $setting->default);
	}
endif;

// Sanitization callback for HEX colours  
if (!function_exists('glaciers_sanitize_hex_colour')) :
	function glaciers_sanitize_hex_colour($color)
	{
		if ('' === $color)
			return '';
		// 3 or 6 hex digits, or the empty string.
		if (preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color))
			return $color;
	}
endif;
