-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Vicente Canales <vicente.canales@automattic.com>
- Loading branch information
Showing
13 changed files
with
399 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
|
||
class Theme_Fonts { | ||
|
||
/** | ||
* Copy any ACTIVATED fonts from USER configuration to THEME configuration including any font face assets. | ||
* Remove any DEACTIVATED fronts from the THEME configuration. | ||
*/ | ||
public static function persist_font_settings() { | ||
static::remove_deactivated_fonts_from_theme(); | ||
static::copy_activated_fonts_to_theme(); | ||
} | ||
|
||
public static function copy_activated_fonts_to_theme() { | ||
|
||
$user_settings = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); | ||
$theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents(); | ||
|
||
// If there are no custom fonts, bounce out | ||
if ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) { | ||
return; | ||
} | ||
|
||
$font_families_to_copy = $user_settings['typography']['fontFamilies']['custom']; | ||
|
||
// copy font face assets to theme and change the src to the new location | ||
require_once ABSPATH . 'wp-admin/includes/file.php'; | ||
$theme_font_asset_location = get_stylesheet_directory() . '/assets/fonts/'; | ||
if ( ! file_exists( $theme_font_asset_location ) ) { | ||
mkdir( $theme_font_asset_location, 0777, true ); | ||
} | ||
foreach ( $font_families_to_copy as &$font_family ) { | ||
if ( ! isset( $font_family['fontFace'] ) ) { | ||
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 ); | ||
} | ||
|
||
$font_face['src'] = 'file:./assets/fonts/' . $font_filename; | ||
} | ||
} | ||
|
||
// Copy user fonts to theme | ||
if ( ! isset( $theme_json['settings']['typography']['fontFamilies'] ) ) { | ||
$theme_json['settings']['typography']['fontFamilies'] = array(); | ||
} | ||
$theme_json['settings']['typography']['fontFamilies'] = array_merge( | ||
$theme_json['settings']['typography']['fontFamilies'], | ||
$font_families_to_copy | ||
); | ||
|
||
// Remove user fonts | ||
unset( $user_settings['typography']['fontFamilies']['custom'] ); | ||
if ( empty( $user_settings['typography']['fontFamilies'] ) ) { | ||
unset( $user_settings['typography']['fontFamilies'] ); | ||
} | ||
if ( empty( $user_settings['typography'] ) ) { | ||
unset( $user_settings['typography'] ); | ||
} | ||
|
||
// Update the user settings | ||
MY_Theme_JSON_Resolver::write_user_settings( $user_settings ); | ||
|
||
// Update theme.json | ||
MY_Theme_JSON_Resolver::write_theme_file_contents( $theme_json ); | ||
|
||
} | ||
|
||
public static function remove_deactivated_fonts_from_theme() { | ||
|
||
$user_settings = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); | ||
$theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents(); | ||
|
||
// If there are no deactivated theme fonts, bounce out | ||
if ( ! isset( $user_settings['typography']['fontFamilies']['theme'] ) ) { | ||
return; | ||
} | ||
|
||
$font_families_to_not_remove = $user_settings['typography']['fontFamilies']['theme']; | ||
|
||
// Remove font assets from theme | ||
$theme_font_asset_location = get_stylesheet_directory() . '/assets/fonts/'; | ||
$font_families_to_remove = array_values( | ||
array_filter( | ||
$theme_json['settings']['typography']['fontFamilies'], | ||
function( $theme_font_family ) use ( $font_families_to_not_remove ) { | ||
return ! in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); | ||
} | ||
) | ||
); | ||
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 ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Remove user fonts from theme | ||
$theme_json['settings']['typography']['fontFamilies'] = array_values( | ||
array_filter( | ||
$theme_json['settings']['typography']['fontFamilies'], | ||
function( $theme_font_family ) use ( $font_families_to_not_remove ) { | ||
return in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); | ||
} | ||
) | ||
); | ||
MY_Theme_JSON_Resolver::write_theme_file_contents( $theme_json ); | ||
|
||
// Remove user preferences for theme font activation | ||
unset( $user_settings['typography']['fontFamilies']['theme'] ); | ||
if ( empty( $user_settings['typography']['fontFamilies'] ) ) { | ||
unset( $user_settings['typography']['fontFamilies'] ); | ||
} | ||
if ( empty( $user_settings['typography'] ) ) { | ||
unset( $user_settings['typography'] ); | ||
} | ||
|
||
MY_Theme_JSON_Resolver::write_user_settings( $user_settings ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.