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

Remove font management #595

Merged
merged 11 commits into from
Apr 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Returned the font management portion still needed
pbking committed Apr 25, 2024
commit 03d08b16bb6d34d4002bbba03d8abd47746e2995
1 change: 1 addition & 0 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
require_once __DIR__ . '/create-theme/theme-readme.php';
require_once __DIR__ . '/create-theme/theme-form.php';
require_once __DIR__ . '/create-theme/form-messages.php';
require_once __DIR__ . '/create-theme/theme-fonts.php';
require_once __DIR__ . '/create-theme/theme-create.php';
/**
* The admin-specific functionality of the plugin.
148 changes: 148 additions & 0 deletions admin/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?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 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();
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 ( is_null( $font_families_to_copy ) ) {
return;
}

$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';
$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 );
}

}
Binary file added tests/data/fonts/OpenSans-Regular.otf
Binary file not shown.
Binary file added tests/data/fonts/OpenSans-Regular.ttf
Binary file not shown.
Binary file added tests/data/fonts/OpenSans-Regular.woff
Binary file not shown.
Binary file added tests/data/fonts/OpenSans-Regular.woff2
Binary file not shown.
216 changes: 216 additions & 0 deletions tests/test-theme-fonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php
/**
* Class WP_Create_Block_Theme_Admin
*
* @package Create_Block_Theme
*/
class Test_Create_Block_Theme_Fonts extends WP_UnitTestCase {

protected static $admin_id;
protected static $editor_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
self::$editor_id = $factory->user->create(
array(
'role' => 'editor',
)
);
}


public function test_copy_activated_fonts_to_theme() {

wp_set_current_user( self::$admin_id );

$user_data_begin = MY_Theme_JSON_Resolver::get_user_data()->get_settings();

$test_theme_slug = $this->create_blank_theme();

$this->activate_user_font();

$user_data_before = MY_Theme_JSON_Resolver::get_user_data()->get_settings();
$theme_data_before = MY_Theme_JSON_Resolver::get_theme_data()->get_settings();

$this->save_theme();

$user_data_after = MY_Theme_JSON_Resolver::get_user_data()->get_settings();
$theme_data_after = MY_Theme_JSON_Resolver::get_theme_data()->get_settings();

// ensure that the font was added and then removed from user space
$this->assertarraynothaskey( 'typography', $user_data_begin );
$this->assertequals( 'open-sans', $user_data_before['typography']['fontFamilies']['custom'][0]['slug'] );
$this->assertarraynothaskey( 'typography', $user_data_after );

// Ensure that the font was added to the theme
$this->assertCount( 1, $theme_data_before['typography']['fontFamilies']['theme'] );
$this->assertCount( 2, $theme_data_after['typography']['fontFamilies']['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->assertTrue( file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' ) );

$this->uninstall_theme( $test_theme_slug );

}

public function test_remove_deactivated_fonts_from_theme() {
wp_set_current_user( self::$admin_id );

$test_theme_slug = $this->create_blank_theme();

$this->activate_font_in_theme_and_override_in_user();

$user_data_before = MY_Theme_JSON_Resolver::get_user_data()->get_settings();
$theme_data_before = MY_Theme_JSON_Resolver::get_theme_data()->get_settings();
$merged_data_before = MY_Theme_JSON_Resolver::get_merged_data()->get_settings();
$theme_file_exists_before = file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' );

$this->save_theme();

$user_data_after = MY_Theme_JSON_Resolver::get_user_data()->get_settings();
$theme_data_after = MY_Theme_JSON_Resolver::get_theme_data()->get_settings();
$merged_data_after = MY_Theme_JSON_Resolver::get_merged_data()->get_settings();
$theme_file_exists_after = file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' );

// ensure that the font was added to the theme settings and removed in user settings and therefore missing in merged settings
$this->assertCount( 2, $theme_data_before['typography']['fontFamilies']['theme'] );
$this->assertequals( 'open-sans', $theme_data_before['typography']['fontFamilies']['theme'][1]['slug'] );
$this->assertCount( 1, $user_data_before['typography']['fontFamilies']['theme'] );
$this->assertnotequals( 'open-sans', $user_data_before['typography']['fontFamilies']['theme'][0]['slug'] );
$this->assertCount( 1, $merged_data_before['typography']['fontFamilies']['theme'] );
$this->assertnotequals( 'open-sans', $merged_data_before['typography']['fontFamilies']['theme'][0]['slug'] );

// ensure that the font was removed from the user settings and removed from the theme settings and therefore missing in merged settings
$this->assertCount( 1, $theme_data_after['typography']['fontFamilies']['theme'] );
$this->assertnotequals( 'open-sans', $theme_data_after['typography']['fontFamilies']['theme'][0]['slug'] );
$this->assertarraynothaskey( 'typography', $user_data_after );
$this->assertnotequals( 'open-sans', $theme_data_after['typography']['fontFamilies']['theme'][0]['slug'] );
$this->assertCount( 1, $merged_data_after['typography']['fontFamilies']['theme'] );
$this->assertnotequals( 'open-sans', $merged_data_after['typography']['fontFamilies']['theme'][0]['slug'] );

// ensure that the file resource was removed
$this->assertTrue( $theme_file_exists_before );
$this->assertFalse( $theme_file_exists_after );

$this->uninstall_theme( $test_theme_slug );
}

private function save_theme() {
Theme_Fonts::persist_font_settings();
// Theme_Templates::add_templates_to_local( 'all' );
// Theme_Json::add_theme_json_to_local( 'all' );
// Theme_Styles::clear_user_styles_customizations();
// Theme_Templates::clear_user_templates_customizations();
}

private function create_blank_theme() {

$test_theme_slug = 'cbttesttheme';

delete_theme( $test_theme_slug );

$request = new WP_REST_Request( 'POST', '/create-block-theme/v1/create-blank' );
$request->set_param( 'name', $test_theme_slug );
$request->set_param( 'description', '' );
$request->set_param( 'uri', '' );
$request->set_param( 'author', '' );
$request->set_param( 'author_uri', '' );
$request->set_param( 'tags_custom', '' );
$request->set_param( 'subfolder', '' );
$request->set_param( 'recommended_plugins', '' );

rest_do_request( $request );

MY_Theme_JSON_Resolver::clean_cached_data();

return $test_theme_slug;
}

private function uninstall_theme( $theme_slug ) {
MY_Theme_JSON_Resolver::write_user_settings( array() );
delete_theme( $theme_slug );
}

private function activate_user_font() {

$font_dir = wp_get_font_dir();
$font_test_url = $font_dir['url'] . '/open-sans-normal-400.ttf';
$font_test_source = __DIR__ . '/data/fonts/OpenSans-Regular.ttf';
$font_test_destination = $font_dir['path'] . '/open-sans-normal-400.ttf';

if ( ! file_exists( $font_dir['path'] ) ) {
mkdir( $font_dir['path'] );
}
copy( $font_test_source, $font_test_destination );

$settings = array();
$settings['typography']['fontFamilies']['custom'] = array(
array(
'slug' => 'open-sans',
'name' => 'Open Sans',
'fontFamily' => 'Open Sans',
'fontFace' => array(
array(
'fontFamily' => 'Open Sans',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => $font_test_url,
),
),
),
);

MY_Theme_JSON_Resolver::write_user_settings( $settings );
}

private function activate_font_in_theme_and_override_in_user() {

// Copy the font asset
$font_dir = get_stylesheet_directory() . '/assets/fonts/';
$font_test_source = __DIR__ . '/data/fonts/OpenSans-Regular.ttf';
$font_test_destination = $font_dir . '/open-sans-normal-400.ttf';

if ( ! file_exists( get_stylesheet_directory() . '/assets/' ) ) {
mkdir( get_stylesheet_directory() . '/assets/' );
}
if ( ! file_exists( get_stylesheet_directory() . '/assets/fonts/' ) ) {
mkdir( get_stylesheet_directory() . '/assets/fonts/' );
}

copy( $font_test_source, $font_test_destination );

// Add the font to the theme
$theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents();
$theme_original_font_families = $theme_json['settings']['typography']['fontFamilies'];
$theme_json['settings']['typography']['fontFamilies'][] = array(
'slug' => 'open-sans',
'name' => 'Open Sans',
'fontFamily' => 'Open Sans',
'fontFace' => array(
array(
'fontFamily' => 'Open Sans',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => 'file:./assets/fonts/open-sans-normal-400.ttf',
),
),
);
MY_Theme_JSON_Resolver::write_theme_file_contents( $theme_json );

// Deactivate the test font in the theme. To do this the 'theme' collection
// is overwritten to declare the intention of having it gone.
// Here we're writing the font family settings as they existed BEFORE we added
// the test font family to the theme.
$settings = array();
$settings['typography']['fontFamilies']['theme'] = $theme_original_font_families;
MY_Theme_JSON_Resolver::write_user_settings( $settings );
}


}