<?php
/**
 * Typography
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}
/**
 * Prepares @font-face css queries for fonts chosen in customizer
 */
function multimagazine_custom_typography() {
	$display = get_theme_mod( 'multi_font_display', MULTIMAGAZINE_FONT_DISPLAY );

	$google_fonts_url = '//fonts.googleapis.com/css?display=' . esc_attr( $display ) . '&family=';

	$primary_font        = get_theme_mod( 'multi_primary_font', MULTIMAGAZINE_PRIMARY_FONT );
	$primary_google_font = '';
	$pri_subset_list     = '';
	$pri_subsets         = array();

	if ( 'google' === $primary_font ) {
		$primary_google_font = get_theme_mod( 'multi_primary_google_font', MULTIMAGAZINE_PRIMARY_GOOGLE_FONT );
		if ( empty( $primary_google_font ) ) {
			$primary_google_font = MULTIMAGAZINE_PRIMARY_GOOGLE_FONT;
		}
		if ( get_theme_mod( 'multi_primary_subset_latin', MULTIMAGAZINE_PRIMARY_SUBSET_LATIN ) ) {
			$pri_subsets[] = 'latin,latin-ext';
		}
		if ( get_theme_mod( 'multi_primary_subset_greek', MULTIMAGAZINE_PRIMARY_SUBSET_GREEK ) ) {
			$pri_subsets[] = 'greek,greek-ext';
		}
		if ( get_theme_mod( 'multi_primary_subset_vietnamese', MULTIMAGAZINE_PRIMARY_SUBSET_VIETNAMESE ) ) {
			$pri_subsets[] = 'vietnamese';
		}
		if ( get_theme_mod( 'multi_primary_subset_cyryllic', MULTIMAGAZINE_PRIMARY_SUBSET_CYRYLLIC ) ) {
			$pri_subsets[] = 'cyrillic,cyrillic-ext';
		}
		$pri_subset_list = '&subset=' . implode( ',', $pri_subsets );
	}

	$pri_weights = array();

	if ( get_theme_mod( 'multi_primary_font_weight_300', MULTIMAGAZINE_PRIMARY_FONT_WEIGHT_300 ) ) {
		$pri_weights[] = 300;
	}
	if ( get_theme_mod( 'multi_primary_font_weight_400', MULTIMAGAZINE_PRIMARY_FONT_WEIGHT_400 ) ) {
		$pri_weights[] = 400;
	}
	if ( get_theme_mod( 'multi_primary_font_weight_600', MULTIMAGAZINE_PRIMARY_FONT_WEIGHT_600 ) ) {
		$pri_weights[] = 600;
	}
	if ( get_theme_mod( 'multi_primary_font_weight_700', MULTIMAGAZINE_PRIMARY_FONT_WEIGHT_700 ) ) {
		$pri_weights[] = 700;
	}

	$secondary_font        = get_theme_mod( 'multi_secondary_font', MULTIMAGAZINE_SECONDARY_FONT );
	$secondary_google_font = '';
	$sec_subset_list       = '';
	$sec_subsets           = array();

	if ( 'google' === $secondary_font ) {
		$secondary_google_font = get_theme_mod( 'multi_secondary_google_font', MULTIMAGAZINE_SECONDARY_GOOGLE_FONT );
		if ( empty( $secondary_google_font ) ) {
			$secondary_font = '';
		}
		if ( get_theme_mod( 'multi_secondary_subset_latin' ) ) {
			$sec_subsets[] = 'latin,latin-ext';
		}
		if ( get_theme_mod( 'multi_secondary_subset_greek' ) ) {
			$sec_subsets[] = 'greek,greek-ext';
		}
		if ( get_theme_mod( 'multi_secondary_subset_vietnamese' ) ) {
			$sec_subsets[] = 'vietnamese';
		}
		if ( get_theme_mod( 'multi_secondary_subset_cyryllic' ) ) {
			$sec_subsets[] = 'cyrillic,cyrillic-ext';
		}
		$sec_subset_list = '&subset=' . implode( ',', $sec_subsets );
	}
	$sec_weights = array();
	if ( get_theme_mod( 'multi_secondary_font_weight_300', 0 ) ) {
		$sec_weights[] = 300;
	}
	if ( get_theme_mod( 'multi_secondary_font_weight_400', 1 ) ) {
		$sec_weights[] = 400;
	}
	if ( get_theme_mod( 'multi_secondary_font_weight_600', 0 ) ) {
		$sec_weights[] = 600;
	}
	if ( get_theme_mod( 'multi_secondary_font_weight_700', 1 ) ) {
		$sec_weights[] = 700;
	}

	$both_fonts_google = 'google' === $primary_font && ( 'google' === $secondary_font || '' === $secondary_font );
	$same_fonts        = ( $both_fonts_google && $primary_google_font === $secondary_google_font ) || '' === $secondary_font || ( ! $both_fonts_google && $primary_font === $secondary_font );
	$same_subsets      = $pri_subset_list === $sec_subset_list;
	$same_weights      = $pri_weights === $sec_weights;
	$fonts_url         = '';

	if ( $both_fonts_google && $same_fonts ) {
		$subsets     = array_unique( array_merge( $pri_subsets, $sec_subsets ) );
		$subset_list = '&subset=' . implode( ',', $subsets );
		$weights     = array_unique( array_merge( $pri_weights, $sec_weights ) );
		$weight_list = implode( ',', $weights );
		$fonts_url   = $google_fonts_url . $primary_google_font . ':' . $weight_list . $subset_list;
		wp_enqueue_style( 'multimagazine-fonts', $fonts_url, array(), 1 );

	} elseif ( $both_fonts_google && ! $same_fonts ) {
		$pri         = $primary_google_font . ':' . implode( ',', $pri_weights );
		$sec         = $secondary_google_font . ':' . implode( ',', $sec_weights );
		$subsets     = array_unique( array_merge( $pri_subsets, $sec_subsets ) );
		$subset_list = '&subset=' . implode( ',', $subsets );
		$fonts_url   = $google_fonts_url . $pri . '|' . $sec . $subset_list;
		wp_enqueue_style( 'multimagazine-fonts', $fonts_url, array(), 1 );

	} else {
		if ( 'google' === $primary_font ) {
			$primary = $google_fonts_url . $primary_google_font . ':' . implode( ',', $pri_weights ) . $pri_subset_list;
			wp_enqueue_style( 'multimagazine-primary-font', $primary, array(), 1 );
		}
		if ( 'google' === $secondary_font ) {
			$secondary = $google_fonts_url . $secondary_google_font . ':' . implode( ',', $sec_weights ) . $sec_subset_list;
			wp_enqueue_style( 'multimagazine-secondary-font', $secondary, array(), 1 );
		}

		if ( 'typekit' === $primary_font || 'typekit' === $secondary_font ) {
			/**
			 * Fires when one or both font sources are set to Adobe Typekit, so that
			 * extensions (e.g. Multi Pro) can enqueue the Typekit embed stylesheet.
			 *
			 * @param string $primary_font   Value of the primary font setting.
			 * @param string $secondary_font Value of the secondary font setting.
			 */
			do_action( 'multimagazine_enqueue_typekit_fonts', $primary_font, $secondary_font );
		}
	}

	if ( 'google' === $primary_font ) {
		$primary_font = $primary_google_font;
	} elseif ( 'typekit' === $primary_font ) {
		/**
		 * Filters the resolved CSS font-family name when the primary font source
		 * is set to Adobe Typekit. Multi Pro hooks here to return the slugified
		 * font name; without Pro the value falls back to 'inherit' so the browser
		 * inherits its default font rather than rendering an unknown family name.
		 *
		 * @param string $resolved Resolved font-family value. Default 'inherit'.
		 * @param string $which    Which font slot: 'primary'.
		 */
		$primary_font = apply_filters( 'multimagazine_resolve_typekit_font', 'inherit', 'primary' );
	}

	if ( 'google' === $secondary_font ) {
		$secondary_font = $secondary_google_font;
	} elseif ( 'typekit' === $secondary_font ) {
		/** This filter is documented in functions/typography.php */
		$secondary_font = apply_filters( 'multimagazine_resolve_typekit_font', 'inherit', 'secondary' );
	}

	$fonts = array(
		'primary'   => $primary_font,
		'secondary' => empty( $secondary_font ) ? $primary_font : $secondary_font,
	);

	return $fonts;
}
