<?php
/**
 * This file is used to setup the Google Font Options and output
 * @package Ultra WordPress Theme
 * @since 1.0
 * @author AJ Clarke : http://wpexplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://wpexplorer.com
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */
 

 	
/**
 * Returns an array of system fonts
 * Feel free to edit this, update the font fallbacks, etc.
 * @since 1.0
 */
function wpex_typography_get_os_fonts() {
	// OS Font Defaults
	$os_faces = array(
		'Arial, sans-serif'				 			=> 'Arial',
		'Georgia, serif' 							=> 'Georgia',
		'"Helvetica Neue", Helvetica, sans-serif'	=> 'Helvetica Neue',
		'Tahoma, Geneva, sans-serif' 				=> 'Tahoma'
	);
	return $os_faces;
}


/**
 * Returns a select list of Google fonts
 * Feel free to edit this, update the fallbacks, etc.
 * @since 1.0
 */
function wpex_typography_get_google_fonts() {
	// Google Font Defaults
	$google_faces = array('Abel' => 'Abel','Abril Fatface' => 'Abril Fatface','Aclonica' => 'Aclonica','Actor' => 'Actor','Adamina' => 'Adamina','Aguafina Script' => 'Aguafina Script','Aladin' => 'Aladin','Aldrich' => 'Aldrich','Alice' => 'Alice','Alike Angular' => 'Alike Angular','Alike' => 'Alike','Allan' => 'Allan','Allerta Stencil' => 'Allerta Stencil','Allerta' => 'Allerta','Amaranth' => 'Amaranth','Amatic SC' => 'Amatic SC','Andada' => 'Andada','Andika' => 'Andika','Annie Use Your Telescope' => 'Annie Use Your Telescope','Anonymous Pro' => 'Anonymous Pro','Antic' => 'Antic','Anton' => 'Anton','Arapey' => 'Arapey','Architects Daughter' => 'Architects Daughter','Arimo' => 'Arimo','Artifika' => 'Artifika','Arvo' => 'Arvo','Asset' => 'Asset','Astloch' => 'Astloch','Atomic Age' => 'Atomic Age','Aubrey' => 'Aubrey','Bangers' => 'Bangers','Bentham' => 'Bentham','Bevan' => 'Bevan','Bigshot One' => 'Bigshot One','Bitter' => 'Bitter','Black Ops One' => 'Black Ops One','Bowlby One SC' => 'Bowlby One SC','Bowlby One' => 'Bowlby One','Brawler' => 'Brawler','Bubblegum Sans' => 'Bubblegum Sans','Buda' => 'Buda','Butcherman Caps' => 'Butcherman Caps','Cabin Condensed' => 'Cabin Condensed','Cabin Sketch' => 'Cabin Sketch','Cabin' => 'Cabin','Cagliostro' => 'Cagliostro','Calligraffitti' => 'Calligraffitti','Candal' => 'Candal','Cantarell' => 'Cantarell','Cardo' => 'Cardo','Carme' => 'Carme','Carter One' => 'Carter One','Caudex' => 'Caudex','Cedarville' => 'Cedarville','Changa One' => 'Changa One','Cherry Cream Soda' => 'Cherry Cream Soda','Chewy' => 'Chewy','Chicle' => 'Chicle','Chivo' => 'Chivo','Coda Caption' => 'Coda Caption','Coda' => 'Coda','Comfortaa' => 'Comfortaa','Coming Soon' => 'Coming Soon','Contrail One' => 'Contrail One','Convergence' => 'Convergence','Cookie' => 'Cookie','Copse' => 'Copse','Corben' => 'Corben','Cousine' => 'Cousine','Coustard' => 'Coustard','Covered By Your Grace' => 'Covered By Your Grace','Crafty Girls' => 'Crafty Girls','Creepster Caps' => 'Creepster Caps','Crimson Text' => 'Crimson Text','Crushed' => 'Crushed','Cuprum' => 'Cuprum','Damion' => 'Damion','Dancing Script' => 'Dancing Script','Dawning of a New Day' => 'Dawning of a New Day','Days One' => 'Days One','Delius Swash Caps' => 'Delius Swash Caps','Delius Unicase' => 'Delius Unicase','Delius' => 'Delius','Devonshire' => 'Devonshire','Didact Gothic' => 'Didact Gothic','Dorsa' => 'Dorsa','Dr Sugiyama' => 'Dr Sugiyama','Droid Sans Mono' => 'Droid Sans Mono','Droid Sans' => 'Droid Sans','Droid Serif' => 'Droid Serif','EB Garamond' => 'EB Garamond','Eater Caps' => 'Eater Caps','Expletus Sans' => 'Expletus Sans','Fanwood Text' => 'Fanwood Text','Federant' => 'Federant','Federo' => 'Federo','Fjord One' => 'Fjord One','Fondamento' => 'Fondamento','Fontdiner Swanky' => 'Fontdiner Swanky','Forum' => 'Forum','Francois One' => 'Francois One','Gentium Basic' => 'Gentium Basic','Gentium Book Basic' => 'Gentium Book Basic','Geo' => 'Geo','Geostar Fill' => 'Geostar Fill','Geostar' => 'Geostar','Give You Glory' => 'Give You Glory','Gloria Hallelujah' => 'Gloria Hallelujah','Goblin One' => 'Goblin One','Gochi Hand' => 'Gochi Hand','Goudy Bookletter 1911' => 'Goudy Bookletter 1911','Gravitas One' => 'Gravitas One','Gruppo' => 'Gruppo','Hammersmith One' => 'Hammersmith One','Herr Von Muellerhoff' => 'Herr Von Muellerhoff','Holtwood One SC' => 'Holtwood One SC','Homemade Apple' => 'Homemade Apple','IM Fell DW Pica SC' => 'IM Fell DW Pica SC','IM Fell DW Pica' => 'IM Fell DW Pica','IM Fell Double Pica SC' => 'IM Fell Double Pica SC','IM Fell Double Pica' => 'IM Fell Double Pica','IM Fell English SC' => 'IM Fell English SC','IM Fell English' => 'IM Fell English','IM Fell French Canon SC' => 'IM Fell French Canon SC','IM Fell French Canon' => 'IM Fell French Canon','IM Fell Great Primer SC' => 'IM Fell Great Primer SC','IM Fell Great Primer' => 'IM Fell Great Primer','Iceland' => 'Iceland','Inconsolata' => 'Inconsolata','Indie Flower' => 'Indie Flower','Irish Grover' => 'Irish Grover','Istok Web' => 'Istok Web','Jockey One' => 'Jockey One','Josefin Sans' => 'Josefin Sans','Josefin Slab' => 'Josefin Slab','Judson' => 'Judson','Julee' => 'Julee','Jura' => 'Jura','Just Another Hand' => 'Just Another Hand','Just Me Again Down Here' => 'Just Me Again Down Here','Kameron' => 'Kameron','Kelly Slab' => 'Kelly Slab','Kenia' => 'Kenia','Knewave' => 'Knewave','Kranky' => 'Kranky','Kreon' => 'Kreon','Kristi' => 'Kristi','La Belle Aurore' => 'La Belle Aurore','Lancelot' => 'Lancelot','Lato' => 'Lato','League Script' => 'League Script','Leckerli One' => 'Leckerli One','Lekton' => 'Lekton','Lemon' => 'Lemon','Limelight' => 'Limelight','Linden Hill' => 'Linden Hill','Lobster Two' => 'Lobster Two','Lobster' => 'Lobster','Lora' => 'Lora','Love Ya Like A Sister' => 'Love Ya Like A Sister','Loved by the King' => 'Loved by the King','Luckiest Guy' => 'Luckiest Guy','Maiden Orange' => 'Maiden Orange','Mako' => 'Mako','Marck Script' => 'Marck Script','Marvel' => 'Marvel','Mate SC' => 'Mate SC','Mate' => 'Mate','Maven Pro' => 'Maven Pro','Meddon' => 'Meddon','MedievalSharp' => 'MedievalSharp','Megrim' => 'Megrim','Merienda One' => 'Merienda One','Merriweather' => 'Merriweather','Metrophobic' => 'Metrophobic','Michroma' => 'Michroma','Miltonian Tattoo' => 'Miltonian Tattoo','Miltonian' => 'Miltonian','Miss Fajardose' => 'Miss Fajardose','Miss Saint Delafield' => 'Miss Saint Delafield','Modern Antiqua' => 'Modern Antiqua','Molengo' => 'Molengo','Monofett' => 'Monofett','Monoton' => 'Monoton','Monsieur La Doulaise' => 'Monsieur La Doulaise','Montez' => 'Montez','Mountains of Christmas' => 'Mountains of Christmas','Mr Bedford' => 'Mr Bedford','Mr Dafoe' => 'Mr Dafoe','Mr De Haviland' => 'Mr De Haviland','Mrs Sheppards' => 'Mrs Sheppards','Muli' => 'Muli','Neucha' => 'Neucha','Neuton' => 'Neuton','News Cycle' => 'News Cycle','Niconne' => 'Niconne','Nixie One' => 'Nixie One','Nobile' => 'Nobile','Nosifer Caps' => 'Nosifer Caps','Nothing You Could Do' => 'Nothing You Could Do','Nova Cut' => 'Nova Cut','Nova Flat' => 'Nova Flat','Nova Mono' => 'Nova Mono','Nova Oval' => 'Nova Oval','Nova Round' => 'Nova Round','Nova Script' => 'Nova Script','Nova Slim' => 'Nova Slim','Nova Square' => 'Nova Square','Numans' => 'Numans','Nunito' => 'Nunito','Old Standard TT' => 'Old Standard TT','Open Sans Condensed' => 'Open Sans Condensed','Open Sans' => 'Open Sans','Orbitron' => 'Orbitron','Oswald' => 'Oswald','Over the Rainbow' => 'Over the Rainbow','Ovo' => 'Ovo','PT Sans Caption' => 'PT Sans Caption','PT Sans Narrow' => 'PT Sans Narrow','PT Sans' => 'PT Sans','PT Serif Caption' => 'PT Serif Caption','PT Serif' => 'PT Serif','Pacifico' => 'Pacifico','Passero One' => 'Passero One','Patrick Hand' => 'Patrick Hand','Paytone One' => 'Paytone One','Permanent Marker' => 'Permanent Marker','Petrona' => 'Petrona','Philosopher' => 'Philosopher','Piedra' => 'Piedra','Pinyon Script' => 'Pinyon Script','Play' => 'Play','Playfair Display' => 'Playfair Display','Podkova' => 'Podkova','Poller One' => 'Poller One','Poly' => 'Poly','Pompiere' => 'Pompiere','Prata' => 'Prata','Prociono' => 'Prociono','Puritan' => 'Puritan','Quattrocento Sans' => 'Quattrocento Sans','Quattrocento' => 'Quattrocento','Questrial' => 'Questrial','Quicksand' => 'Quicksand','Radley' => 'Radley','Raleway' => 'Raleway','Rammetto One' => 'Rammetto One','Rancho' => 'Rancho','Rationale' => 'Rationale','Redressed' => 'Redressed','Reenie Beanie' => 'Reenie Beanie','Ribeye Marrow' => 'Ribeye Marrow','Ribeye' => 'Ribeye','Righteous' => 'Righteous','Rochester' => 'Rochester','Rock Salt' => 'Rock Salt','Rokkitt' => 'Rokkitt','Rosario' => 'Rosario','Ruslan Display' => 'Ruslan Display','Salsa' => 'Salsa','Sancreek' => 'Sancreek','Sansita One' => 'Sansita One','Satisfy' => 'Satisfy','Schoolbell' => 'Schoolbell','Shadows Into Light' => 'Shadows Into Light','Shanti' => 'Shanti','Short Stack' => 'Short Stack','Sigmar One' => 'Sigmar One','Signika Negative' => 'Signika Negative','Signika' => 'Signika','Six Caps' => 'Six Caps','Slackey' => 'Slackey','Smokum' => 'Smokum','Smythe' => 'Smythe','Sniglet' => 'Sniglet','Snippet' => 'Snippet','Sorts Mill Goudy' => 'Sorts Mill Goudy','Special Elite' => 'Special Elite','Spinnaker' => 'Spinnaker','Spirax' => 'Spirax','Stardos Stencil' => 'Stardos Stencil','Sue Ellen Francisco' => 'Sue Ellen Francisco','Sunshiney' => 'Sunshiney','Supermercado One' => 'Supermercado One','Swanky and Moo Moo' => 'Swanky and Moo Moo','Syncopate' => 'Syncopate','Tangerine' => 'Tangerine','Tenor Sans' => 'Tenor Sans','Terminal Dosis' => 'Terminal Dosis','The Girl Next Door' => 'The Girl Next Door','Tienne' => 'Tienne','Tinos' => 'Tinos','Tulpen One' => 'Tulpen One','Ubuntu Condensed' => 'Ubuntu Condensed','Ubuntu Mono' => 'Ubuntu Mono','Ubuntu' => 'Ubuntu','Ultra' => 'Ultra','UnifrakturCook' => 'UnifrakturCook','UnifrakturMaguntia' => 'UnifrakturMaguntia','Unkempt' => 'Unkempt','Unlock' => 'Unlock','Unna' => 'Unna','VT323' => 'VT323','Varela Round' => 'Varela Round','Varela' => 'Varela','Vast Shadow' => 'Vast Shadow','Vibur' => 'Vibur','Vidaloka' => 'Vidaloka','Volkhov' => 'Volkhov','Vollkorn' => 'Vollkorn','Voltaire' => 'Voltaire','Waiting for the Sunrise' => 'Waiting for the Sunrise','Wallpoet' => 'Wallpoet','Walter Turncoat' => 'Walter Turncoat','Wire One' => 'Wire One','Yanone Kaffeesatz' => 'Yanone Kaffeesatz','Yellowtail' => 'Yellowtail','Yeseva One' => 'Yeseva One','Zeyada' => 'Zeyada');
	return $google_faces;
}

/**
 * Checks font options to see if a Google font is selected.
 * If so, options_typography_enqueue_google_font is called to enqueue the font.
 * Ensures that each Google font is only enqueued once.
 * @since 1.0
 */
 
if ( !function_exists( 'wpex_of_typography_google_fonts' ) ) {
	
	add_action( 'wp_enqueue_scripts', 'wpex_of_typography_google_fonts' );
	function wpex_of_typography_google_fonts() {
		
		$all_google_fonts = array_keys( wpex_typography_get_google_fonts() );

		// Define all the options that possibly have a unique Google font
		$body_font = of_get_option('body_font', 'Helvetica Neue');
		$main_nav_font = of_get_option('main_nav_font');
		$main_nav_dropdown_font = of_get_option('main_nav_dropdown_font');
		$page_heading_fonts = of_get_option('page_heading_fonts');
		$heading_fonts = of_get_option('heading_fonts');
		$page_img_overlay_fonts = of_get_option('page_img_overlay_fonts');
		$symple_heading_fonts = of_get_option('symple_heading_fonts');
		
		// Get the font face for each option and put it in an array
		$selected_fonts = array(
			$body_font['face'],
			$main_nav_font['face'],
			$main_nav_dropdown_font['face'],
			$page_heading_fonts['face'],
			$heading_fonts['face'],
			$page_img_overlay_fonts['face'],
			$symple_heading_fonts['face']
		);
			
		// Remove any duplicates in the list
		$selected_fonts = array_unique($selected_fonts);
		
		// Check each of the unique fonts against the defined Google fonts
		// If it is a Google font, go ahead and call the function to enqueue it
		foreach ( $selected_fonts as $font ) {
			if ( in_array( $font, $all_google_fonts ) ) {
				wpex_of_typography_enqueue_google_font($font);
			}
		}
	}
}


/**
 * Enqueues the Google $font that is passed
 * @since 1.0
 */
 
if ( !function_exists( 'wpex_of_typography_enqueue_google_font' ) ) {
	 
	function wpex_of_typography_enqueue_google_font($font) {
		$font = explode(',', $font);
		$font = $font[0];
		$font = str_replace(" ", "+", $font);
		wp_enqueue_style( 'options_typography_'. $font, 'http://fonts.googleapis.com/css?family='. $font .':300,400,600,700&subset=latin,cyrillic-ext,cyrillic,greek-ext,vietnamese,latin-ext', false, null, 'all' );
	}
	
}


/**
 * Ouputs Custom CSS for typography options
 * @since 1.0
 */
 
if ( !function_exists( 'wpex_of_typography_css' ) ) {
	
	add_action('wp_head', 'wpex_of_typography_css'); // Hook wpex_of_typography_css() function to wp_head
	
	function wpex_of_typography_css() {
		
		// Define all the options that possibly have a unique Google font
		$body_font = of_get_option('body_font');
		$main_nav_font = of_get_option('main_nav_font');
		$main_nav_dropdown_font = of_get_option('main_nav_dropdown_font');
		$page_heading_fonts = of_get_option('page_heading_fonts');
		$heading_fonts = of_get_option('heading_fonts');
		$page_img_overlay_fonts = of_get_option('page_img_overlay_fonts');
		$symple_heading_fonts = of_get_option('symple_heading_fonts');
	
		// Setup output
		$custom_css ='';	


			// Body Font
			if ( $body_font ) {
				if ( $body_font['face'] !== '"Helvetica Neue", Helvetica, sans-serif' || $body_font['size'] !== '13px' ) {	
					$custom_css .= 'body{font: '. $body_font['size'] .'/1.6 '. $body_font['face'] .';}';
				}
			}
			
			
			// Nav Font
			if ( $main_nav_font ) {
				if ( $main_nav_font['face'] !== '"Helvetica Neue", Helvetica, sans-serif' || $main_nav_font['style'] !== 'normal' || $main_nav_font['size'] !== '13px' ) {	
					$custom_css .= '#navigation .sf-menu > li{font:'. $main_nav_font['style'] .' '. $main_nav_font['size'] .'/1.6 '. $main_nav_font['face'] .' !important;}';
				}
			}
			
			
			// Nav Font Drop-Down
			if ( $main_nav_dropdown_font ) {
				if ( $main_nav_dropdown_font['face'] !== '"Helvetica Neue", Helvetica, sans-serif' || $main_nav_dropdown_font['style'] !== 'normal' || $main_nav_dropdown_font['size'] !== '12px' ) {	
					$custom_css .= '#navigation .sf-menu ul li{font:'. $main_nav_dropdown_font['style'] .' '. $main_nav_dropdown_font['size'] .'/1.6 '. $main_nav_dropdown_font['face'] .';}';
				}
			}
			
			
			// Main Titles
			if ( $page_heading_fonts ) {
				if ( $page_heading_fonts['face'] !== '"Helvetica Neue", Helvetica, sans-serif' || $page_heading_fonts['style'] !== 'bold' || $page_heading_fonts['size'] !== '26px' ) {	
					$custom_css .= '#page-heading h1,#page-heading h2,#page-heading #main-title {font:'. $page_heading_fonts['style'] .' '. $page_heading_fonts['size'] .'/1.6 '. $page_heading_fonts['face'] .';}';
				}
			}
			
			
			// General Headings
			if ( $heading_fonts ) {
				if ( $heading_fonts['face'] !== '"Helvetica Neue", Helvetica, sans-serif' ) {	
					$custom_css .= 'h2,h3,h4,h5,h5, .footer-widget h6 { font-family: '. $heading_fonts['face'] .' }';
				}
				if ( $heading_fonts['style'] !== 'italic' && $heading_fonts['style'] !== 'bold' ) {	
					$custom_css .= 'h2,h3,h4,h5,h6, .footer-widget h6 { font-weight: '. $heading_fonts['style'] .' }';
				}
			}
			
			
			// Page Featured Image Overlay
			if ( $page_img_overlay_fonts ) {
				if ( $page_img_overlay_fonts['face'] !== '"Helvetica Neue", Helvetica, sans-serif' || $page_img_overlay_fonts['style'] !== 'normal' || $page_img_overlay_fonts['size'] !== '18px' ) {	
					$custom_css .= '#page-featured-img-overlay {font:'. $page_img_overlay_fonts['style'] .' '. $page_img_overlay_fonts['size'] .'/1.6 '. $page_img_overlay_fonts['face'] .';}';
				}
			}
			
			
			// Symple Heading
			if ( $symple_heading_fonts ) {
				if ( $symple_heading_fonts['face'] !== '"Helvetica Neue", Helvetica, sans-serif' || $symple_heading_fonts['style'] !== 'bold' || $symple_heading_fonts['size'] !== '14px' ) {	
					$custom_css .= '.symple-heading {font:'. $symple_heading_fonts['style'] .' '. $symple_heading_fonts['size'] .'/1.6 '. $symple_heading_fonts['face'] .';}';
				}
			}

	
		// trim white space for faster page loading
		$custom_css_trimmed =  preg_replace( '/\s+/', ' ', $custom_css );
		
		// output css on front end
		$css_output = "<!-- Custom CSS -->\n<style type=\"text/css\">\n" . $custom_css_trimmed . "\n</style>";
		if( !empty($custom_css) ) { echo $css_output;}
		
	} //end wpex_of_typography_css()

}