<?php

/* Theme Customizer setup. 
 * This hook allows you define new Theme Customizer sections, settings, and controls.
 *
 */

add_action( 'customize_register', 'airy_customize_register' );

function airy_customize_register( $wp_customize ) {

	/* Enable live preview for WordPress theme features. */
	$wp_customize->get_setting( 'blogname' )->transport = 'refresh';
	
	function airy_sanitize_checkbox( $checked ) {
	  // Boolean check.
	  return ( ( isset( $checked ) && true == $checked ) ? true : false );
	}

	/* *************** */
	/*                 */
	/* Font Customizer */
	/*                 */
	/* *************** */
	// PANEL
	$wp_customize->add_panel( 'airy_fonts_panel', array(
		  'title' => esc_html__( 'Fonts', 'airy' ),
		  'description' => esc_html__( 'Choose where to load fonts from.', 'airy' ),
		  'priority' => 30, 
	) );
	// SECTIONS
	$wp_customize->add_section( 'airy_websafe_fonts' , array(
		'panel'	    => 'airy_fonts_panel',
		'title'       => esc_html__( 'Web safe fonts', 'airy' ),
		'priority'    => 1,
	) );
	$wp_customize->add_section( 'airy_google_fonts' , array(
		'panel'	    => 'airy_fonts_panel',
		'title'       => esc_html__( 'Google fonts', 'airy' ),
		'priority'    => 2,
	) );
	$wp_customize->add_section( 'airy_adobe_fonts' , array(
		'panel'	    => 'airy_fonts_panel',
		'title'       => esc_html__( 'Adobe fonts (Typekit)', 'airy' ),
		'priority'    => 3,
	) );
	$wp_customize->add_section( 'airy_self_fonts' , array(
		'panel'	    => 'airy_fonts_panel',
		'title'       => esc_html__( 'Self-hosted fonts', 'airy' ),
		'priority'    => 4,
	) );
	
	/* ************* */
	/* Web safe font */
	/* ************* */
	
	// CHECKBOX
	$wp_customize->add_setting( 'airy_websafe_fonts_checkbox', array(
		  'capability' 	  => 'edit_theme_options',
		  'default' 		  => true,
		  'sanitize_callback' => 'airy_sanitize_checkbox',
		) 
	);
	$wp_customize->add_control( 'airy_websafe_fonts_checkbox', array(
		  'type' => 'checkbox',
		  'section' => 'airy_websafe_fonts', 
		  'label' => esc_html__( 'Enable Web safe fonts', 'airy' ),
		  'description' => esc_html__( 'Use web-safe fonts to speed up performance.', 'airy' )
		) 
	);
	
	//SETTINGS
	$wp_customize->add_setting( 'websafe_font', array(
			'default' => '-apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Open Sans", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'websafe_font_2', array(
			'default' => 'Georgia, serif',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);

	//CONTROLS
	$wp_customize->add_control( 'websafe_font', array(
			'label' => esc_html__( 'Font Family', 'airy' ),
			'section' => 'airy_websafe_fonts',
			'settings' => 'websafe_font',
			'type' => 'text',
			'description' => esc_html__( 'Font family for headings (sans-serif):', 'airy' )
        )
	);
	$wp_customize->add_control( 'websafe_font_2', array(
			'label' => esc_html__( 'Font Family 2', 'airy' ),
			'section' => 'airy_websafe_fonts',
			'settings' => 'websafe_font_2',
			'type' => 'text',
			'description' => esc_html__( 'Font family for body copy (serif):', 'airy' )
        )
	);
	
	
	/* ************ */
	/* Google Fonts */
	/* ************ */
	
	// CHECKBOX
	$wp_customize->add_setting( 'airy_google_fonts_checkbox', array(
		  'capability' 	  => 'edit_theme_options',
		  'default' 		  => false,
		  'sanitize_callback' => 'airy_sanitize_checkbox',
		) 
	);
	
	$wp_customize->add_control( 'airy_google_fonts_checkbox', array(
		  'type' => 'checkbox',
		  'section' => 'airy_google_fonts', 
		  'label' => esc_html__( 'Enable Google fonts', 'airy' ),
		  'description' => esc_html__( 'Use Google fonts. Choose first font sans-serif for headings and second serif for copy. For multiple weights, separate them with comma (eg. 400,700).', 'airy' )
		) 
	);
	
	//SETTINGS
	$wp_customize->add_setting( 'google_font', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'google_font_weights', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'google_font_2', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'google_font_2_weights', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'google_fonts_subset', array(
			'default' => 'latin',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	// CONTROLS
	$wp_customize->add_control( 'google_font', array(
			'label' => esc_html__( 'Google Font', 'airy' ),
			'section' => 'airy_google_fonts',
			'settings' => 'google_font',
			'type' => 'text',
			'description' => esc_html__( 'Font family for headings (sans-serif):', 'airy' )
        )
	);
	$wp_customize->add_control( 'google_font_weights', array(
			'label' => esc_html__( 'Google Font Weight', 'airy' ),
			'section' => 'airy_google_fonts',
			'settings' => 'google_font_weights',
			'type' => 'text'
        )
	);
	$wp_customize->add_control( 'google_font_2', array(
			'label' => esc_html__( 'Google Font 2', 'airy' ),
			'section' => 'airy_google_fonts',
			'settings' => 'google_font_2',
			'type' => 'text',
			'description' => esc_html__( 'Font family for body copy (serif):', 'airy' )
        )
	);
	$wp_customize->add_control( 'google_font_2_weights', array(
			'label' => esc_html__( 'Google Font 2 Weight', 'airy' ),
			'section' => 'airy_google_fonts',
			'settings' => 'google_font_2_weights',
			'type' => 'text'
        )
	);
	$wp_customize->add_control( 'google_fonts_subset', array(
			'label' => esc_html__( 'Character Subset', 'airy' ),
			'section' => 'airy_google_fonts',
			'settings' => 'google_fonts_subset',
			'type' => 'select',
			'choices' => array(
		            'latin' => 'Latin (latin)',
		            'latin-ext' => 'Latin Extended (latin-ext)',
		            'greek' => 'Greek (greek)',
		            'greek-ext' => 'Greek Extended (greek-ext)',
		            'vietnamese' => 'Vietnamese (vietnamese)',
		            'cyrillic-ext' => 'Cyrillic Extended (cyrillic-ext)',
		            'cyrillic' => 'Cyrillic (cyrillic)'
		        )
		)
	);
	
	
	/* *********** */
	/* Adobe Fonts */
	/* *********** */
	
	// CHECKBOX
	$wp_customize->add_setting( 'airy_adobe_fonts_checkbox', array(
		  'capability' 	  => 'edit_theme_options',
		  'default' 		  => false,
		  'sanitize_callback' => 'airy_sanitize_checkbox',
		) 
	);
	
	$wp_customize->add_control( 'airy_adobe_fonts_checkbox', array(
		  'type' => 'checkbox',
		  'section' => 'airy_adobe_fonts', 
		  'label' => esc_html__( 'Enable Adobe fonts', 'airy' ),
		  'description' => esc_html__( 'Use Adobe fonts. Choose first font sans-serif for headings with a bold weight and second serif for copy with a normal weight. Use subsetting and turn off OpenType features to speed up performance.', 'airy' )
		) 
	);
	
	//SETTINGS
	$wp_customize->add_setting( 'adobe_embed_id', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'adobe_font', array(
			'default' => ' ',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'adobe_font_2', array(
			'default' => ' ',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);

	// CONTROLS
	$wp_customize->add_control( 'adobe_embed_id', array(
			'label' => esc_html__( 'Adobe Embed ID', 'airy' ),
			'section' => 'airy_adobe_fonts',
			'settings' => 'adobe_embed_id',
			'type' => 'text',
			'description'  => esc_html__( 'Paste the generated embed ID here.', 'airy' )
        )
	);
	$wp_customize->add_control( 'adobe_font', array(
			'label' => esc_html__( 'Adobe Font', 'airy' ),
			'section' => 'airy_adobe_fonts',
			'settings' => 'adobe_font',
			'type' => 'text',
			'description' => esc_html__( 'Font family for headings (sans-serif):', 'airy' )
        )
	);
	$wp_customize->add_control( 'adobe_font_2', array(
			'label' => esc_html__( 'Adobe Font 2', 'airy' ),
			'section' => 'airy_adobe_fonts',
			'settings' => 'adobe_font_2',
			'type' => 'text',
			'description' => esc_html__( 'Font family for body copy (serif):', 'airy' )
        )
	);
	
	/* ***************** */
	/* Self-hosted Fonts */
	/* ***************** */
	
	// CHECKBOX
	$wp_customize->add_setting( 'airy_self_fonts_checkbox', array(
		  'capability' 	  => 'edit_theme_options',
		  'default' 		  => false,
		  'sanitize_callback' => 'airy_sanitize_checkbox',
		) 
	);
	
	$wp_customize->add_control( 'airy_self_fonts_checkbox', array(
		  'type' => 'checkbox',
		  'section' => 'airy_self_fonts', 
		  'label' => esc_html__( 'Enable self-hosted fonts', 'airy' ),
		  'description' => esc_html__( 'Use self-hosted fonts. Choose first font sans-serif for headings and second serif for copy. Upload the fonts to /fonts directory of the theme. Make sure the file name matches font name (eg. my-font.ttf = My Font).', 'airy' )
		) 
	);
	
	//SETTINGS
	$wp_customize->add_setting( 'self_font', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_setting( 'self_font_2', array(
			'default' => '',
			'sanitize_callback' => 'sanitize_text_field'
		)
	);

	//CONTROLS
	$wp_customize->add_control( 'self_font', array(
			'label' => esc_html__( 'Font Name', 'airy' ),
			'section' => 'airy_self_fonts',
			'settings' => 'self_font',
			'type' => 'text',
			'description' => esc_html__( 'Font name for headings (sans-serif):', 'airy' )
        )
	);
	$wp_customize->add_control( 'self_font_2', array(
			'label' => esc_html__( 'Font Name 2', 'airy' ),
			'section' => 'airy_self_fonts',
			'settings' => 'self_font_2',
			'type' => 'text',
			'description' => esc_html__( 'Font name for body copy (serif):', 'airy' )
        )
	);

	
	
	/* *************** */
	/*                 */
	/*  Color Schemer  */
	/*                 */
	/* *************** */
	$colors = array();

	$colors[] = array(
		'slug'=>'accent_color', 
		'default' => '#265CBF',
		'label' => esc_html__('Links', 'airy')
	);
	$colors[] = array(
		'slug'=>'text_color', 
		'default' => '#333333',
		'label' => esc_html__('Text Color', 'airy')
	);
	$colors[] = array(
		'slug'=>'button_color', 
		'default' => '#333333',
		'label' => esc_html__('Buttons', 'airy')
	);
	$colors[] = array(
		'slug'=>'meta_color', 
		'default' => '#727478',
		'label' => esc_html__('Meta Text', 'airy')
	);
	$colors[] = array(
		'slug'=>'line_color', 
		'default' => '#DFDFDF',
		'label' => esc_html__('Borders', 'airy')
	);
	$colors[] = array(
		'slug'=>'block_color', 
		'default' => '#f5f5f5',
		'label' => esc_html__('Blocks', 'airy')
	);
	foreach( $colors as $color ) {
		// SETTINGS
		$wp_customize->add_setting(
			$color['slug'], array(
				'default' => $color['default'],
				'type' => 'option', 
				'capability' => 'edit_theme_options',
				'sanitize_callback' => 'sanitize_hex_color'
			)
		);
		// CONTROLS
		$wp_customize->add_control(
			new WP_Customize_Color_Control(
				$wp_customize,
				$color['slug'], 
				array(
					'label' => $color['label'], 
					'section' => 'colors',
					'settings' => $color['slug']
				)
			)
		);
	}
	
}

/**
 * Output settings CSS into the head.
 * 
 */
function airy_customize_css()
{	
	$bg_color = get_background_color();
	$accent_color = get_option('accent_color','#265CBF');
	$text_color = get_option('text_color',    '#333333');
	$button_color = get_option('button_color',    '#333333');
	$meta_color = get_option('meta_color',    '#727478');
	$line_color = get_option('line_color',    '#dfdfdf');
	$block_color = get_option('block_color',  '#f5f5f5');
	$font  = 'sans-serif';
	$font2 = 'serif';

	if ( get_theme_mod('airy_websafe_fonts_checkbox') == 1 ) {
		$font = get_theme_mod('websafe_font');
		$font2 = get_theme_mod('websafe_font_2');
	}	
	if ( get_theme_mod('airy_google_fonts_checkbox') == 1 ) {
		$font = get_theme_mod('google_font');
		$font2 = get_theme_mod('google_font_2');
	}
	if ( get_theme_mod('airy_adobe_fonts_checkbox') == 1 ) {
		$font = get_theme_mod('adobe_font');
		$font2 = get_theme_mod('adobe_font_2');
	}
	if ( get_theme_mod('airy_self_fonts_checkbox') == 1 ) {
		$font = get_theme_mod('self_font');
		$font2 = get_theme_mod('self_font_2');
		
		$fonts_dir = get_template_directory_uri() . '/fonts/';
		$font_file =   str_replace(' ', '-', strtolower($font));
		$font_file_2 = str_replace(' ', '-', strtolower($font2));
		
		$airy_self_fonts_custom_css = "
		/* Embed Font */
		@font-face {
		  font-family: '{$font}';
		  src: url('{$fonts_dir}{$font_file}.woff2') format('woff2'),
		       url('{$fonts_dir}{$font_file}.woff') format('woff'),
		       url('{$fonts_dir}{$font_file}.ttf') format('truetype');
		}
		
		@font-face {
		  font-family: '{$font2}';
		  src: url('{$fonts_dir}{$font_file_2}.woff2') format('woff2'),
		       url('{$fonts_dir}{$font_file_2}.woff') format('woff'),
		       url('{$fonts_dir}{$font_file_2}.ttf') format('truetype');
		}
		";
		wp_add_inline_style( 'hybrid-style',   $airy_self_fonts_custom_css );
		wp_add_inline_style( 'wp-edit-blocks', $airy_self_fonts_custom_css );
	}

	$airy_custom_css = "
		/* Font */
		body, 
		button, input, label { font-family: {$font}; }
		.editor-styles-wrapper,
		.editor-post-title__block .editor-post-title__input { font-family: {$font} !important; }
		em,
		article p,           .editor-styles-wrapper p,
		article li,          .editor-styles-wrapper li,
		article blockquote,  .editor-styles-wrapper blockquote,
		article table,       .editor-styles-wrapper table,
		article dl,          .editor-styles-wrapper dl  { font-family: {$font2}; }
		
	
		/* Colors */
		:root {
		  --bg-color:     {$bg_color};
		  --accent-color: {$accent_color};
		  --text-color:   {$text_color};
		  --button-color: {$button_color};
		  --meta-color:   {$meta_color};
		  --line-color:   {$line_color};
		  --block-color:  {$block_color};
		}
	";
	
	wp_add_inline_style( 'hybrid-style',   $airy_custom_css );
	wp_add_inline_style( 'wp-edit-blocks', $airy_custom_css );
}


/**
 * Enqueue fonts.
 *
 */
function airy_fonts() {
	
	/* Google fonts Enabled */
	if ( get_theme_mod('airy_google_fonts_checkbox') == 1 ) {
		$googlefonts_args = array(
			'family' => str_replace(' ', '+', get_theme_mod('google_font')).':'.get_theme_mod('google_font_weights').'|'.str_replace(' ', '+', get_theme_mod('google_font_2')).':'.get_theme_mod('google_font_2_weights'),
			'subset' => get_theme_mod('google_fonts_subset')
		);
		
		wp_register_style( 'airy-google-fonts', add_query_arg( $googlefonts_args, "//fonts.googleapis.com/css" ), array(), null );
		wp_enqueue_style( 'airy-google-fonts' );
	}
	
	/* Adobe fonts Enabled */
	if ( get_theme_mod('airy_adobe_fonts_checkbox') == 1 ) {
		wp_enqueue_script( 'airy-adobe-fonts', 'https://use.typekit.net/'. get_theme_mod('adobe_embed_id') .'.js', array(), '1.0' );
	}
	
}

/* Output settings CSS into the head. */
add_action( 'wp_enqueue_scripts',    'airy_customize_css', 30);
add_action( 'admin_enqueue_scripts', 'airy_customize_css', 30);

/* Enqueue fonts */
add_action( 'wp_enqueue_scripts',    'airy_fonts' );
add_action( 'admin_enqueue_scripts', 'airy_fonts' );