Skip to content

Commit

Permalink
reduce the scope of the function
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Nov 24, 2023
1 parent 1e69e75 commit a414e3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
27 changes: 11 additions & 16 deletions lib/experimental/fonts/font-library/class-wp-font-family-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,19 @@ public static function has_font_mime_type( $filepath ) {
}

/**
* Format font slug and family.
* Format font family to make it CSS ready.
*
* @since 6.5.0
*
* @param array $font The font to format.
* @return array The formatted font.
* @param string $font_family Font family attribute.
* @return string The formatted font family attribute.
*/
public static function format_slug_and_family( $font ) {
// Ensure slugs are kebab-cased and font families are wrapped in quotes.
if ( isset( $font['slug'] ) ) {
$font['slug'] = _wp_to_kebab_case( $font['slug'] );
}

if ( isset( $font['fontFamily'] ) ) {
$font_families = explode( ',', $font['fontFamily'] );
public static function format_font_family( $font_family ) {
if ( $font_family ) {
$font_families = explode( ',', $font_family );
$wrapped_font_families = array_map(
function ( $font_family ) {
$trimmed = trim( $font_family );
function ( $family ) {
$trimmed = trim( $family );
if ( ! empty( $trimmed ) && strpos( $trimmed, ' ' ) !== false && strpos( $trimmed, "'" ) === false && strpos( $trimmed, '"' ) === false ) {
return "'" . $trimmed . "'";
}
Expand All @@ -119,12 +114,12 @@ function ( $font_family ) {
);

if ( count( $wrapped_font_families ) === 1 ) {
$font['fontFamily'] = $wrapped_font_families[0];
$font_family = $wrapped_font_families[0];
} else {
$font['fontFamily'] = implode( ', ', $wrapped_font_families );
$font_family = implode( ', ', $wrapped_font_families );
}
}

return $font;
return $font_family;
}
}
3 changes: 2 additions & 1 deletion lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ private function sanitize() {
? $theme_data['settings']['typography']['fontFamilies'][0]
: array();

$sanitized_font = WP_Font_Family_Utils::format_slug_and_family( $sanitized_font );
$sanitized_font['slug'] = _wp_to_kebab_case( $sanitized_font['slug'] );
$sanitized_font['fontFamily'] = WP_Font_Family_Utils::format_font_family( $sanitized_font['fontFamily'] );
$this->data = $sanitized_font;
return $this->data;
}
Expand Down

0 comments on commit a414e3f

Please sign in to comment.