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

    $jewelry_accessories_fonts_json_data = file_get_contents( $jewelry_accessories_webfonts_json );
    if ( false === $jewelry_accessories_fonts_json_data ) {
        return array();
    }

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

    $jewelry_accessories_google_fonts = array();
    foreach ( $jewelry_accessories_all_fonts as $jewelry_accessories_font ) {
        $jewelry_accessories_google_fonts[ $jewelry_accessories_font['family'] ] = array(
            'family'   => $jewelry_accessories_font['family'],
            'variants' => $jewelry_accessories_font['variants'],
        );
    }
    return $jewelry_accessories_google_fonts;
}

function jewelry_accessories_get_all_google_font_families() {
    $jewelry_accessories_google_fonts  = jewelry_accessories_get_all_google_fonts();
    $jewelry_accessories_font_families = array();
    foreach ( $jewelry_accessories_google_fonts as $jewelry_accessories_font ) {
        $jewelry_accessories_font_families[ $jewelry_accessories_font['family'] ] = $jewelry_accessories_font['family'];
    }
    return $jewelry_accessories_font_families;
}

function jewelry_accessories_get_fonts_url() {
    $jewelry_accessories_fonts_url = '';
    $jewelry_accessories_fonts     = array();

    $jewelry_accessories_all_fonts = jewelry_accessories_get_all_google_fonts();

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

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

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

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

    $jewelry_accessories_fonts = array_unique( $jewelry_accessories_fonts );

    foreach ( $jewelry_accessories_fonts as $jewelry_accessories_font ) {
        $jewelry_accessories_variants      = $jewelry_accessories_all_fonts[ $jewelry_accessories_font ]['variants'];
        $jewelry_accessories_font_family[] = $jewelry_accessories_font . ':' . implode( ',', $jewelry_accessories_variants );
    }

    $jewelry_accessories_query_args = array(
        'family' => urlencode( implode( '|', $jewelry_accessories_font_family ) ),
    );

    if ( ! empty( $jewelry_accessories_font_family ) ) {
        $jewelry_accessories_fonts_url = add_query_arg( $jewelry_accessories_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $jewelry_accessories_fonts_url;
}