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

    $moving_truck_rental_fonts_json_data = file_get_contents( $moving_truck_rental_webfonts_json );
    if ( false === $moving_truck_rental_fonts_json_data ) {
        return array();
    }

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

    $moving_truck_rental_google_fonts = array();
    foreach ( $moving_truck_rental_all_fonts as $moving_truck_rental_font ) {
        $moving_truck_rental_google_fonts[ $moving_truck_rental_font['family'] ] = array(
            'family'   => $moving_truck_rental_font['family'],
            'variants' => $moving_truck_rental_font['variants'],
        );
    }
    return $moving_truck_rental_google_fonts;
}


function moving_truck_rental_get_all_google_font_families() {
    $moving_truck_rental_google_fonts  = moving_truck_rental_get_all_google_fonts();
    $moving_truck_rental_font_families = array();
    foreach ( $moving_truck_rental_google_fonts as $moving_truck_rental_font ) {
        $moving_truck_rental_font_families[ $moving_truck_rental_font['family'] ] = $moving_truck_rental_font['family'];
    }
    return $moving_truck_rental_font_families;
}

function moving_truck_rental_get_fonts_url() {
    $moving_truck_rental_fonts_url = '';
    $moving_truck_rental_fonts     = array();

    $moving_truck_rental_all_fonts = moving_truck_rental_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'moving_truck_rental_site_title_font', 'Inter' ) ) ) {
        $moving_truck_rental_fonts[] = get_theme_mod( 'moving_truck_rental_site_title_font', 'Inter' );
    }

    if ( ! empty( get_theme_mod( 'moving_truck_rental_site_description_font', 'Raleway' ) ) ) {
        $moving_truck_rental_fonts[] = get_theme_mod( 'moving_truck_rental_site_description_font', 'Raleway' );
    }

    if ( ! empty( get_theme_mod( 'moving_truck_rental_header_font', 'Raleway' ) ) ) {
        $moving_truck_rental_fonts[] = get_theme_mod( 'moving_truck_rental_header_font', 'Raleway' );
    }

    if ( ! empty( get_theme_mod( 'moving_truck_rental_content_font', 'Work Sans' ) ) ) {
        $moving_truck_rental_fonts[] = get_theme_mod( 'moving_truck_rental_content_font', 'Work Sans' );
    }

    $moving_truck_rental_fonts = array_unique( $moving_truck_rental_fonts );

    foreach ( $moving_truck_rental_fonts as $moving_truck_rental_font ) {
        $moving_truck_rental_variants      = $moving_truck_rental_all_fonts[ $moving_truck_rental_font ]['variants'];
        $moving_truck_rental_font_family[] = $moving_truck_rental_font . ':' . implode( ',', $moving_truck_rental_variants );
    }

    $moving_truck_rental_query_args = array(
        'family' => urlencode( implode( '|', $moving_truck_rental_font_family ) ),
    );

    if ( ! empty( $moving_truck_rental_font_family ) ) {
        $moving_truck_rental_fonts_url = add_query_arg( $moving_truck_rental_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $moving_truck_rental_fonts_url;
}

