<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
// Replace default Google Fonts URL with custom Fonts
add_filter('pmthemes_google_fonts_url', 'pmthemes_google_fonts_url');
function pmthemes_google_fonts_url() { 
	// Get Theme Options from Database
	$theme_options = pmthemes_theme_options();
	// Default Fonts which haven't to be load from Google
	$default_fonts = Pmthemes_Custom_Font_Lists::default_browser_fonts();
	// Set Google Font Array
	$google_font_families = array();
	// Add Text Font
	if( isset($theme_options['text_font']) and !in_array($theme_options['text_font'], $default_fonts)) :
		$google_font_families[] = $theme_options['text_font'];
		$default_fonts[] = $theme_options['text_font']; 
	endif;
	// Add Site Title Font
	if( isset($theme_options['site_title_font']) and !in_array($theme_options['site_title_font'], $default_fonts)) :
		$google_font_families[] = $theme_options['site_title_font'];
		$default_fonts[] = $theme_options['site_title_font']; 
		
	endif;
	// Add Title Font
	if( isset($theme_options['title_font']) and !in_array($theme_options['title_font'], $default_fonts)) :
		$google_font_families[] = $theme_options['title_font'];
		$default_fonts[] = $theme_options['title_font']; 
	endif;
	// Add Navigation Font
	if( isset($theme_options['navi_font']) and !in_array($theme_options['navi_font'], $default_fonts)) :
		$google_font_families[] = $theme_options['navi_font'];
		$default_fonts[] = $theme_options['navi_font'];
	endif;
	// Add Widget Title Font
	if( isset($theme_options['widget_title_font']) and !in_array($theme_options['widget_title_font'], $default_fonts)) :
		$google_font_families[] = $theme_options['widget_title_font'];
		$default_fonts[] = $theme_options['widget_title_font'];
	endif;
	// Setup Google Font URLs
	$query_args = array(
		'family' => urlencode( implode( '|', $google_font_families ) ),
		'subset' => urlencode( 'latin,latin-ext' ),
    );
	$google_fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
	return $google_fonts_url;
}
// Add new Custom CSS styles for custom fonts
add_action( 'wp_enqueue_scripts', 'pmthemes_css_fonts');
function pmthemes_css_fonts() {
	// Get Theme Options from Database
	$theme_options = pmthemes_theme_options();
	// Get Default Fonts from settings
    $theme_def = pmthemes_default_options();
	// Set Font CSS Variable
	$font_css = '';
	
	// Set Default Text Font
	if ( isset($theme_options['text_font']) and $theme_options['text_font'] <> $theme_def['text_font'] ) :
		$font_css .= 'body, input, textarea {font-family: "'.esc_attr($theme_options['text_font']).'";}';
	endif;
	
	// Site Title Font
	if ( isset($theme_options['site_title_font']) and $theme_options['site_title_font'] <> $theme_def['site_title_font'] ) :
		$font_css .= '#logo .site-title {font-family: "'.esc_attr($theme_options['site_title_font']).'";}';
	endif;
	
	// Set Title Font
	if ( isset($theme_options['title_font']) and $theme_options['title_font'] <> $theme_def['title_font'] ) :
		$font_css .= '.page-title, .post-title, #frontpage-slider .pmsslide .slide-entry .slide-title,#comments .comments-title, #respond #reply-title {font-family: "'.esc_attr($theme_options['title_font']).'";}';
	endif;
	
	// Set Navigation Font
	if ( isset($theme_options['navi_font']) and $theme_options['navi_font'] <> $theme_def['navi_font'] ) :
		$font_css .= '.menu-toggle, .nav-menu li a, #topnav-menu a {font-family: "'.esc_attr($theme_options['navi_font']).'";}';
	endif;
	
	// Set Widget Title Font
	if ( isset($theme_options['widget_title_font']) and $theme_options['widget_title_font'] <> $theme_def['widget_title_font'] ) :
		$font_css .= '.widgettitle, #frontpage-magazine-widgets .widget .widgettitle {font-family: "'.esc_attr($theme_options['widget_title_font']).'";}';
	endif;
	
	// Print Font CSS
    if ( $font_css ) {
        wp_add_inline_style( 'pmthemes-custom', $font_css );
    }
}