<?php
/**
 * kaira Theme Customizer
 *
 * @package kaira
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function kaira_customize_register( $wp_customize ) {

	$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-header .site-branding .site-title a',
			'render_callback' => 'kaira_customize_partial_blogname',
		) );
		$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
			'selector'        => '.site-description',
			'render_callback' => 'kaira_customize_partial_blogdescription',
		) );
	}


	/**
	 * Main Panel for Customization
	 */
	$wp_customize->add_panel(
	'kaira_main_panel',
	array(
		'title'            => esc_html__('Theme Options', 'kaira'),
		'priority'         => 70
		)
	);

	//Remove header image control
	$wp_customize->remove_control('header_image');

	/*
	 * Slider Controls
	 */
	$wp_customize->add_section( 'kaira_slider', array(
		'title'		  => esc_html__( 'Slider', 'kaira' ),
		'priority'	  => 201,
		'panel' 	  => 'kaira_main_panel'
	) );

	//Checkbox
	$wp_customize->add_setting( 'kaira_slider_settings', array(
		'default'           => 0,
		'sanitize_callback' => 'kaira_sanitize_checkbox',
		) );

	$wp_customize->add_control( 'kaira_slider_settings_control', array(
		'label'             => esc_html__( 'Display Slider at the Front Page.', 'kaira' ),
		'section'           => 'kaira_slider',
		'settings'			=> 'kaira_slider_settings',
		'type'              => 'checkbox',
		) );

	//Tag selection
	$wp_customize->add_setting( 'kaira_slider_tag', array(
			'default'           => ' ',
			'sanitize_callback' => 'kaira_sanitize_terms',
		) );

	$wp_customize->add_control( 'kaira_slider_tag', array(
		'label'             => esc_html__( 'Select Tag For Slider Posts', 'kaira' ),
		'description'		=> esc_html__( 'Latest 6 posts from the selected tag will be shown in slider.', 'kaira' ),
		'section'           => 'kaira_slider',
		'type'              => 'select',
		'choices' 			=> kaira_get_terms(),
		) );

	/**
	 * Pagination Controls
	 */
	$wp_customize->add_section( 'kaira_pagination_custom', array(
		'title'	=> esc_html__( 'Pagination Type', 'kaira' ),
		'priority' => 199,
		'panel'	=> 'kaira_main_panel'
	) );

	$wp_customize->add_setting( 'kaira_pagination_type', array(
		'capability'	=> 'edit_theme_options', 
		'default'	=> 'numbered',
		'sanitize_callback'	=> 'kaira_sanitize_select'
		) );

	$wp_customize->add_control( 'kaira_pagination_type',  array(
		'label'		=> esc_html__( 'Custom Pagination Selection', 'kaira' ),
		'section'	=> 'kaira_pagination_custom',
		'description' => esc_html__( 'Select the type of pagination', 'kaira' ),
		'type'	=> 'radio',
		'choices'	=> array(
			'numbered'	=>	__( 'Numbered (1,2,3...)', 'kaira'),
			'defaulted'	=>	__( 'Default (Previous/Next)', 'kaira'),
			
		),
		) );

	/**
	 * Featured Post
	 */
	$wp_customize->add_section( 'kaira_front_page', array(
		'title'		  => esc_html__( 'Featured Content Blocks', 'kaira' ),
		'panel'		  => 'kaira_main_panel',
		'priority'	  => 200,
	) );

	//Checkbox
	$wp_customize->add_setting( 'kaira_front_featured_posts', array(
		'default'           => 0,
		'sanitize_callback' => 'kaira_sanitize_checkbox',
	) );

	$wp_customize->add_control( 'kaira_front_control', array(
		'label'             => esc_html__( 'Display Featured Posts at the top of the Front Page.', 'kaira' ),
		'section'           => 'kaira_front_page',
		'settings'			=> 'kaira_front_featured_posts',
		'type'              => 'checkbox',
	) );

	//Label
	$wp_customize->add_setting( 'kaira_front_featured_posts_label', array(
		'transport'			=> 'postMessage',
		'sanitize_callback' => 'sanitize_text_field',
	) );

	$wp_customize->add_control( 'kaira_front_featured_posts_label', array(
		'label'             => esc_html__( 'Section Title', 'kaira' ),
		'section'           => 'kaira_front_page',
		'type'              => 'text',
	) );

	//Tag Selection
	$wp_customize->add_setting( 'kaira_featured_term_1', array(
		'default'           => ' ',
		'sanitize_callback' => 'kaira_sanitize_terms',
	) );

	$wp_customize->add_control( 'kaira_featured_term_1', array(
		'label'             => esc_html__( 'Select the tag for feature post', 'kaira' ),
		'description'		=> esc_html__( 'Latest 6 posts from the selected tag will be shown in featured section.', 'kaira' ),
		'section'           => 'kaira_front_page',
		'default'           => ' ',
		'type'              => 'select',
		'choices' 			=> kaira_get_terms(),
	) );


	/**
	 * Related Post customizer
	 */
	$wp_customize->add_section( 'kaira_relation_post', array(
		'title'		  => esc_html__( 'Related Content Blocks', 'kaira' ),
		'panel'		  => 'kaira_main_panel',
		'priority'	  => 200,
	) );

	//Checkbox
	$wp_customize->add_setting( 'kaira_related_posts', array(
		'default'           => 0,
		'sanitize_callback' => 'kaira_sanitize_checkbox',
	) );

	$wp_customize->add_control( 'kaira_related_posts_control', array(
		'label'             => esc_html__( 'Display Related Posts in single post page.', 'kaira' ),
		'section'           => 'kaira_relation_post',
		'settings'			=> 'kaira_related_posts',
		'type'              => 'checkbox',
	) );

	//Label
	$wp_customize->add_setting( 'kaira_related_posts_label', array(
		'sanitize_callback' => 'sanitize_text_field',
	) );

	$wp_customize->add_control( 'kaira_related_posts_label', array(
		'label'             => esc_html__( 'Section Title', 'kaira' ),
		'section'           => 'kaira_relation_post',
		'description'		=> esc_html__( 'Latest 6 posts belonging to the category of current post  will be shown in related section.', 'kaira' ),
		'type'              => 'text',
	) );


	/** 
	 * Custom Colors
	 */
	//Navigarion Color
	$wp_customize->add_setting( 'kaira_nav_link_color', array(
		'default'			=> '#373737',
		'transport'			=> 'postMessage',
		'sanitize_callback' => 'sanitize_hex_color',
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control(
		$wp_customize, 'kaira_nav_link_control',
		array(
		'label'		=> esc_html__( 'Navigation Link Color', 'kaira' ),
		'section'	=> 'colors',
		'default'			=> '#373737',
		'settings'	=> 'kaira_nav_link_color'
	) ) );

	//Link Colors
	$wp_customize->add_setting( 'kaira_category_link_color', array(
		'default'			=> '#BD15CF',
		'transport'			=> 'postMessage',
		'sanitize_callback' => 'sanitize_hex_color',
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control(
		$wp_customize, 'kaira_category_link_control',
		array(
		'label'		=> esc_html__( 'Category Link Color', 'kaira' ),
		'section'	=> 'colors',
		'settings'	=> 'kaira_category_link_color'
	) ) );

	//Button Colors
	$wp_customize->add_setting( 'kaira_button_color', array(
		'default'			=> '#0F89DB',
		'transport'			=> 'postMessage',
		'sanitize_callback' => 'sanitize_hex_color',
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control(
		$wp_customize, 'kaira_button_color_control',
		array(
		'label'		=> esc_html__( 'Button Color', 'kaira' ),
		'section'	=> 'colors',
		'settings'	=> 'kaira_button_color'
	) ) );

	//Header Color
	$wp_customize->add_setting( 'kaira_header_color', array(
		'default'			=> '#ffffff',
		'sanitize_callback' => 'sanitize_hex_color',
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control(
		$wp_customize, 'kaira_header_color_control',
		array(
		'label'		=> esc_html__( 'Header Color', 'kaira' ),
		'section'	=> 'colors',
		'settings'	=> 'kaira_header_color'
	) ) );

	function kaira_sanitize_select( $input, $setting ) {
		// Ensure input is a slug.
		$input = sanitize_key( $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 );
	}

	/**
	 * Custom Font Selection
	 */
	//Header Typography
	$wp_customize->add_section(
		'kaira_custom_typography',
		array(
			'title'            =>   esc_html__('Custom Fonts', 'kaira'),
			'panel'            =>   'kaira_main_panel'
			)
		);

	$wp_customize->add_setting(
		'kaira_header_font_setting',
		array(
			'capability'       => 'edit_theme_options',
			'default'          =>  'source_sans_pro',
			'transport'        =>  'refresh',
	        'sanitize_callback' => 'kaira_sanitize_font',
			)
		);

	$wp_customize->add_control(
		'kaira_header_font_control',
		array(
			'type'             => 'select',
			'choices'          => array(
				'lato'            => 'Lato',
				'ubuntu'          => 'Ubuntu',
				'source_sans_pro' => 'Source Sans Pro',
				'roboto'          => 'Roboto',
				'open-sans'       => 'Open Sans'
				),
				'default'          =>  'source_sans_pro',
	            'section'          => 'kaira_custom_typography', // Add a default or your own section
	            'settings'         => 'kaira_header_font_setting',
	            'label'            => __( 'Headings', 'kaira' ),
	            'description'      => __( 'This dropdown enables to change the font of all the headings in the site.', 'kaira'),
	            )
		);
		
	$wp_customize->add_setting(
		'kaira_body_font_setting',
		array(
			'capability'       => 'edit_theme_options',
			'default'          =>  'open-sans',
			'transport'        =>  'refresh',
	        'sanitize_callback' => 'kaira_sanitize_font',
			)
		);

	$wp_customize->add_control(
		'kaira_body_font_control',
		array(
			'type'             => 'select',
			'choices'          => array(
				'lato'            => 'Lato',
				'ubuntu'          => 'Ubuntu',
				'source_sans_pro' => 'Source Sans Pro',
				'roboto'          => 'Roboto',
				'open-sans'       => 'Open Sans'
				),
				'default'          =>  'open-sans',
	            'section'          => 'kaira_custom_typography', // Add a default or your own section
	            'settings'         => 'kaira_body_font_setting',
	            'label'            => __( 'Body Text', 'kaira' ),
	            'description'      => __( 'This dropdown enables to change the font of the body texts.', 'kaira'),
	            )
		);
	
}
add_action( 'customize_register', 'kaira_customize_register' );

/**
 * Sanitization for tags and slugs
 */
if ( ! function_exists( 'kaira_get_terms' ) ) :
/**
 * Return an array of tag names and slugs
 */
function kaira_get_terms() {

	$choices = array( 0 );

	// Default
	$choices = array( ' ' => esc_html__( 'Select a tag', 'kaira' ) );

	// Post Tags
	$type_terms = get_terms( 'post_tag' );
	if ( ! empty( $type_terms ) ) {
		$type_slugs = wp_list_pluck( $type_terms, 'slug' );
		$type_names = wp_list_pluck( $type_terms, 'name' );
		$type_list = array_combine( $type_slugs, $type_names );
		$choices = $choices + $type_list;
	}

	return apply_filters( 'kaira_get_terms', $choices );
}
endif;

/**
 * Return an array of category names and slugs
 */
if ( ! function_exists( 'kaira_get_category_terms' ) ) :
function kaira_get_category_terms() {

	$choices = array( 0 );

	// Default
	$choices = array( ' ' => esc_html__( 'Select a Category', 'kaira' ) );

	// Category
	$type_terms = get_terms( 'category' );
	if ( ! empty( $type_terms ) ) {
		$type_slugs = wp_list_pluck( $type_terms, 'slug' );
		$type_names = wp_list_pluck( $type_terms, 'name' );
		$type_list = array_combine( $type_slugs, $type_names );
		$choices = $choices + $type_list;
	}

	return apply_filters( 'kaira_get_category_terms', $choices );
}
endif;

/**
 * Sanitization for tags
 */
if ( ! function_exists( 'kaira_sanitize_terms' ) ) :
/**
 * Sanitize a value from a list of allowed values.
 *
 * @since 1.0.0.
 *
 * @param  mixed    $value      The value to sanitize.
 * @return mixed                The sanitized value.
 */
function kaira_sanitize_terms( $value ) {

	$choices = kaira_get_terms();
	$valid	 = array_keys( $choices );

	if ( ! in_array( $value, $valid ) ) {
		$value = 'none';
	}

	return $value;
}
endif;

/**
 * Sanitization for categories
 */
if ( ! function_exists( 'kaira_sanitize_category_terms' ) ) :
/**
 * Sanitize a value from a list of allowed values.
 *
 * @since 1.0.0.
 *
 * @param  mixed    $value      The value to sanitize.
 * @return mixed                The sanitized value.
 */
function kaira_sanitize_category_terms( $value ) {

	$choices = kaira_get_category_terms();
	$valid	 = array_keys( $choices );

	if ( ! in_array( $value, $valid ) ) {
		$value = 'none';
	}

	return $value;
}
endif;

/**
 * Sanitization for fonts
 */
function kaira_sanitize_font( $value ) {

	$choices = array(
			'lato'            => 'Lato',
			'ubuntu'          => 'Ubuntu',
			'source_sans_pro' => 'Source Sans Pro',
			'roboto'          => 'Roboto',
			'open-sans'       => 'Open Sans'
			);
	$valid = array_keys( $choices );

	if( ! in_array( $value, $valid) ) {
		$value = 'open-sans';
	}

	return $value;
}

/* Sanitization Functions
 * 
 * @link https://github.com/WPTRT/code-examples/blob/master/customizer/sanitization-callbacks.php 
 */
     function kaira_sanitize_checkbox( $checked ){
        // Boolean check.
        return ( ( isset( $checked ) && true == $checked ) ? true : false );
     }

/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function kaira_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 *
 * @return void
 */
function kaira_customize_partial_blogdescription() {
	bloginfo( 'description' );
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function kaira_customize_preview_js() {
	wp_enqueue_script( 'kaira_customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), '20151215', true );
}
add_action( 'customize_preview_init', 'kaira_customize_preview_js' );


//Custom Bg callback
function change_custom_background_cb() {
    $background = get_background_image();
    $color = get_background_color();
 
    if ( ! $background && ! $color )
        return;

    if( ! $background && $color) {
    	$style = " background-color: #".$color; 
    }
 
    if ( $background ) {
        $image = " background-image: url('$background');";
 
        $repeat = get_theme_mod( 'background_repeat', 'repeat' );
 
        if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
            $repeat = 'repeat';
 
        $repeat = " background-repeat: $repeat;";
 
        $position = get_theme_mod( 'background_position_x', 'left' );
 
        if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
            $position = 'left';
 
        $position = " background-position: top $position;";
 
        $attachment = get_theme_mod( 'background_attachment', 'scroll' );
 
        if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
            $attachment = 'scroll';
 
        $attachment = " background-attachment: $attachment;";
 
        $style = $image . $repeat . $position . $attachment;
    }
?>
<style type="text/css">
.site-content { <?php echo trim( $style ); ?> }
</style>
<?php
}

/* HeaderText Color */
function kaira_custom_header_setup() {
	add_theme_support( 'custom-header', apply_filters( 'kaira_custom_header_args', array(
		'default-image'          => '',
		'default-text-color'     => '000000',
		'width'                  => 1000,
		'height'                 => 250,
		'flex-height'            => true,
		'wp-head-callback'       => 'kaira_header_style',
	) ) );
}
add_action( 'after_setup_theme', 'kaira_custom_header_setup' );

if ( ! function_exists( 'kaira_header_style' ) ) :
/**
 * Styles the header image and text displayed on the blog.
 *
 * @see kaira_custom_header_setup().
 */
function kaira_header_style() {
	$header_text_color = get_header_textcolor();

	/*
	 * If no custom options for text are set, let's bail.
	 * get_header_textcolor() options: Any hex value, 'blank' to hide text. Default: add_theme_support( 'custom-header' ).
	 */
	if ( get_theme_support( 'custom-header', 'default-text-color' ) === $header_text_color ) {
		return;
	}

	// If we get this far, we have custom styles. Let's do this.
	?>
	<style type="text/css">
	<?php
		// Has the text been hidden?
		if ( ! display_header_text() ) :
	?>
		.site-title,
		.site-description {
			position: absolute;
			clip: rect(1px, 1px, 1px, 1px);
		}
	<?php
		// If the user has set a custom color for the text use that.
		else :
	?>
		.site-title a,
		.site-description {
			color: #<?php echo esc_attr( $header_text_color ); ?>;
		}
	<?php endif; ?>
	</style>
	<?php
}
endif;