diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php index c48d28a6f01fb6..154621ead50fbe 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php @@ -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 * @@ -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 ) { @@ -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. */ @@ -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 ); @@ -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 ) ); } @@ -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 ) { @@ -216,6 +214,7 @@ private function validate_data( $data ) { return new WP_Error( 'font_collection_missing_property', $message ); } } + return $data; } } diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php index a33aead5571ec0..d5db42c499814c 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php @@ -86,7 +86,7 @@ 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 ) { @@ -94,7 +94,7 @@ public static function unregister_font_collection( $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; @@ -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 ) ) { @@ -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. * diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php index 72a362a3a42a4d..792a5aaa80eef6 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php @@ -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 @@ -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 ) { @@ -76,23 +77,18 @@ 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'] ); @@ -100,8 +96,7 @@ public static function get_font_face_slug( $settings ) { $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( @@ -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. * diff --git a/lib/compat/wordpress-6.5/fonts/fonts.php b/lib/compat/wordpress-6.5/fonts/fonts.php index 339ab32646d8b4..28b019b9c1fa61 100644 --- a/lib/compat/wordpress-6.5/fonts/fonts.php +++ b/lib/compat/wordpress-6.5/fonts/fonts.php @@ -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( @@ -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' ) ) { @@ -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 ); @@ -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 ); } } @@ -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. * @@ -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 ); } } @@ -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 ) { @@ -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 ) { @@ -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. @@ -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. @@ -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'] );