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

    $aster_travel_blog_fonts_json_data = file_get_contents( $aster_travel_blog_webfonts_json );
    if ( false === $aster_travel_blog_fonts_json_data ) {
        return array();
    }

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

    $aster_travel_blog_google_fonts = array();
    foreach ( $aster_travel_blog_all_fonts as $aster_travel_blog_font ) {
        $aster_travel_blog_google_fonts[ $aster_travel_blog_font['family'] ] = array(
            'family'   => $aster_travel_blog_font['family'],
            'variants' => $aster_travel_blog_font['variants'],
        );
    }
    return $aster_travel_blog_google_fonts;
}


function aster_travel_blog_get_all_google_font_families() {
    $aster_travel_blog_google_fonts  = aster_travel_blog_get_all_google_fonts();
    $aster_travel_blog_font_families = array();
    foreach ( $aster_travel_blog_google_fonts as $aster_travel_blog_font ) {
        $aster_travel_blog_font_families[ $aster_travel_blog_font['family'] ] = $aster_travel_blog_font['family'];
    }
    return $aster_travel_blog_font_families;
}

function aster_travel_blog_get_fonts_url() {
    $aster_travel_blog_fonts_url = '';
    $aster_travel_blog_fonts     = array();

    $aster_travel_blog_all_fonts = aster_travel_blog_get_all_google_fonts();

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

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

    if ( ! empty( get_theme_mod( 'aster_travel_blog_header_font', 'Epilogue' ) ) ) {
        $aster_travel_blog_fonts[] = get_theme_mod( 'aster_travel_blog_header_font', 'Epilogue' );
    }

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

    $aster_travel_blog_fonts = array_unique( $aster_travel_blog_fonts );

    foreach ( $aster_travel_blog_fonts as $aster_travel_blog_font ) {
        $aster_travel_blog_variants      = $aster_travel_blog_all_fonts[ $aster_travel_blog_font ]['variants'];
        $aster_travel_blog_font_family[] = $aster_travel_blog_font . ':' . implode( ',', $aster_travel_blog_variants );
    }

    $aster_travel_blog_query_args = array(
        'family' => urlencode( implode( '|', $aster_travel_blog_font_family ) ),
    );

    if ( ! empty( $aster_travel_blog_font_family ) ) {
        $aster_travel_blog_fonts_url = add_query_arg( $aster_travel_blog_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $aster_travel_blog_fonts_url;
}