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

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

    $grocery_supermarket_chain_fonts_json_data = file_get_contents( $grocery_supermarket_chain_webfonts_json );
    if ( false === $grocery_supermarket_chain_fonts_json_data ) {
        return array();
    }

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

    $grocery_supermarket_chain_google_fonts = array();
    foreach ( $grocery_supermarket_chain_all_fonts as $grocery_supermarket_chain_font ) {
        $grocery_supermarket_chain_google_fonts[ $grocery_supermarket_chain_font['family'] ] = array(
            'family'   => $grocery_supermarket_chain_font['family'],
            'variants' => $grocery_supermarket_chain_font['variants'],
        );
    }
    return $grocery_supermarket_chain_google_fonts;
}


function grocery_supermarket_chain_get_all_google_font_families() {
    $grocery_supermarket_chain_google_fonts  = grocery_supermarket_chain_get_all_google_fonts();
    $grocery_supermarket_chain_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'grocery-supermarket-chain' ),
    );

    foreach ( $grocery_supermarket_chain_google_fonts as $grocery_supermarket_chain_font ) {
        $grocery_supermarket_chain_font_families[ $grocery_supermarket_chain_font['family'] ] = $grocery_supermarket_chain_font['family'];
    }

    return $grocery_supermarket_chain_font_families;
}


function grocery_supermarket_chain_get_fonts_url() {
    $grocery_supermarket_chain_fonts_url = '';
    $grocery_supermarket_chain_fonts     = array();

    $grocery_supermarket_chain_all_fonts = grocery_supermarket_chain_get_all_google_fonts();
    $grocery_supermarket_chain_selected_font = get_theme_mod( 'grocery_supermarket_chain_global_font_setting', '' );

    if ( ! empty( $grocery_supermarket_chain_selected_font ) && isset( $grocery_supermarket_chain_all_fonts[ $grocery_supermarket_chain_selected_font ] ) ) {
        $grocery_supermarket_chain_variants = $grocery_supermarket_chain_all_fonts[ $grocery_supermarket_chain_selected_font ]['variants'];
        $grocery_supermarket_chain_font_family[] = $grocery_supermarket_chain_selected_font . ':' . implode( ',', $grocery_supermarket_chain_variants );

        $grocery_supermarket_chain_query_args = array(
            'family' => urlencode( implode( '|', $grocery_supermarket_chain_font_family ) ),
        );

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

    return $grocery_supermarket_chain_fonts_url;
}

function grocery_supermarket_chain_sanitize_google_fonts( $grocery_supermarket_chain_input, $grocery_supermarket_chain_setting ) {
    $grocery_supermarket_chain_choices = $grocery_supermarket_chain_setting->manager->get_control( $grocery_supermarket_chain_setting->id )->choices;
    return ( array_key_exists( $grocery_supermarket_chain_input, $grocery_supermarket_chain_choices ) ? $grocery_supermarket_chain_input : $grocery_supermarket_chain_setting->default );
}

function grocery_supermarket_chain_dynamic_css() {
    $grocery_supermarket_chain_color       = get_theme_mod( 'grocery_supermarket_chain_global_color', '#31b67e' );
    $grocery_supermarket_chain_font_family = get_theme_mod( 'grocery_supermarket_chain_global_font_setting', '' );

    // Enqueue Google Fonts if font is selected
    $grocery_supermarket_chain_fonts_url = grocery_supermarket_chain_get_fonts_url();
    if ( ! empty( $grocery_supermarket_chain_fonts_url ) ) {
        wp_enqueue_style( 'grocery-supermarket-chain-google-fonts', esc_url( $grocery_supermarket_chain_fonts_url ), array(), null );
    }

    $grocery_supermarket_chain_custom_css = ':root { --global-color: ' . esc_attr( $grocery_supermarket_chain_color ) . '; }';

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

    wp_add_inline_style( 'grocery-supermarket-chain-style', $grocery_supermarket_chain_custom_css );
}
add_action( 'wp_enqueue_scripts', 'grocery_supermarket_chain_dynamic_css', 99 );