Automattic\WooCommerce\Internal\ComingSoon

ComingSoonRequestHandler::experimental_filter_theme_json_theme │ public │ WC 1.0

Filters the theme.json data to add Coming Soon fonts. This runs after child theme merging to ensure parent theme fonts are included.

Method of the class: ComingSoonRequestHandler{}

No Hooks.

Returns

WP_Theme_JSON_Data. The filtered theme json data.

Usage

$ComingSoonRequestHandler = new ComingSoonRequestHandler();
$ComingSoonRequestHandler->experimental_filter_theme_json_theme( $theme_json );
$theme_json(WP_Theme_JSON_Data) (required)
The theme json data object.

ComingSoonRequestHandler::experimental_filter_theme_json_theme() code WC 11.3.0

public function experimental_filter_theme_json_theme( $theme_json ) {
	$theme_data = $theme_json->get_data();
	$font_data  = $theme_data['settings']['typography']['fontFamilies']['theme'] ?? array();

	// Check if the current theme is a child theme. And if so, merge the parent theme fonts with the existing fonts.
	if ( wp_get_theme()->parent() ) {
		$parent_theme           = wp_get_theme()->parent();
		$parent_theme_json_file = $parent_theme->get_file_path( 'theme.json' );

		if ( is_readable( $parent_theme_json_file ) ) {
			$parent_theme_json_data = json_decode( file_get_contents( $parent_theme_json_file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents

			if ( isset( $parent_theme_json_data['settings']['typography']['fontFamilies'] ) ) {
				$parent_fonts = $parent_theme_json_data['settings']['typography']['fontFamilies'];

				// Merge parent theme fonts with existing fonts.
				foreach ( $parent_fonts as $parent_font ) {
					$found = false;
					foreach ( $font_data as $existing_font ) {
						if ( isset( $parent_font['name'] ) && isset( $existing_font['name'] ) &&
						$parent_font['name'] === $existing_font['name'] ) {
							$found = true;
							break;
						}
					}

					if ( ! $found ) {
						$font_data[] = $parent_font;
					}
				}
			}
		}
	}

	$fonts_to_add = array(
		array(
			'fontFamily' => '"Inter", sans-serif',
			'name'       => 'Inter',
			'slug'       => 'inter',
			'fontFace'   => array(
				array(
					'fontFamily'  => 'Inter',
					'fontStretch' => 'normal',
					'fontStyle'   => 'normal',
					'fontWeight'  => '300 900',
					'src'         => array( WC()->plugin_url() . '/assets/fonts/Inter-VariableFont_slnt,wght.woff2' ),
				),
			),
		),
		array(
			'fontFamily' => 'Cardo',
			'name'       => 'Cardo',
			'slug'       => 'cardo',
			'fontFace'   => array(
				array(
					'fontFamily' => 'Cardo',
					'fontStyle'  => 'normal',
					'fontWeight' => '400',
					'src'        => array( WC()->plugin_url() . '/assets/fonts/cardo_normal_400.woff2' ),
				),
			),
		),
	);

	// Add WooCommerce fonts if they don't already exist.
	foreach ( $fonts_to_add as $font_to_add ) {
		$found      = false;
		$slug_index = null;
		foreach ( $font_data as $index => $font ) {
			// Theme presets may omit the name or use another one, so also match the same primary font family and slug.
			// Coming Soon patterns and saved templates use the bundled slugs, and need the theme to load the font files.
			if (
				( isset( $font['name'] ) && $font['name'] === $font_to_add['name'] )
				|| (
					isset( $font['fontFamily'], $font['slug'] )
					&& is_string( $font['fontFamily'] )
					&& $this->is_same_font_family( $font['fontFamily'], $font_to_add['fontFamily'] )
					&& $font['slug'] === $font_to_add['slug']
					&& is_array( $font['fontFace'] ?? null )
					&& $this->declares_font_files( $font['fontFace'] )
				)
			) {
				$found = true;
				break;
			}

			if ( null === $slug_index && ( $font['slug'] ?? null ) === $font_to_add['slug'] ) {
				$slug_index = $index;
			}
		}

		if ( $found ) {
			continue;
		}

		if ( null === $slug_index ) {
			$font_data[] = $font_to_add;
			continue;
		}

		// The theme already owns this slug, and a second preset with it would override the theme's CSS variable.
		// Only add the bundled font files when the theme preset is the same font without font files.
		$font = $font_data[ $slug_index ];
		if ( isset( $font['fontFamily'] ) && is_string( $font['fontFamily'] ) && $this->is_same_font_family( $font['fontFamily'], $font_to_add['fontFamily'] ) ) {
			$font_faces                           = is_array( $font['fontFace'] ?? null ) ? $font['fontFace'] : array();
			$font_data[ $slug_index ]['fontFace'] = array_merge( $font_faces, $font_to_add['fontFace'] );
		}
	}

	$new_data = array(
		'version'  => 1,
		'settings' => array(
			'typography' => array(
				'fontFamilies' => array(
					'theme' => $font_data,
				),
			),
		),
	);
	$theme_json->update_with( $new_data );
	return $theme_json;
}