<?php

/**
 * Merging the fonts defined by author vs added by users
 */
add_filter('dragblock_default_theme_json', 'dragblock_default_theme_json_font_lib');
function dragblock_default_theme_json_font_lib($theme_json)
{
	/**
	 * Even when you don't define "theme" in the theme.json/settings/typography/fontFamilies
	 * The 
	 */
	if (!isset($theme_json['settings']['typography']['fontFamilies']['theme'])) {
		$theme_json['settings']['typography']['fontFamilies']['theme'] = array();
	}
	// save all existing font families defined by the theme author
	$existed_families = array();
	foreach ($theme_json['settings']['typography']['fontFamilies']['theme'] as $index => $fontFamily) {
		if (empty($fontFamily['slug'])) {
			continue;
		}
		$existed_families[$fontFamily['slug']] = $index;
	}

	// merge the existing font families with uploaded font families
	$uploaded_families = get_option(DRAGBLOCK_FONT_LIB_SLUG, array());
	foreach ($uploaded_families as $fontFamily) {
		if (empty($fontFamily['slug']) || empty($fontFamily['fontFace'])) {
			continue;
		}

		// check if this font family is loaded by other plugins than ours
		// if loaded by other plugins, we should skip
		if (
			isset($existed_families[$fontFamily['slug']]) &&
			!empty($theme_json['settings']['typography']['fontFamilies']['theme'][$existed_families[$fontFamily['slug']]]['src'][0]) &&
			strpos($theme_json['settings']['typography']['fontFamilies']['theme'][$existed_families[$fontFamily['slug']]]['src'][0], '/dragblock/') === false
		) {
			continue;
		}

		// force using font-display: 'swap' to allow loading fonts at the first load
		foreach ($fontFamily['fontFace'] as $key => $value) {
			if (empty($value['fontDisplay']) || 'fallback' === $value['fontDisplay']) {
				$fontFamily['fontFace'][$key]['fontDisplay'] = 'swap';
			}
		}
		$theme_json['settings']['typography']['fontFamilies']['theme'][] = ($fontFamily);

		// checked as processed
		if (isset($existed_families[$fontFamily['slug']])) {
			unset($theme_json['settings']['typography']['fontFamilies']['theme'][$existed_families[$fontFamily['slug']]]);
			unset($existed_families[$fontFamily['slug']]);
		}
	}

	// add system fonts
	if (!empty(DRAG_BLOCK_DEFAULT_THEME_JSON['settings']['typography']['fontFamilies'])) {
		// get default system fonts
		foreach (DRAG_BLOCK_DEFAULT_THEME_JSON['settings']['typography']['fontFamilies'] as $fontFamily) {
			if (empty($fontFamily['slug'])) {
				continue;
			}

			$theme_json['settings']['typography']['fontFamilies']['theme'][] = ($fontFamily);

			// checked as processed
			if (isset($existed_families[$fontFamily['slug']])) {
				unset($theme_json['settings']['typography']['fontFamilies']['theme'][$existed_families[$fontFamily['slug']]]);
				unset($existed_families[$fontFamily['slug']]);
			}
		}
	}
	// remove all fonts cached by us but removed from the uploded list
	foreach ($existed_families as $slug => $index) {
		// check if this font family is loaded by us
		// then we remove
		if (
			!empty($theme_json['settings']['typography']['fontFamilies']['theme'][$index]['src'][0]) &&
			strpos($theme_json['settings']['typography']['fontFamilies']['theme'][$index]['src'][0], '/dragblock/') === false
		) {
			continue;
		}

		unset($theme_json['settings']['typography']['fontFamilies']['theme'][$index]);
	}

	// update font families indexes
	$theme_json['settings']['typography']['fontFamilies']['theme'] = array_values($theme_json['settings']['typography']['fontFamilies']['theme']);
	
	return $theme_json;
}


/**
 * WordPress seems to cache the settings into user theme data json 
 * to override the default theme json data
 * so we also add this to update our font list
 */
add_filter('wp_theme_json_data_user', 'dragblock_theme_json_data_user_font', 100);
function dragblock_theme_json_data_user_font($theme_json_object)
{
	$theme_json = $theme_json_object->get_data();
	$theme_json = dragblock_default_theme_json_font_lib($theme_json);

	return $theme_json_object->update_with($theme_json);
}
