diff --git a/lib/experimental/fonts/font-library/class-wp-font-family-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php similarity index 64% rename from lib/experimental/fonts/font-library/class-wp-font-family-utils.php rename to lib/experimental/fonts/font-library/class-wp-font-utils.php index b291a8f1ee348d..b75f70bd80de87 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -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. @@ -117,6 +64,7 @@ function ( $family ) { * unicode ranges. * * @since 6.5.0 + * @access private * * @link https://drafts.csswg.org/css-fonts/#font-style-matching * diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php index 68173dd43f47dc..b0788fb7b33e2d 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php @@ -218,7 +218,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; @@ -296,7 +296,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, ) @@ -718,7 +718,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']; diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 4d5a89f6de1a05..d0689ce8c7d011 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -133,7 +133,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. diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 0bf9374f9f9454..8489c91b75e8a4 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -289,7 +289,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; diff --git a/lib/load.php b/lib/load.php index 4b2b4d5d8b0db8..6ded190b04e3fb 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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'; diff --git a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/getFilenameFromFontFace.php b/phpunit/tests/fonts/font-library/wpFontFamilyUtils/getFilenameFromFontFace.php deleted file mode 100644 index 0bd5b47ea23785..00000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/getFilenameFromFontFace.php +++ /dev/null @@ -1,64 +0,0 @@ -assertSame( - $expected, - WP_Font_Family_Utils::get_filename_from_font_face( - $slug, - $font_face, - $font_face['src'], - $suffix - ) - ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_get_filename() { - return array( - 'piazzolla' => array( - 'slug' => 'piazzolla', - 'font_face' => array( - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/font_file.ttf', - ), - 'suffix' => '', - 'expected_file_name' => 'piazzolla_italic_400.ttf', - ), - 'inter' => array( - 'slug' => 'inter', - 'font_face' => array( - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/font_file.otf', - ), - 'suffix' => '', - 'expected_file_name' => 'inter_normal_600.otf', - ), - ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php b/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php deleted file mode 100644 index 21517a0970f932..00000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php +++ /dev/null @@ -1,300 +0,0 @@ -assertWPError( $actual, 'WP_Error should have been returned' ); - $this->assertSame( - array( 'fonts_must_have_same_slug' => array( 'Fonts must have the same slug to be merged.' ) ), - $actual->errors, - 'WP_Error should have "fonts_must_have_same_slug" error' - ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_fail_merge() { - return array( - 'different slugs' => array( - 'font1' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - ), - ), - 'font2' => array( - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'normal', - 'fontWeight' => '700', - 'src' => 'http://example.com/fonts/inter_700_normal.ttf', - ), - ), - ), - 'expected_result' => 'WP_Error', - ), - ); - } - - - /** - * @dataProvider data_should_merge - * - * @param array $font1 First font data in theme.json format. - * @param array $font2 Second font data in theme.json format. - * @param array $expected_result Expected result. - */ - public function test_should_merge( array $font1, array $font2, array $expected_result ) { - $result = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 ); - $this->assertSame( $expected_result, $result, 'Merged font data should match expected result.' ); - $json_result = wp_json_encode( $result ); - $this->assertStringContainsString( '"fontFace":[', $json_result, 'fontFace data should be enconded as an array and not an object.' ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_merge() { - return array( - 'with different font faces' => array( - 'font1' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - ), - ), - 'font2' => array( - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '700', - 'src' => 'http://example.com/fonts/piazzolla_700_normal.ttf', - ), - ), - ), - 'expected_result' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '700', - 'src' => 'http://example.com/fonts/piazzolla_700_normal.ttf', - ), - ), - ), - ), - - 'repeated font faces' => array( - 'font1' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - ), - ), - 'font2' => array( - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - ), - ), - 'expected_result' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', - ), - ), - ), - ), - 'repeated font faces with non consecutive index positions' => array( - 'font1' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - - ), - ), - 'font2' => array( - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - ), - ), - 'expected_result' => array( - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', - ), - ), - ), - ), - ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php index 485587060f16a1..579baa2d248e0b 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php @@ -1,6 +1,6 @@ assertSame( $expected, - WP_Font_Family_Utils::format_font_family( + WP_Font_Utils::format_font_family( $font_family ) ); diff --git a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/getFontFaceSlug.php b/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php similarity index 90% rename from phpunit/tests/fonts/font-library/wpFontFamilyUtils/getFontFaceSlug.php rename to phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php index 1f87d0d2fd5a11..ded19749b9123e 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/getFontFaceSlug.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php @@ -1,18 +1,18 @@ assertSame( $expected_slug, $slug ); } diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 6091a3c91dc9ec..d6a95814b205a2 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -69,7 +69,7 @@ public static function wpTearDownAfterClass() { public static function create_font_face_post( $parent_id, $settings = array() ) { $settings = array_merge( self::$default_settings, $settings ); - $title = WP_Font_Family_Utils::get_font_face_slug( $settings ); + $title = WP_Font_Utils::get_font_face_slug( $settings ); return self::factory()->post->create( wp_slash( array(