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

    $property_dealer_fonts_json_data = file_get_contents( $property_dealer_webfonts_json );
    if ( false === $property_dealer_fonts_json_data ) {
        return array();
    }

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

    $property_dealer_google_fonts = array();
    foreach ( $property_dealer_all_fonts as $property_dealer_font ) {
        $property_dealer_google_fonts[ $property_dealer_font['family'] ] = array(
            'family'   => $property_dealer_font['family'],
            'variants' => $property_dealer_font['variants'],
        );
    }
    return $property_dealer_google_fonts;
}

function property_dealer_get_all_google_font_families() {
    $property_dealer_google_fonts  = property_dealer_get_all_google_fonts();
    $property_dealer_font_families = array();
    foreach ( $property_dealer_google_fonts as $property_dealer_font ) {
        $property_dealer_font_families[ $property_dealer_font['family'] ] = $property_dealer_font['family'];
    }
    return $property_dealer_font_families;
}

function property_dealer_get_fonts_url() {
    $property_dealer_fonts_url = '';
    $property_dealer_fonts     = array();

    $property_dealer_all_fonts = property_dealer_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'property_dealer_site_title_font', 'Cormorant Garamond' ) ) ) {
        $property_dealer_fonts[] = get_theme_mod( 'property_dealer_site_title_font', 'Cormorant Garamond' );
    }

    if ( ! empty( get_theme_mod( 'property_dealer_site_description_font', 'Yantramanav' ) ) ) {
        $property_dealer_fonts[] = get_theme_mod( 'property_dealer_site_description_font', 'Yantramanav' );
    }

    if ( ! empty( get_theme_mod( 'property_dealer_header_font', 'Cormorant Garamond' ) ) ) {
        $property_dealer_fonts[] = get_theme_mod( 'property_dealer_header_font', 'Cormorant Garamond' );
    }

    if ( ! empty( get_theme_mod( 'property_dealer_content_font', 'Yantramanav' ) ) ) {
        $property_dealer_fonts[] = get_theme_mod( 'property_dealer_content_font', 'Yantramanav' );
    }

    $property_dealer_fonts = array_unique( $property_dealer_fonts );

    foreach ( $property_dealer_fonts as $property_dealer_font ) {
        $property_dealer_variants      = $property_dealer_all_fonts[ $property_dealer_font ]['variants'];
        $property_dealer_font_family[] = $property_dealer_font . ':' . implode( ',', $property_dealer_variants );
    }

    $property_dealer_query_args = array(
        'family' => urlencode( implode( '|', $property_dealer_font_family ) ),
    );

    if ( ! empty( $property_dealer_font_family ) ) {
        $property_dealer_fonts_url = add_query_arg( $property_dealer_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $property_dealer_fonts_url;
}