<?php

if ( ! function_exists( 'aster_vlogger_get_all_google_fonts' ) ) :
	/**
	 * Returns list of Google fonts.
	 */
	function aster_vlogger_get_all_google_fonts() {
		$webfonts_json   = get_template_directory() . '/theme-library/google-webfonts.json';
		$aster_vlogger_fonts_json_data = file_get_contents( $webfonts_json );

		$aster_vlogger_all_fonts = json_decode( $aster_vlogger_fonts_json_data, true );

		$aster_vlogger_google_fonts = array();
		foreach ( $aster_vlogger_all_fonts as $aster_vlogger_font ) {
			$aster_vlogger_google_fonts[ $aster_vlogger_font['family'] ] = array(
				'family'   => $aster_vlogger_font['family'],
				'variants' => $aster_vlogger_font['variants'],
			);
		}
		return $aster_vlogger_google_fonts;
	}
endif;

if ( ! function_exists( 'aster_vlogger_get_all_google_font_families' ) ) :
	/**
	 * Returns list of Google font families.
	 */
	function aster_vlogger_get_all_google_font_families() {
		$aster_vlogger_google_fonts  = aster_vlogger_get_all_google_fonts();
		$aster_vlogger_font_families = array();
		foreach ( $aster_vlogger_google_fonts as $aster_vlogger_font ) {
			$aster_vlogger_font_families[ $aster_vlogger_font['family'] ] = $aster_vlogger_font['family'];
		}
		return $aster_vlogger_font_families;
	}
endif;

if ( ! function_exists( 'aster_vlogger_get_fonts_url' ) ) :
	/**
	 * Return Google fonts URL.
	 */
	function aster_vlogger_get_fonts_url() {
		$aster_vlogger_fonts_url = '';
		$aster_vlogger_fonts     = array();

		$aster_vlogger_all_fonts = aster_vlogger_get_all_google_fonts();

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

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

		if ( ! empty( get_theme_mod( 'aster_vlogger_header_font', 'Muli' ) ) ) {
			$aster_vlogger_fonts[] = get_theme_mod( 'aster_vlogger_header_font', 'Muli' );
		}

		if ( ! empty( get_theme_mod( 'aster_vlogger_body_font', 'Muli' ) ) ) {
			$aster_vlogger_fonts[] = get_theme_mod( 'aster_vlogger_body_font', 'Muli' );
		}

		$aster_vlogger_fonts = array_unique( $aster_vlogger_fonts );

		foreach ( $aster_vlogger_fonts as $aster_vlogger_font ) {
			$aster_vlogger_variants      = $aster_vlogger_all_fonts[ $aster_vlogger_font ]['variants'];
			$aster_vlogger_font_family[] = $aster_vlogger_font . ':' . implode( ',', $aster_vlogger_variants );
		}

		$aster_vlogger_query_args = array(
			'family' => urlencode( implode( '|', $aster_vlogger_font_family ) ),
		);

		if ( ! empty( $aster_vlogger_font_family ) ) {
			$aster_vlogger_fonts_url = add_query_arg( $aster_vlogger_query_args, 'https://fonts.googleapis.com/css' );
		}

		return $aster_vlogger_fonts_url;
	}
endif;