Skip to content

Commit

Permalink
Extract utility functions into their own help files
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Apr 7, 2022
1 parent d124318 commit 10e9964
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 100 deletions.
58 changes: 1 addition & 57 deletions lib/experimental/add-registered-webfonts-to-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,6 @@
* @package gutenberg
*/

function gutenberg_is_webfont_equal( $a, $b, $is_camel_case = true ) {
$equality_attrs = $is_camel_case ? array(
'fontFamily',
'fontStyle',
'fontWeight',
) : array(
'font-family',
'font-style',
'font-weight',
);

foreach ( $equality_attrs as $attr ) {
if ( $a[ $attr ] !== $b[ $attr ] ) {
return false;
}
}

return true;
}

function gutenberg_find_webfont( $webfonts, $webfont_to_find ) {
if ( ! count( $webfonts ) ) {
return false;
}

$is_camel_case = isset( $webfonts[0]['fontFamily'] );

foreach ( $webfonts as $index => $webfont ) {
if ( gutenberg_is_webfont_equal( $webfont, $webfont_to_find, $is_camel_case ) ) {
return $index;
}
}

return false;
}

function gutenberg_webfont_to_camel_case( $webfont ) {
$camel_cased_webfont = array();

foreach ( $webfont as $key => $value ) {
$camel_cased_webfont[ lcfirst( str_replace( '-', '', ucwords( $key, '-' ) ) ) ] = $value;
}

return $camel_cased_webfont;
}

function gutenberg_get_font_family_indexes( $families ) {
$indexes = array();

foreach ( $families as $index => $family ) {
$indexes[ wp_webfonts()->get_font_slug( $family ) ] = $index;
}

return $indexes;
}

/**
* Add missing fonts data to the global styles.
*
Expand All @@ -86,7 +30,7 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) {
$data['settings']['typography']['fontFamilies'] = array();
}

$font_family_indexes_in_theme_json = gutenberg_get_font_family_indexes( $data['settings']['typography']['fontFamilies'] );
$font_family_indexes_in_theme_json = gutenberg_map_font_family_indexes( $data['settings']['typography']['fontFamilies'] );

foreach ( $registered_font_families as $slug => $registered_font_faces ) {
// Font family not in theme.json, so let's add it.
Expand Down
43 changes: 0 additions & 43 deletions lib/experimental/register-webfonts-from-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,6 @@
* @package gutenberg
*/

/**
* Transforms the keys of the webfont defined in theme.json into
* kebab case, so the Webfonts API can handle it.
*
* @param array $webfont The webfont to be tranformed.
*
* @return array The kebab-case version of the webfont.
*/
function gutenberg_webfont_to_kebab_case( $webfont ) {
$kebab_cased_webfont = array();

foreach ( $webfont as $key => $value ) {
$kebab_cased_webfont[ _wp_to_kebab_case( $key ) ] = $value;
}

return $kebab_cased_webfont;
}

/**
* Transforms the source of the font face from `file.:/` into an actual URI.
*
* @param array $font_face The font face.
*
* @return array The URI-resolved font face.
*/
function gutenberg_resolve_font_face_uri( $font_face ) {
if ( empty( $font_face['src'] ) ) {
return $font_face;
}

$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 ( ! str_starts_with( $url, 'file:./' ) ) {
continue;
}
$font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
}

return $font_face;
}

/**
* Register webfonts defined in theme.json
*
Expand Down
136 changes: 136 additions & 0 deletions lib/experimental/webfonts-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Stores utility functions used by the Webfonts API.
*
* @package gutenberg
*/

/**
* Transforms the keys of the webfont defined in theme.json into
* kebab case, so the Webfonts API can handle it.
*
* @param array $webfont The webfont to be tranformed.
*
* @return array The kebab-case version of the webfont.
*/
function gutenberg_webfont_to_kebab_case( $webfont ) {
$kebab_cased_webfont = array();

foreach ( $webfont as $key => $value ) {
$kebab_cased_webfont[ _wp_to_kebab_case( $key ) ] = $value;
}

return $kebab_cased_webfont;
}

/**
* Transforms the source of the font face from `file.:/` into an actual URI.
*
* @param array $font_face The font face.
*
* @return array The URI-resolved font face.
*/
function gutenberg_resolve_font_face_uri( $font_face ) {
if ( empty( $font_face['src'] ) ) {
return $font_face;
}

$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 ( ! str_starts_with( $url, 'file:./' ) ) {
continue;
}
$font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
}

return $font_face;
}

/**
* Compares two webfonts.
*
* @param array $a The first webfont.
* @param array $b The second webfont.
* @param boolean $is_camel_case If the font attributes are in camel case or kebab case. Defaults to camel case.
*
* @return boolean True if they're equal, false otherwise.
*/
function gutenberg_is_webfont_equal( $a, $b, $is_camel_case = true ) {
$equality_attrs = $is_camel_case ? array(
'fontFamily',
'fontStyle',
'fontWeight',
) : array(
'font-family',
'font-style',
'font-weight',
);

foreach ( $equality_attrs as $attr ) {
if ( $a[ $attr ] !== $b[ $attr ] ) {
return false;
}
}

return true;
}

/**
* Finds $webfont_to_find in $webfonts.
*
* @param array[] $webfonts The webfonts array.
* @param array $webfont_to_find The webfont to find.
*
* @return integer|false The index of $webfont in $webfonts if found. False otherwise.
*/
function gutenberg_find_webfont( $webfonts, $webfont_to_find ) {
if ( ! count( $webfonts ) ) {
return false;
}

$is_camel_case = isset( $webfonts[0]['fontFamily'] );

foreach ( $webfonts as $index => $webfont ) {
if ( gutenberg_is_webfont_equal( $webfont, $webfont_to_find, $is_camel_case ) ) {
return $index;
}
}

return false;
}

/**
* Converts webfont attributes from kebab case to camel case.
*
* @param array $webfont The kebab-cased webfont.
*
* @return array The camel-cased webfont.
*/
function gutenberg_webfont_to_camel_case( $webfont ) {
$camel_cased_webfont = array();

foreach ( $webfont as $key => $value ) {
$camel_cased_webfont[ lcfirst( str_replace( '-', '', ucwords( $key, '-' ) ) ) ] = $value;
}

return $camel_cased_webfont;
}

/**
* Map the font families by slug and their corresponding indexes in $families.
*
* @param array[] $families The font families array.
*
* @return array An array in array( [slug] => [index]) format.
*/
function gutenberg_map_font_family_indexes( $families ) {
$indexes = array();

foreach ( $families as $index => $family ) {
$indexes[ wp_webfonts()->get_font_slug( $family ) ] = $index;
}

return $indexes;
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/class-wp-webfonts-provider.php';
require __DIR__ . '/experimental/class-wp-webfonts-provider-local.php';
require __DIR__ . '/experimental/webfonts.php';
require __DIR__ . '/experimental/webfonts-utils.php';
require __DIR__ . '/experimental/blocks.php';

require __DIR__ . '/blocks.php';
Expand Down

0 comments on commit 10e9964

Please sign in to comment.