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

    $lawyer_consultant_firm_fonts_json_data = file_get_contents( $lawyer_consultant_firm_webfonts_json );
    if ( false === $lawyer_consultant_firm_fonts_json_data ) {
        return array();
    }

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

    $lawyer_consultant_firm_google_fonts = array();
    foreach ( $lawyer_consultant_firm_all_fonts as $lawyer_consultant_firm_font ) {
        $lawyer_consultant_firm_google_fonts[ $lawyer_consultant_firm_font['family'] ] = array(
            'family'   => $lawyer_consultant_firm_font['family'],
            'variants' => $lawyer_consultant_firm_font['variants'],
        );
    }
    return $lawyer_consultant_firm_google_fonts;
}

function lawyer_consultant_firm_get_all_google_font_families() {
    $lawyer_consultant_firm_google_fonts  = lawyer_consultant_firm_get_all_google_fonts();
    $lawyer_consultant_firm_font_families = array();
    foreach ( $lawyer_consultant_firm_google_fonts as $lawyer_consultant_firm_font ) {
        $lawyer_consultant_firm_font_families[ $lawyer_consultant_firm_font['family'] ] = $lawyer_consultant_firm_font['family'];
    }
    return $lawyer_consultant_firm_font_families;
}

function lawyer_consultant_firm_get_fonts_url() {
    $lawyer_consultant_firm_fonts_url = '';
    $lawyer_consultant_firm_fonts     = array();

    $lawyer_consultant_firm_all_fonts = lawyer_consultant_firm_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'lawyer_consultant_firm_site_title_font', 'Space Grotesk' ) ) ) {
        $lawyer_consultant_firm_fonts[] = get_theme_mod( 'lawyer_consultant_firm_site_title_font', 'Space Grotesk' );
    }

    if ( ! empty( get_theme_mod( 'lawyer_consultant_firm_site_description_font', 'Content' ) ) ) {
        $lawyer_consultant_firm_fonts[] = get_theme_mod( 'lawyer_consultant_firm_site_description_font', 'Content' );
    }

    if ( ! empty( get_theme_mod( 'lawyer_consultant_firm_header_font', 'Space Grotesk' ) ) ) {
        $lawyer_consultant_firm_fonts[] = get_theme_mod( 'lawyer_consultant_firm_header_font', 'Space Grotesk' );
    }

    if ( ! empty( get_theme_mod( 'lawyer_consultant_firm_content_font', 'Content' ) ) ) {
        $lawyer_consultant_firm_fonts[] = get_theme_mod( 'lawyer_consultant_firm_content_font', 'Content' );
    }

    $lawyer_consultant_firm_fonts = array_unique( $lawyer_consultant_firm_fonts );

    foreach ( $lawyer_consultant_firm_fonts as $lawyer_consultant_firm_font ) {
        $lawyer_consultant_firm_variants      = $lawyer_consultant_firm_all_fonts[ $lawyer_consultant_firm_font ]['variants'];
        $lawyer_consultant_firm_font_family[] = $lawyer_consultant_firm_font . ':' . implode( ',', $lawyer_consultant_firm_variants );
    }

    $lawyer_consultant_firm_query_args = array(
        'family' => urlencode( implode( '|', $lawyer_consultant_firm_font_family ) ),
    );

    if ( ! empty( $lawyer_consultant_firm_font_family ) ) {
        $lawyer_consultant_firm_fonts_url = add_query_arg( $lawyer_consultant_firm_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $lawyer_consultant_firm_fonts_url;
}