<?php

/**
 * Adds WP_Customize_Textarea_Control
 */

if ( class_exists( 'WP_Customize_Control' ) ) {
	class WP_Customize_Textarea_Control extends WP_Customize_Control {
		public $type = 'textarea';

    	public function render_content() { ?>
			<label>
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
				<textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
			</label>
			<?php
		}
  	}
}

/**
 * Adds sections to the customizer
 * @param WP_Customize_Control $wp_customize
 */
function ya_customize_register( $wp_customize ){

	$priority = 200;
	$sections = array();
	$ya_options = ya_options();	
	$i = 0;
	foreach ( $ya_options->sections as $section) {
		
		if ( isset($section['fields']) && is_array($section['fields']) ) {
			
			foreach ($section['fields'] as $field ){
				
				$customize = $field['id'].'_customize_allow';
				if ($ya_options->get($customize)) {
					
					if ( isset($field['options']) ) $field['choices'] = $field['options'];
					
					if ($field['type'] == 'upload') $field['type'] = 'image';
					
					if ($field['type'] == 'radio_img') {
						$field['type'] = 'select';
						$field['choices'] = array();
		
						foreach ( $field['options'] as $key => $choices ) {
							$field['choices'][$key] = $choices['title']; 
						}
					}
					
					$field['label'] = $field['title'];
					$field['section'] = sanitize_title($section['title']);
					$field['settings'] = $ya_options->args['opt_name'].'['.$field['id'].']';
					$field['priority'] = $priority++;
					$sections[$i]['fields'][] = $field;
				}
			} 
			
			if ( isset($sections[$i]['fields']) ) {
				$sections[$i]['title'] = $section['title'];
				$sections[$i]['priority'] = $priority++;
				$sections[$i]['capability'] = 'edit_theme_options';
				$i++;
			}
		}
	}
	
	foreach ( $sections as $section ) {
			
		// Add Section
		$wp_customize->add_section( sanitize_title($section['title']), $section );
		
		foreach ($section['fields'] as $field ){ 

			//Add Setting
			$wp_customize->add_setting( 
					$ya_options->args['opt_name'].'['.$field['id'].']', 
					array(
						'default'  	=> $ya_options->get($field['id']),
    					'type' 		=> 'option',
						'transport' => 'postMessage',
						'capability' => 'edit_theme_options'
					)
				);
				
			// Add Control
			switch( $field['type'] ){
				case 'color':
					$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $field['id'], $field ) );
					break;
				case 'image':
					$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, $field['id'], $field ) );
					break;
				case 'textarea':
					$wp_customize->add_control( new WP_Customize_Textarea_Control( $wp_customize, $field['id'], $field ) );
					break;
				default:
					$wp_customize->add_control( $field['id'], $field );
			}
			//$wp_customize->get_setting($ya_options->args['opt_name'].'['.$field['id'].']')->transport = 'postMessage';
		}		
	}
	$wp_customize -> get_setting( 'blogname' )-> transport = 'postMessage';
	$wp_customize -> get_setting( 'blogdescription' )-> transport = 'postMessage';
}
add_action( 'customize_register', 'ya_customize_register' );

/*
function ya_typography_css(){
	$styles = '';
	if ( ya_options()->getCpanelValue('google_webfonts') ):
		
		$webfonts_assign = ya_options()->getCpanelValue('webfonts_assign');
		$styles = '<style>';
		if ( $webfonts_assign == 'headers' ){
			$styles .= 'h1, h2, h3, h4, h5, h6 {';
		} else if ( $webfonts_assign == 'custom' ){
			$custom_assign = ya_options()->getCpanelValue('webfonts_custom');
			$custom_assign = trim($custom_assign);
			if (!$custom_assign) return '';
			$styles .= $custom_assign . ' {';
		} else {
			$styles .= 'body, input, button, select, textarea, .search-query {';
		}
		$styles .= 'font-family: ' . ya_options()->getCpanelValue('google_webfonts') . ';}</style>';
	endif;
	return $styles;
}

function ya_typography_css_cache(){
	$data = get_transient( 'ya_typography_css' );
	if ( $data === false ) {
		$data = ya_typography_css();
		set_transient( 'ya_typography_css', $data, 3600 * 24 );
	}
	echo $data;
}
add_action( 'wp_head', 'ya_typography_css_cache', 12, 0 );

function ya_typography_css_cache_reset(){
	delete_transient( 'ya_typography_css' );
	ya_typography_css_cache();
}
add_action( 'customize_preview_init', 'ya_typography_css_cache_reset' );


function ya_typography_webfonts(){
	if ( ya_options()->getCpanelValue('google_webfonts') ):
		$webfont				= ya_options()->getCpanelValue('google_webfonts');
		$webfont_weight			= ya_options()->getCpanelValue('webfonts_weight');
		$webfont_character_set	= ya_options()->getCpanelValue('webfonts_character_set');
		
		$f = strlen($webfont);
		if ($f > 3){
			$webfontname = str_replace( ' ', '+', $webfont );
			return '<link href="http://fonts.googleapis.com/css?family=' . $webfontname . ':' . $webfont_weight . '&subset=' . $webfont_character_set . '" rel="stylesheet">';
		}
	endif;
}

function ya_typography_webfonts_cache(){
	$data = get_transient( 'ya_typography_webfont' );
	if ( $data === false ) {
		$data = ya_typography_webfonts();
		set_transient( 'ya_typography_webfont', $data, 3600 * 24 );
	}
	echo $data;
}
add_action( 'wp_head', 'ya_typography_webfonts_cache', 11, 0 );


function ya_typography_webfonts_cache_reset(){
	delete_transient( 'ya_typography_webfont' );
	ya_typography_webfonts_cache();
}
add_action( 'customize_preview_init', 'ya_typography_webfonts_cache_reset' );

function ya_custom_header_scripts() {
	if ( ya_options()->getCpanelValue('advanced_head') ){
		echo ya_options()->getCpanelValue('advanced_head');
	}
}
add_action( 'wp_head', 'ya_custom_header_scripts', 200 );

function add_favicon(){
	if ( ya_options()->getCpanelValue('favicon') ){
		echo '<link rel="shortcut icon" href="' . ya_options()->getCpanelValue('favicon') . '" />';
	}
}
add_action('wp_head', 'add_favicon');

function ya_query_vars( $qvars ){
	global $add_query_vars;
	if ( is_array($add_query_vars) ){
		foreach ( $add_query_vars as $field ){
			$qvars[] = $field;
		}
	}
	return $qvars;
}

function ya_parse_request( &$wp ){
	global $add_query_vars;
	if ( is_array($add_query_vars) ){
		foreach ( $add_query_vars as $field ){
			if ( array_key_exists($field, $wp->query_vars) ){
				$current_value = ya_options()->getCpanelValue($field);
				$request_value = $wp->query_vars[$field];
				if ($request_value != $current_value){
					setcookie(
						$field,
						$request_value,
						time() + 86400,
						COOKIEPATH,
						COOKIE_DOMAIN,
						0
					);
					if (!isset($_COOKIE[$field]) || $request_value != $_COOKIE[$field]){
						$_COOKIE[$field] = $request_value;
					}
				}
			}
		}
	}
}

add_action('parse_request', 'ya_parse_request');
add_filter('query_vars',    'ya_query_vars');
*/