From 92f67683f6f10c56746cbf0841e26e0b2c6512a1 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 30 Aug 2024 16:57:33 -0300 Subject: [PATCH] use sub-directories for each font family --- includes/create-theme/theme-fonts.php | 23 ++++++++++++++--------- includes/create-theme/theme-zip.php | 6 ++++-- tests/test-theme-fonts.php | 4 ++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/includes/create-theme/theme-fonts.php b/includes/create-theme/theme-fonts.php index 6b7e1ea7..9e889fd3 100644 --- a/includes/create-theme/theme-fonts.php +++ b/includes/create-theme/theme-fonts.php @@ -120,17 +120,20 @@ 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 @@ -138,17 +141,19 @@ public static function copy_activated_fonts_to_theme() { 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 ); } } } diff --git a/includes/create-theme/theme-zip.php b/includes/create-theme/theme-zip.php index a9495e76..c243be78 100644 --- a/includes/create-theme/theme-zip.php +++ b/includes/create-theme/theme-zip.php @@ -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'] ) ) { @@ -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 ); } } } diff --git a/tests/test-theme-fonts.php b/tests/test-theme-fonts.php index 95eac2ae..00df764c 100644 --- a/tests/test-theme-fonts.php +++ b/tests/test-theme-fonts.php @@ -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 );