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

    $roof_water_proofing_fonts_json_data = file_get_contents( $roof_water_proofing_webfonts_json );
    if ( false === $roof_water_proofing_fonts_json_data ) {
        return array();
    }

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

    $roof_water_proofing_google_fonts = array();
    foreach ( $roof_water_proofing_all_fonts as $roof_water_proofing_font ) {
        $roof_water_proofing_google_fonts[ $roof_water_proofing_font['family'] ] = array(
            'family'   => $roof_water_proofing_font['family'],
            'variants' => $roof_water_proofing_font['variants'],
        );
    }
    return $roof_water_proofing_google_fonts;
}


function roof_water_proofing_get_all_google_font_families() {
    $roof_water_proofing_google_fonts  = roof_water_proofing_get_all_google_fonts();
    $roof_water_proofing_font_families = array();
    foreach ( $roof_water_proofing_google_fonts as $roof_water_proofing_font ) {
        $roof_water_proofing_font_families[ $roof_water_proofing_font['family'] ] = $roof_water_proofing_font['family'];
    }
    return $roof_water_proofing_font_families;
}

function roof_water_proofing_get_fonts_url() {
    $roof_water_proofing_fonts_url = '';
    $roof_water_proofing_fonts     = array();

    $roof_water_proofing_all_fonts = roof_water_proofing_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'roof_water_proofing_site_title_font', 'Work Sans' ) ) ) {
        $roof_water_proofing_fonts[] = get_theme_mod( 'roof_water_proofing_site_title_font', 'Work Sans' );
    }

    if ( ! empty( get_theme_mod( 'roof_water_proofing_site_description_font', 'Work Sans' ) ) ) {
        $roof_water_proofing_fonts[] = get_theme_mod( 'roof_water_proofing_site_description_font', 'Work Sans' );
    }

    if ( ! empty( get_theme_mod( 'roof_water_proofing_header_font', 'Bebas Neue' ) ) ) {
        $roof_water_proofing_fonts[] = get_theme_mod( 'roof_water_proofing_header_font', 'Bebas Neue' );
    }

    if ( ! empty( get_theme_mod( 'roof_water_proofing_content_font', 'Work Sans' ) ) ) {
        $roof_water_proofing_fonts[] = get_theme_mod( 'roof_water_proofing_content_font', 'Work Sans' );
    }

    $roof_water_proofing_fonts = array_unique( $roof_water_proofing_fonts );

    foreach ( $roof_water_proofing_fonts as $roof_water_proofing_font ) {
        $roof_water_proofing_variants      = $roof_water_proofing_all_fonts[ $roof_water_proofing_font ]['variants'];
        $roof_water_proofing_font_family[] = $roof_water_proofing_font . ':' . implode( ',', $roof_water_proofing_variants );
    }

    $roof_water_proofing_query_args = array(
        'family' => urlencode( implode( '|', $roof_water_proofing_font_family ) ),
    );

    if ( ! empty( $roof_water_proofing_font_family ) ) {
        $roof_water_proofing_fonts_url = add_query_arg( $roof_water_proofing_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $roof_water_proofing_fonts_url;
}