<?php
/**
 * Contains methods for customizing the theme customization screen.
 */
 
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Kktfwp_Customize' ) ) {
  
	class Kktfwp_Customize extends Kktfwp_Settings {
		
		// Array for postMessage
		public static $js_controls = array();		

		
		/**
		* This outputs the javascript needed to automate the live settings preview.
		* Also keep in mind that this function isn't necessary unless your settings 
		* are using 'transport'=>'postMessage' instead of the default 'transport'
		* => 'refresh'
		* 
		* Used by hook: 'customize_preview_init'
		*/

		public static function live_preview() {
			
			wp_enqueue_script( 'kktfwp-webfont', get_theme_file_uri( '/includes/customizer/js/webfont.js' ), array( 'jquery' ),	'1.6.26', true );
								   
			wp_enqueue_script( 'kktfwp-customizer-preview',	get_theme_file_uri( '/includes/customizer/js/customize-preview.js' ), array( 'jquery','customize-preview' ),	kktfwp_themeData(), true );	
			
			$controls = self::$js_controls;
			$to_send = array();
			
			foreach ( $controls as $setting ) {
				if ( $setting['auto_preview'] && isset( $setting['js_vars'] ) && ! empty( $setting['js_vars'] ) && is_array( $setting['js_vars'] ) ) {
					
					/* $to_send[ ( isset( $setting['settings']['variant'] ) ) ? $setting['settings']['variant'] : $setting['settings']['default'] ] = $setting['js_vars']; */
					
					if ( isset( $setting['settings']['variant'] ) ) {
						
						$setting['js_vars']['property'] = 'fontWeight';
						$to_send[ $setting['settings']['variant'] ] = $setting['js_vars'];
						
						$setting['js_vars']['property'] = 'fontFamily';
						$to_send[ $setting['settings']['default'] ] = $setting['js_vars'];			
					} else {
						$to_send[ $setting['settings']['default'] ] = $setting['js_vars'];						
					}
				}
				
			}
		  
			wp_localize_script( 'kktfwp-customizer-preview', 'kktfwp_jsvars', $to_send );
		}
	   
		/**
		 * Controls API
		 */
		
		public static function controls_api() {
					
			wp_enqueue_script( 'kktfwp-customize-control', get_theme_file_uri( '/includes/customizer/js/customize-controls.js' ), array( 'customize-controls' ), kktfwp_themeData(), true );
			
			$controls = self::$js_controls;
			$to_send = array();
			$fonts = array();
			
			foreach ( $controls as $setting ) {
				if ( $setting['auto_control'] && $setting['auto_control'] !== 'font' && isset( $setting['js_vars'] ) && ! empty( $setting['js_vars'] ) ) {
					$to_send[ $setting['settings']['default'] ] = $setting['js_vars'];
				}	
				if ( $setting['auto_control'] === 'font' ) {
					$fonts[] = $setting['id'];
				}
			}
		  
			wp_localize_script( 'kktfwp-customize-control', 'kktfwp_controlVars', $to_send );
			wp_localize_script( 'kktfwp-customize-control', 'kktfwp_fonts', $fonts );
		}
	}
}

// Setup the Theme Customizer settings and controls...
add_action( 'customize_register' , array( 'Kktfwp_Customize', 'register' ) );

// Enqueue controls API
add_action( 'customize_controls_enqueue_scripts' , array( 'Kktfwp_Customize', 'controls_api' ) );

// Enqueue live preview javascript in Theme Customizer admin screen
add_action( 'customize_preview_init' , array( 'Kktfwp_Customize', 'live_preview' ) );