Skip to content

Commit

Permalink
Use CORE for Font Management (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Vicente Canales <vicente.canales@automattic.com>
  • Loading branch information
pbking and vcanales committed Mar 22, 2024
1 parent 1545d48 commit 99a35c5
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ jobs:
- name: Run PHP tests
run: |
mysql -uroot -h127.0.0.1 -e 'SELECT version()' \
&& ./bin/install-wp-tests.sh --recreate-db wordpress_test root '' > /dev/null \
&& ./bin/install-wp-tests.sh --wp-version=trunk --recreate-db wordpress_test root '' > /dev/null \
&& composer run-script test
1 change: 1 addition & 0 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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';

/**
* The admin-specific functionality of the plugin.
Expand Down
136 changes: 136 additions & 0 deletions admin/create-theme/theme-fonts.php
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 );
}

}
32 changes: 32 additions & 0 deletions admin/resolver_additions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
$theme->merge( $theme_theme );
}

// Merge the User Data
if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
$theme->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );
} else {
Expand Down Expand Up @@ -97,6 +98,37 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {

}

public static function get_theme_file_contents() {
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
return $theme_json_data;
}

public static function write_theme_file_contents( $theme_json_data ) {
$theme_json = wp_json_encode( $theme_json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
file_put_contents( static::get_file_path_from_theme( 'theme.json' ), $theme_json );
static::clean_cached_data();
}

public static function write_user_settings( $user_settings ) {
$global_styles_id = static::get_user_global_styles_post_id();
$request = new WP_REST_Request( 'POST', '/wp/v2/global-styles/' . $global_styles_id );
$request->set_param( 'settings', $user_settings );
rest_do_request( $request );
static::clean_cached_data();
}

public static function clean_cached_data() {
parent::clean_cached_data();

if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
}

//TODO: Clearing the cache should clear this too.
// Does this clear the Gutenberg equivalent?
static::$theme_json_file_cache = array();
}

}
}

Expand Down
8 changes: 6 additions & 2 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Forked from https://github.com/wp-cli/scaffold-command/blob/main/features/install-wp-tests.feature
# Function to display usage instructions
display_usage() {
echo "Usage: $0 [options] <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
echo "Usage: $0 [options] <db-name> <db-user> <db-pass> [db-host] [skip-database-creation]"
echo "Options:"
echo " --recreate-db Recreate the database"
echo " --wp-version The WordPress version to install. Default is 'latest'."
echo " --help Displays this help message"
}

Expand All @@ -13,6 +14,10 @@ RECREATE_DB=0
for arg in "$@"
do
case $arg in
--wp-version=*)
WP_VERSION="${arg#*=}"
shift
;;
--recreate-db)
RECREATE_DB=1
shift
Expand Down Expand Up @@ -40,7 +45,6 @@ DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-127.0.0.1}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-false}

TMPDIR=${TMPDIR-/tmp}
Expand Down
4 changes: 4 additions & 0 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ function rest_update_theme( $request ) {
* Save the user changes to the theme and clear user changes.
*/
function rest_save_theme( $request ) {

Theme_Fonts::persist_font_settings();

if ( is_child_theme() ) {
Theme_Templates::add_templates_to_local( 'current' );
Theme_Json::add_theme_json_to_local( 'current' );
Expand Down Expand Up @@ -471,6 +474,7 @@ function relocate_theme( $new_theme_subfolder ) {
);

function is_valid_screenshot( $file ) {
return 0;
$filetype = wp_check_filetype( $file['name'], self::ALLOWED_SCREENSHOT_TYPES );
if ( is_uploaded_file( $file['tmp_name'] ) && in_array( $filetype['type'], self::ALLOWED_SCREENSHOT_TYPES, true ) && $file['size'] < 2097152 ) {
return 1;
Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
=== Create Block Theme ===
Contributors: wordpressdotorg, mikachan, onemaggie, pbking, scruffian, mmaattiiaass, jffng, madhudollu, egregor, vcanales, jeffikus, cwhitmore
Tags: themes, theme, block-theme
Requires at least: 6.0
Tested up to: 6.4
Requires at least: 6.5
Tested up to: 6.5
Stable tag: 1.13.8
Requires PHP: 7.4
License: GPLv2 or later
Expand Down
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

define( 'DIR_TESTDATA', __DIR__ . '/data' );

// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
Expand Down
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.
Loading

0 comments on commit 99a35c5

Please sign in to comment.