diff --git a/includes/create-theme/theme-fonts.php b/includes/create-theme/theme-fonts.php index e2539c1d..0e403b69 100644 --- a/includes/create-theme/theme-fonts.php +++ b/includes/create-theme/theme-fonts.php @@ -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; } } @@ -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 ); + } } } } diff --git a/tests/test-theme-fonts.php b/tests/test-theme-fonts.php index e1a3aeff..24046f53 100644 --- a/tests/test-theme-fonts.php +++ b/tests/test-theme-fonts.php @@ -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 ); @@ -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', + ), ), ), );