<?php
/**
 * Typography Related Functions
 *
 * @package
*/
/**
 * Google Fonts
 */
require get_template_directory() . '/inc/lib/typography/google-fonts.php';

if ( ! function_exists( 'feminine_lifestyle_blog_typography_default_fonts' ) ) :
/**
 * Set the default system fonts.
 */
function feminine_lifestyle_blog_typography_default_fonts() {
	$fonts = array(
		'System Stack',
		'Arial, Helvetica, sans-serif',
		'Century Gothic',
		'Comic Sans MS',
		'Courier New',
		'Georgia, Times New Roman, Times, serif',
		'Helvetica',
		'Impact',
		'Lucida Console',
		'Lucida Sans Unicode',
		'Palatino Linotype',
		'Segoe UI, Helvetica Neue, Helvetica, sans-serif',
		'Tahoma, Geneva, sans-serif',
		'Trebuchet MS, Helvetica, sans-serif',
		'Verdana, Geneva, sans-serif',
	);

	return apply_filters( 'feminine_lifestyle_blog_typography_default_fonts', $fonts );
}
endif;

if( ! function_exists( 'feminine_lifestyle_blog_get_font_family' ) ) :
/**
 * Get font family 
 */
function feminine_lifestyle_blog_get_font_family( $font ){
	$output = '';
	$no_quotes = array(
		'inherit',
		'Arial, Helvetica, sans-serif',
		'Georgia, Times New Roman, Times, serif',
		'Helvetica',
		'Impact',
		'Segoe UI, Helvetica Neue, Helvetica, sans-serif',
		'Tahoma, Geneva, sans-serif',
		'Trebuchet MS, Helvetica, sans-serif',
		'Verdana, Geneva, sans-serif',		
	);

	if( $font['family'] === 'System Stack' ){
		$output = apply_filters( 'feminine_lifestyle_blog_typography_system_stack', '-apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"' );
	}else{
		if( in_array( $font['family'], $no_quotes ) || ( $font['family'] ==='System Stack' ) ){
			$wrapper_start = null;
			$wrapper_end = null;
		}else{
			$wrapper_start = '"';
			$wrapper_end = ( $font['category'] ) ? '", ' . $font['category'] : '"';
		}
		$output = $wrapper_start . $font['family'] . $wrapper_end;
	}

	return $output;
}
endif;

if ( ! function_exists( 'feminine_lifestyle_blog_google_fonts_url' ) ) :	
/**
 * Google Fonts url
 */
function feminine_lifestyle_blog_google_fonts_url() {
	$defaults  = feminine_lifestyle_blog_get_typography_defaults();
	$fonts_url = '';
	$settings  = array();

	$settings['primary_font']    = wp_parse_args( $defaults['primary_font'] );
	$settings['secondary_font']  = wp_parse_args( $defaults['secondary_font'] );

	$not_google = str_replace( ' ', '+', feminine_lifestyle_blog_typography_default_fonts() );

	$font_settings = array(
		'primary_font',
		'secondary_font'
	);

	$google_fonts = array();		

	foreach( $font_settings as $key ){

		$value = str_replace( ' ', '+', $settings[ $key ]['family'] );

		$variants = $settings[ $key ]['variants'];

		$value = ! empty( $variants ) ? $value . ':' . $variants : $value;
		
		// Make sure we don't add the same font twice.
		if ( ! in_array( $value, $google_fonts ) ) {
			$google_fonts[] = $value;
		}
	}
	
	// Ignore any non-Google fonts.
	$google_fonts = array_diff( $google_fonts, $not_google );
	
	$google_fonts = implode( '|', $google_fonts );
	$google_fonts = apply_filters( 'feminine_lifestyle_blog_typography_google_fonts', $google_fonts );

	$subset = apply_filters( 'feminine_lifestyle_blog_fonts_subset', '' );

	$font_args = array();
	$font_args['family'] = $google_fonts;
	
	if ( '' !== $subset ) {
		$font_args['subset'] = rawurlencode( $subset );
	}
	
	$display = apply_filters( 'feminine_lifestyle_blog_google_font_display', 'swap' );

	if ( $display ) {
		$font_args['display'] = $display;
	}
			
	if ( $google_fonts ) {
		$fonts_url = add_query_arg( $font_args, '//fonts.googleapis.com/css' );			
	}

	if( get_theme_mod( 'ed_localgoogle_fonts', false ) ) {
        $fonts_url = feminine_lifestyle_blog_get_webfont_url( add_query_arg( $font_args, 'https://fonts.googleapis.com/css' ) );
    }

	return esc_url( $fonts_url );
}
endif;

if ( ! function_exists( 'feminine_lifestyle_blog_get_all_google_fonts_ajax' ) ) :
/**
 * Return an array of all of our Google Fonts.
 */
function feminine_lifestyle_blog_get_all_google_fonts_ajax() {
	// Bail if the nonce doesn't check out
	if ( ! isset( $_POST['feminine_lifestyle_blog_customize_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['feminine_lifestyle_blog_customize_nonce'] ), 'feminine_lifestyle_blog_customize_nonce' ) ) {
		wp_die();
	}

	// Do another nonce check
	check_ajax_referer( 'feminine_lifestyle_blog_customize_nonce', 'feminine_lifestyle_blog_customize_nonce' );

	// Bail if user can't edit theme options
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_die();
	}

	// Get all of our fonts
	$fonts = feminine_lifestyle_blog_get_all_google_fonts();

	// Send all of our fonts in JSON format
	echo wp_json_encode( $fonts );

	// Exit
	die();
}
endif;
add_action( 'wp_ajax_feminine_lifestyle_blog_get_all_google_fonts_ajax', 'feminine_lifestyle_blog_get_all_google_fonts_ajax' );

if( ! function_exists( 'feminine_lifestyle_blog_get_typography_defaults' ) ) :
/**
 * Typography Defaults
 */
function feminine_lifestyle_blog_get_typography_defaults(){
    $defaults = array(
        'primary_font' => array(
            'family'           => 'Questrial',
            'variants'         => '400,600,500,700,300',
            'category'         => '',
            'weight'           => '400',
            'transform'        => 'none',
            'font_size'        => 18,
        ),
		'secondary_font' => array(
            'family'           => 'Source Serif Pro',
            'variants'         => '400,600,500,700,300',
            'category'         => '',
            'weight'           => '400',
            'transform'        => 'none',
            'font_size'        => 18,
        )
    );

    return apply_filters( 'feminine_lifestyle_blog_typography_options_defaults', $defaults ); 
}
endif;