<?php
/**
 * pageflow-2k21 Theme Customizer
 *
 * @package pageflow-2k21
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function pageflow_2k21_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' => 'pageflow_2k21_customize_partial_blogname',
				)
			);
			$wp_customize->selective_refresh->add_partial(
				'blogdescription',
				array(
					'selector'        => '.site-description',
					'render_callback' => 'pageflow_2k21_customize_partial_blogdescription',
				)
			);
		}
		$wp_customize->add_section('heading_colors' ,array(
							'title'      =>  esc_html__('Heading Settings', 'pageflow-2k21'),
							'priority'   => 20,
						));	
		$h1_text_color_label = __( 'Heading 1 (H1) Color', 'pageflow-2k21');
		pageflow_heading_color($wp_customize, 'h1_text', $h1_text_color_label, 'heading_colors', 40,get_theme_mod('heading_text_color', get_customizer_defaults('h1_text')));
		
		$h2_text_color_label = __( 'Heading 2 (H2) Color', 'pageflow-2k21');								
		pageflow_heading_color($wp_customize, 'h2_text', $h2_text_color_label, 'heading_colors', 50,get_theme_mod('heading_text_color', get_customizer_defaults('h2_text')));
		
		$h3_text_color_label = __( 'Heading 3 (H3) Color', 'pageflow-2k21');								
		pageflow_heading_color($wp_customize, 'h3_text', $h3_text_color_label, 'heading_colors', 60,get_theme_mod('heading_text_color', get_customizer_defaults('h3_text')));
		
		$h4_text_color_label = __( 'Heading 4 (H4) Color', 'pageflow-2k21');								
		pageflow_heading_color($wp_customize, 'h4_text', $h4_text_color_label, 'heading_colors', 70,get_theme_mod('heading_text_color', get_customizer_defaults('h4_text')));
		
		$h5_text_color_label = __( 'Heading 5 (H5) Color', 'pageflow-2k21');								
		pageflow_heading_color($wp_customize, 'h5_text', $h5_text_color_label, 'heading_colors', 80,get_theme_mod('heading_text_color', get_customizer_defaults('h5_text')));
		
		$h6_text_color_label = __( 'Heading 6 (H6) Color', 'pageflow-2k21');								
		pageflow_heading_color($wp_customize, 'h6_text', $h6_text_color_label, 'heading_colors', 80,get_theme_mod('heading_text_color', get_customizer_defaults('h6_text')));
		
		$wp_customize->add_section('sample_custom_controls_section' ,array(
							'title'      =>  esc_html__('Google Font Settings', 'pageflow-2k21'),
							'priority'   => 20,
						));	
		
		$wp_customize->add_setting('sample_google_font_select',
			array(
			 'default' => get_theme_mod('google_font_select', get_customizer_defaults('sample_google_font_select')),
			 'type'              => 'theme_mod',
			 'sanitize_callback' => 'pageflow_sanitize_background',
			 'transport'         => 'postMessage',
			)
			
		);
		$wp_customize->add_control( new Skyrocket_Google_Font_Select_Custom_Control( $wp_customize, 'sample_google_font_select',
			array(
				'label' => __( 'Google Font Control' ,'pageflow-2k21'),
				'description' => esc_html__('Select font for heading','pageflow-2k21'),
				'section' => 'sample_custom_controls_section',
				'input_attrs' => array(
					'font_count' => 'all',
					'orderby' => 'alpha',
				),
			)
		));
			
 }
add_action('customize_register', 'pageflow_2k21_customize_register');

function pageflow_heading_color($wp_customize, $element, $label, $section,$priority, $default, $active_call = null, $desc = '' ){
	
	$wp_customize->add_setting(
		$element,
		array(
			'default'           => $default,
			'type'              => 'theme_mod',
			'sanitize_callback' => 'pageflow_sanitize_background',
			'transport'         => 'postMessage',
		)
	);
	
	$wp_customize->add_control(
				new WP_Customize_Color_Control(
					$wp_customize,
					$element,
					array(
						'label'           => $label,
						'section'         => $section,
						'priority'        => $priority,
						'active_callback' => $active_call,
						'settings'        => $element,
						'active_callback' => $active_call,
						'description'     => $desc,
     				)
				)
			);
	
}
function get_customizer_defaults( $option ) {

	$theme_options = defaults();
	$default_value = '';
	if ( isset( $theme_options[ $option ] ) ) {
		$default_value = $theme_options[ $option ];
	}
	return $default_value;
}
/**
 * Check if function present
 */
if (!function_exists( 'pageflow_sanitize_background' ) ) {
	/**
	 * Menu Background
	 *
	 * @param  string $input Color value.
	 * @return string        Output color.
	 */
	function pageflow_sanitize_background( $input ) {

		$output = apply_filters('pageflow_sanitize_hex', $input );

		return $output;
	}
	add_filter('pageflow_sanitize_background', 'pageflow_sanitize_background');
}
function defaults() {
	// Defaults list of options.
	$theme_options = apply_filters(
		'pageflow_theme_defaults',
		array(
			'header_background'                   => '#ffffff',
			'header_border'                       => '#eaeaea',
			'header_site_title'                   => '#333333',
			'header_site_title_hover'             => '#10659C',
			'header_text'                         => '#999999',
			'header_widget_text'                  => '#333333',
			'header_widget_background'            => '#ffffff',
			'header_widget_border'                => '#eaeaea',
			'header_widget_link'                  => '#0066CC',
			'header_widget_link_hover'            => '#10659C',

			'header_menu_background'              => '#ffffff',
			'header_menu_border'                  => '#eaeaea',
			'header_active_menu_background'       => '#ffffff',
			'header_menu_link'                    => '#333333',
			'header_menu_link_hover'              => '#10659C',
			'header_sub_menu_background'          => '#ffffff',
			'header_sub_menu_link'                => '#333333',
			'header_sub_menu_link_hover'          => '#10659C',
			'header_menu_toggle_background'       => 'transparent',
			'header_menu_toggle'                  => '#333333',
			'mobile_menu_toggle_border_color'     => '#333333',
			'menu_button_radius'                  => 0,
			'box_background'                      => '#ffffff',
			'alt_background'                      => '#eaeaea',
			'body_text'                           => '#333333',
			'h1_text'                             => '#333333',
			'h2_text'                             => '#333333',
			'h3_text'                             => '#333333',
			'h4_text'                             => '#333333',
			'h5_text'                             => '#333333',
			'h6_text'                             => '#333333',
			'meta_text'                           => '#999999',
			'link'                                => '#0066CC',
			'link_hover'                          => '#10659C',
			'sample_google_font_select' => json_encode(
				array(
					'font-family' => 'Open Sans',
					'regularweight' => 'regular',
					'italicweight' => 'italic',
					'font-weight' => '700',
					'category' => 'sans-serif'
				)
			),
		
		)
	);
	return $theme_options;
}
/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function pageflow_2k21_customize_partial_blogname() {
	bloginfo( 'name' );
}

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



/**
	 * Google Font sanitization
	 *
	 * @param  string	JSON string to be sanitized
	 * @return string	Sanitized input
	 */
	if (! function_exists('pageflow_google_font_sanitization' ) ) {
		function pageflow_google_font_sanitization( $input ) {
			$val =  json_decode( $input, true );
			if( is_array( $val ) ) {
				foreach ( $val as $key => $value ) {
					$val[$key] = sanitize_text_field( $value );
				}
				$input = json_encode( $val );
			}
			else {
				$input = json_encode( sanitize_text_field($val) );
			}
			return $input;
		}
	}

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