Skip to content

Commit

Permalink
copy font assets to the local theme folder when creating a style vari…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
matiasbenedetto committed Aug 30, 2024
1 parent 0094043 commit 3c08b3c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 28 deletions.
9 changes: 8 additions & 1 deletion includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,15 @@ function rest_create_child_theme( $request ) {
}

function rest_create_variation( $request ) {
$options = $request->get_params();

$save_fonts = isset( $options['saveFonts'] ) && true === $options['saveFonts'];

$response = CBT_Theme_JSON::add_theme_json_variation_to_local( 'variation', $this->sanitize_theme_data( $request->get_params() ) );
$response = CBT_Theme_JSON::add_theme_json_variation_to_local(
'variation',
$this->sanitize_theme_data( $options ),
$save_fonts
);

wp_cache_flush();

Expand Down
35 changes: 28 additions & 7 deletions includes/create-theme/resolver_additions.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
}

// Merge the User Data
if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
$theme->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );
} else {
$theme->merge( static::get_user_data() );
}
$theme->merge( static::get_user_data() );

// Merge the extra theme data received as a parameter
if ( ! empty( $extra_theme_data ) ) {
Expand All @@ -93,8 +89,33 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
$schema = 'https://schemas.wp.org/' . $theme_json_version . '/theme.json';
}
$data['$schema'] = $schema;
$theme_json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json );
return static::stringify( $data );
}

/**
* Get the user data.
*
* This is a copy of the parent function with the addition of the Gutenberg resolver.
*
* @return array
*/
public static function get_user_data() {
// Determine the correct method to retrieve user data
return class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' )
? WP_Theme_JSON_Resolver_Gutenberg::get_user_data()
: parent::get_user_data();
}

/**
* Stringify the array data.
*
* $data is an array of data to be converted to a JSON string.
* @return string JSON string.
*/
public static function stringify( $data ) {
$data = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
// Convert spaces to tabs
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $data );
}

public static function get_theme_file_contents() {
Expand Down
33 changes: 23 additions & 10 deletions includes/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ public static function get_user_activated_fonts() {
return $user_settings['typography']['fontFamilies']['custom'] ?? null;
}

public static function copy_activated_fonts_to_theme() {
$font_families_to_copy = self::get_user_activated_fonts();

if ( is_null( $font_families_to_copy ) ) {
return;
}

$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
/*
* Copy the font assets to the theme.
*
* @param array $font_families The font families to copy.
* @return array $font_families The font families with the font face src updated to the theme font asset location.
*/
public static function copy_font_assets_to_theme( $font_families ) {
$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 );
}

foreach ( $font_families_to_copy as &$font_family ) {
foreach ( $font_families as &$font_family ) {
if ( ! isset( $font_family['fontFace'] ) ) {
continue;
}
Expand All @@ -124,9 +124,22 @@ public static function copy_activated_fonts_to_theme() {
}
}

return $font_families;
}

public static function copy_activated_fonts_to_theme() {
$font_families_to_copy = self::get_user_activated_fonts();

if ( is_null( $font_families_to_copy ) ) {
return;
}

$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
$copied_font_families = self::copy_font_assets_to_theme( $font_families_to_copy );

$theme_json['settings']['typography']['fontFamilies'] = array_merge(
$theme_json['settings']['typography']['fontFamilies'] ?? array(),
$font_families_to_copy
$copied_font_families
);

$user_settings = CBT_Theme_JSON_Resolver::get_user_data()->get_settings();
Expand Down
30 changes: 20 additions & 10 deletions includes/create-theme/theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function add_theme_json_to_local( $export_type ) {
);
}

public static function add_theme_json_variation_to_local( $export_type, $theme ) {
public static function add_theme_json_variation_to_local( $export_type, $theme, $save_fonts = false ) {
$variation_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR;

if ( ! file_exists( $variation_path ) ) {
Expand All @@ -20,18 +20,28 @@ public static function add_theme_json_variation_to_local( $export_type, $theme )
return new WP_Error( 'variation_already_exists', __( 'Variation already exists.', 'create-block-theme' ) );
}

$_POST['theme']['variation_slug'] = $theme['slug'];

$extra_theme_data = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'title' => $theme['name'],
);

$variation_theme_json = CBT_Theme_JSON_Resolver::export_theme_data( $export_type, $extra_theme_data );
$theme_json = class_exists( 'WP_Theme_JSON_Gutenberg' ) ? new WP_Theme_JSON_Gutenberg() : new WP_Theme_JSON();
$user_data = CBT_Theme_JSON_Resolver::get_user_data();
$theme_json->merge( $user_data );
$variation = $theme_json->get_data();
$variation['title'] = $theme['name'];

if (
$save_fonts &&
isset( $variation['settings']['typography']['fontFamilies'] )
) {
$font_families = $variation['settings']['typography']['fontFamilies'];
// Copy the font assets to the theme assets folder.
$copied_font_families = CBT_Theme_Fonts::copy_font_assets_to_theme( $font_families );
// Update the the variation theme json with the font families with the new paths.
$variation['settings']['typography']['fontFamilies'] = $copied_font_families;
}

file_put_contents(
$variation_path . $theme['slug'] . '.json',
$variation_theme_json
CBT_Theme_JSON_Resolver::stringify( $variation )
);

return $variation;
}
}

0 comments on commit 3c08b3c

Please sign in to comment.