Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions classes/class-custom-typekit-fonts-render.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function __construct() {
// Elementor page builder.
add_filter( 'elementor/fonts/groups', array( $this, 'elementor_group' ) );
add_filter( 'elementor/fonts/additional_fonts', array( $this, 'add_elementor_fonts' ) );
// Block Editor
add_filter( 'wp_theme_json_data_theme', array( $this, 'add_block_editor_fonts' ) );

add_action( 'enqueue_block_editor_assets', array( $this, 'typekit_embed_css' ) );
// Astra filter before creating google fonts URL.
Expand Down Expand Up @@ -236,6 +238,52 @@ public function bb_custom_fonts( $bb_fonts ) {

return array_merge( $bb_fonts, $custom_fonts );
}

/**
* Add Adobe Fonts to block editor options
*
* @since x.y.z
* @param WP_Theme_JSON_Data $theme_json theme.json data from the theme.
*/
public function add_block_editor_fonts( $theme_json ) {
$kit_info = get_option( 'custom-typekit-fonts' );
$fonts = $kit_info['custom-typekit-font-details'];
if ( empty( $fonts ) ) {
return $theme_json;
}
$new_data = [
'version' => 3,
'settings' => [
'typography' => [
'fontFamilies' => [],
],
],
];
foreach ( $fonts as $font_family_name => $font ) {
$font_definition = [
'fontFamily' => $font['fallback'],
'name' => $font['family'],
'slug' => $font['slug'] ?? explode(',', $font['fallback'])[0],
];

if(!empty($font['variations'])) {
$font_definition['fontFace'] = [];
foreach($font['variations'] as $style => $weights) {
foreach($weights as $weight) {
$font_definition['fontFace'][] = [
'fontFamily' => $font['family'],
'fontStyle' => $style,
'fontWeight' => $weight,
];
}
}
}

$new_data['settings']['typography']['fontFamilies'][] = $font_definition;
}

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

/**
* Remove Typekit Font from Google Font URL.
Expand Down
9 changes: 6 additions & 3 deletions classes/class-custom-typekit-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public function get_custom_typekit_details( $kit_id ) {

$data = json_decode( wp_remote_retrieve_body( $response ), true );
$families = $data['kit']['families'];

foreach ( $families as $family ) {

$family_name = str_replace( ' ', '-', $family['name'] );
Expand All @@ -131,16 +130,18 @@ public function get_custom_typekit_details( $kit_id ) {
'family' => $family_name,
'fallback' => str_replace( '"', '', $family['css_stack'] ),
'weights' => array(),
'variations' => array(),
);

foreach ( $family['variations'] as $variation ) {

$variations = str_split( $variation );

switch ( $variations[0] ) {
case 'n':
$style = 'normal';
case 'i':
$style = 'italic';
break;
case 'n':
default:
$style = 'normal';
break;
Expand All @@ -151,6 +152,8 @@ public function get_custom_typekit_details( $kit_id ) {
if ( ! in_array( $weight, $typekit_info[ $family_name ]['weights'] ) ) {
$typekit_info[ $family_name ]['weights'][] = $weight;
}

$typekit_info[ $family_name ]['variations'][ $style ][] = $weight;
}

$typekit_info[ $family_name ]['slug'] = $family['slug'];
Expand Down