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

    $charitable_funds_fonts_json_data = file_get_contents( $charitable_funds_webfonts_json );
    if ( false === $charitable_funds_fonts_json_data ) {
        return array();
    }

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

    $charitable_funds_google_fonts = array();
    foreach ( $charitable_funds_all_fonts as $charitable_funds_font ) {
        $charitable_funds_google_fonts[ $charitable_funds_font['family'] ] = array(
            'family'   => $charitable_funds_font['family'],
            'variants' => $charitable_funds_font['variants'],
        );
    }
    return $charitable_funds_google_fonts;
}

function charitable_funds_get_all_google_font_families() {
    $charitable_funds_google_fonts  = charitable_funds_get_all_google_fonts();
    $charitable_funds_font_families = array();
    foreach ( $charitable_funds_google_fonts as $charitable_funds_font ) {
        $charitable_funds_font_families[ $charitable_funds_font['family'] ] = $charitable_funds_font['family'];
    }
    return $charitable_funds_font_families;
}

function charitable_funds_get_fonts_url() {
    $charitable_funds_fonts_url = '';
    $charitable_funds_fonts     = array();

    $charitable_funds_all_fonts = charitable_funds_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'charitable_funds_site_title_font', 'Sora' ) ) ) {
        $charitable_funds_fonts[] = get_theme_mod( 'charitable_funds_site_title_font', 'Sora' );
    }

    if ( ! empty( get_theme_mod( 'charitable_funds_site_description_font', 'Sora' ) ) ) {
        $charitable_funds_fonts[] = get_theme_mod( 'charitable_funds_site_description_font', 'Sora' );
    }

    if ( ! empty( get_theme_mod( 'charitable_funds_header_font', 'Sora' ) ) ) {
        $charitable_funds_fonts[] = get_theme_mod( 'charitable_funds_header_font', 'Sora' );
    }

    if ( ! empty( get_theme_mod( 'charitable_funds_content_font', 'Sora' ) ) ) {
        $charitable_funds_fonts[] = get_theme_mod( 'charitable_funds_content_font', 'Sora' );
    }

    $charitable_funds_fonts = array_unique( $charitable_funds_fonts );

    foreach ( $charitable_funds_fonts as $charitable_funds_font ) {
        $charitable_funds_variants      = $charitable_funds_all_fonts[ $charitable_funds_font ]['variants'];
        $charitable_funds_font_family[] = $charitable_funds_font . ':' . implode( ',', $charitable_funds_variants );
    }

    $charitable_funds_query_args = array(
        'family' => urlencode( implode( '|', $charitable_funds_font_family ) ),
    );

    if ( ! empty( $charitable_funds_font_family ) ) {
        $charitable_funds_fonts_url = add_query_arg( $charitable_funds_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $charitable_funds_fonts_url;
}