<?php
function bridal_jewelry_store_get_all_google_fonts() {
    $bridal_jewelry_store_webfonts_json = get_template_directory() . '/theme-library/google-webfonts.json';
    if ( ! file_exists( $bridal_jewelry_store_webfonts_json ) ) {
        return array();
    }

    $bridal_jewelry_store_fonts_json_data = file_get_contents( $bridal_jewelry_store_webfonts_json );
    if ( false === $bridal_jewelry_store_fonts_json_data ) {
        return array();
    }

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

    $bridal_jewelry_store_google_fonts = array();
    foreach ( $bridal_jewelry_store_all_fonts as $bridal_jewelry_store_font ) {
        $bridal_jewelry_store_google_fonts[ $bridal_jewelry_store_font['family'] ] = array(
            'family'   => $bridal_jewelry_store_font['family'],
            'variants' => $bridal_jewelry_store_font['variants'],
        );
    }
    return $bridal_jewelry_store_google_fonts;
}


function bridal_jewelry_store_get_all_google_font_families() {
    $bridal_jewelry_store_google_fonts  = bridal_jewelry_store_get_all_google_fonts();
    $bridal_jewelry_store_font_families = array();
    foreach ( $bridal_jewelry_store_google_fonts as $bridal_jewelry_store_font ) {
        $bridal_jewelry_store_font_families[ $bridal_jewelry_store_font['family'] ] = $bridal_jewelry_store_font['family'];
    }
    return $bridal_jewelry_store_font_families;
}

function bridal_jewelry_store_get_fonts_url() {
    $bridal_jewelry_store_fonts_url = '';
    $bridal_jewelry_store_fonts     = array();

    $bridal_jewelry_store_all_fonts = bridal_jewelry_store_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'bridal_jewelry_store_site_title_font', 'Raleway' ) ) ) {
        $bridal_jewelry_store_fonts[] = get_theme_mod( 'bridal_jewelry_store_site_title_font', 'Raleway' );
    }

    if ( ! empty( get_theme_mod( 'bridal_jewelry_store_site_description_font', 'Raleway' ) ) ) {
        $bridal_jewelry_store_fonts[] = get_theme_mod( 'bridal_jewelry_store_site_description_font', 'Raleway' );
    }

    if ( ! empty( get_theme_mod( 'bridal_jewelry_store_header_font', 'Cinzel' ) ) ) {
        $bridal_jewelry_store_fonts[] = get_theme_mod( 'bridal_jewelry_store_header_font', 'Cinzel' );
    }

    if ( ! empty( get_theme_mod( 'bridal_jewelry_store_content_font', 'Tenor Sans' ) ) ) {
        $bridal_jewelry_store_fonts[] = get_theme_mod( 'bridal_jewelry_store_content_font', 'Tenor Sans' );
    }

    $bridal_jewelry_store_fonts = array_unique( $bridal_jewelry_store_fonts );

    foreach ( $bridal_jewelry_store_fonts as $bridal_jewelry_store_font ) {
        $bridal_jewelry_store_variants      = $bridal_jewelry_store_all_fonts[ $bridal_jewelry_store_font ]['variants'];
        $bridal_jewelry_store_font_family[] = $bridal_jewelry_store_font . ':' . implode( ',', $bridal_jewelry_store_variants );
    }

    $bridal_jewelry_store_query_args = array(
        'family' => urlencode( implode( '|', $bridal_jewelry_store_font_family ) ),
    );

    if ( ! empty( $bridal_jewelry_store_font_family ) ) {
        $bridal_jewelry_store_fonts_url = add_query_arg( $bridal_jewelry_store_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $bridal_jewelry_store_fonts_url;
}