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

    $online_school_courses_fonts_json_data = file_get_contents( $online_school_courses_webfonts_json );
    if ( false === $online_school_courses_fonts_json_data ) {
        return array();
    }

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

    $online_school_courses_google_fonts = array();
    foreach ( $online_school_courses_all_fonts as $online_school_courses_font ) {
        $online_school_courses_google_fonts[ $online_school_courses_font['family'] ] = array(
            'family'   => $online_school_courses_font['family'],
            'variants' => $online_school_courses_font['variants'],
        );
    }
    return $online_school_courses_google_fonts;
}

function online_school_courses_get_all_google_font_families() {
    $online_school_courses_google_fonts  = online_school_courses_get_all_google_fonts();
    $online_school_courses_font_families = array();
    foreach ( $online_school_courses_google_fonts as $online_school_courses_font ) {
        $online_school_courses_font_families[ $online_school_courses_font['family'] ] = $online_school_courses_font['family'];
    }
    return $online_school_courses_font_families;
}

function online_school_courses_get_fonts_url() {
    $online_school_courses_fonts_url = '';
    $online_school_courses_fonts     = array();

    $online_school_courses_all_fonts = online_school_courses_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'online_school_courses_site_title_font', 'Be Vietnam Pro' ) ) ) {
        $online_school_courses_fonts[] = get_theme_mod( 'online_school_courses_site_title_font', 'Be Vietnam Pro' );
    }

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

    if ( ! empty( get_theme_mod( 'online_school_courses_header_font', 'Be Vietnam Pro' ) ) ) {
        $online_school_courses_fonts[] = get_theme_mod( 'online_school_courses_header_font', 'Be Vietnam Pro' );
    }

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

    $online_school_courses_fonts = array_unique( $online_school_courses_fonts );

    foreach ( $online_school_courses_fonts as $online_school_courses_font ) {
        $online_school_courses_variants      = $online_school_courses_all_fonts[ $online_school_courses_font ]['variants'];
        $online_school_courses_font_family[] = $online_school_courses_font . ':' . implode( ',', $online_school_courses_variants );
    }

    $online_school_courses_query_args = array(
        'family' => urlencode( implode( '|', $online_school_courses_font_family ) ),
    );

    if ( ! empty( $online_school_courses_font_family ) ) {
        $online_school_courses_fonts_url = add_query_arg( $online_school_courses_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $online_school_courses_fonts_url;
}