<?php
/**
 * dulcie Theme Customizer
 *
 * @package dulcie
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function dulcie_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial(
			'blogname',
			array(
				'selector'        => '.site-title a',
				'render_callback' => 'dulcie_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'dulcie_customize_partial_blogdescription',
			)
		);

		/*Main custom panel for theme settings*/
		$wp_customize->add_panel(
			'dulcie_panel',
			array(
				'priority'   => 10,
				'capability' => 'edit_theme_options',
				'title'      => __( 'Theme Options', 'dulcie' ),
			)
		);

		/*Header Options*/
		$wp_customize->add_section(
			'header_section',
			array(
				'title' => __( 'Header Options', 'dulcie' ),
				'panel' => 'dulcie_panel',
			)
		);

		/*Search Icon*/
		$wp_customize->add_setting(
			'dulcie-search-icon',
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => 1,
				'sanitize_callback' => 'dulcie_sanitize_checkbox',
			)
		);

		$wp_customize->add_control(
			'dulcie-search-icon',
			array(
				'label'       => __( 'Show Search Icon', 'dulcie' ),
				'description' => __( 'Checked to show the search icon.', 'dulcie' ),
				'section'     => 'header_section',
				'type'        => 'checkbox',
			)
		);


		/*Header  Title Text*/
		$wp_customize->add_setting(
			'header-title-text',
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => __( 'News and Stories', 'dulcie' ),
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'header-title-text',
			array(
				'label'       => __( 'Header Title', 'dulcie' ),
				'description' => __( 'Enter your Header title text here.', 'dulcie' ),
				'section'     => 'header_section',
				'type'        => 'text',
			)
		);
		/*Header  Description Text*/
		$wp_customize->add_setting(
			'header-description-text',
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => __( 'Enter your header description text here.', 'dulcie' ),
				'sanitize_callback' => 'sanitize_textarea_field',
			)
		);

		$wp_customize->add_control(
			'header-description-text',
			array(
				'label'       => __( 'Description Text', 'dulcie' ),
				'description' => __( 'Enter your description text here.', 'dulcie' ),
				'section'     => 'header_section',
				'type'        => 'textarea',
			)
		);

		/*Preloader Options*/
		$wp_customize->add_section(
			'preloader_option',
			array(
				'title' => __( 'Preloader Options', 'dulcie' ),
				'panel' => 'dulcie_panel',
			)
		);

		/*Search Icon*/
		$wp_customize->add_setting(
			'dulcie-preloader',
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => 0,
				'sanitize_callback' => 'dulcie_sanitize_checkbox',
			)
		);

		$wp_customize->add_control(
			'dulcie-preloader',
			array(
				'label'       => __( 'Enable Preloader', 'dulcie' ),
				'description' => __( 'Checked to enable preloader.', 'dulcie' ),
				'section'     => 'preloader_option',
				'type'        => 'checkbox',
			)
		);

		/*Blog Page Options*/
		$wp_customize->add_section(
			'dulcie_blog_section',
			array(
				'capability'     => 'edit_theme_options',
				'theme_supports' => '',
				'title'          => __( 'Blog Section Options', 'dulcie' ),
				'panel'          => 'dulcie_panel',
			)
		);

		/*Sidebar Options*/
		$wp_customize->add_setting(
			'dulcie-sidebar-options',
			array(
				'capability'        => 'edit_theme_options',
				'default'           => 'right-sidebar',
				'sanitize_callback' => 'dulcie_sanitize_select',
			)
		);

		$wp_customize->add_control(
			'dulcie-sidebar-options',
			array(
				'label'       => __( 'Sidebar Options', 'dulcie' ),
				'description' => __( 'You can manage the individual sidebar for single post by using the post templates.', 'dulcie' ),
				'section'     => 'dulcie_blog_section',
				'type'        => 'select',
				'choices'     => array(
					'right-sidebar' => __( 'Right sidebar', 'dulcie' ),
					'no-sidebar'    => __( 'No sidebar', 'dulcie' ),
				),
			)
		);
		/*Read More Text*/
		$wp_customize->add_setting(
			'dulcie-read-more-text',
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => esc_html__( 'Read More', 'dulcie' ),
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'dulcie-read-more-text',
			array(
				'label'       => __( 'Continue Reading Text', 'dulcie' ),
				'description' => __( 'Enter your custom continue reading text. The title will be included after this text.', 'dulcie' ),
				'section'     => 'dulcie_blog_section',
				'type'        => 'text',
			)
		);

		/*Footer Options*/
		$wp_customize->add_section(
			'copyright_option',
			array(
				'title' => __( 'Copyright Options', 'dulcie' ),
				'panel' => 'dulcie_panel',
			)
		);


		/*Copyright Text*/
		$wp_customize->add_setting(
			'dulcie-copyright-text',
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => __( '© 2023', 'dulcie' ),
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'dulcie-copyright-text',
			array(
				'label'       => __( 'Copyright Text', 'dulcie' ),
				'description' => __( 'Enter your copyright text here.', 'dulcie' ),
				'section'     => 'copyright_option',
				'type'        => 'text',
			)
		);


	}
}
add_action( 'customize_register', 'dulcie_customize_register' );

/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function dulcie_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 *
 * @return void
 */
function dulcie_customize_partial_blogdescription() {
	bloginfo( 'description' );
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function dulcie_customize_preview_js() {
	wp_enqueue_script( 'dulcie-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), DULCIE_VERSION, true );
}
add_action( 'customize_preview_init', 'dulcie_customize_preview_js' );
