<?php
/**
 * Super Blog Theme Customizer.
 *
 * @package Super_Blog
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function super_blog_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';


	// Option Panel
	$wp_customize->add_panel(
		'basic_panel', 
		array(
			'title'				=> __('Theme Options', 'super-blog'),
			'priority' 			=> 30		
			)
	);

	// Banner Section
	$wp_customize->add_section(
		'banners', 
		array(    
			'title'       => __('Banner Type', 'super-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	$wp_customize->add_setting(
		'banner_type', 
		array(
			'default' 			=> 'image',	
			'sanitize_callback' => 'super_blog_sanitize_select'
		)
	);

	$wp_customize->add_control(
		'banner_type', 
		array(		
			'label' 	=> __('Banner Type', 'super-blog'),
			'section' 	=> 'banners',
			'settings'  => 'banner_type',
			'type' 		=> 'radio',
			'choices' 	=> array(
				'image' 	=> __('Header Image', 'super-blog'),
				'slider' 	=> __('Meta Slider (Requires MetaSlider Plugin)', 'super-blog'),			
				'disable' 	=> __('Disable', 'super-blog'),			
				),		
			)
	);

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

	$wp_customize->add_control(
		'metaslider', 
		array(		
			'label' 	=> __('MetaSlider Shortcode', 'super-blog'),
			'description' => __( 'Add MetaSlider shortcode here', 'super-blog' ),
			'section' 	=> 'banners',
			'settings'  => 'metaslider',
			'type'      => 'text',			
		)
	);

	// Post Options Section
	$wp_customize->add_section(
		'post_options', 
		array(    
			'title'       => __('Post Options', 'super-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	$wp_customize->add_setting(
		'format', 
		array(
			'default' 			=> 'post-excerpt',	
			'sanitize_callback' => 'super_blog_sanitize_select'
		)
	);

	$wp_customize->add_control(
		'format', 
		array(		
			'label' 	=> __('How would you like to diplay a post?', 'super-blog'),
			'section' 	=> 'post_options',
			'settings'  => 'format',
			'type' 		=> 'radio',
			'choices' 	=> array(
				'post-excerpt' 	=> __('Post Excerpt', 'super-blog'),
				'full-post' 	=> __('Full Post', 'super-blog'),		
				),		
			)
	);

	$wp_customize->add_setting(
		'excerpt_length', 
		array(
			'default' 			=> '60',		
			'sanitize_callback' => 'absint'
		)
	);

	$wp_customize->add_control(
		'excerpt_length', 
		array(		
			'label' 		=> __('Excerpt Length', 'super-blog'),
			'description' 	=> __( 'Select number of words to display in excerpt', 'super-blog' ),
			'section' 		=> 'post_options',
			'settings'  	=> 'excerpt_length',
			'type'      	=> 'number',
			'input_attrs' 	=> array(
									'min' 	=> 20,		
									'max' 	=> 220,
									'step'  => 1
								),			
		)
	);

	$wp_customize->add_setting('readmore', 
		array(
			'default'           => __( 'Read More', 'super-blog' ),   		
			'sanitize_callback' => 'sanitize_text_field'
		)
	);

	$wp_customize->add_control(
		'readmore', 
		array(
			'label'       => __('Read More Text', 'super-blog'),  
			'description' => __('Change text if you want to use other than "Read More"', 'super-blog'),   
			'settings'    => 'readmore',
			'section'     => 'post_options',
			'type'        => 'text'
		)
	);

	$wp_customize->add_setting(
		'meta', 
		array(
			'default'           => 0,			
			'sanitize_callback' => 'super_blog_sanitize_checkbox'
		)
	);

	$wp_customize->add_control(
		'meta', 
		array(
			'label'       => __('Hide Date/Author/Archives', 'super-blog'),
			'section'     => 'post_options',   
			'settings'    => 'meta',		
			'type'        => 'checkbox'
		)
	);

	// Breadcrumb Section
	$wp_customize->add_section(
		'breadcrumb_options', 
		array(    
			'title'       => __('Breadcrumb', 'super-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	$wp_customize->add_setting(
		'breadcrumb', 
		array(
			'default'           => 0,			
			'sanitize_callback' => 'super_blog_sanitize_checkbox'
		)
	);

	$wp_customize->add_control(
		'breadcrumb', 
		array(
			'label'       => __('Show breadcrumb', 'super-blog'),
			'section'     => 'breadcrumb_options',   
			'settings'    => 'breadcrumb',		
			'type'        => 'checkbox'
		)
	);


	// General Section
	$wp_customize->add_section(
		'general', 
		array(    
			'title'       => __('General Settings', 'super-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	$wp_customize->add_setting(
		'sticky', 
		array(
			'default'           => 0,			
			'sanitize_callback' => 'super_blog_sanitize_checkbox'
		)
	);

	$wp_customize->add_control(
		'sticky', 
		array(
			'label'       => __('Disable Sticky Header', 'super-blog'),
			'section'     => 'general',   
			'settings'    => 'sticky',		
			'type'        => 'checkbox'
		)
	);
	$wp_customize->add_setting(
		'sidebar', 
		array(
			'default' 			=> 'right-siderbar',	
			'sanitize_callback' => 'super_blog_sanitize_select'
		)
	);

	$wp_customize->add_control(
		'sidebar', 
		array(		
			'label' 	=> __('Choose Sidebar', 'super-blog'),
			'section' 	=> 'general',
			'settings'  => 'sidebar',
			'type' 		=> 'radio',
			'choices' 	=> array(
				'left-siderbar' 	=> __('Left Sidebar', 'super-blog'),
				'right-siderbar' 	=> __('Right Sidebar', 'super-blog'),
				'no-sidebar' 		=> __('No Sidebar', 'super-blog'),			
				),		
			)
	);
	$wp_customize->add_setting(
		'pagination_option', 
		array(
			'default' 			=> 'default',	
			'sanitize_callback' => 'super_blog_sanitize_select'
		)
	);

	$wp_customize->add_control(
		'pagination_option', 
		array(		
			'label' 	=> __('Choose Pagination Options', 'super-blog'),
			'section' 	=> 'general',
			'settings'  => 'pagination_option',
			'type' 		=> 'radio',
			'choices' 	=> array(
				'default' 		=> esc_html__('Default', 'super-blog'),							
				'numeric' 		=> esc_html__('Numeric', 'super-blog'),			
			),		
		)
	);


	$wp_customize->add_setting(
		'copyright', 
		array(
			'default' 			=>  __('Copyright 2016. All rights reserved', 'super-blog'),	
			'sanitize_callback' => 'sanitize_text_field'
		)
	);

	$wp_customize->add_control(
		'copyright', 
		array(
			'label'       => __('Copyright Text', 'super-blog'),  
			'description' => __('Copyright text of the site', 'super-blog'),   
			'settings'    => 'copyright',
			'section'     => 'general',
			'type'        => 'text'
		)
	);


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

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


//=============================================================
// Select/radio santitization
//=============================================================
if ( ! function_exists( 'super_blog_sanitize_select' ) ) :

	function super_blog_sanitize_select( $input, $setting ) {
	  
		$input = esc_attr( $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 );

	}

endif;

//=============================================================
// Checkbox santitization
//=============================================================
if ( ! function_exists( 'super_blog_sanitize_checkbox' ) ) :

	function super_blog_sanitize_checkbox( $input ) {

		if ( $input == 1 ) {

			return 1;

		} else {

			return '';

		}
	}

endif;