Skip to content

Commit

Permalink
Move formatting functions to utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Nov 20, 2023
1 parent 52d3e7f commit efa7602
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
37 changes: 37 additions & 0 deletions lib/experimental/fonts/font-library/class-wp-font-family-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,41 @@ public static function has_font_mime_type( $filepath ) {

return in_array( $filetype['type'], $allowed_mime_types, true );
}

/**
* Format font slug and family.
*
* @since 6.5.0
*
* @param array $font The font to format.
* @return array The formatted font.
*/
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'] );
$wrapped_font_families = array_map(
function ( $font_family ) {
$trimmed = trim( $font_family );
if ( ! empty( $trimmed ) && strpos( $trimmed, ' ' ) !== false && strpos( $trimmed, "'" ) === false && strpos( $trimmed, '"' ) === false ) {
return "'" . $trimmed . "'";
}
return $trimmed;
},
$font_families
);

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

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

// Ensure slugs are kebab-cased and font families are wrapped in quotes.
if ( isset( $sanitized_font['slug'] ) ) {
$sanitized_font['slug'] = _wp_to_kebab_case( $sanitized_font['slug'] );
}

if ( isset( $sanitized_font['fontFamily'] ) ) {
$font_families = explode( ',', $sanitized_font['fontFamily'] );
$wrapped_font_families = array_map(
function ( $font_family ) {
$trimmed = trim( $font_family );
if ( ! empty( $trimmed ) && strpos( $trimmed, ' ' ) !== false && strpos( $trimmed, "'" ) === false && strpos( $trimmed, '"' ) === false ) {
return "'" . $trimmed . "'";
}
return $trimmed;
},
$font_families
);

if ( count( $wrapped_font_families ) === 1 ) {
$sanitized_font['fontFamily'] = $wrapped_font_families[0];
} else {
$sanitized_font['fontFamily'] = implode( ', ', $wrapped_font_families );
}
}
$this->data = $sanitized_font;
$sanitized_font = WP_Font_Family_Utils::format_slug_and_family( $sanitized_font );
$this->data = $sanitized_font;
return $this->data;
}

Expand Down

0 comments on commit efa7602

Please sign in to comment.