Skip to content

Commit

Permalink
The array_keys_to_camel_case function can be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Apr 20, 2022
1 parent 0de1117 commit e2fe01c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
22 changes: 19 additions & 3 deletions lib/experimental/add-registered-webfonts-to-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ function _wp_add_registered_webfonts_to_theme_json( array $data ) {
$font_family_indexes_in_theme_json[ wp_webfonts()->get_font_slug( $family ) ] = $index;
}

/**
* Transforms the keys in the given array to camelCase.
*
* @param array $to_transform The array to transform.
* @return array Given array with camelCase keys.
*/
$array_keys_to_camel_case = function( array $to_transform ) {
$camel_cased_array = array();

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

return $camel_cased_array;
};

foreach ( $registered_font_families as $slug => $registered_font_faces ) {
// Font family not in theme.json, so let's add it.
if ( ! isset( $font_family_indexes_in_theme_json[ $slug ] ) ) {
Expand All @@ -56,10 +72,10 @@ function _wp_add_registered_webfonts_to_theme_json( array $data ) {
'name' => $family_name,
'slug' => $slug,
'fontFaces' => array_map(
function( $font_face ) {
function( $font_face ) use ( $array_keys_to_camel_case ) {
$font_face['origin'] = 'gutenberg_wp_webfonts_api';

return _wp_array_keys_to_camel_case( $font_face );
return $array_keys_to_camel_case( $font_face );
},
$registered_font_faces
),
Expand All @@ -80,7 +96,7 @@ function( $font_face ) {
$font_faces_in_theme_json = $font_family_in_theme_json['fontFaces'];

foreach ( $registered_font_faces as $registered_font_face ) {
$registered_font_face = _wp_array_keys_to_camel_case( $registered_font_face );
$registered_font_face = $array_keys_to_camel_case( $registered_font_face );

if ( false !== wp_webfonts()->find_webfont( $font_faces_in_theme_json, $registered_font_face ) ) {
// Webfont is already there, so let's not add it.
Expand Down
21 changes: 0 additions & 21 deletions lib/experimental/webfonts-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@
* @package gutenberg
*/

if ( ! function_exists( '_wp_array_keys_to_camel_case' ) ) {
/**
* Transforms the keys in the given array to camelCase.
*
* @since 6.0.0
* @private
*
* @param array $to_transform The array to transform.
* @return array Given array with camelCase keys.
*/
function _wp_array_keys_to_camel_case( array $to_transform ) {
$camel_cased_array = array();

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

return $camel_cased_array;
}
}

if ( ! function_exists( '_wp_array_keys_to_kebab_case' ) ) {
/**
* Transforms the keys in the given array to kebab-case.
Expand Down

0 comments on commit e2fe01c

Please sign in to comment.