Skip to content

Commit

Permalink
Fonts Library: Backend (#52704)
Browse files Browse the repository at this point in the history
* Adding fonts library backend

* load font library

* Format php.

* Update comment.

* Fix array key merge.

* Format php.

* linting: lint and add comments where needed

* linting: add comments on rest fonts library file

* lint: fix filedocs

* lint: fix lib loader errors

* Misc updates to comments for consistency

* Update controller name in load.php

* Add an install method to the font family class to simplify the code

* Renaming the Fonts Library class to Fonts Library Family to emphasize the class produces Font Family Objects

* Renaming WP_Fonts_Library_Family to WP_Font_Family

* lint: make sure coding standards test passes

* Replace include for admin.php with file.php

* Fix linter errors

* adding 1 php test

* fix test

* Move a few methods from Font Family class to Fonts Library

* move more methods to Fonts Library class

* Breaking the classes in to smaller ones and adding tests

* add tests font Font Family class

* Create fonts dir at init of wordpress

* Testing and refactoring WP_Font_Family class

* PHP formatting

* fix syntax

* move comment

* update wording

Co-authored-by: Jeff Ong <[email protected]>

* update comment

Co-authored-by: Jeff Ong <[email protected]>

* update comment

* update comment

* update comment

* call to sanitize before creating the post

* fix return of method

* Addding a failing test for local fonts upload

* micro-optimization

Co-authored-by: Tonya Mork <[email protected]>

* interting the order of static and access control keywords in method definition

* To avoid fatal error when the class is introduced into Core, add a guard to the opening of the file. This guard will not be merged into Core, but rather will stay in Gutenberg.

Co-authored-by: [email protected]

* updating comments

* adding missing access keywords for methods

* WP_Theme_JSON_Gutenberg instead of WP_Theme_JSON

* Moving init calls to a new file.

* adding comment to file

* format PHP

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* syntax change

Co-authored-by: Colin Stewart <[email protected]>

* syntax change

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Anton Vlasenko <[email protected]>

* add missing 'new' while returning a WP_Error

Co-authored-by: Anton Vlasenko <[email protected]>

* fix return comment

Co-authored-by: Anton Vlasenko <[email protected]>

* renaming variable

Co-authored-by: Anton Vlasenko <[email protected]>

* stylistic change to early return

* shortening the syntax and variable renaming

* Stylistic change: switching the order of parameters of assertEquals call

* simplify delete_asset method code

* refactor uninstall endpoint response

* fixing return type comment

* fixing return type and refactor to early return

* check is post_id is an error

Co-authored-by: Colin Stewart <[email protected]>

* using assertStringEndsWith assertion

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* replace assertEquals by assertSame

* assertSame and assertCount

* assertFileExists

* assertFileDoesNotExist

* simplify has_font_faces_method

* return early if there are no fonts to be installed

* fix assertWPError calls

* adding public keyword to test cases

* improving the suffix and sanitiztion for get_filename_from_font_face method

* simplify uninstall method

* updating comments

* adding covers in php tests

* require font slug in the get_filename_from_font_face method

* improve get_datamethod test

* add fixture for WP_Font_Family_Test

* deleting unused data

* renaminng test case

* fixing WP_Font_Family_Utils_Test tests

* format php

* adding dataProviders in WP_Font_Family_Utils_Test

* adding params comments in test cases

* adding messages on tests with multiple assertions

* test refactor

* refactor test

* php format

* add comments to data_provider

* change test case name

* raname custom post type

* add missing parameter in docbloc

* adding comment

* adding full stops at the end of param comments

* change test assert type

Co-authored-by: Colin Stewart <[email protected]>

* change test assert type

Co-authored-by: Colin Stewart <[email protected]>

* change test assert type

Co-authored-by: Colin Stewart <[email protected]>

* use wp_handle_upload to write thee files and use wp-content/uploads/fonts as folder instead of wp-content/fonts

* strict suffix check

Co-authored-by: Anton Vlasenko <[email protected]>

* update comment

Co-authored-by: Anton Vlasenko <[email protected]>

* update comment

Co-authored-by: Anton Vlasenko <[email protected]>

* moving fonts library loading before checking if its available

* Fixes local font upload tests

* allowing the installation of fonts with remote urls that won't be donwloaded

* delete google fonts data and endpoint

* PHP code formatting

* lint

* php lint

* moving post_type registration to the WP_Fonts_Library class

* change endpoint url

* adding test file for WP_REST_Fonts_Library_Controller class

* adding tests for innstall endpoint

* add test for font family constructor failure

* Font Family constructo raise an exception if the slug was not provided

* add tests for uninstall endpoint

* format php

* add validation for install endpoint

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* improve comment text

Co-authored-by: Colin Stewart <[email protected]>

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* code format fix

Co-authored-by: Colin Stewart <[email protected]>

* fix test assertion and comment

Co-authored-by: Colin Stewart <[email protected]>

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* code formatting

Co-authored-by: Colin Stewart <[email protected]>

* fix comment

Co-authored-by: Colin Stewart <[email protected]>

* update test name

Co-authored-by: Colin Stewart <[email protected]>

* update assertion type

Co-authored-by: Colin Stewart <[email protected]>

* Improves test descripcion

Co-authored-by: Colin Stewart <[email protected]>

* code formatting

Co-authored-by: Colin Stewart <[email protected]>

* update test assertion type

Co-authored-by: Colin Stewart <[email protected]>

* comment update

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* code and comments style

Co-authored-by: Colin Stewart <[email protected]>

* comment style

Co-authored-by: Colin Stewart <[email protected]>

* comment update

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update comment

Co-authored-by: Colin Stewart <[email protected]>

* update assertion type

Co-authored-by: Colin Stewart <[email protected]>

* fix get_validation_error function

* improving comment

* improve comment

* adding test method description

* code format

* lint fixes

* fix test

* removing unwated parameter comment

* format

* lint

* Exclude fontsapi tests.

Modifies the phpunit.xml file to exclude running the Fonts API tests.
These tests will be removed once the Fonts Library is merged.

* Exclude Fonts API in multisite tests

* Core parity: since, formatting, package.

To prepare the code for Core, this commit does the following:

* Adds `@since` to each class, method, property.
* Sets the since to 6.4.0, as this is targeted for release in WP 6.4.0.
* Removes empty lines between @param and @return.
* Adds empty line before each `return`.
* Reduces long lines for readability.
* Makes the `@package` WordPress.
* Rewords comments to remove "we" words.
* Adds empty line between code groups for readability.

* init global function: renames & adds Core merge notes.

The function is for the plugin only and will not be merged into Core.

However, the code within the function will go into Core, albeit
in different Core functions.

`@core-merge` annotation is added with Core merge notes to help
whomever creates the Core PR.

* Moves post_type registration to init function.

WP Core registers all post types in `create_initial_post_types()`.
The code for registering will be placed within that global function.

This commit removes the WP_Fonts_Library static method and moves
the code into the gutenberg_init_fonts_library() function.

This is done to prepare the Fonts Library for and ease work in merging into Core.

* Remove function_exist wrap from Gutenberg init function.

This function will only exists in Gutenberg, thus it does
not the guard to protect it from the same function in Core
(that is once the Fonts Library is merged into Core).

* adding constant check to load Fonts Library files

* Add constant to turn on Fonts Library in tests.

* Add constant to turn on Fonts Library in mu tests.

---------

Co-authored-by: Jeff Ong <[email protected]>
Co-authored-by: Vicente Canales <[email protected]>
Co-authored-by: Sarah Norris <[email protected]>
Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Colin Stewart <[email protected]>
Co-authored-by: Anton Vlasenko <[email protected]>
  • Loading branch information
7 people authored Aug 11, 2023
1 parent 7050a7b commit 69d9ca3
Show file tree
Hide file tree
Showing 12 changed files with 2,156 additions and 2 deletions.
92 changes: 92 additions & 0 deletions lib/experimental/fonts-library/class-wp-font-family-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Fonts Family Utils class.
*
* This file contains utils fot Font Family class.
*
* @package WordPress
* @subpackage Fonts Library
* @since 6.4.0
*/

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

/**
* A class of utilities for working with the Fonts Library.
*
* @since 6.4.0
*/
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.4.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.4.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'] = $unique_faces;

return $merged_font;
}

/**
* Returns whether the given file has a font MIME type.
*
* @since 6.4.0
*
* @param string $filepath The file to check.
* @return bool True if the file has a font MIME type, false otherwise.
*/
public static function has_font_mime_type( $filepath ) {
$filetype = wp_check_filetype( $filepath, WP_Fonts_Library::ALLOWED_FONT_MIME_TYPES );

return in_array( $filetype['type'], WP_Fonts_Library::ALLOWED_FONT_MIME_TYPES, true );
}
}
Loading

0 comments on commit 69d9ca3

Please sign in to comment.