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

    $cleaning_services_agent_fonts_json_data = file_get_contents( $cleaning_services_agent_webfonts_json );
    if ( false === $cleaning_services_agent_fonts_json_data ) {
        return array();
    }

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

    $cleaning_services_agent_google_fonts = array();
    foreach ( $cleaning_services_agent_all_fonts as $cleaning_services_agent_font ) {
        $cleaning_services_agent_google_fonts[ $cleaning_services_agent_font['family'] ] = array(
            'family'   => $cleaning_services_agent_font['family'],
            'variants' => $cleaning_services_agent_font['variants'],
        );
    }
    return $cleaning_services_agent_google_fonts;
}


function cleaning_services_agent_get_all_google_font_families() {
    $cleaning_services_agent_google_fonts  = cleaning_services_agent_get_all_google_fonts();
    $cleaning_services_agent_font_families = array();
    foreach ( $cleaning_services_agent_google_fonts as $cleaning_services_agent_font ) {
        $cleaning_services_agent_font_families[ $cleaning_services_agent_font['family'] ] = $cleaning_services_agent_font['family'];
    }
    return $cleaning_services_agent_font_families;
}

function cleaning_services_agent_get_fonts_url() {
    $cleaning_services_agent_fonts_url = '';
    $cleaning_services_agent_fonts     = array();

    $cleaning_services_agent_all_fonts = cleaning_services_agent_get_all_google_fonts();

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

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

    if ( ! empty( get_theme_mod( 'cleaning_services_agent_header_font', 'Play' ) ) ) {
        $cleaning_services_agent_fonts[] = get_theme_mod( 'cleaning_services_agent_header_font', 'Play' );
    }

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

    $cleaning_services_agent_fonts = array_unique( $cleaning_services_agent_fonts );

    foreach ( $cleaning_services_agent_fonts as $cleaning_services_agent_font ) {
        $cleaning_services_agent_variants      = $cleaning_services_agent_all_fonts[ $cleaning_services_agent_font ]['variants'];
        $cleaning_services_agent_font_family[] = $cleaning_services_agent_font . ':' . implode( ',', $cleaning_services_agent_variants );
    }

    $cleaning_services_agent_query_args = array(
        'family' => urlencode( implode( '|', $cleaning_services_agent_font_family ) ),
    );

    if ( ! empty( $cleaning_services_agent_font_family ) ) {
        $cleaning_services_agent_fonts_url = add_query_arg( $cleaning_services_agent_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $cleaning_services_agent_fonts_url;
}