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

    $doctor_physician_fonts_json_data = file_get_contents( $doctor_physician_webfonts_json );
    if ( false === $doctor_physician_fonts_json_data ) {
        return array();
    }

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

    $doctor_physician_google_fonts = array();
    foreach ( $doctor_physician_all_fonts as $doctor_physician_font ) {
        $doctor_physician_google_fonts[ $doctor_physician_font['family'] ] = array(
            'family'   => $doctor_physician_font['family'],
            'variants' => $doctor_physician_font['variants'],
        );
    }
    return $doctor_physician_google_fonts;
}


function doctor_physician_get_all_google_font_families() {
    $doctor_physician_google_fonts  = doctor_physician_get_all_google_fonts();
    $doctor_physician_font_families = array();
    foreach ( $doctor_physician_google_fonts as $doctor_physician_font ) {
        $doctor_physician_font_families[ $doctor_physician_font['family'] ] = $doctor_physician_font['family'];
    }
    return $doctor_physician_font_families;
}

function doctor_physician_get_fonts_url() {
    $doctor_physician_fonts_url = '';
    $doctor_physician_fonts     = array();

    $doctor_physician_all_fonts = doctor_physician_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'doctor_physician_site_title_font', 'Nunito' ) ) ) {
        $doctor_physician_fonts[] = get_theme_mod( 'doctor_physician_site_title_font', 'Nunito' );
    }

    if ( ! empty( get_theme_mod( 'doctor_physician_site_description_font', 'Nunito' ) ) ) {
        $doctor_physician_fonts[] = get_theme_mod( 'doctor_physician_site_description_font', 'Nunito' );
    }

    if ( ! empty( get_theme_mod( 'doctor_physician_header_font', 'Poppins' ) ) ) {
        $doctor_physician_fonts[] = get_theme_mod( 'doctor_physician_header_font', 'Poppins' );
    }

    if ( ! empty( get_theme_mod( 'doctor_physician_content_font', 'Nunito' ) ) ) {
        $doctor_physician_fonts[] = get_theme_mod( 'doctor_physician_content_font', 'Nunito' );
    }

    $doctor_physician_fonts = array_unique( $doctor_physician_fonts );

    foreach ( $doctor_physician_fonts as $doctor_physician_font ) {
        $doctor_physician_variants      = $doctor_physician_all_fonts[ $doctor_physician_font ]['variants'];
        $doctor_physician_font_family[] = $doctor_physician_font . ':' . implode( ',', $doctor_physician_variants );
    }

    $doctor_physician_query_args = array(
        'family' => urlencode( implode( '|', $doctor_physician_font_family ) ),
    );

    if ( ! empty( $doctor_physician_font_family ) ) {
        $doctor_physician_fonts_url = add_query_arg( $doctor_physician_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $doctor_physician_fonts_url;
}

