Add `Cardo` and `Inter` fonts to global styles via theme json filter (#47417)

* Add global style font filter to add Cardo and Inter fonts when not available

* Changelog
This commit is contained in:
Ilyas Foo 2024-05-20 16:44:35 +08:00 committed by GitHub
parent ad6eea6e00
commit 815d23bf09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add custom fonts via wp_theme_json_data_theme filter for coming soon pages

View File

@ -26,6 +26,7 @@ class ComingSoonRequestHandler {
final public function init( ComingSoonHelper $coming_soon_helper ) {
$this->coming_soon_helper = $coming_soon_helper;
add_filter( 'template_include', array( $this, 'handle_template_include' ) );
add_filter( 'wp_theme_json_data_theme', array( $this, 'filter_theme_json_theme' ) );
}
@ -112,4 +113,76 @@ class ComingSoonRequestHandler {
}
return true;
}
/**
* Filters the theme.json data to add the Inter and Cardo fonts when they don't exist.
*
* @param WP_Theme_JSON $theme_json The theme json object.
*/
public function filter_theme_json_theme( $theme_json ) {
if ( ! Features::is_enabled( 'launch-your-store' ) ) {
return $theme_json;
}
$theme_data = $theme_json->get_data();
$font_data = $theme_data['settings']['typography']['fontFamilies']['theme'];
$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' ),
),
),
),
);
// Loops through all existing fonts and append when the font's name is not found.
foreach ( $fonts_to_add as $font_to_add ) {
$found = false;
foreach ( $font_data as $font ) {
if ( $font['name'] === $font_to_add['name'] ) {
$found = true;
break;
}
}
if ( ! $found ) {
$font_data[] = $font_to_add;
}
}
$new_data = array(
'version' => 1,
'settings' => array(
'typography' => array(
'fontFamilies' => array(
'theme' => $font_data,
),
),
),
);
$theme_json->update_with( $new_data );
return $theme_json;
}
}