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

    $aster_portfolio_fonts_json_data = file_get_contents( $aster_portfolio_webfonts_json );
    if ( false === $aster_portfolio_fonts_json_data ) {
        return array();
    }

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

    $aster_portfolio_google_fonts = array();
    foreach ( $aster_portfolio_all_fonts as $aster_portfolio_font ) {
        $aster_portfolio_google_fonts[ $aster_portfolio_font['family'] ] = array(
            'family'   => $aster_portfolio_font['family'],
            'variants' => $aster_portfolio_font['variants'],
        );
    }
    return $aster_portfolio_google_fonts;
}

function aster_portfolio_get_all_google_font_families() {
    $aster_portfolio_google_fonts  = aster_portfolio_get_all_google_fonts();
    $aster_portfolio_font_families = array();
    foreach ( $aster_portfolio_google_fonts as $aster_portfolio_font ) {
        $aster_portfolio_font_families[ $aster_portfolio_font['family'] ] = $aster_portfolio_font['family'];
    }
    return $aster_portfolio_font_families;
}

function aster_portfolio_get_fonts_url() {
    $aster_portfolio_fonts_url = '';
    $aster_portfolio_fonts     = array();

    $aster_portfolio_all_fonts = aster_portfolio_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'aster_portfolio_site_title_font', 'DM Sans' ) ) ) {
        $aster_portfolio_fonts[] = get_theme_mod( 'aster_portfolio_site_title_font', 'DM Sans' );
    }

    if ( ! empty( get_theme_mod( 'aster_portfolio_site_description_font', 'Sora' ) ) ) {
        $aster_portfolio_fonts[] = get_theme_mod( 'aster_portfolio_site_description_font', 'Sora' );
    }

    if ( ! empty( get_theme_mod( 'aster_portfolio_header_font', 'DM Sans' ) ) ) {
        $aster_portfolio_fonts[] = get_theme_mod( 'aster_portfolio_header_font', 'DM Sans' );
    }

    if ( ! empty( get_theme_mod( 'aster_portfolio_content_font', 'Sora' ) ) ) {
        $aster_portfolio_fonts[] = get_theme_mod( 'aster_portfolio_content_font', 'Sora' );
    }

    $aster_portfolio_fonts = array_unique( $aster_portfolio_fonts );

    foreach ( $aster_portfolio_fonts as $aster_portfolio_font ) {
        $aster_portfolio_variants      = $aster_portfolio_all_fonts[ $aster_portfolio_font ]['variants'];
        $aster_portfolio_font_family[] = $aster_portfolio_font . ':' . implode( ',', $aster_portfolio_variants );
    }

    $aster_portfolio_query_args = array(
        'family' => urlencode( implode( '|', $aster_portfolio_font_family ) ),
    );

    if ( ! empty( $aster_portfolio_font_family ) ) {
        $aster_portfolio_fonts_url = add_query_arg( $aster_portfolio_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $aster_portfolio_fonts_url;
}