Skip to content

Commit

Permalink
move find_webfont function to the WP_Webfonts class
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Apr 20, 2022
1 parent 260156a commit b2eb078
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/experimental/add-registered-webfonts-to-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function( $font_face ) {
foreach ( $registered_font_faces as $registered_font_face ) {
$registered_font_face = _wp_array_keys_to_camel_case( $registered_font_face );

if ( false !== _gutenberg_find_webfont( $font_faces_in_theme_json, $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.
continue;
}
Expand Down
43 changes: 42 additions & 1 deletion lib/experimental/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private function unregister_font_face( array $font_face_to_unregister ) {
}

$font_family = $this->registered_webfonts[ $font_family_slug ];
$index = _gutenberg_find_webfont( $font_family, $font_face_to_unregister );
$index = $this->find_webfont( $font_family, $font_face_to_unregister );

// Font face not found.
if ( false === $index ) {
Expand Down Expand Up @@ -530,4 +530,45 @@ private function get_webfonts_by_provider( array $font_families ) {

return $webfonts_by_provider;
}

/**
* Finds $webfont_to_find in $webfonts.
*
* @since 6.0.0
*
* @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.
*/
public function find_webfont( array $webfonts, $webfont_to_find ) {
if ( empty( $webfonts ) ) {
return false;
}

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

foreach ( $webfonts as $index => $webfont ) {
$equality_attrs = $is_camel_case
? array( 'fontFamily', 'fontStyle', 'fontWeight' )
: array( 'font-family', 'font-style', 'font-weight' );

$found = $index;
foreach ( $equality_attrs as $attr ) {
// Bail out if the attribute does not exist, or if the values are not equal.
if (
empty( $webfont[ $attr ] ) ||
empty( $webfont_to_find[ $attr ] ) ||
$webfont[ $attr ] !== $webfont_to_find[ $attr ]
) {
$found = false;
break;
}
}
if ( false !== $found ) {
return $found;
}
}

return false;
}
}
42 changes: 0 additions & 42 deletions lib/experimental/webfonts-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,6 @@ function _wp_resolve_font_face_uri( array $font_face ) {
}
}

/**
* Finds $webfont_to_find in $webfonts.
*
* @since 6.0.0
* @private
*
* @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( array $webfonts, $webfont_to_find ) {
if ( empty( $webfonts ) ) {
return false;
}

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

foreach ( $webfonts as $index => $webfont ) {
$equality_attrs = $is_camel_case
? array( 'fontFamily', 'fontStyle', 'fontWeight' )
: array( 'font-family', 'font-style', 'font-weight' );

$found = $index;
foreach ( $equality_attrs as $attr ) {
// Bail out if the attribute does not exist, or if the values are not equal.
if (
empty( $webfont[ $attr ] ) ||
empty( $webfont_to_find[ $attr ] ) ||
$webfont[ $attr ] !== $webfont_to_find[ $attr ]
) {
$found = false;
break;
}
}
if ( false !== $found ) {
return $found;
}
}

return false;
}

if ( ! function_exists( '_wp_array_keys_to_camel_case' ) ) {
/**
* Transforms the keys in the given array to camelCase.
Expand Down

0 comments on commit b2eb078

Please sign in to comment.