Automattic\WooCommerce\EmailEditor\Engine

Site_Style_Sync_Controller::convert_to_email_safe_fontprivateWC 1.0

Convert font family to email-safe alternative

Method of the class: Site_Style_Sync_Controller{}

No Hooks.

Returns

String. Email-safe font family.

Usage

// private - for code of main (parent) class only
$result = $this->convert_to_email_safe_font( $font_family ): string;
$font_family(string) (required)
Original font family.

Site_Style_Sync_Controller::convert_to_email_safe_font() code WC 10.4.3

private function convert_to_email_safe_font( string $font_family ): string {
	// Get email-safe fonts.
	$email_safe_fonts = $this->get_email_safe_fonts();

	// Map common web fonts to email-safe alternatives.
	$font_map = array(
		'helvetica' => $email_safe_fonts['arial'], // Arial fallback.
		'times'     => $email_safe_fonts['georgia'], // Georgia fallback.
		'courier'   => $email_safe_fonts['courier-new'], // Courier New.
		'trebuchet' => $email_safe_fonts['trebuchet-ms'],
	);

	$email_safe_fonts = array_merge( $email_safe_fonts, $font_map );

	$get_font_family = function ( $font_name ) use ( $email_safe_fonts ) {
		$font_name_lower = strtolower( $font_name );

		// First check for match in the email-safe slug.
		if ( isset( $email_safe_fonts[ $font_name_lower ] ) ) {
			return $email_safe_fonts[ $font_name_lower ];
		}

		// If no match in the slug, check for match in the font family name.
		foreach ( $email_safe_fonts as $safe_font_slug => $safe_font ) {
			if ( stripos( $safe_font, $font_name_lower ) !== false ) {
				return $safe_font;
			}
		}
		return null;
	};

	// Check if it's already an email-safe font.
	$font_family_array = explode( ',', $font_family );
	$safe_font_family  = $get_font_family( trim( $font_family_array[0] ) );
	if ( $safe_font_family ) {
		return $safe_font_family;
	}

	// Default to arial font if no match found.
	return $email_safe_fonts['arial'];
}