Skip to content

Commit

Permalink
Fixed font utilities to work with font sources as an (optional) array.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed May 22, 2024
1 parent 8f03e53 commit a0ea7f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
43 changes: 28 additions & 15 deletions admin/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ public static function copy_activated_fonts_to_theme() {
continue;
}
foreach ( $font_family['fontFace'] as &$font_face ) {
$font_filename = basename( $font_face['src'] );
$font_dir = wp_get_font_dir();
if ( str_contains( $font_face['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_filename );
} else {
// otherwise download it from wherever it is hosted
$tmp_file = download_url( $font_face['src'] );
copy( $tmp_file, $theme_font_asset_location . $font_filename );
unlink( $tmp_file );
// src can be a string or an array
// if it is a string, cast it to an array
if ( ! is_array( $font_face['src'] ) ) {
$font_face['src'] = array( $font_face['src'] );
}
foreach ( $font_face['src'] as $font_src_index => &$font_src ) {
$font_filename = basename( $font_src );
$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_filename );
} else {
// otherwise download it from wherever it is hosted
$tmp_file = download_url( $font_src );
copy( $tmp_file, $theme_font_asset_location . $font_filename );
unlink( $tmp_file );
}
$font_face['src'][ $font_src_index ] = 'file:./assets/fonts/' . $font_filename;
}

$font_face['src'] = 'file:./assets/fonts/' . $font_filename;
}
}

Expand Down Expand Up @@ -114,9 +120,16 @@ function( $theme_font_family ) use ( $font_families_to_not_remove ) {
foreach ( $font_families_to_remove as $font_family ) {
if ( isset( $font_family['fontFace'] ) ) {
foreach ( $font_family['fontFace'] as $font_face ) {
$font_filename = basename( $font_face['src'] );
if ( file_exists( $theme_font_asset_location . $font_filename ) ) {
unlink( $theme_font_asset_location . $font_filename );
// src can be a string or an array
// if it is a string, cast it to an array
if ( ! is_array( $font_face['src'] ) ) {
$font_face['src'] = array( $font_face['src'] );
}
foreach ( $font_face['src'] as $font_src ) {
$font_filename = basename( $font_src );
if ( file_exists( $theme_font_asset_location . $font_filename ) ) {
unlink( $theme_font_asset_location . $font_filename );
}
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions tests/test-theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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-normal-400.ttf', $theme_data_after['typography']['fontFamilies']['theme'][1]['fontFace'][0]['src'] );
$this->assertEquals( 'file:./assets/fonts/open-sans-normal-400.ttf', $theme_data_after['typography']['fontFamilies']['theme'][1]['fontFace'][0]['src'][0] );
$this->assertTrue( file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' ) );

$this->uninstall_theme( $test_theme_slug );
Expand Down Expand Up @@ -103,10 +103,6 @@ public function test_remove_deactivated_fonts_from_theme() {

private function save_theme() {
CBT_Theme_Fonts::persist_font_settings();
// CBT_Theme_Templates::add_templates_to_local( 'all' );
// CBT_Theme_JSON::add_theme_json_to_local( 'all' );
// CBT_Theme_Styles::clear_user_styles_customizations();
// CBT_Theme_Templates::clear_user_templates_customizations();
}

private function create_blank_theme() {
Expand Down Expand Up @@ -197,7 +193,10 @@ private function activate_font_in_theme_and_override_in_user() {
'fontFamily' => 'Open Sans',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => 'file:./assets/fonts/open-sans-normal-400.ttf',
'src' => array(
'file:./assets/fonts/open-sans-normal-400.ttf',
'file:./assets/fonts/open-sans-normal-400.ttf',
),
),
),
);
Expand Down

0 comments on commit a0ea7f4

Please sign in to comment.