Skip to content

Commit

Permalink
move function to compat/wordpress-6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Dec 8, 2021
1 parent 7ad9c91 commit c5832b3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
57 changes: 57 additions & 0 deletions lib/compat/wordpress-6.0/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,61 @@
* @package gutenberg
*/

/**
* Register webfonts defined in theme.json.
*/
function gutenberg_register_webfonts_from_theme_json() {
// Get settings from theme.json.
$theme_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings();

// Bail out early if there are no settings for webfonts.
if ( empty( $theme_settings['typography'] ) || empty( $theme_settings['typography']['fontFamilies'] ) ) {
return;
}

$webfonts = array();

// Look for fontFamilies.
foreach ( $theme_settings['typography']['fontFamilies'] as $font_families ) {
foreach ( $font_families as $font_family ) {

// Skip if fontFace is not defined.
if ( empty( $font_family['fontFace'] ) ) {
continue;
}

$font_family['fontFace'] = (array) $font_family['fontFace'];

foreach ( $font_family['fontFace'] as $font_face ) {
// Check if webfonts have a "src" param, and if they do account for the use of "file:./".
if ( ! empty( $font_face['src'] ) ) {
$font_face['src'] = (array) $font_face['src'];

foreach ( $font_face['src'] as $src_key => $url ) {
// Tweak the URL to be relative to the theme root.
if ( 0 !== strpos( $url, 'file:./' ) ) {
continue;
}
$font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
}
}

// Convert keys to kebab-case.
foreach ( $font_face as $property => $value ) {
$kebab_case = _wp_to_kebab_case( $property );
$font_face[ $kebab_case ] = $value;
if ( $kebab_case !== $property ) {
unset( $font_face[ $property ] );
}
}

$webfonts[] = $font_face;
}
}
}
wp_register_webfonts( $webfonts );
}

/**
* Add missing fonts data to the global styles.
*
Expand Down Expand Up @@ -76,3 +131,5 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) {

return $data;
}

add_action( 'wp_loaded', 'gutenberg_register_webfonts_from_theme_json' );
56 changes: 0 additions & 56 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,61 +288,6 @@ function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $
return ! ! preg_match( '/^var\(--wp-[a-zA-Z0-9\-]+\)$/', trim( $parts[1] ) );
}

/**
* Register webfonts defined in theme.json.
*/
function gutenberg_register_webfonts_from_theme_json() {
// Get settings from theme.json.
$theme_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings();

// Bail out early if there are no settings for webfonts.
if ( empty( $theme_settings['typography'] ) || empty( $theme_settings['typography']['fontFamilies'] ) ) {
return;
}

$webfonts = array();

// Look for fontFamilies.
foreach ( $theme_settings['typography']['fontFamilies'] as $font_families ) {
foreach ( $font_families as $font_family ) {

// Skip if fontFace is not defined.
if ( empty( $font_family['fontFace'] ) ) {
continue;
}

$font_family['fontFace'] = (array) $font_family['fontFace'];

foreach ( $font_family['fontFace'] as $font_face ) {
// Check if webfonts have a "src" param, and if they do account for the use of "file:./".
if ( ! empty( $font_face['src'] ) ) {
$font_face['src'] = (array) $font_face['src'];

foreach ( $font_face['src'] as $src_key => $url ) {
// Tweak the URL to be relative to the theme root.
if ( 0 !== strpos( $url, 'file:./' ) ) {
continue;
}
$font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
}
}

// Convert keys to kebab-case.
foreach ( $font_face as $property => $value ) {
$kebab_case = _wp_to_kebab_case( $property );
$font_face[ $kebab_case ] = $value;
if ( $kebab_case !== $property ) {
unset( $font_face[ $property ] );
}
}

$webfonts[] = $font_face;
}
}
}
wp_register_webfonts( $webfonts );
}

// The else clause can be removed when plugin support requires WordPress 5.8.0+.
if ( function_exists( 'get_block_editor_settings' ) ) {
add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
Expand All @@ -352,7 +297,6 @@ function gutenberg_register_webfonts_from_theme_json() {

add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' );
add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' );
add_action( 'wp_loaded', 'gutenberg_register_webfonts_from_theme_json' );

// kses actions&filters.
add_action( 'init', 'gutenberg_global_styles_kses_init' );
Expand Down

0 comments on commit c5832b3

Please sign in to comment.