<?php
/**
 * Particular Typography function for this theme
 *
 * Some of this functionality may eventually be superseded by essential features.
 *
 * @package Fashion Outfits Trend
 */

function fashion_outfits_trend_get_all_google_fonts() {
    $fashion_outfits_trend_webfonts_json = get_template_directory() . '/inc/typography-settings/google-webfonts.json';
    if ( ! file_exists( $fashion_outfits_trend_webfonts_json ) ) {
        return array();
    }

    $fashion_outfits_trend_fonts_json_data = file_get_contents( $fashion_outfits_trend_webfonts_json );
    if ( false === $fashion_outfits_trend_fonts_json_data ) {
        return array();
    }

    $fashion_outfits_trend_all_fonts = json_decode( $fashion_outfits_trend_fonts_json_data, true );
    if ( json_last_error() !== JSON_ERROR_NONE ) {
        return array();
    }

    $fashion_outfits_trend_google_fonts = array();
    foreach ( $fashion_outfits_trend_all_fonts as $fashion_outfits_trend_font ) {
        $fashion_outfits_trend_google_fonts[ $fashion_outfits_trend_font['family'] ] = array(
            'family'   => $fashion_outfits_trend_font['family'],
            'variants' => $fashion_outfits_trend_font['variants'],
        );
    }
    return $fashion_outfits_trend_google_fonts;
}


function fashion_outfits_trend_get_all_google_font_families() {
    $fashion_outfits_trend_google_fonts  = fashion_outfits_trend_get_all_google_fonts();
    $fashion_outfits_trend_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'fashion-outfits-trend' ),
    );

    foreach ( $fashion_outfits_trend_google_fonts as $fashion_outfits_trend_font ) {
        $fashion_outfits_trend_font_families[ $fashion_outfits_trend_font['family'] ] = $fashion_outfits_trend_font['family'];
    }

    return $fashion_outfits_trend_font_families;
}


function fashion_outfits_trend_get_fonts_url() {
    $fashion_outfits_trend_fonts_url = '';
    $fashion_outfits_trend_fonts     = array();

    $fashion_outfits_trend_all_fonts = fashion_outfits_trend_get_all_google_fonts();
    $fashion_outfits_trend_selected_font = get_theme_mod( 'fashion_outfits_trend_global_font_setting', '' );

    if ( ! empty( $fashion_outfits_trend_selected_font ) && isset( $fashion_outfits_trend_all_fonts[ $fashion_outfits_trend_selected_font ] ) ) {
        $fashion_outfits_trend_variants = $fashion_outfits_trend_all_fonts[ $fashion_outfits_trend_selected_font ]['variants'];
        $fashion_outfits_trend_font_family[] = $fashion_outfits_trend_selected_font . ':' . implode( ',', $fashion_outfits_trend_variants );

        $fashion_outfits_trend_query_args = array(
            'family' => urlencode( implode( '|', $fashion_outfits_trend_font_family ) ),
        );

        $fashion_outfits_trend_fonts_url = add_query_arg( $fashion_outfits_trend_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $fashion_outfits_trend_fonts_url;
}

function fashion_outfits_trend_sanitize_google_fonts( $fashion_outfits_trend_input, $fashion_outfits_trend_setting ) {
    $fashion_outfits_trend_choices = $fashion_outfits_trend_setting->manager->get_control( $fashion_outfits_trend_setting->id )->choices;
    return ( array_key_exists( $fashion_outfits_trend_input, $fashion_outfits_trend_choices ) ? $fashion_outfits_trend_input : $fashion_outfits_trend_setting->default );
}

function fashion_outfits_trend_dynamic_css() {
    $fashion_outfits_trend_color       = get_theme_mod( 'fashion_outfits_trend_global_color', '#F47771' );
    $fashion_outfits_trend_font_family = get_theme_mod( 'fashion_outfits_trend_global_font_setting', '' );

    // Enqueue Google Fonts if font is selected
    $fashion_outfits_trend_fonts_url = fashion_outfits_trend_get_fonts_url();
    if ( ! empty( $fashion_outfits_trend_fonts_url ) ) {
        wp_enqueue_style( 'fashion-outfits-trend-google-fonts', esc_url( $fashion_outfits_trend_fonts_url ), array(), null );
    }

    $fashion_outfits_trend_custom_css = ':root { --global-color: ' . esc_attr( $fashion_outfits_trend_color ) . '; }';

    if ( ! empty( $fashion_outfits_trend_font_family ) ) {
        $fashion_outfits_trend_custom_css .= ':root { --global-font-family: "' . esc_attr( $fashion_outfits_trend_font_family ) . '", sans-serif !important; }';
    }

    wp_add_inline_style( 'fashion-outfits-trend-style', $fashion_outfits_trend_custom_css );
}
add_action( 'wp_enqueue_scripts', 'fashion_outfits_trend_dynamic_css', 99 );