diff --git a/classes/class-custom-typekit-fonts-render.php b/classes/class-custom-typekit-fonts-render.php index 4b6f42e..dcd63a0 100644 --- a/classes/class-custom-typekit-fonts-render.php +++ b/classes/class-custom-typekit-fonts-render.php @@ -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. @@ -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. diff --git a/classes/class-custom-typekit-fonts.php b/classes/class-custom-typekit-fonts.php index 6f09dd3..f5530f4 100644 --- a/classes/class-custom-typekit-fonts.php +++ b/classes/class-custom-typekit-fonts.php @@ -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'] ); @@ -131,6 +130,7 @@ 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 ) { @@ -138,9 +138,10 @@ public function get_custom_typekit_details( $kit_id ) { $variations = str_split( $variation ); switch ( $variations[0] ) { - case 'n': - $style = 'normal'; + case 'i': + $style = 'italic'; break; + case 'n': default: $style = 'normal'; break; @@ -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'];