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

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */

function ivory_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' => 'ivory_customize_partial_blogname',
			)
		);

		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'ivory_customize_partial_blogdescription',
			)
		);	

	 // adding copyright section to the ivory customizer 

    	$wp_customize-> add_section('ivory_copyright_sec', array(
		'title'                    => __('Copyright','ivory'),
		'description' => __('Please, type here your copyright','ivory')
	 ));
	
	 $wp_customize-> add_setting('ivory_copyright_set', array(
		'type'                     => 'theme_mod',
		'sanitize_callback'        => 'esc_html'
	 ));

    	$wp_customize-> add_control( 'ivory_ctrl_copyright', array(
		'label'                   => __('Copyright Information','ivory'),
		'description'             => __('Please, type your copyright here.','ivory'),
		'section'                 => 'ivory_copyright_sec',
		'settings'                => 'ivory_copyright_set',
		'type'                    => 'text'
    	));

		// adding footer column choice 

	$wp_customize-> add_section('ivory_foot_col_choice_sc', array(
		'title'                    => __('Footer Column Choice','ivory'),
		'description' => __('Please, choose your Footer Column Layout','ivory')
	 ));
	

	 $wp_customize-> add_setting('ivory_foot_col_choice_st', array(
		'type'                     => 'theme_mod',
		'sanitize_callback'        => 'ivory_sanitize_footer_widget'
	 ));


	 $wp_customize-> add_control( 'ivory_foot_col_choice_ctrl', array(
		'label'                   => __('Footer Column Choice','ivory'),
		'description'             => __('Please, choose your Footer Column Layout','ivory'),
		'section'                 => 'ivory_foot_col_choice_sc',
		'settings'                => 'ivory_foot_col_choice_st',
		'type'                    => 'select',
		'choices'                 => array(
		'two'                    => __( 'Two Column','ivory' ),
		'three'                   => __( 'Three Column','ivory' ),
		'four'                    => __( 'Four Column','ivory' ),
		  ),
	 ));

	 function ivory_sanitize_footer_widget($input, $setting) {

		$choices = array('two','three','four');
		if (in_array($input, $choices)) {
			return $input;
		} else {
			return "three";
		}
	 }


	// adding sidebar location setting to customizer
	 $wp_customize-> add_section('ivory_sidebar_sec', array(
		'title'                    => __('Sidebar Location','ivory'),
		 'description'             => __('Please, choose your sidebar location','ivory')
	 ));
	

	 $wp_customize-> add_setting('ivory_sidebar_set', array(
		'type'                     => 'theme_mod',
		'sanitize_callback'        => 'ivory_sanitize_sidebar'
	 ));

	 $wp_customize-> add_control( 'ivory_ctrl_sidebar', array(
		'label'                   => __('Sidebar Location','ivory'),
		'description'             => __('Please, choose your sidebar location','ivory'),
		'section'                 => 'ivory_sidebar_sec',
		'settings'                => 'ivory_sidebar_set',
		'type'                    => 'select',
		'choices'                 => array(
		'left'                    => __( 'Left Side' ,'ivory'),
		'right'                   => __( 'Right Side','ivory' ),
		'none'                    => __( 'No Sidebar','ivory' ),
		  ),
	 ));
      
	   function ivory_sanitize_sidebar($input, $setting) {

		$choices = array('right','left','none');
		if (in_array($input, $choices)) {
			return $input;
		} else {
			return "right";
		}
	 }

	 // adding featured posts
	 $wp_customize-> add_section('ivory_enable_featured_post_sec', array(
		'title'                    => __('Featured Posts','ivory'),
		'description'              => __('Check this box to enable featured post','ivory')
	 ));
	
	 $wp_customize-> add_setting('ivory_enable_featured_post', array(
		'type'                     => 'theme_mod',
		'sanitize_callback'        => 'ivory_sanitize_checkbox'
	 ));

	 $wp_customize-> add_control( 'ivory_enable_featured_post_ctrl', array(
		'label'                   => __('Featured Posts','ivory'),
		'description'             => __('Check this box to enable featured post','ivory'),
		'section'                 => 'ivory_enable_featured_post_sec',
		'settings'                => 'ivory_enable_featured_post',
		'type'                    => 'checkbox',
		
	));

	//checkbox sanitization function adding to show featured post
	function ivory_sanitize_checkbox( $input ){
					
		//returns true if checkbox is checked
			return ( isset( $input ) ? true : false );
			}

	//  adding show title area to the customizer

	 $wp_customize-> add_setting('ivory_display_title_set', array(
		'type'                     => 'theme_mod',
		'sanitize_callback'        => 'ivory_sanitize_display_title',
	
	 ));

	 $wp_customize-> add_control( 'ivory_disp_title_ctrl', array(
		'label'                   => __('Show Title','ivory'),
		'description'             => __('Check this box to show category title','ivory'),
		'section'                 => 'ivory_enable_featured_post_sec',
		'settings'                => 'ivory_display_title_set',
		'type'                    => 'checkbox',
		
	));

	 //performing sanitization on show featured post checkbox 
	 function ivory_sanitize_display_title( $input ){
              
		//returns true if checkbox is checked
		return ( isset( $input ) ? true : false );
	}

	//  adding category select to the customizer

	    //Get an array with the category list
		$ivory_array_cat_full_list = get_categories(array( 'orderby' => 'name', ));
		
		//Create an empty array
		$ivory_array_emp_cat_list = [];

		//Loop through the array and add the correct values every time
		foreach( $ivory_array_cat_full_list as $ivory_single_cat ) {
			$ivory_array_emp_cat_list[$ivory_single_cat->slug] = $ivory_single_cat->name;
		}
	
	 $wp_customize-> add_section('ivory_sel_category_post_sec', array(
		'title'                    => __('Category','ivory'),
		'description'              => __('Category you select here will be displayed in featured area','ivory')
	 ));
	
	 $wp_customize-> add_setting('ivory_sel_category_post', array(
		'type'                     => 'theme_mod',
		'capability'               => 'edit_theme_options',
		'sanitize_callback'        => 'ivory_sanitize_featured_select_category'

	 ));

	 $wp_customize-> add_control( 'ivory_category_post_ctrl', array(
		'label'                   => __('Category','ivory'),
		'description'             => __('Category you select here will be displayed in featured area','ivory'),
		'section'                 => 'ivory_enable_featured_post_sec',
		'settings'                => 'ivory_sel_category_post',
		'type'                    => 'select',
		'choices'                 => $ivory_array_emp_cat_list, 
	));

	function ivory_sanitize_featured_select_category($input, $setting) {

		//Get an array with the category list
		$collon_array_cat_full_list = get_categories(array( 'orderby' => 'name', ));
	   
		//Create an empty array
		$collon_array_emp_cat_list = [];

		//Loop through the array and add the correct values every time
		foreach( $collon_array_cat_full_list as $collon_single_cat ) {
			$collon_array_emp_cat_list[] = $collon_single_cat->slug;
		}

	   $choices = $collon_array_emp_cat_list;
	   

	   
	   if (in_array($input, $choices)) {
		   return $input;
	   } else {
		   return "none";
	   }
	}
	

	//  adding featured post count
	$wp_customize-> add_section('ivory_posts_count', array(
		'title'                    => __('Featured Posts Layout','ivory'),
		'description' => __('Please, choose number of posts here','ivory')
	 ));
	
	 $wp_customize-> add_setting('ivory_posts_count', array(
		'type'                     => 'theme_mod',
		'sanitize_callback'        => 'ivory_sanitize_post_count'
	 ));


	 $wp_customize-> add_control( 'ivory_posts_count_ctrl', array(
		'label'                   => __('Featured Posts Layout','ivory'),
		'description'             => __('Please, choose number of posts here','ivory'),
		'section'                 => 'ivory_posts_count',
		'settings'                => 'ivory_posts_count',
		'type'                    => 'select',
		'choices'                 => array(
		'4'                       => __( 'Four','ivory' ),
		'6'                       => __( 'Six','ivory' ),
		'8'                       => __( 'Eight','ivory' ),
		'10'                      => __( 'Ten' ,'ivory'),
		                    
		  ),
	 ));

	 function ivory_sanitize_post_count($input, $setting) {

		$choices = array('4','6','8','10');
		if (in_array($input, $choices)) {
			return $input;
		} else {
			return "4";
		}
	 }


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




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

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

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