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

    $events_and_celebrations_fonts_json_data = file_get_contents( $events_and_celebrations_webfonts_json );
    if ( false === $events_and_celebrations_fonts_json_data ) {
        return array();
    }

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

    $events_and_celebrations_google_fonts = array();
    foreach ( $events_and_celebrations_all_fonts as $events_and_celebrations_font ) {
        $events_and_celebrations_google_fonts[ $events_and_celebrations_font['family'] ] = array(
            'family'   => $events_and_celebrations_font['family'],
            'variants' => $events_and_celebrations_font['variants'],
        );
    }
    return $events_and_celebrations_google_fonts;
}

function events_and_celebrations_get_all_google_font_families() {
    $events_and_celebrations_google_fonts  = events_and_celebrations_get_all_google_fonts();
    $events_and_celebrations_font_families = array();
    foreach ( $events_and_celebrations_google_fonts as $events_and_celebrations_font ) {
        $events_and_celebrations_font_families[ $events_and_celebrations_font['family'] ] = $events_and_celebrations_font['family'];
    }
    return $events_and_celebrations_font_families;
}

function events_and_celebrations_get_fonts_url() {
    $events_and_celebrations_fonts_url = '';
    $events_and_celebrations_fonts     = array();

    $events_and_celebrations_all_fonts = events_and_celebrations_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'events_and_celebrations_site_title_font', 'Sail' ) ) ) {
        $events_and_celebrations_fonts[] = get_theme_mod( 'events_and_celebrations_site_title_font', 'Sail' );
    }

    if ( ! empty( get_theme_mod( 'events_and_celebrations_site_description_font', 'Arimo' ) ) ) {
        $events_and_celebrations_fonts[] = get_theme_mod( 'events_and_celebrations_site_description_font', 'Arimo' );
    }

    if ( ! empty( get_theme_mod( 'events_and_celebrations_header_font', 'Sail' ) ) ) {
        $events_and_celebrations_fonts[] = get_theme_mod( 'events_and_celebrations_header_font', 'Sail' );
    }

    if ( ! empty( get_theme_mod( 'events_and_celebrations_content_font', 'Arimo' ) ) ) {
        $events_and_celebrations_fonts[] = get_theme_mod( 'events_and_celebrations_content_font', 'Arimo' );
    }

    $events_and_celebrations_fonts = array_unique( $events_and_celebrations_fonts );

    foreach ( $events_and_celebrations_fonts as $events_and_celebrations_font ) {
        $events_and_celebrations_variants      = $events_and_celebrations_all_fonts[ $events_and_celebrations_font ]['variants'];
        $events_and_celebrations_font_family[] = $events_and_celebrations_font . ':' . implode( ',', $events_and_celebrations_variants );
    }

    $events_and_celebrations_query_args = array(
        'family' => urlencode( implode( '|', $events_and_celebrations_font_family ) ),
    );

    if ( ! empty( $events_and_celebrations_font_family ) ) {
        $events_and_celebrations_fonts_url = add_query_arg( $events_and_celebrations_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $events_and_celebrations_fonts_url;
}