Skip to content

Commit

Permalink
Font Library: remove unused utilities and rename class (#58342)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder authored and youknowriad committed Jan 31, 2024
1 parent 628a844 commit 1569c69
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,24 @@
* @since 6.5.0
*/

if ( class_exists( 'WP_Font_Family_Utils' ) ) {
if ( class_exists( 'WP_Font_Utils' ) ) {
return;
}

/**
* A class of utilities for working with the Font Library.
*
* These utilities may change or be removed in the future and are intended for internal use only.
*
* @since 6.5.0
* @access private
*/
class WP_Font_Family_Utils {

/**
* Generates a filename for a font face asset.
*
* Creates a filename for a font face asset using font family, style, weight and
* extension information.
*
* @since 6.5.0
*
* @param string $font_slug The font slug to use in the filename.
* @param array $font_face The font face array containing 'fontFamily', 'fontStyle', and
* 'fontWeight' attributes.
* @param string $url The URL of the font face asset, used to derive the file extension.
* @param string $suffix Optional. The suffix added to the resulting filename. Default empty string.
* @return string The generated filename for the font face asset.
*/
public static function get_filename_from_font_face( $font_slug, $font_face, $url, $suffix = '' ) {
$extension = pathinfo( $url, PATHINFO_EXTENSION );
$filename = "{$font_slug}_{$font_face['fontStyle']}_{$font_face['fontWeight']}";
if ( '' !== $suffix ) {
$filename .= "_{$suffix}";
}

return sanitize_file_name( "{$filename}.{$extension}" );
}

/**
* Merges two fonts and their font faces.
*
* @since 6.5.0
*
* @param array $font1 The first font to merge.
* @param array $font2 The second font to merge.
* @return array|WP_Error The merged font or WP_Error if the fonts have different slugs.
*/
public static function merge_fonts_data( $font1, $font2 ) {
if ( $font1['slug'] !== $font2['slug'] ) {
return new WP_Error(
'fonts_must_have_same_slug',
__( 'Fonts must have the same slug to be merged.', 'gutenberg' )
);
}

$font_faces_1 = isset( $font1['fontFace'] ) ? $font1['fontFace'] : array();
$font_faces_2 = isset( $font2['fontFace'] ) ? $font2['fontFace'] : array();
$merged_font_faces = array_merge( $font_faces_1, $font_faces_2 );

$serialized_faces = array_map( 'serialize', $merged_font_faces );
$unique_serialized_faces = array_unique( $serialized_faces );
$unique_faces = array_map( 'unserialize', $unique_serialized_faces );

$merged_font = array_merge( $font1, $font2 );
$merged_font['fontFace'] = array_values( $unique_faces );

return $merged_font;
}

class WP_Font_Utils {
/**
* Format font family to make it valid CSS.
* Format font family names with surrounding quotes when the name contains a space.
*
* @since 6.5.0
* @access private
*
* @param string $font_family Font family attribute.
* @return string The formatted font family attribute.
Expand Down Expand Up @@ -117,6 +64,7 @@ function ( $family ) {
* unicode ranges.
*
* @since 6.5.0
* @access private
*
* @link https://drafts.csswg.org/css-fonts/#font-style-matching
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function sanitize_font_face_settings( $value ) {
$settings = json_decode( $value, true );

if ( isset( $settings['fontFamily'] ) ) {
$settings['fontFamily'] = WP_Font_Family_Utils::format_font_family( $settings['fontFamily'] );
$settings['fontFamily'] = WP_Font_Utils::format_font_family( $settings['fontFamily'] );
}

return $settings;
Expand Down Expand Up @@ -309,7 +309,7 @@ public function create_item( $request ) {
array(
'post_type' => $this->post_type,
'posts_per_page' => 1,
'title' => WP_Font_Family_Utils::get_font_face_slug( $settings ),
'title' => WP_Font_Utils::get_font_face_slug( $settings ),
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
)
Expand Down Expand Up @@ -731,7 +731,7 @@ protected function prepare_item_for_database( $request ) {

// Store this "slug" as the post_title rather than post_name, since it uses the fontFamily setting,
// which may contain multibyte characters.
$title = WP_Font_Family_Utils::get_font_face_slug( $settings );
$title = WP_Font_Utils::get_font_face_slug( $settings );

$prepared_post->post_type = $this->post_type;
$prepared_post->post_parent = $request['font_family_id'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function sanitize_font_family_settings( $value ) {
$settings = json_decode( $value, true );

if ( isset( $settings['fontFamily'] ) ) {
$settings['fontFamily'] = WP_Font_Family_Utils::format_font_family( $settings['fontFamily'] );
$settings['fontFamily'] = WP_Font_Utils::format_font_family( $settings['fontFamily'] );
}

// Provide default for preview, if not provided.
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function gutenberg_convert_legacy_font_family_format() {
foreach ( $font_faces as $font_face ) {
$args = array();
$args['post_type'] = 'wp_font_face';
$args['post_title'] = WP_Font_Family_Utils::get_font_face_slug( $font_face );
$args['post_title'] = WP_Font_Utils::get_font_face_slug( $font_face );
$args['post_name'] = sanitize_title( $args['post_title'] );
$args['post_status'] = 'publish';
$args['post_parent'] = $font_family->ID;
Expand Down
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function gutenberg_is_experiment_enabled( $name ) {
// Loads the Font Library.
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-collection.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-library.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-family-utils.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-utils.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-families-controller.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-collections-controller.php';
Expand Down

This file was deleted.

Loading

0 comments on commit 1569c69

Please sign in to comment.