Skip to content

Commit

Permalink
use sub-directories for each font family
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Aug 30, 2024
1 parent 128d363 commit 92f6768
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
23 changes: 14 additions & 9 deletions includes/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,40 @@ public static function copy_activated_fonts_to_theme() {
}

$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
$theme_font_asset_location = get_stylesheet_directory() . '/assets/fonts/';

require_once ABSPATH . 'wp-admin/includes/file.php';
if ( ! file_exists( $theme_font_asset_location ) ) {
mkdir( $theme_font_asset_location, 0777, true );
}
$theme_font_asset_location = path_join( get_stylesheet_directory(), 'assets/fonts/' );
// Create the font asset directory if it does not exist.
wp_mkdir_p( $theme_font_asset_location );

foreach ( $font_families_to_copy as &$font_family ) {
if ( ! isset( $font_family['fontFace'] ) ) {
continue;
}

$font_family_dir_name = sanitize_title( $font_family['name'] );
$font_family_dir_path = path_join( $theme_font_asset_location, $font_family_dir_name );
// Crete a font family specific directory if it does not exist.
wp_mkdir_p( $font_family_dir_path );

foreach ( $font_family['fontFace'] as &$font_face ) {
// src can be a string or an array
// if it is a string, cast it to an array
$font_face['src'] = (array) $font_face['src'];
foreach ( $font_face['src'] as $font_src_index => &$font_src ) {
$font_filename = basename( $font_src );
$font_pretty_filename = self::make_filename_from_fontface( $font_face, $font_src, $font_src_index );
$font_face_path = path_join( $font_family_dir_path, $font_pretty_filename );
$font_dir = wp_get_font_dir();
if ( str_contains( $font_src, $font_dir['url'] ) ) {
// If the file is hosted on this server then copy it to the theme
copy( $font_dir['path'] . '/' . $font_filename, $theme_font_asset_location . '/' . $font_pretty_filename );
copy( path_join( $font_dir['path'], $font_filename ), $font_face_path );
} else {
// otherwise download it from wherever it is hosted
$tmp_file = download_url( $font_src );
copy( $tmp_file, $theme_font_asset_location . $font_pretty_filename );
copy( $tmp_file, $font_face_path );
unlink( $tmp_file );
}
$font_face['src'][ $font_src_index ] = 'file:./assets/fonts/' . $font_pretty_filename;
$font_face_family_path = path_join( $font_family_dir_name, $font_pretty_filename );
$font_face['src'][ $font_src_index ] = path_join( 'file:./assets/fonts/', $font_face_family_path );
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions includes/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public static function add_activated_fonts_to_zip( $zip, $theme_json_string ) {
foreach ( $font_face['src'] as $font_src_index => &$font_src ) {
$font_filename = basename( $font_src );
$font_pretty_filename = CBT_Theme_Fonts::make_filename_from_fontface( $font_face, $font_src, $font_src_index );
$font_face_path = path_join( $theme_font_asset_location, $font_pretty_filename );
$font_family_dir_name = sanitize_title( $font_family['name'] );
$font_family_dir_path = path_join( $theme_font_asset_location, $font_family_dir_name );
$font_face_path = path_join( $font_family_dir_path, $font_pretty_filename );

$font_dir = wp_get_font_dir();
if ( str_contains( $font_src, $font_dir['url'] ) ) {
Expand All @@ -64,7 +66,7 @@ public static function add_activated_fonts_to_zip( $zip, $theme_json_string ) {
$zip->addFileToTheme( $tmp_file, $font_face_path );
unlink( $tmp_file );
}
$font_face['src'][ $font_src_index ] = 'file:./assets/fonts/' . $font_pretty_filename;
$font_face['src'][ $font_src_index ] = 'file:./assets/fonts/' . path_join( $font_family_dir_name, $font_pretty_filename );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test-theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function test_copy_activated_fonts_to_theme() {
$this->assertEquals( 'open-sans', $theme_data_after['typography']['fontFamilies']['theme'][1]['slug'] );

// Ensure that the URL was changed to a local file and that it was copied to where it should be
$this->assertEquals( 'file:./assets/fonts/open-sans-400-normal.ttf', $theme_data_after['typography']['fontFamilies']['theme'][1]['fontFace'][0]['src'][0] );
$this->assertTrue( file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-400-normal.ttf' ) );
$this->assertEquals( 'file:./assets/fonts/open-sans/open-sans-400-normal.ttf', $theme_data_after['typography']['fontFamilies']['theme'][1]['fontFace'][0]['src'][0] );
$this->assertTrue( file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans/open-sans-400-normal.ttf' ) );

$this->uninstall_theme( $test_theme_slug );

Expand Down

0 comments on commit 92f6768

Please sign in to comment.