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
#645)

Co-authored-by: Matias Benedetto <matias.benedetto@gmail.com>
  • Loading branch information
pbking and matiasbenedetto authored May 30, 2024
1 parent 10b9dcb commit 485f893
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
43 changes: 28 additions & 15 deletions includes/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,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 @@ -183,9 +189,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
7 changes: 5 additions & 2 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 @@ -306,7 +306,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 485f893

Please sign in to comment.