<?php
/**
 * Title: wp-head hooks
 *
 * Description: Defines actions/hooks to be called in wp-head action.
 *
 * Please do not edit this file. This file is part of the CyberChimps Framework and all modifications
 * should be made in a child theme.
 *
 * @category CyberChimps Framework
 * @package  Framework
 * @since    1.0
 * @author   CyberChimps
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v3.0 (or later)
 * @link     http://www.cyberchimps.com/
 */

if ( ! function_exists( 'cyberchimps_css_styles' ) ) {
	// adds styles to header created from functions at the bottom
	function cyberchimps_css_styles() {
		$body_styles      = cyberchimps_body_styles();
		$link_styles      = cyberchimps_link_styles();
		$headings_styles  = cyberchimps_headings_styles();
		?>

		<style type="text/css" media="all">
			<?php if ( ! empty( $body_styles ) ) : ?>
			body {
			<?php
			foreach ( $body_styles as $key => $body_style ) :
?>
	<?php echo $key; ?> : <?php echo $body_style; ?>;
			<?php endforeach; ?>
			}

			<?php endif; ?>
			<?php if ( ! empty( $link_styles ) ) : ?>
			<?php foreach ( $link_styles as $key2 => $link_style ) : ?>
			<?php echo $key2; ?>
			{
				color:
			<?php echo $link_style; ?>
			;
			}
			<?php endforeach; ?>
			<?php endif; ?>
			<?php if ( ! empty( $container_styles ) ) : ?>
			.container {
			<?php
			foreach ( $container_styles as $key3 => $container_style ) :
?>
	<?php echo $key3; ?> : <?php echo $container_style; ?>px;
			<?php endforeach; ?>
			}

			<?php endif; ?>

			<?php if ( ! empty( $headings_styles ) ) { ?>
			h1, h2, h3, h4, h5, h6 {
			<?php
			foreach ( $headings_styles as $key => $headings_style ) {
				if ( ! empty( $headings_style ) ) {
					echo $key;
				?>
				 : <?php echo $headings_style; ?>;

									<?php
				}
			}
							?>
			}

			<?php
}

			$width = intval( get_theme_mod( 'max_width' ) ) . 'px';
			$theme_options_main = get_option( 'theme_mods_' . get_template() );
if ( ! isset( $theme_options_main['max_width'] ) ) {
	set_theme_mod( 'max_width', '1020' );
}

if ( ! get_theme_mod( 'responsive_design', 'checked' ) ) {
?>
@media screen and (max-width: <?php echo $width; ?>) {
.container-full-width {
width: <?php echo $width; ?>;
}
}
<?php
}

			?>

		</style>
		<?php
		return;
	}
}

add_action( 'wp_head', 'cyberchimps_css_styles', 50 );

// Creat headings_styles array from options.
function cyberchimps_headings_styles() {

	// Set header font family.
		$fetch_customize_options = get_theme_mod( 'customize_options' );
	$headings_styles      = isset( $fetch_customize_options['font_family_headings'] ) ? $fetch_customize_options['font_family_headings'] : '';
	$google_font_headings = isset( $fetch_customize_options['google_font_headings'] ) ? $fetch_customize_options['google_font_headings'] : '';

	// older versions will have saved the font family as a string so we need to check for that first
	if ( is_array( $headings_styles ) ) {
		$headings_styles['font-family'] = $headings_styles['face'];
	} else {
		$headings_styles                = array();
		$headings_styles['font-family'] = $headings_styles;
	}

	// Check if Google fonts have been selected
	if ( $headings_styles['font-family'] == 'Google Fonts' && $google_font_headings != '' ) {
		$headings_styles['font-family'] = '"' . $google_font_headings . '"';

		// Check if SSL is present, if so then use https othereise use http
		$protocol = is_ssl() ? 'https' : 'http';

		wp_register_style( 'google-font-headings', $protocol . '://fonts.googleapis.com/css?family=' . $google_font_headings );
		wp_enqueue_style( 'google-font-headings' );
	}

	//TODO recreate original settings so they are actually named by the css style they refer to
	// eg face becomes font-family, size is font-size etc

	unset( $headings_styles['size'] );
	unset( $headings_styles['face'] );
	unset( $headings_styles['color'] );
	unset( $headings_styles['style'] );

	return $headings_styles;
}

// creates body_styles array from options
function cyberchimps_body_styles() {
	$body_styles = array();
	if ( get_theme_mod( 'text_colorpicker' ) ) {
		$body_styles['color'] = get_theme_mod( 'text_colorpicker' );
	}
	   $customize_typography_options = get_theme_mod( 'customize_options' );
	if ( $customize_typography_options['typography_options'] ) {
		$typography_options = $customize_typography_options['typography_options'];
		// changes terminology for typography to css elements
		foreach ( $typography_options as $option => $value ) {
			if ( $option == 'size' ) {
				$option = 'font-size';
			}
			if ( $option == 'face' ) {
				$option = 'font-family';
			}
			if ( $option == 'style' ) {
				$option = 'font-weight';
			}
			if ( $value != '' ) {
				$body_styles[ $option ] = $value;
			}
		}
	}

	// Set font-family if google font is on
	$google_font = isset( $customize_typography_options['google_font_field'] ) ? $customize_typography_options['google_font_field'] : '';

	if ( isset( $body_styles['font-family'] ) && $body_styles['font-family'] == 'Google Fonts' && $google_font != '' ) {
		$body_styles['font-family'] = '"' . $google_font . '"';

		// Check if SSL is present, if so then use https othereise use http
		$protocol = is_ssl() ? 'https' : 'http';

		wp_register_style( 'google-font', $protocol . '://fonts.googleapis.com/css?family=' . $google_font );
		wp_enqueue_style( 'google-font' );
	}

	return $body_styles;
}

// creates link color array for just a tag
function cyberchimps_link_styles() {
	$link_styles = array();
	if ( get_theme_mod( 'link_colorpicker' ) ) {
		$link_styles['a'] = get_theme_mod( 'link_colorpicker' );
	}
	if ( get_theme_mod( 'link_hover_colorpicker' ) ) {
		$link_styles['a:hover'] = get_theme_mod( 'link_hover_colorpicker' );
	}

	return $link_styles;
}
?>
