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

    $clothing_marketplace_fonts_json_data = file_get_contents( $clothing_marketplace_webfonts_json );
    if ( false === $clothing_marketplace_fonts_json_data ) {
        return array();
    }

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

    $clothing_marketplace_google_fonts = array();
    foreach ( $clothing_marketplace_all_fonts as $clothing_marketplace_font ) {
        $clothing_marketplace_google_fonts[ $clothing_marketplace_font['family'] ] = array(
            'family'   => $clothing_marketplace_font['family'],
            'variants' => $clothing_marketplace_font['variants'],
        );
    }
    return $clothing_marketplace_google_fonts;
}

function clothing_marketplace_get_all_google_font_families() {
    $clothing_marketplace_google_fonts  = clothing_marketplace_get_all_google_fonts();
    $clothing_marketplace_font_families = array();
    foreach ( $clothing_marketplace_google_fonts as $clothing_marketplace_font ) {
        $clothing_marketplace_font_families[ $clothing_marketplace_font['family'] ] = $clothing_marketplace_font['family'];
    }
    return $clothing_marketplace_font_families;
}

function clothing_marketplace_get_fonts_url() {
    $clothing_marketplace_fonts_url = '';
    $clothing_marketplace_fonts     = array();

    $clothing_marketplace_all_fonts = clothing_marketplace_get_all_google_fonts();

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

    if ( ! empty( get_theme_mod( 'clothing_marketplace_site_description_font', 'Cabin' ) ) ) {
        $clothing_marketplace_fonts[] = get_theme_mod( 'clothing_marketplace_site_description_font', 'Cabin' );
    }

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

    if ( ! empty( get_theme_mod( 'clothing_marketplace_content_font', 'Cabin' ) ) ) {
        $clothing_marketplace_fonts[] = get_theme_mod( 'clothing_marketplace_content_font', 'Cabin' );
    }

    $clothing_marketplace_fonts = array_unique( $clothing_marketplace_fonts );

    foreach ( $clothing_marketplace_fonts as $clothing_marketplace_font ) {
        $clothing_marketplace_variants      = $clothing_marketplace_all_fonts[ $clothing_marketplace_font ]['variants'];
        $clothing_marketplace_font_family[] = $clothing_marketplace_font . ':' . implode( ',', $clothing_marketplace_variants );
    }

    $clothing_marketplace_query_args = array(
        'family' => urlencode( implode( '|', $clothing_marketplace_font_family ) ),
    );

    if ( ! empty( $clothing_marketplace_font_family ) ) {
        $clothing_marketplace_fonts_url = add_query_arg( $clothing_marketplace_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $clothing_marketplace_fonts_url;
}