Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Font Library non-REST API code to align with Core standards #58607

Merged
33 changes: 16 additions & 17 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php
Expand Up @@ -36,7 +36,7 @@ final class WP_Font_Collection {
private $data;

/**
* Font collection JSON file path or url.
* Font collection JSON file path or URL.
*
* @since 6.5.0
*
Expand All @@ -51,12 +51,12 @@ final class WP_Font_Collection {
*
* @param string $slug Font collection slug.
* @param array|string $data_or_file {
* Font collection data array or a file path or url to a JSON file containing the font collection.
* Font collection data array or a file path or URL to a JSON file containing the font collection.
*
* @type string $name Name of the font collection.
* @type string $description Description of the font collection.
* @type array $font_families Array of font family definitions that are in the collection.
* @type array $categories Array of categories for the fonts that are in the collection.
* @type string $name Name of the font collection.
* @type string $description Description of the font collection.
* @type array $font_families Array of font family definitions included in the collection.
* @type array $categories Array of categories associated with the fonts in the collection.
* }
*/
public function __construct( $slug, $data_or_file ) {
Expand Down Expand Up @@ -104,23 +104,21 @@ public function get_data() {
}

// Set defaults for optional properties.
$data = wp_parse_args(
return wp_parse_args(
$data,
array(
'description' => '',
'categories' => array(),
)
);

return $data;
}

/**
* Loads the font collection data from a JSON file path or url.
* Loads font collection data from a JSON file or URL.
*
* @since 6.5.0
*
* @param string $file_or_url File path or url to a JSON file containing the font collection data.
* @param string $file_or_url File path or URL to a JSON file containing the font collection data.
* @return array|WP_Error An array containing the font collection data on success,
* else an instance of WP_Error on failure.
*/
Expand All @@ -129,7 +127,7 @@ private function load_from_json( $file_or_url ) {
$file = file_exists( $file_or_url ) ? wp_normalize_path( realpath( $file_or_url ) ) : false;

if ( ! $url && ! $file ) {
// translators: %s: File path or url to font collection JSON file.
// translators: %s: File path or URL to font collection JSON file.
$message = __( 'Font collection JSON file is invalid or does not exist.', 'gutenberg' );
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
return new WP_Error( 'font_collection_json_missing', $message );
Expand Down Expand Up @@ -157,23 +155,23 @@ private function load_from_file( $file ) {
}

/**
* Loads the font collection data from a JSON file url.
* Loads the font collection data from a JSON file URL.
*
* @since 6.5.0
*
* @param string $url Url to a JSON file containing the font collection data.
* @param string $url URL to a JSON file containing the font collection data.
* @return array|WP_Error An array containing the font collection data on success,
* else an instance of WP_Error on failure.
*/
private function load_from_url( $url ) {
// Limit key to 167 characters to avoid failure in the case of a long url.
// Limit key to 167 characters to avoid failure in the case of a long URL.
$transient_key = substr( 'wp_font_collection_url_' . $url, 0, 167 );
$data = get_site_transient( $transient_key );

if ( false === $data ) {
$response = wp_safe_remote_get( $url );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
// translators: %s: Font collection url.
// translators: %s: Font collection URL.
return new WP_Error( 'font_collection_request_error', sprintf( __( 'Error fetching the font collection data from "%s".', 'gutenberg' ), $url ) );
}

Expand All @@ -199,7 +197,7 @@ private function load_from_url( $url ) {
*
* @since 6.5.0
*
* @param array $data Font collection configuration.
* @param array $data Font collection configuration to validate.
* @return array|WP_Error Array of data if valid, otherwise a WP_Error instance.
*/
private function validate_data( $data ) {
Expand All @@ -216,6 +214,7 @@ private function validate_data( $data ) {
return new WP_Error( 'font_collection_missing_property', $message );
}
}

return $data;
}
}
Expand Down
9 changes: 4 additions & 5 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-library.php
Expand Up @@ -86,15 +86,15 @@ public static function register_font_collection( $slug, $data_or_file ) {
*
* @since 6.5.0
*
* @param string $collection_slug Font collection slug.
* @param string $slug Font collection slug.
* @return bool True if the font collection was unregistered successfully and false otherwise.
*/
public static function unregister_font_collection( $slug ) {
if ( ! self::is_collection_registered( $slug ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Font collection slug. */
sprintf( __( 'Font collection "%s" not found.', 'default' ), $slug ),
sprintf( __( 'Font collection "%s" not found.' ), $slug ),
'6.5.0'
);
return false;
Expand Down Expand Up @@ -132,7 +132,8 @@ public static function get_font_collections() {
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @return WP_Font_Collection Font collection object.
* @return WP_Font_Collection|WP_Error Font collection object,
* or WP_Error object if the font collection doesn't exist.
*/
public static function get_font_collection( $slug ) {
if ( array_key_exists( $slug, self::$collections ) ) {
Expand All @@ -141,8 +142,6 @@ public static function get_font_collection( $slug ) {
return new WP_Error( 'font_collection_not_found', 'Font collection not found.' );
}



/**
* Sets the allowed mime types for fonts.
*
Expand Down
35 changes: 15 additions & 20 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php
Expand Up @@ -2,7 +2,7 @@
/**
* Font Utils class.
*
* This file contains utils related to the Font Library.
* Provides utility functions for working with font families.
*
* @package WordPress
* @subpackage Font Library
Expand All @@ -21,14 +21,15 @@
*/
class WP_Font_Utils {
/**
* Format font family names with surrounding quotes when the name contains a space.
* Format font family names.
*
* Adds surrounding quotes to font family names containing spaces and not already quoted.
*
* @since 6.5.0
* @access private
*
* @param string $font_family Font family attribute.
*
* @return string The formatted font family attribute.
* @param string $font_family Font family name(s), comma-separated.
* @return string Formatted font family name(s).
*/
public static function format_font_family( $font_family ) {
if ( $font_family ) {
Expand Down Expand Up @@ -76,32 +77,26 @@ function ( $family ) {
* @type string $fontStretch Optional font stretch, defaults to '100%'.
* @type string $unicodeRange Optional unicode range, defaults to 'U+0-10FFFF'.
* }
*
* @return string Font face slug.
*/
public static function get_font_face_slug( $settings ) {
$settings = wp_parse_args(
$settings,
array(
'fontFamily' => '',
'fontStyle' => 'normal',
'fontWeight' => '400',
'fontStretch' => '100%',
'unicodeRange' => 'U+0-10FFFF',
)
$defaults = array(
'fontFamily' => '',
'fontStyle' => 'normal',
'fontWeight' => '400',
'fontStretch' => '100%',
'unicodeRange' => 'U+0-10FFFF',
);
$settings = wp_parse_args( $settings, $defaults );

// Convert all values to lowercase for comparison.
// Font family names may use multibyte characters.
$font_family = mb_strtolower( $settings['fontFamily'] );
$font_style = strtolower( $settings['fontStyle'] );
$font_weight = strtolower( $settings['fontWeight'] );
$font_stretch = strtolower( $settings['fontStretch'] );
$unicode_range = strtoupper( $settings['unicodeRange'] );

// Convert weight keywords to numeric strings.
$font_weight = str_replace( 'normal', '400', $font_weight );
$font_weight = str_replace( 'bold', '700', $font_weight );
$font_weight = str_replace( array( 'normal', 'bold' ), array( '400', '700' ), $font_weight );

// Convert stretch keywords to numeric strings.
$font_stretch_map = array(
Expand Down Expand Up @@ -137,7 +132,7 @@ function ( $elem ) {
}

/**
* Sanitize a tree of data using an schema that defines the sanitization to apply to each key.
* Sanitize a tree of data using a schema that defines the sanitization to apply to each key.
*
* It removes the keys not in the schema and applies the sanitizer to the values.
*
Expand Down
45 changes: 34 additions & 11 deletions lib/compat/wordpress-6.5/fonts/fonts.php
Expand Up @@ -19,7 +19,7 @@
*
* @since 6.5.0
*/
function gutenberg_init_font_library_routes() {
function gutenberg_create_initial_post_types() {
// @core-merge: This code will go into Core's `create_initial_post_types()`.
$args = array(
'labels' => array(
Expand Down Expand Up @@ -82,13 +82,30 @@ function gutenberg_init_font_library_routes() {
'autosave_rest_controller_class' => 'stdClass',
)
);
}

/**
* Initializes REST routes.
*
* @since 6.5
*/
function gutenberg_create_initial_rest_routes() {
// @core-merge: This code will go into Core's `create_initial_rest_routes()`.
$font_collections_controller = new WP_REST_Font_Collections_Controller();
$font_collections_controller->register_routes();
}

add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' );
/**
* Initializes REST routes and post types.
*
* @since 6.5
*/
function gutenberg_init_font_library() {
gutenberg_create_initial_post_types();
gutenberg_create_initial_rest_routes();
}

add_action( 'rest_api_init', 'gutenberg_init_font_library' );


if ( ! function_exists( 'wp_register_font_collection' ) ) {
Expand All @@ -108,7 +125,7 @@ function gutenberg_init_font_library_routes() {
* @type array $categories Array of categories for the fonts that are in the collection.
* }
* @return WP_Font_Collection|WP_Error A font collection is it was registered
* successfully, else WP_Error.
* successfully, or WP_Error object on failure.
*/
function wp_register_font_collection( $slug, $data_or_file ) {
return WP_Font_Library::register_font_collection( $slug, $data_or_file );
Expand All @@ -122,9 +139,10 @@ function wp_register_font_collection( $slug, $data_or_file ) {
* @since 6.5.0
*
* @param string $collection_id The font collection ID.
* @return bool True if the font collection was unregistered successfully, else false.
*/
function wp_unregister_font_collection( $collection_id ) {
WP_Font_Library::unregister_font_collection( $collection_id );
return WP_Font_Library::unregister_font_collection( $collection_id );
}
}

Expand All @@ -151,7 +169,6 @@ function gutenberg_register_font_collections() {
* @type string $baseurl URL path without subdir.
* @type string|false $error False or error message.
* }
*
* @return array $defaults {
* Array of information about the upload directory.
*
Expand All @@ -178,7 +195,15 @@ function wp_get_font_dir( $defaults = array() ) {
$defaults['baseurl'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
$defaults['error'] = false;

// Filters the fonts directory data.
/**
* Filters the fonts directory data.
*
* This filter allows developers to modify the fonts directory data.
*
* @since 6.5.0
*
* @param array $defaults The original fonts directory data.
*/
return apply_filters( 'font_dir', $defaults );
}
}
Expand All @@ -194,7 +219,6 @@ function wp_get_font_dir( $defaults = array() ) {
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @return void
*/
function _wp_after_delete_font_family( $post_id, $post ) {
if ( 'wp_font_family' !== $post->post_type ) {
Expand Down Expand Up @@ -224,7 +248,6 @@ function _wp_after_delete_font_family( $post_id, $post ) {
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @return void
*/
function _wp_before_delete_font_face( $post_id, $post ) {
if ( 'wp_font_face' !== $post->post_type ) {
Expand Down Expand Up @@ -274,7 +297,7 @@ function gutenberg_convert_legacy_font_family_format() {
continue;
}

$font_faces = $font_family_json['fontFace'] ?? array();
$font_faces = isset( $font_family_json['fontFace'] ) ? $font_family_json['fontFace'] : array();
unset( $font_family_json['fontFace'] );

// Save wp_font_face posts within the family.
Expand All @@ -289,7 +312,7 @@ function gutenberg_convert_legacy_font_family_format() {

$font_face_id = wp_insert_post( wp_slash( $args ) );

$file_urls = (array) $font_face['src'] ?? array();
$file_urls = (array) ( isset( $font_face['src'] ) ? $font_face['src'] : array() );

foreach ( $file_urls as $file_url ) {
// continue if the file is not local.
Expand All @@ -305,7 +328,7 @@ function gutenberg_convert_legacy_font_family_format() {
// Update the font family post to remove the font face data.
$args = array();
$args['ID'] = $font_family->ID;
$args['post_title'] = $font_family_json['name'] ?? '';
$args['post_title'] = isset( $font_family_json['name'] ) ? $font_family_json['name'] : '';
$args['post_name'] = sanitize_title( $font_family_json['slug'] );

unset( $font_family_json['name'] );
Expand Down