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

    $bakery_confectionery_fonts_json_data = file_get_contents( $bakery_confectionery_webfonts_json );
    if ( false === $bakery_confectionery_fonts_json_data ) {
        return array();
    }

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

    $bakery_confectionery_google_fonts = array();
    foreach ( $bakery_confectionery_all_fonts as $bakery_confectionery_font ) {
        $bakery_confectionery_google_fonts[ $bakery_confectionery_font['family'] ] = array(
            'family'   => $bakery_confectionery_font['family'],
            'variants' => $bakery_confectionery_font['variants'],
        );
    }
    return $bakery_confectionery_google_fonts;
}


function bakery_confectionery_get_all_google_font_families() {
    $bakery_confectionery_google_fonts  = bakery_confectionery_get_all_google_fonts();
    $bakery_confectionery_font_families = array();
    foreach ( $bakery_confectionery_google_fonts as $bakery_confectionery_font ) {
        $bakery_confectionery_font_families[ $bakery_confectionery_font['family'] ] = $bakery_confectionery_font['family'];
    }
    return $bakery_confectionery_font_families;
}

function bakery_confectionery_get_fonts_url() {
    $bakery_confectionery_fonts_url = '';
    $bakery_confectionery_fonts     = array();

    $bakery_confectionery_all_fonts = bakery_confectionery_get_all_google_fonts();

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

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

    if ( ! empty( get_theme_mod( 'bakery_confectionery_header_font', 'Mouse Memoirs' ) ) ) {
        $bakery_confectionery_fonts[] = get_theme_mod( 'bakery_confectionery_header_font', 'Mouse Memoirs' );
    }

    if ( ! empty( get_theme_mod( 'bakery_confectionery_content_font', 'Patrick Hand' ) ) ) {
        $bakery_confectionery_fonts[] = get_theme_mod( 'bakery_confectionery_content_font', 'Patrick Hand' );
    }

    $bakery_confectionery_fonts = array_unique( $bakery_confectionery_fonts );

    foreach ( $bakery_confectionery_fonts as $bakery_confectionery_font ) {
        $bakery_confectionery_variants      = $bakery_confectionery_all_fonts[ $bakery_confectionery_font ]['variants'];
        $bakery_confectionery_font_family[] = $bakery_confectionery_font . ':' . implode( ',', $bakery_confectionery_variants );
    }

    $bakery_confectionery_query_args = array(
        'family' => urlencode( implode( '|', $bakery_confectionery_font_family ) ),
    );

    if ( ! empty( $bakery_confectionery_font_family ) ) {
        $bakery_confectionery_fonts_url = add_query_arg( $bakery_confectionery_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $bakery_confectionery_fonts_url;
}

