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

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function kreatif_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';
	$wp_customize->get_setting( 'header_textcolor' )->default	= '8d99ae';
	$wp_customize->get_setting( 'header_textcolor' )->sanitize_callback	=	'sanitize_hex_color';
	$wp_customize->get_control( 'header_textcolor' )->label		= __('Site Description Color', 'kreatif');
	
	/*---- Featured Category ----*/
	
	class Kreatif_Customize_Category_Control extends WP_Customize_Control{
		
		public function render_content() {
			$dropdown = wp_dropdown_categories(
                array(
                    'name'              => '_customize-dropdown-categories-' . $this->id,
                    'echo'              => 0,
                    'show_option_none'  => __( '&mdash;', 'kreatif' ),
                    'option_none_value' => '0',
                    'taxonomy'			=> 'category',
                    'selected'          => $this->value(),
                )
            );
 
            $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
 
            printf(
                '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
                $this->label,
                $dropdown
            );
		}
	}
	
	$wp_customize->add_section(
		'kreatif-fc',
		array(
			'title'	=> __('Featured Category', 'kreatif'),
			'description'	=> __('Manage the Featured Category Area Settings', 'kindler'),
			'priority'	=> 11
		)
	);
	
	$wp_customize->add_setting(
		'kreatif-fc-enable',
		array(
			'default'	=> false,
			'sanitize_callback'	=> 'kreatif_sanitize_checkbox'
		)
	);
	
	$wp_customize->add_control(
		'kreatif-fc-enable',
		array(
			'label' => __('Enable Featured Category Area','kreatif'),
			'section' => 'kreatif-fc',
			'settings' => 'kreatif-fc-enable',
			'type'	=> 'checkbox',
			'priority'	=> 1
		)
	);
	
	$wp_customize->add_setting(
		'kreatif-fc-title',
		array(
			'default'	=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text'
		)
	);
	
	$wp_customize->add_control(
		'kreatif-fc-title',
		array(
			'label' => __('Featured Category Title','kreatif'),
			'section' => 'kreatif-fc',
			'settings' => 'kreatif-fc-title',
			'type'	=> 'text',
			'priority'	=> 5
		)
	);
	
	$wp_customize->add_setting(
		'kreatif-fc-select',
		array(
			'default'	=> '—',
			'sanitize_callback'	=> 'kreatif_sanitize_category'
		)
	);
	
	function kreatif_sanitize_category( $input ) {
		if ( term_exists(get_cat_name( $input ), 'category') )
			return $input;
		else 
			return '';	
	}
	
	$wp_customize->add_control(new Kreatif_Customize_Category_Control( 
		$wp_customize, 
		'kreatif-fc-select', array(
			'label' => __('Select the Featured Category','kreatif'),
			'section' => 'kreatif-fc',
			'settings' => 'kreatif-fc-select',
			'priority'	=> 10
		) ) 
	);
		
	/*---- Layout Settings ----*/
	
	$wp_customize->add_section(
	'kreatif-layout',
	array(
    	'title'			=> __('Layout Settings','kreatif'),
    	'description'	=> __('Manage the Layout Settings of your site.','kreatif'),
    	'priority'		=> 3,
    	)
	);
	
	$wp_customize-> add_setting(
    'kreatif-f-img',
    array(
    	'sanitize_callback'	=> 'kreatif_sanitize_checkbox',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-f-img',
    array(
    	'type'		=> 'checkbox',
    	'label'		=> __('Show Featured Image in the Post.','kreatif'),
    	'section'	=> 'kreatif-layout',
    	'priority'	=> 1,
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-h-disable',
    array(
    	'sanitize_callback'	=> 'kreatif_sanitize_checkbox',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-h-disable',
    array(
    	'type'		=> 'checkbox',
    	'label'		=> __('Disable the Header Image.','kreatif'),
    	'section'	=> 'kreatif-layout',
    	'priority'	=> 2,
    	)
    );
	
	/*---- Color Settings ----*/
	
	$wp_customize-> add_setting(
		'kreatif-title-color',
		array(
			'default'	=> 'ffffff',
			'sanitize_callback'	=> 'sanitize_hex_color',
			'transport'	=> 'postMessage'
		)
	);
	
	$wp_customize->add_control(
	    new WP_Customize_Color_Control(
	        $wp_customize,
	        'kreatif-title-color',
	        array(
	            'label' => __('Site Title Color','kreatif'),
	            'section' => 'colors',
	            'settings' => 'kreatif-title-color',
	            'priority'	=> 2,
	        )
	    )
	);
	
	$wp_customize-> add_section(
    'kreatif-sec1',
    array(
    	'title'			=> __('Social Settings','kreatif'),
    	'description'	=> __('Manage the Social Icon Setings of your site.','kreatif'),
    	'priority'		=> 3,
    	)
    );
    
    function is_social_enable() {
	    if (get_theme_mod('kreatif-social', false) == true) {
		    return true;
	    } else {
		    return false;
	    }
    }
    
    $wp_customize-> add_setting(
    'kreatif-social',
    array(
    	'default'			=> false,
    	'sanitize_callback'	=> 'kreatif_sanitize_checkbox',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-social',
    array(
    	'type'		=> 'checkbox',
    	'label'		=> __('Enable Social Icons','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'priority'	=> 1,
    	)
    );

    $wp_customize-> add_setting(
    'kreatif-facebook',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-facebook',
    array(
    	'label'		=> __('Facebook URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	 'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-twitter',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-twitter',
    array(
    	'label'		=> __('Twitter URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-google-plus',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-google-plus',
    array(
    	'label'		=> __('Google Plus URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-instagram',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-instagram',
    array(
    	'label'		=> __('Instagram URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-pinterest-p',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-pinterest-p',
    array(
    	'label'		=> __('Pinterest URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-youtube',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-youtube',
    array(
    	'label'		=> __('Youtube URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-vimeo',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-vimeo',
    array(
    	'label'		=> __('Vimeo URL','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-envelope',
    array(
    	'default'	=> '',
    	'sanitize_callback' => 'esc_url_raw',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-envelope',
    array(
    	'label'		=> __('Your E-Mail Info','kreatif'),
    	'section'	=> 'kreatif-sec1',
    	'type'		=> 'text',
    	'active_callback'	=> 'is_social_enable'
    	)
    );
    
    /*---- Slider Settings ----*/
    
    $wp_customize-> add_panel(
    'kreatif-slider', array(
    'priority'       => 10,
    'capability'     => 'edit_theme_options',
    'theme_supports' => '',
    'title'          => __('Slider Settings', 'kreatif'),
    'description'    => '',
    ));
    
    $wp_customize-> add_section(
    'kreatif-slides',
    array(
    	'title'			=> __('Enable Slider','kreatif'),
    	'priority'		=> 3,
    	'panel'			=> 'kreatif-slider',
    	)
    );
    
    $wp_customize-> add_setting(
    'kreatif-slide-enable',
    array(
    	'default'			=> false,
    	'sanitize_callback'	=> 'kreatif_sanitize_checkbox',
    	)
    );
    
    $wp_customize-> add_control(
    'kreatif-slide-enable',
    array(
    	'type'		=> 'checkbox',
    	'label'		=> __('Enable Slider','kreatif'),
    	'section'	=> 'kreatif-slides',
    	'priority'	=> 1,
    	)
    );
    
    $wp_customize-> add_section(
    'kreatif-slide-1', array(
    	'title'		=> __('Slide 1', 'kreatif'),
    	'panel'		=> 'kreatif-slider',
    	)
    );
    
    $wp_customize->add_setting( 
    'kreatif-slide_1', array(
    	'default'			=> '',
    	'sanitize_callback'	=> 'esc_url_raw',
    	)
     );
 
	$wp_customize->add_control(
	    new WP_Customize_Image_Control(
	        $wp_customize,
	        'kreatif-slide_1',
	        array(
	            'label' => 'Slide Upload',
	            'section' => 'kreatif-slide-1',
	            'settings' => 'kreatif-slide_1',
	            'active_callback'	=> 'kreatif_slider'
	        )
	    )
	);
	
	$wp_customize-> add_setting( 
	'kreatif-title-1', array(
			'default'			=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-title-1', array(
		'label'		=> __('Title','kreatif'),
		'section'	=> 'kreatif-slide-1',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_setting( 
	'kreatif-desc-1', array(
			'default'			=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-desc-1', array(
		'label'		=> __('Description','kreatif'),
		'section'	=> 'kreatif-slide-1',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_setting( 
	'kreatif-url-1', array(
			'default'			=> '',
			'sanitize_callback'	=> 'esc_url_raw',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-url-1', array(
		'label'		=> __('URL','kreatif'),
		'section'	=> 'kreatif-slide-1',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_section(
    'kreatif-slide-2', array(
    	'title'		=> __('Slide 2', 'kreatif'),
    	'panel'		=> 'kreatif-slider',
    	'active_callback'	=> 'kreatif_slider'
    	)
    );
    
	$wp_customize->add_setting( 
    'kreatif-slide_2', array(
    	'default'			=> '',
    	'sanitize_callback'	=> 'esc_url_raw',
    	)
     );
 
	$wp_customize->add_control(
	    new WP_Customize_Image_Control(
	        $wp_customize,
	        'kreatif-slide_2',
	        array(
	            'label' => 'Slide Upload',
	            'section' => 'kreatif-slide-2',
	            'settings' => 'kreatif-slide_2',
	            'active_callback'	=> 'kreatif_slider'
	        )
	    )
	);
	
	$wp_customize-> add_setting( 
	'kreatif-title-2', array(
			'default'			=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-title-2', array(
		'label'		=> __('Title','kreatif'),
		'section'	=> 'kreatif-slide-2',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_setting( 
	'kreatif-desc-2', array(
			'default'			=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-desc-2', array(
		'label'		=> __('Description','kreatif'),
		'section'	=> 'kreatif-slide-2',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_setting( 
	'kreatif-url-2', array(
			'default'			=> '',
			'sanitize_callback'	=> 'esc_url_raw',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-url-2', array(
		'label'		=> __('URL','kreatif'),
		'section'	=> 'kreatif-slide-2',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_section(
    'kreatif-slide-3', array(
    	'title'		=> __('Slide 3', 'kreatif'),
    	'panel'		=> 'kreatif-slider',
    	'active_callback'	=> 'kreatif_slider'
    	)
    );
    
	$wp_customize->add_setting( 
    'kreatif-slide_3', array(
    	'default'			=> '',
    	'sanitize_callback'	=> 'esc_url_raw',
    	)
     );
 
	$wp_customize->add_control(
	    new WP_Customize_Image_Control(
	        $wp_customize,
	        'kreatif-slide_3',
	        array(
	            'label' => 'Slide Upload',
	            'section' => 'kreatif-slide-3',
	            'settings' => 'kreatif-slide_3',
	            'active_callback'	=> 'kreatif_slider'
	        )
	    )
	);
	
	$wp_customize-> add_setting( 
	'kreatif-title-3', array(
			'default'			=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-title-3', array(
		'label'		=> __('Title','kreatif'),
		'section'	=> 'kreatif-slide-3',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_setting( 
	'kreatif-desc-3', array(
			'default'			=> '',
			'sanitize_callback'	=> 'kreatif_sanitize_text',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-desc-3', array(
		'label'		=> __('Description','kreatif'),
		'section'	=> 'kreatif-slide-3',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	$wp_customize-> add_setting( 
	'kreatif-url-3', array(
			'default'			=> '',
			'sanitize_callback'	=> 'esc_url_raw',
			 )
	);
	
	$wp_customize-> add_control(
	'kreatif-url-3', array(
		'label'		=> __('URL','kreatif'),
		'section'	=> 'kreatif-slide-3',
		'type'		=> 'text',
		'active_callback'	=> 'kreatif_slider'
		)
	);
	
	function kreatif_slider() {
	    if (get_theme_mod('kreatif-slide-enable', false) == true) {
		    return true;
	    } else {
		    return false;
	    }
    }
    
    //---- Pro Settings ----//
	
	class kreatif_Review_Control extends WP_Customize_Control {
		
		public $type = 'kreatif-options';
		 
	    public function render_content() {
	        ?>
	        <li><h2 class="dvt-features"><?php _e('There are some restrictions with WordPress Guidelines due to which there cannot be more than one Featured Area integrated in the theme. For more Featured areas and other options in the theme, download it from <br><a href="https://www.inkhive.com/product/kreatif">https://www.inkhive.com/product/kreatif</a>', 'kreatif'); ?></h2></li>
			<br>
				
	        <?php
	    }
	}
	
	$wp_customize-> add_section(
    'kreatif_pro',
    array(
    	'title'			=> __('Show your Support','kreatif'),
    	'priority'		=> 1,
    	)
    );
    
    $wp_customize-> add_setting(
	    'kreatif_review',
		array(
			'sanitize_callback'	=> 'esc_url_raw'
		)
    );
    
    $wp_customize->add_control(
	    new kreatif_Review_Control(
		    $wp_customize,
                'kreatif_review', array(
                'section' => 'kreatif_pro',
                'type' => 'kreatif-options',
            )
	    )
    );
    
    /*---- ProKreatif ----*/
    
    $wp_customize->add_section(
	    'prokreatif',
	    array(
		    'title'	=> __('Get Prokreatif', 'kreatif'),
		    'description'	=> __('<i>Now, you can get the Premium version of the Kreatif WordPress Theme, Prokreatif and enjoy all the premium features of the Premium versions making your blog the masterpiece it deserves to be.</i>', 'kreatif'),
		    'priority'	=> 2
	    )
    );
    
    class Prokreatif_Customize_Control extends WP_Customize_Control {
	    
	    public $type	=	'prokreatif_link';
	    
	    public function render_content() {
		    ?>
		    <label>
		    	<a href="http://www.divjot.co/product/prokreatif" target="_blank"><button type="button" class="button"><i class="fa fa-star" aria-hidden="true"></i>
Get ProKreatif</button></a>
		    </label>
		    <a href="http://www.divjot.co/product/prokreatif" target="_blank"><img src="<?php echo get_template_directory_uri(). "/images/pro-thumb.jpg"; ?>"></a>
		    <br/>
		    <br/>
		    <label>
		    	<p>If you don't want to purchase the theme, no worries. But kindly show your support by liking <a href="http://www.divjot.co" target="_blank">Divjot.Co</a> on Facebook and Following me on Twitter. The respective links can be found in the "Show your Support" Section.</p>
		    </label>
		    <?php
	    }
    }
    
    $wp_customize->add_setting(
	    'pro_link',
	    array(
		    'sanitize_callback'	=> 'esc_html'
	    )
    );
    
    $wp_customize->add_control( new Prokreatif_Customize_Control( $wp_customize,
    	'pro_link',
    		array(
	    		'type'	=> 'prokreatif_link',
	    		'label'	=> __('Get Prokreatif', 'kreatif'),
	    		'section'	=> 'prokreatif',
	    		'priority'	=> 1
    		)
    	)    
    );
    
    class MyCustom_Customize_Control extends WP_Customize_Control {    
	    public function render_content() {
	        ?>
	        <label>
						<input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
						<?php echo esc_html( $this->label ); ?>
						<?php if ( ! empty( $this->description ) ) : ?>
							<span class="description customize-control-description"><?php echo $this->description; ?></span>
						<?php endif; ?>
					</label>
					
					<script>
					jQuery(function($){
						/* $('#customize-control-pro_hide' ).show(); */
						/*
wp.customize( 'pro_hide', function( value ) {
							value.bind( function( to ) {
*/
								$( '#customize-control-pro_hide' ).hide();
								$( '#accordion-section-kreatif_pro #accordion-section-title' ).css({"color":"#fff"});
							/*
} );
						} );
*/						
					});
					</script>

					
	        <?php
	    }
	}
    
    $wp_customize->add_setting(
	'pro_hide',
	array(
		'default'			=> false,
		'sanitize_callback'	=> 'kreatif_sanitize_checkbox',
		)
	);
 
	$wp_customize-> add_control( new MyCustom_Customize_Control( $wp_customize,
    'pro_hide',
    array(
    	'type'		=> 'checkbox',
    	'label'		=> __('Hide this section forever.','kreatif'),
    	'section'	=> 'kreatif_pro',
    	'priority'	=> 1,
    	)
    ));
    
	function kreatif_sanitize_text( $t ) {
    return wp_kses_post( force_balance_tags( $t ) );
    }
    
    function kreatif_sanitize_checkbox( $i ) {
	    if ( $i == 1 ) {
	        return 1;
	    } 
	    else {
	        return '';
	    }
	}
	
	if ( $wp_customize->is_preview() ) {
	    add_action( 'wp_footer', 'kreatif_customize_preview', 21);
	}
	
	function kreatif_customize_preview() {
    ?>
    <script type="text/javascript">
        ( function( jQuery ) {
            wp.customize('kreatif-title-color',function( value ) {
                value.bind(function(to) {
                    jQuery('.site-title a').css('color', to );
                });
            });
            wp.customize('header_textcolor',function( value ) {
                value.bind(function(to) {
                    jQuery('.site-description').css('color', to );
                });
            });
        } )( jQuery )
    </script>
    <?php
}  // End function kreatif_customize_preview()
	
}
add_action( 'customize_register', 'kreatif_customize_register' );

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

