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

    $handmade_craft_fonts_json_data = file_get_contents( $handmade_craft_webfonts_json );
    if ( false === $handmade_craft_fonts_json_data ) {
        return array();
    }

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

    $handmade_craft_google_fonts = array();
    foreach ( $handmade_craft_all_fonts as $handmade_craft_font ) {
        $handmade_craft_google_fonts[ $handmade_craft_font['family'] ] = array(
            'family'   => $handmade_craft_font['family'],
            'variants' => $handmade_craft_font['variants'],
        );
    }
    return $handmade_craft_google_fonts;
}

function handmade_craft_get_all_google_font_families() {
    $handmade_craft_google_fonts  = handmade_craft_get_all_google_fonts();
    $handmade_craft_font_families = array();
    foreach ( $handmade_craft_google_fonts as $handmade_craft_font ) {
        $handmade_craft_font_families[ $handmade_craft_font['family'] ] = $handmade_craft_font['family'];
    }
    return $handmade_craft_font_families;
}

function handmade_craft_get_fonts_url() {
    $handmade_craft_fonts_url = '';
    $handmade_craft_fonts     = array();

    $handmade_craft_all_fonts = handmade_craft_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'handmade_craft_site_title_font', 'Marcellus' ) ) ) {
        $handmade_craft_fonts[] = get_theme_mod( 'handmade_craft_site_title_font', 'Marcellus' );
    }

    if ( ! empty( get_theme_mod( 'handmade_craft_site_description_font', 'Jost' ) ) ) {
        $handmade_craft_fonts[] = get_theme_mod( 'handmade_craft_site_description_font', 'Jost' );
    }

    if ( ! empty( get_theme_mod( 'handmade_craft_header_font', 'Marcellus' ) ) ) {
        $handmade_craft_fonts[] = get_theme_mod( 'handmade_craft_header_font', 'Marcellus' );
    }

    if ( ! empty( get_theme_mod( 'handmade_craft_content_font', 'Jost' ) ) ) {
        $handmade_craft_fonts[] = get_theme_mod( 'handmade_craft_content_font', 'Jost' );
    }

    $handmade_craft_fonts = array_unique( $handmade_craft_fonts );

    foreach ( $handmade_craft_fonts as $handmade_craft_font ) {
        $handmade_craft_variants      = $handmade_craft_all_fonts[ $handmade_craft_font ]['variants'];
        $handmade_craft_font_family[] = $handmade_craft_font . ':' . implode( ',', $handmade_craft_variants );
    }

    $handmade_craft_query_args = array(
        'family' => urlencode( implode( '|', $handmade_craft_font_family ) ),
    );

    if ( ! empty( $handmade_craft_font_family ) ) {
        $handmade_craft_fonts_url = add_query_arg( $handmade_craft_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $handmade_craft_fonts_url;
}