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

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function blogdaily_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' => 'blogdaily_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'blogdaily_customize_partial_blogdescription',
			)
		);
	}

	$wp_customize->add_section(
		'layout_section',
		array(
			'title'    => __( 'Layout (site width) ', 'blogdaily' ),
			'priority' => 30,
		)
	);

	$wp_customize->add_section(
		'header_content',
		array(
			'title'    => __( 'Header Settings', 'blogdaily' ),
			'priority' => 40,
		)
	);

	

	$wp_customize->add_section(
		'service_section',
		array(
			'title' => __( 'Service Section', 'blogdaily' ),
		)
	);

	$wp_customize->add_section(
		'footer_section',
		array(
			'title' => __( 'Footer', 'blogdaily' ),
		)
	);

	

	

	$wp_customize->add_setting(
		'show_breadcrumb',
		array(
			'default'           => true,
			'sanitize_callback' => 'blogdaily_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'show_breadcrumb',
		array(
			'type'    => 'checkbox',
			'label'   => __( 'Enable breadcrumbs', 'blogdaily' ),
			'section' => 'header_content',
		)
	);
	
	 

	$wp_customize->add_setting(
		'header__heading',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'header__heading',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Main Heading :', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'header_content',
		)
	);

	$wp_customize->add_setting(
		'header__sub_heading',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'header__sub_heading',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Sub Heading:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'header_content',
		)
	);
	
	 
	
	
	$wp_customize->add_setting(
		'service__heading',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service__heading',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Service Heading:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	
	$wp_customize->add_setting(
		'service_title_one',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service_title_one',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Service 1:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	$wp_customize->add_setting(
		'service_description_one',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service_description_one',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Description 1:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	
	
	
	
	
	
	
	$wp_customize->add_setting(
		'service_title_two',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service_title_two',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Service 2:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	$wp_customize->add_setting(
		'service_description_two',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service_description_two',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Description 2:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	
	
	
	
	
	$wp_customize->add_setting(
		'service_title_three',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service_title_three',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Service 3:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	$wp_customize->add_setting(
		'service_description_three',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'service_description_three',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter Description 3:', 'blogdaily' ),
			'description' => __( 'Leave this setting blank to disable it.', 'blogdaily' ),
			'section'     => 'service_section',
		)
	);
	
	
	
	
	

	

	$wp_customize->add_setting(
		'copyright_text',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'copyright_text',
		array(
			'type'        => 'text',
			'label'       => __( 'Enter a copyright :', 'blogdaily' ),
			'input_attrs' => array(
				'placeholder' => __( 'Copyright: ', 'blogdaily' ) . gmdate( 'Y' ),
			),
			'section'     => 'footer_section',
		)
	);

	 

	

	 

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

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

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

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

/**
 * Checkbox sanitization callback, from
 * https://github.com/WPTRT/code-examples/blob/master/customizer/sanitization-callbacks.php
 *
 * Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked`
 * as a boolean value, either TRUE or FALSE.
 *
 * @param bool $checked Whether the checkbox is checked.
 * @return bool Whether the checkbox is checked.
 */
function blogdaily_sanitize_checkbox( $checked ) {
	// Boolean check.
	return ( ( isset( $checked ) && true == $checked ) ? true : false );
}


/**
 * Select sanitization callback example.
 *
 * - Sanitization: select
 * - Control: select, radio
 * Sanitization callback for 'select' and 'radio' type controls. This callback sanitizes `$input`
 * as a slug, and then validates `$input` against the choices defined for the control.
 *
 * @see sanitize_text_field()               https://developer.wordpress.org/reference/functions/sanitize_text_field/
 * @see $wp_customize->get_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/get_control/
 *
 * @param string               $input   Slug to sanitize.
 * @param WP_Customize_Setting $setting Setting instance.
 * @return string Sanitized slug if it is a valid choice; otherwise, the setting default.
 */
function blogdaily_sanitize_select( $input, $setting ) {
	// Ensure input is a slug.
	$input = sanitize_text_field( $input );

	// Get list of choices from the control associated with the setting.
	$choices = $setting->manager->get_control( $setting->id )->choices;

	// If the input is a valid key, return it; otherwise, return the default.
	return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
