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

    $construction_engineer_fonts_json_data = file_get_contents( $construction_engineer_webfonts_json );
    if ( false === $construction_engineer_fonts_json_data ) {
        return array();
    }

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

    $construction_engineer_google_fonts = array();
    foreach ( $construction_engineer_all_fonts as $construction_engineer_font ) {
        $construction_engineer_google_fonts[ $construction_engineer_font['family'] ] = array(
            'family'   => $construction_engineer_font['family'],
            'variants' => $construction_engineer_font['variants'],
        );
    }
    return $construction_engineer_google_fonts;
}


function construction_engineer_get_all_google_font_families() {
    $construction_engineer_google_fonts  = construction_engineer_get_all_google_fonts();
    $construction_engineer_font_families = array();
    foreach ( $construction_engineer_google_fonts as $construction_engineer_font ) {
        $construction_engineer_font_families[ $construction_engineer_font['family'] ] = $construction_engineer_font['family'];
    }
    return $construction_engineer_font_families;
}

function construction_engineer_get_fonts_url() {
    $construction_engineer_fonts_url = '';
    $construction_engineer_fonts     = array();

    $construction_engineer_all_fonts = construction_engineer_get_all_google_fonts();

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

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

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

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

    $construction_engineer_fonts = array_unique( $construction_engineer_fonts );

    foreach ( $construction_engineer_fonts as $construction_engineer_font ) {
        $construction_engineer_variants      = $construction_engineer_all_fonts[ $construction_engineer_font ]['variants'];
        $construction_engineer_font_family[] = $construction_engineer_font . ':' . implode( ',', $construction_engineer_variants );
    }

    $construction_engineer_query_args = array(
        'family' => urlencode( implode( '|', $construction_engineer_font_family ) ),
    );

    if ( ! empty( $construction_engineer_font_family ) ) {
        $construction_engineer_fonts_url = add_query_arg( $construction_engineer_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $construction_engineer_fonts_url;
}