Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include activated Fonts on theme zip export functions #564

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function export_child_theme( $theme ) {

$zip = Theme_Zip::copy_theme_to_zip( $zip, null, null );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'current' ) );

$zip->close();

Expand Down Expand Up @@ -144,7 +144,7 @@ function create_sibling_theme( $theme, $screenshot ) {

$zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'current' ) );

// Add readme.txt.
$zip->addFromStringToTheme(
Expand Down Expand Up @@ -213,7 +213,7 @@ function clone_theme( $theme, $screenshot ) {
$zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] );

$zip = Theme_Zip::add_templates_to_zip( $zip, 'all', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'all' ) );

// Add readme.txt.
$zip->addFromStringToTheme(
Expand Down Expand Up @@ -277,7 +277,7 @@ function create_child_theme( $theme, $screenshot ) {
$zip = Theme_Zip::create_zip( $filename );

$zip = Theme_Zip::add_templates_to_zip( $zip, 'user', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'user' );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'user' ) );

// Add readme.txt.
$zip->addFromStringToTheme(
Expand Down Expand Up @@ -321,7 +321,7 @@ function export_theme( $theme ) {

$zip = Theme_Zip::copy_theme_to_zip( $zip, null, null );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'all', null );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'all' ) );

$zip->close();

Expand Down
20 changes: 16 additions & 4 deletions admin/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@ public static function persist_font_settings() {
static::copy_activated_fonts_to_theme();
}

public static function copy_activated_fonts_to_theme() {
public static function get_user_activated_fonts() {
$user_settings = MY_Theme_JSON_Resolver::get_user_data()->get_settings();
if ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) {
return null;
}

return $user_settings['typography']['fontFamilies']['custom'];
}

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 ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) {
return null;
}

$font_families_to_copy = $user_settings['typography']['fontFamilies']['custom'];

// If there are no custom fonts, bounce out
if ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) {
if ( is_null( $font_families_to_copy ) ) {
return;
}

$font_families_to_copy = $user_settings['typography']['fontFamilies']['custom'];
$theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents();

// copy font face assets to theme and change the src to the new location
require_once ABSPATH . 'wp-admin/includes/file.php';
Expand Down
55 changes: 53 additions & 2 deletions admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,65 @@ public static function create_zip( $filename, $theme_slug = null ) {
return $zip;
}

public static function add_theme_json_to_zip( $zip, $export_type ) {
public static function add_theme_json_to_zip( $zip, $theme_json ) {
$zip->addFromStringToTheme(
'theme.json',
MY_Theme_JSON_Resolver::export_theme_data( $export_type )
$theme_json
);
return $zip;
}

public static function add_activated_fonts_to_zip( $zip, $theme_json_string ) {

$theme_json = json_decode( $theme_json_string, true );

$font_families_to_copy = Theme_Fonts::get_user_activated_fonts();
$theme_font_asset_location = '/assets/fonts/';
$font_slugs_to_remove = array();

foreach ( $font_families_to_copy as &$font_family ) {
if ( ! isset( $font_family['fontFace'] ) ) {
continue;
}
$font_slugs_to_remove[] = $font_family['slug'];
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'] ) ) {
$zip->addFileToTheme( $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'] );
$zip->addFileToTheme( $tmp_file, $theme_font_asset_location . '/' . $font_filename );
unlink( $tmp_file );
}

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

if ( ! isset( $theme_json['settings']['typography']['fontFamilies'] ) ) {
$theme_json['settings']['typography']['fontFamilies'] = array();
}

// Remove user fonts that have already been added to the theme_json
// otherwise they will be duplicated when we add them next
foreach ( $theme_json['settings']['typography']['fontFamilies'] as $key => $theme_font_family ) {
if ( in_array( $theme_font_family['slug'], $font_slugs_to_remove, true ) ) {
unset( $theme_json['settings']['typography']['fontFamilies'][ $key ] );
}
}

// Copy user fonts to theme
$theme_json['settings']['typography']['fontFamilies'] = array_merge(
$theme_json['settings']['typography']['fontFamilies'],
$font_families_to_copy
);

return wp_json_encode( $theme_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

}

public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) {

// Get real path for our folder
Expand Down
29 changes: 20 additions & 9 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,14 @@ function rest_export_cloned_theme( $request ) {
// Create ZIP file in the temporary directory.
$filename = tempnam( get_temp_dir(), $theme['slug'] );
$zip = Theme_Zip::create_zip( $filename, $theme['slug'] );
$zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'all', $theme['slug'] );

$zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'all', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' );
//TODO: Should the font persistent be optional?
// If so then the Font Library fonts will need to be removed from the theme.json settings.
$theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'all' );
$theme_json = Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, $theme_json );

// Add readme.txt.
$zip->addFromStringToTheme(
Expand Down Expand Up @@ -316,8 +320,11 @@ function rest_export_child_cloned_theme( $request ) {
$filename = tempnam( get_temp_dir(), $theme['slug'] );
$zip = Theme_Zip::create_zip( $filename, $theme['slug'] );

$zip = Theme_Zip::add_templates_to_zip( $zip, 'user', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'variation' );
//TODO: Should the font persistent be optional?
// If so then the Font Library fonts will need to be removed from the theme.json settings.
$theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'variation' );
$theme_json = Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, $theme_json );

// Add readme.txt.
$zip->addFromStringToTheme(
Expand Down Expand Up @@ -369,13 +376,17 @@ function rest_export_theme( $request ) {
$zip = Theme_Zip::copy_theme_to_zip( $zip, null, null );

if ( is_child_theme() ) {
$zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme_slug );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme_slug );
$theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'current' );
} else {
$zip = Theme_Zip::add_templates_to_zip( $zip, 'all', null );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'all', null );
$theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'all' );
}

$theme_json = Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json );

$zip = Theme_Zip::add_theme_json_to_zip( $zip, $theme_json );

$zip->close();

header( 'Content-Type: application/zip' );
Expand Down
Loading