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

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function covfefe_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' => 'covfefe_customize_partial_blogname',
		) );
		$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
			'selector'        => '.site-description',
			'render_callback' => 'covfefe_customize_partial_blogdescription',
		) );
	}
	
	
	/**
	 * Theme options.
	 */
	
	$wp_customize->add_panel( 'covfefe_panel_id', array(
	    'priority' => 10,
	    'capability' => 'edit_theme_options',
	    'theme_supports' => '',
	    'title' => __( 'Theme Options', 'covfefe' ),
	    'description' => __( 'Description of what this panel does.', 'covfefe' ),
	) );
	
	
	/**
	* General
	*/
	// Section
	$wp_customize->add_section(
		'covfefe_section_general', array(
			'title' => __('General', 'covfefe'), 
			'priority' => 2,
			'panel' => 'covfefe_panel_id'
		)
	);
	// Setting
	$wp_customize->add_setting(
		'general_sidebar', array(
			'default' => 'both', 
			'sanitize_callback' => 'covfefe_sanitize_select'
		)
	);
	// Control
	$wp_customize->add_control(
		'general_sidebar', array(
			'label' => __('Sidebar', 'covfefe'), 
			'section' => 'covfefe_section_general', 
			'setting' => 'general_sidebar', 
			'priority' => 1, 
			'type' => 'select', 
			'choices' => array('both' => __('Both', 'covfefe'),'left' => __('Left', 'covfefe'),'right' => __('Right', 'covfefe'))
		)
	);
	
	
	/**
	 * Personal Info options.
	 */
	 
	// Section
	$wp_customize->add_section(
		'covfefe_section_personal', array(
			'title' => __('Personal Info Options', 'covfefe'), 
			'priority' => 3,
			'panel' => 'covfefe_panel_id'
		)
	);
	// Setting
	$wp_customize->add_setting(
		'personal_show', array(
			'default' => 'no', 
			'sanitize_callback' => 'covfefe_sanitize_select'
		)
	);
	// Control
	$wp_customize->add_control(
		'personal_show', array(
			'label' => __('Show Personal (on Frontpage)', 'covfefe'), 
			'section' => 'covfefe_section_personal', 
			'setting' => 'personal_show', 
			'priority' => 1, 
			'type' => 'select', 
			'choices' => array('yes' => __('yes', 'covfefe'),'no' => __('no', 'covfefe'))
		)
	);
	
	$wp_customize->add_setting('personal_img_src',array('default' => '', 'sanitize_callback' => 'absint'));
	$wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'personal_img_src' ,
	   array(
		  'label' => __('Personal Image', 'covfefe'),
		  'description' => __( 'Personal Image (240x240)','covfefe' ),
		  'section' => 'covfefe_section_personal',
		  'setting' => 'personal_img_src',
		  'flex_width' => true, // Optional. Default: false
		  'flex_height' => true, // Optional. Default: false
          'width' => 240, // Optional. Default: 150
          'height' => 240, // Optional. Default: 150
		  'button_labels' => array( // Optional.
			 'select' => __('Select Image', 'covfefe'),
			 'change' => __('Change Image', 'covfefe'),
			 'remove' => __('Remove Image', 'covfefe'),
			 'default' => __('Default', 'covfefe'),
			 'placeholder' => __('No Image Selected', 'covfefe'),
			 'frame_title' => __('Select Image', 'covfefe'),
			 'frame_button' => __('Choose Image', 'covfefe'),
		  )
	   )
	) );
	
	$wp_customize->add_setting('personal_welcome',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('personal_welcome',array(
		'label'	=> __('Welcome Text','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_welcome',
		'type'		=> 'text'
	));
	
	$wp_customize->add_setting('personal_title',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('personal_title',array(
		'label'	=> __('Title','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_title',
		'type'		=> 'text'
	));
	$wp_customize->add_setting('personal_title_sub',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('personal_title_sub',array(
		'label'	=> __('Title Sub','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_title_sub',
		'type'		=> 'text'
	));
	
	$wp_customize->add_setting('personal_description',array(
		'default'	=> '',
		'sanitize_callback'	=> 'wp_kses_post'
	));	
	$wp_customize->add_control('personal_description',array(
		'label'	=> __('Description','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_description',
		'type'		=> 'textarea'
	));
	
	$wp_customize->add_setting('personal_phone',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('personal_phone',array(
		'label'	=> __('Phone','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_phone',
		'type'		=> 'text'
	));
	
	$wp_customize->add_setting('personal_fax',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('personal_fax',array(
		'label'	=> __('Fax','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_fax',
		'type'		=> 'text'
	));
	$wp_customize->add_setting('personal_email',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('personal_email',array(
		'label'	=> __('Email','covfefe'),
		'section'	=> 'covfefe_section_personal',
		'setting'	=> 'personal_email',
		'type'		=> 'text'
	));
	
	/**
	 * Personal Info options ends
	 */
	 
	 
	 /**
	 * Ticker options.
	 */
	 
	// Section
	$wp_customize->add_section(
		'covfefe_section_ticker', array(
			'title' => __('Ticker Options', 'covfefe'), 
			'priority' => 4 ,
			'panel' => 'covfefe_panel_id'
		)
	);
	// Setting
	$wp_customize->add_setting(
		'ticker_show', array(
			'default' => 'yes', 
			'sanitize_callback' => 'covfefe_sanitize_select'
		)
	);
	// Control
	$wp_customize->add_control(
		'ticker_show', array(
			'label' => __('Ticker Show', 'covfefe'), 
			'section' => 'covfefe_section_ticker', 
			'setting' => 'ticker_show', 
			'type' => 'select', 
			'choices' => array('yes' => __('yes', 'covfefe'),'no' => __('no', 'covfefe'))
		)
	);
	
	$wp_customize->add_setting('ticker_tags',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('ticker_tags',array(
		'label'	=> __('Ticker Tags','covfefe'),
		'section'	=> 'covfefe_section_ticker',
		'setting'	=> 'ticker_tags',
		'type'		=> 'text'
	));
	
	
	
	/**
	 * Ticker options ends
	 */
	
	/**
	 * Footer options.
	 */
	 
	// Section
	$wp_customize->add_section(
		'covfefe_section_footer', array(
			'title' => __('Footer Options', 'covfefe'), 
			'priority' => 5 ,
			'panel' => 'covfefe_panel_id'
		)
	);
	$wp_customize->add_setting('footer_copyright',array(
		'default'	=> '',
		'sanitize_callback'	=> 'wp_kses_post'
	));	
	$wp_customize->add_control('footer_copyright',array(
		'label'	=> __('Footer Copyright','covfefe'),
		'section'	=> 'covfefe_section_footer',
		'setting'	=> 'footer_copyright',
		'type'		=> 'textarea'
	));
	
	
	/**
	 * Ticker options ends
	 */
	
}
add_action( 'customize_register', 'covfefe_customize_register' );

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

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

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

if (!function_exists('covfefe_sanitize_select')) {
	function covfefe_sanitize_select($input) {
		return wp_filter_nohtml_kses( $input );
	}
}