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

    $technology_software_firm_fonts_json_data = file_get_contents( $technology_software_firm_webfonts_json );
    if ( false === $technology_software_firm_fonts_json_data ) {
        return array();
    }

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

    $technology_software_firm_google_fonts = array();
    foreach ( $technology_software_firm_all_fonts as $technology_software_firm_font ) {
        $technology_software_firm_google_fonts[ $technology_software_firm_font['family'] ] = array(
            'family'   => $technology_software_firm_font['family'],
            'variants' => $technology_software_firm_font['variants'],
        );
    }
    return $technology_software_firm_google_fonts;
}

function technology_software_firm_get_all_google_font_families() {
    $technology_software_firm_google_fonts  = technology_software_firm_get_all_google_fonts();
    $technology_software_firm_font_families = array();
    foreach ( $technology_software_firm_google_fonts as $technology_software_firm_font ) {
        $technology_software_firm_font_families[ $technology_software_firm_font['family'] ] = $technology_software_firm_font['family'];
    }
    return $technology_software_firm_font_families;
}

function technology_software_firm_get_fonts_url() {
    $technology_software_firm_fonts_url = '';
    $technology_software_firm_fonts     = array();

    $technology_software_firm_all_fonts = technology_software_firm_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'technology_software_firm_site_title_font', 'Mulish' ) ) ) {
        $technology_software_firm_fonts[] = get_theme_mod( 'technology_software_firm_site_title_font', 'Mulish' );
    }

    if ( ! empty( get_theme_mod( 'technology_software_firm_site_description_font', 'Mulish' ) ) ) {
        $technology_software_firm_fonts[] = get_theme_mod( 'technology_software_firm_site_description_font', 'Mulish' );
    }

    if ( ! empty( get_theme_mod( 'technology_software_firm_header_font', 'Mulish' ) ) ) {
        $technology_software_firm_fonts[] = get_theme_mod( 'technology_software_firm_header_font', 'Mulish' );
    }

    if ( ! empty( get_theme_mod( 'technology_software_firm_content_font', 'Mulish' ) ) ) {
        $technology_software_firm_fonts[] = get_theme_mod( 'technology_software_firm_content_font', 'Mulish' );
    }

    $technology_software_firm_fonts = array_unique( $technology_software_firm_fonts );

    foreach ( $technology_software_firm_fonts as $technology_software_firm_font ) {
        $technology_software_firm_variants      = $technology_software_firm_all_fonts[ $technology_software_firm_font ]['variants'];
        $technology_software_firm_font_family[] = $technology_software_firm_font . ':' . implode( ',', $technology_software_firm_variants );
    }

    $technology_software_firm_query_args = array(
        'family' => urlencode( implode( '|', $technology_software_firm_font_family ) ),
    );

    if ( ! empty( $technology_software_firm_font_family ) ) {
        $technology_software_firm_fonts_url = add_query_arg( $technology_software_firm_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $technology_software_firm_fonts_url;
}