<?php

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

		$social_blog_all_fonts = json_decode( $social_blog_fonts_json_data, true );

		$social_blog_google_fonts = array();
		foreach ( $social_blog_all_fonts as $social_blog_font ) {
			$social_blog_google_fonts[ $social_blog_font['family'] ] = array(
				'family'   => $social_blog_font['family'],
				'variants' => $social_blog_font['variants'],
			);
		}
		return $social_blog_google_fonts;
	}
endif;

if ( ! function_exists( 'social_blog_get_all_google_font_families' ) ) :
	/**
	 * Returns list of Google font families.
	 */
	function social_blog_get_all_google_font_families() {
		$social_blog_google_fonts  = social_blog_get_all_google_fonts();
		$social_blog_font_families = array();
		foreach ( $social_blog_google_fonts as $social_blog_font ) {
			$social_blog_font_families[ $social_blog_font['family'] ] = $social_blog_font['family'];
		}
		return $social_blog_font_families;
	}
endif;

if ( ! function_exists( 'social_blog_get_fonts_url' ) ) :
	/**
	 * Return Google fonts URL.
	 */
	function social_blog_get_fonts_url() {
		$social_blog_fonts_url = '';
		$social_blog_fonts     = array();

		$social_blog_all_fonts = social_blog_get_all_google_fonts();

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

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

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

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

		$social_blog_fonts = array_unique( $social_blog_fonts );

		foreach ( $social_blog_fonts as $social_blog_font ) {
			$variants      = $social_blog_all_fonts[ $social_blog_font ]['variants'];
			$social_blog_font_family[] = $social_blog_font . ':' . implode( ',', $variants );
		}

		$social_blog_query_args = array(
			'family' => urlencode( implode( '|', $social_blog_font_family ) ),
		);

		if ( ! empty( $social_blog_font_family ) ) {
			$social_blog_fonts_url = add_query_arg( $social_blog_query_args, 'https://fonts.googleapis.com/css' );
		}

		return $social_blog_fonts_url;
	}
endif;