From 130fb245cd60c1cb3161501e5de71de61ae90c02 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 17 Mar 2022 19:24:29 -0300 Subject: [PATCH 01/17] Group registered webfonts by font family --- .../wordpress-6.0/class-wp-webfonts.php | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index fe66f215f0218..7c3f7aa2647f9 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -102,22 +102,35 @@ public function get_providers() { */ public function register_font( $font ) { $font = $this->validate_font( $font ); - if ( $font ) { - $id = $this->get_font_id( $font ); - $this->webfonts[ $id ] = $font; + + // If not valid, bail out. + if ( ! $font ) { + return false; } + + $slug = $this->get_font_slug( $font ); + + // Initialize a new font-family collection. + if ( ! isset( $this->webfonts[ $slug ] ) ) { + $this->webfonts[ $slug ] = array(); + } + + $this->webfonts[ $slug ][] = $font; } /** - * Get the font ID. + * Get the font slug. * * @since 6.0.0 * - * @param array $font The font arguments. - * @return string + * @param array|string $to_convert The value to convert into a slug. Expected as the web font's array or a font-family as a string. */ - public function get_font_id( $font ) { - return sanitize_title( "{$font['font-family']}-{$font['font-weight']}-{$font['font-style']}-{$font['provider']}" ); + private function get_font_slug( $to_convert ) { + if ( is_array( $to_convert ) ) { + $to_convert = $to_convert['font-family']; + } + + return sanitize_title( $to_convert ); } /** @@ -274,18 +287,26 @@ public function generate_styles() { $styles = ''; $providers = $this->get_providers(); + $webfonts = array(); + + // Grab only the font face declarations from $font_families. + foreach ( $this->get_fonts() as $font_family ) { + foreach ( $font_family as $font_face ) { + $webfonts[] = $font_face; + } + } + // Group webfonts by provider. $webfonts_by_provider = array(); - $registered_webfonts = $this->get_fonts(); - foreach ( $registered_webfonts as $id => $webfont ) { + foreach ( $webfonts as $slug => $webfont ) { $provider = $webfont['provider']; if ( ! isset( $providers[ $provider ] ) ) { /* translators: %s is the provider name. */ error_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) ); continue; } - $webfonts_by_provider[ $provider ] = isset( $webfonts_by_provider[ $provider ] ) ? $webfonts_by_provider[ $provider ] : array(); - $webfonts_by_provider[ $provider ][ $id ] = $webfont; + $webfonts_by_provider[ $provider ] = isset( $webfonts_by_provider[ $provider ] ) ? $webfonts_by_provider[ $provider ] : array(); + $webfonts_by_provider[ $provider ][ $slug ] = $webfont; } /* From 7ad1a8e1c3af3c89fba7ad5f64a2db9a4017982d Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 17 Mar 2022 19:24:56 -0300 Subject: [PATCH 02/17] Add registered font-family groups to theme.json --- .../register-webfonts-from-theme-json.php | 67 +++++-------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php index f18c301764c71..8e82498b17127 100644 --- a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php +++ b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php @@ -31,6 +31,11 @@ function gutenberg_register_webfonts_from_theme_json() { $font_family['fontFace'] = (array) $font_family['fontFace']; foreach ( $font_family['fontFace'] as $font_face ) { + // Skip if the webfont was registered through the Webfonts API. + if ( isset( $font_face['origin'] ) && 'gutenberg_wp_webfonts_api' === $font_face['origin'] ) { + continue; + } + // Check if webfonts have a "src" param, and if they do account for the use of "file:./". if ( ! empty( $font_face['src'] ) ) { $font_face['src'] = (array) $font_face['src']; @@ -71,44 +76,6 @@ function gutenberg_register_webfonts_from_theme_json() { */ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { $font_families_registered = wp_webfonts()->get_fonts(); - $font_families_from_theme = array(); - if ( ! empty( $data['settings'] ) && ! empty( $data['settings']['typography'] ) && ! empty( $data['settings']['typography']['fontFamilies'] ) ) { - $font_families_from_theme = $data['settings']['typography']['fontFamilies']; - } - - /** - * Helper to get an array of the font-families. - * - * @param array $families_data The font-families data. - * - * @return array The font-families array. - */ - $get_families = function( $families_data ) { - $families = array(); - foreach ( $families_data as $family ) { - if ( isset( $family['font-family'] ) ) { - $families[] = $family['font-family']; - } elseif ( isset( $family['fontFamily'] ) ) { - $families[] = $family['fontFamily']; - } - } - - // Micro-optimization: Use array_flip( array_flip( $array ) ) - // instead of array_unique( $array ) because it's faster. - // The result is the same. - return array_flip( array_flip( $families ) ); - }; - - // Diff the arrays to find the missing fonts. - $to_add = array_diff( - $get_families( $font_families_registered ), - $get_families( $font_families_from_theme ) - ); - - // Bail out early if there are no missing fonts. - if ( empty( $to_add ) ) { - return $data; - } // Make sure the path to settings.typography.fontFamilies.theme exists // before adding missing fonts. @@ -122,24 +89,24 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { $data['settings']['typography']['fontFamilies'] = array(); } - // Add missing fonts. - foreach ( $to_add as $family ) { - $font_face = array(); - foreach ( $font_families_registered as $font_family ) { - if ( $family !== $font_family['font-family'] ) { - continue; - } - $camel_cased = array(); - foreach ( $font_family as $key => $value ) { + foreach ( $font_families_registered as $slug => $font_faces_for_family ) { + $family = $font_faces_for_family[0]['font-family']; + + $font_faces = array(); + + foreach ( $font_faces_for_family as $font_face ) { + $camel_cased = array( 'origin' => 'gutenberg_wp_webfonts_api' ); + foreach ( $font_face as $key => $value ) { $camel_cased[ lcfirst( str_replace( '-', '', ucwords( $key, '-' ) ) ) ] = $value; } - $font_face[] = $camel_cased; + $font_faces[] = $camel_cased; } + $data['settings']['typography']['fontFamilies'][] = array( 'fontFamily' => false !== strpos( $family, ' ' ) ? "'{$family}'" : $family, 'name' => $family, - 'slug' => sanitize_title( $family ), - 'fontFace' => $font_face, + 'slug' => $slug, + 'fontFace' => $font_faces, ); } From f061bdfcb39bd83ddd1f4434fb77cf32679ffd58 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 17 Mar 2022 19:12:10 -0300 Subject: [PATCH 03/17] Separate between registering and enqueueing webfonts --- .../wordpress-6.0/class-wp-webfonts.php | 84 +++++++++++++++---- .../register-webfonts-from-theme-json.php | 2 +- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 7c3f7aa2647f9..66309a6e6493d 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -23,17 +23,22 @@ class WP_Webfonts { * * @since 6.0.0 * - * @access private - * @var array + * @var array[] + */ + private $registered_webfonts = array(); + + /** + * An array of enqueued webfonts. + * + * @var array[] */ - private $webfonts = array(); + private $enqueued_webfonts = array(); /** * An array of registered providers. * * @since 6.0.0 * - * @access private * @var array */ private $providers = array(); @@ -72,14 +77,32 @@ public function init() { } /** - * Get the list of fonts. + * Get the list of registered fonts. * * @since 6.0.0 * - * @return array + * @return array[] */ - public function get_fonts() { - return $this->webfonts; + public function get_registered_webfonts() { + return $this->registered_webfonts; + } + + /** + * Get the list of enqueued fonts. + * + * @return array[] + */ + public function get_enqueued_webfonts() { + return $this->enqueued_webfonts; + } + + /** + * Get the list of all fonts. + * + * @return array[] + */ + public function get_all_webfonts() { + return array_merge( $this->get_registered_webfonts(), $this->get_enqueued_webfonts() ); } /** @@ -111,11 +134,41 @@ public function register_font( $font ) { $slug = $this->get_font_slug( $font ); // Initialize a new font-family collection. - if ( ! isset( $this->webfonts[ $slug ] ) ) { - $this->webfonts[ $slug ] = array(); + if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { + $this->registered_webfonts[ $slug ] = array(); + } + + $this->registered_webfonts[ $slug ][] = $font; + } + + /** + * Enqueue a font-family that has been already registered. + * + * @param string $font_family_name The font family name to be enqueued. + */ + public function enqueue_webfont( $font_family_name ) { + $slug = $this->get_font_slug( $font_family_name ); + + if ( isset( $this->enqueued_webfonts[ $slug ] ) ) { + trigger_error( + sprintf( + /* translators: %s unique slug to identify the webfont */ + __( 'The "%s" font family is already enqueued.', 'gutenberg' ), + $slug + ) + ); + + return false; + } + + if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { + _doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is not registered.' ), $slug ), '6.0.0' ); + + return false; } - $this->webfonts[ $slug ][] = $font; + $this->enqueued_webfonts[ $slug ] = $this->registered_webfonts[ $slug ]; + unset( $this->registered_webfonts[ $slug ] ); } /** @@ -244,7 +297,7 @@ public function register_provider( $provider, $class ) { */ public function generate_and_enqueue_styles() { // Generate the styles. - $styles = $this->generate_styles(); + $styles = $this->generate_styles( $this->get_enqueued_webfonts() ); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -266,7 +319,7 @@ public function generate_and_enqueue_styles() { */ public function generate_and_enqueue_editor_styles() { // Generate the styles. - $styles = $this->generate_styles(); + $styles = $this->generate_styles( $this->get_all_webfonts() ); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -281,16 +334,17 @@ public function generate_and_enqueue_editor_styles() { * * @since 6.0.0 * + * @param array[] $font_families Font families and each of their webfonts. * @return string $styles Generated styles. */ - public function generate_styles() { + public function generate_styles( $font_families ) { $styles = ''; $providers = $this->get_providers(); $webfonts = array(); // Grab only the font face declarations from $font_families. - foreach ( $this->get_fonts() as $font_family ) { + foreach ( $font_families as $font_family ) { foreach ( $font_family as $font_face ) { $webfonts[] = $font_face; } diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php index 8e82498b17127..e4bc8ea837732 100644 --- a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php +++ b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php @@ -75,7 +75,7 @@ function gutenberg_register_webfonts_from_theme_json() { * @return array The global styles with missing fonts data. */ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { - $font_families_registered = wp_webfonts()->get_fonts(); + $font_families_registered = wp_webfonts()->get_all_webfonts(); // Make sure the path to settings.typography.fontFamilies.theme exists // before adding missing fonts. From 000c8ec2d322a848e923cf5fb0723caa0702fb61 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Wed, 23 Mar 2022 16:15:50 -0300 Subject: [PATCH 04/17] Add wp_enqueue_webfont method --- lib/compat/wordpress-6.0/webfonts.php | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/compat/wordpress-6.0/webfonts.php index 452539317f053..4553c55eaa17f 100644 --- a/lib/compat/wordpress-6.0/webfonts.php +++ b/lib/compat/wordpress-6.0/webfonts.php @@ -102,6 +102,53 @@ function wp_register_webfont( array $webfont ) { } } +if ( ! function_exists( 'wp_enqueue_webfonts' ) ) { + /** + * Enqueues a collection of font families. + * + * Example of how to enqueue Source Serif Pro and Roboto font families, both registered beforehand. + * + * + * wp_enqueue_webfonts( + * 'Roboto', + * 'Sans Serif Pro' + * ); + * + * + * Font families should be enqueued from the `init` hook or later. + * + * @since 6.0.0 + * + * @param string[] $webfonts Font families to be enqueued. + */ + function wp_enqueue_webfonts( $webfonts ) { + foreach ( $webfonts as $webfont ) { + wp_enqueue_webfont( $webfont ); + } + } +} + +if ( ! function_exists( 'wp_enqueue_webfont' ) ) { + /** + * Enqueue a single font family that has been registered beforehand. + * + * Example of how to enqueue Source Serif Pro font: + * + * + * wp_enqueue_webfont( 'Source Serif Pro' ); + * + * + * Font families should be enqueued from the `init` hook or later. + * + * @since 6.0.0 + * + * @param string $webfont Font family to be enqueued. + */ + function wp_enqueue_webfont( $webfont ) { + wp_webfonts()->enqueue_font( $webfont ); + } +} + if ( ! function_exists( 'wp_register_webfont_provider' ) ) { /** * Registers a custom font service provider. From f1971d424d9042527c2d92ef252eac4ed2c5fa5f Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Wed, 23 Mar 2022 16:16:08 -0300 Subject: [PATCH 05/17] Fix wp_register_webfont description --- lib/compat/wordpress-6.0/webfonts.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/compat/wordpress-6.0/webfonts.php index 4553c55eaa17f..5345e44d94ad8 100644 --- a/lib/compat/wordpress-6.0/webfonts.php +++ b/lib/compat/wordpress-6.0/webfonts.php @@ -40,16 +40,16 @@ function wp_webfonts() { * array( * array( * 'provider' => 'local', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'normal', + * 'font-family' => 'Source Serif Pro', + * 'font-weight' => '200 900', + * 'font-style' => 'normal', * 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), * ), * array( * 'provider' => 'local', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'italic', + * 'font-family' => 'Source Serif Pro', + * 'font-weight' => '200 900', + * 'font-style' => 'italic', * 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ), * ), * ) @@ -63,8 +63,6 @@ function wp_webfonts() { * @param array $webfonts Webfonts to be registered. * This contains an array of webfonts to be registered. * Each webfont is an array. - * See {@see WP_Webfonts_Registry::register()} for a list of - * supported arguments for each webfont. */ function wp_register_webfonts( array $webfonts = array() ) { foreach ( $webfonts as $webfont ) { @@ -80,22 +78,22 @@ function wp_register_webfonts( array $webfonts = array() ) { * Example of how to register Source Serif Pro font with font-weight range of 200-900: * * If the font file is contained within the theme: - * ``` + * + * * wp_register_webfont( * array( * 'provider' => 'local', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'normal', + * 'font-family' => 'Source Serif Pro', + * 'font-weight' => '200 900', + * 'font-style' => 'normal', * 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), * ) * ); - * ``` + * * * @since 6.0.0 * * @param array $webfont Webfont to be registered. - * See {@see WP_Webfonts_Registry::register()} for a list of supported arguments. */ function wp_register_webfont( array $webfont ) { wp_webfonts()->register_font( $webfont ); From 009d2b2e8a002614fdae8d805a2bdbeaebadacac Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Wed, 23 Mar 2022 18:45:50 -0300 Subject: [PATCH 06/17] Do not duplicate font families if registering from both theme.json and API --- .../wordpress-6.0/class-wp-webfonts.php | 11 ++++- .../register-webfonts-from-theme-json.php | 46 ++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 66309a6e6493d..60729362e4866 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -178,9 +178,16 @@ public function enqueue_webfont( $font_family_name ) { * * @param array|string $to_convert The value to convert into a slug. Expected as the web font's array or a font-family as a string. */ - private function get_font_slug( $to_convert ) { + public static function get_font_slug( $to_convert ) { if ( is_array( $to_convert ) ) { - $to_convert = $to_convert['font-family']; + if ( isset( $to_convert['font-family'] ) ) { + $to_convert = $to_convert['font-family']; + } elseif ( isset( $to_convert['fontFamily'] ) ) { + $to_convert = $to_convert['fontFamily']; + } else { + _doing_it_wrong( __FUNCTION__, __( 'Could not determine the font family name.' ), '6.0.0' ); + return false; + } } return sanitize_title( $to_convert ); diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php index e4bc8ea837732..3c77a10478850 100644 --- a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php +++ b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php @@ -76,6 +76,40 @@ function gutenberg_register_webfonts_from_theme_json() { */ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { $font_families_registered = wp_webfonts()->get_all_webfonts(); + $font_families_from_theme = array(); + if ( ! empty( $data['settings'] ) && ! empty( $data['settings']['typography'] ) && ! empty( $data['settings']['typography']['fontFamilies'] ) ) { + $font_families_from_theme = $data['settings']['typography']['fontFamilies']; + } + + /** + * Helper to get an array of the font-families. + * + * @param array $families_data The font-families data. + * + * @return array The font-families array. + */ + $get_families = function( $families_data ) { + $families = array(); + foreach ( $families_data as $family ) { + $families[] = WP_Webfonts::get_font_slug( $family ); + } + + // Micro-optimization: Use array_flip( array_flip( $array ) ) + // instead of array_unique( $array ) because it's faster. + // The result is the same. + return array_flip( array_flip( $families ) ); + }; + + // Diff the arrays to find the missing fonts. + $to_add = array_diff( + array_keys( $font_families_registered ), + $get_families( $font_families_from_theme ) + ); + + // Bail out early if there are no missing fonts. + if ( empty( $to_add ) ) { + return $data; + } // Make sure the path to settings.typography.fontFamilies.theme exists // before adding missing fonts. @@ -89,10 +123,10 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { $data['settings']['typography']['fontFamilies'] = array(); } - foreach ( $font_families_registered as $slug => $font_faces_for_family ) { - $family = $font_faces_for_family[0]['font-family']; - - $font_faces = array(); + foreach ( $to_add as $slug ) { + $font_faces_for_family = $font_families_registered[ $slug ]; + $family_name = $font_faces_for_family[0]['font-family']; + $font_faces = array(); foreach ( $font_faces_for_family as $font_face ) { $camel_cased = array( 'origin' => 'gutenberg_wp_webfonts_api' ); @@ -103,8 +137,8 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { } $data['settings']['typography']['fontFamilies'][] = array( - 'fontFamily' => false !== strpos( $family, ' ' ) ? "'{$family}'" : $family, - 'name' => $family, + 'fontFamily' => false !== strpos( $family_name, ' ' ) ? "'{$family_name}'" : $family_name, + 'name' => $family_name, 'slug' => $slug, 'fontFace' => $font_faces, ); From 826a94bd8296d83ee9f9784d515dccc6f08d53de Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Wed, 23 Mar 2022 16:19:53 -0700 Subject: [PATCH 07/17] Rename font to webfont where appropriate --- .../wordpress-6.0/class-wp-webfonts.php | 48 +++++++++---------- .../register-webfonts-from-theme-json.php | 2 +- lib/compat/wordpress-6.0/webfonts.php | 4 +- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 60729362e4866..6f46f905ce3d4 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -84,7 +84,7 @@ public function init() { * @return array[] */ public function get_registered_webfonts() { - return $this->registered_webfonts; + return $this->registered_webfonts; } /** @@ -121,24 +121,24 @@ public function get_providers() { * * @since 6.0.0 * - * @param array $font The font arguments. + * @param array $webfont The font argument. */ - public function register_font( $font ) { - $font = $this->validate_font( $font ); + public function register_webfont( $webfont ) { + $webfont = $this->validate_webfont( $webfont ); // If not valid, bail out. - if ( ! $font ) { + if ( ! $webfont ) { return false; } - $slug = $this->get_font_slug( $font ); + $slug = $this->get_font_slug( $webfont ); // Initialize a new font-family collection. if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { $this->registered_webfonts[ $slug ] = array(); } - $this->registered_webfonts[ $slug ][] = $font; + $this->registered_webfonts[ $slug ][] = $webfont; } /** @@ -194,17 +194,17 @@ public static function get_font_slug( $to_convert ) { } /** - * Validate a font. + * Validate a webfont. * * @since 6.0.0 * - * @param array $font The font arguments. + * @param array $webfont The webfont arguments. * - * @return array|false The validated font arguments, or false if the font is invalid. + * @return array|false The validated webfont arguments, or false if the webfont is invalid. */ - public function validate_font( $font ) { - $font = wp_parse_args( - $font, + public function validate_webfont( $webfont ) { + $webfont = wp_parse_args( + $webfont, array( 'provider' => 'local', 'font-family' => '', @@ -215,23 +215,23 @@ public function validate_font( $font ) { ); // Check the font-family. - if ( empty( $font['font-family'] ) || ! is_string( $font['font-family'] ) ) { + if ( empty( $webfont['font-family'] ) || ! is_string( $webfont['font-family'] ) ) { trigger_error( __( 'Webfont font family must be a non-empty string.', 'gutenberg' ) ); return false; } // Local fonts need a "src". - if ( 'local' === $font['provider'] ) { + if ( 'local' === $webfont['provider'] ) { // Make sure that local fonts have 'src' defined. - if ( empty( $font['src'] ) || ( ! is_string( $font['src'] ) && ! is_array( $font['src'] ) ) ) { + if ( empty( $webfont['src'] ) || ( ! is_string( $webfont['src'] ) && ! is_array( $webfont['src'] ) ) ) { trigger_error( __( 'Webfont src must be a non-empty string or an array of strings.', 'gutenberg' ) ); return false; } } // Validate the 'src' property. - if ( ! empty( $font['src'] ) ) { - foreach ( (array) $font['src'] as $src ) { + if ( ! empty( $webfont['src'] ) ) { + foreach ( (array) $webfont['src'] as $src ) { if ( empty( $src ) || ! is_string( $src ) ) { trigger_error( __( 'Each webfont src must be a non-empty string.', 'gutenberg' ) ); return false; @@ -240,14 +240,14 @@ public function validate_font( $font ) { } // Check the font-weight. - if ( ! is_string( $font['font-weight'] ) && ! is_int( $font['font-weight'] ) ) { + if ( ! is_string( $webfont['font-weight'] ) && ! is_int( $webfont['font-weight'] ) ) { trigger_error( __( 'Webfont font weight must be a properly formatted string or integer.', 'gutenberg' ) ); return false; } // Check the font-display. - if ( ! in_array( $font['font-display'], array( 'auto', 'block', 'fallback', 'swap' ), true ) ) { - $font['font-display'] = 'fallback'; + if ( ! in_array( $webfont['font-display'], array( 'auto', 'block', 'fallback', 'swap' ), true ) ) { + $webfont['font-display'] = 'fallback'; } $valid_props = array( @@ -270,13 +270,13 @@ public function validate_font( $font ) { 'provider', ); - foreach ( $font as $prop => $value ) { + foreach ( $webfont as $prop => $value ) { if ( ! in_array( $prop, $valid_props, true ) ) { - unset( $font[ $prop ] ); + unset( $webfont[ $prop ] ); } } - return $font; + return $webfont; } /** diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php index 3c77a10478850..a845b31b078bb 100644 --- a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php +++ b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php @@ -63,7 +63,7 @@ function gutenberg_register_webfonts_from_theme_json() { } } foreach ( $webfonts as $webfont ) { - wp_webfonts()->register_font( $webfont ); + wp_webfonts()->register_webfont( $webfont ); } } diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/compat/wordpress-6.0/webfonts.php index 5345e44d94ad8..c700b89885c64 100644 --- a/lib/compat/wordpress-6.0/webfonts.php +++ b/lib/compat/wordpress-6.0/webfonts.php @@ -96,7 +96,7 @@ function wp_register_webfonts( array $webfonts = array() ) { * @param array $webfont Webfont to be registered. */ function wp_register_webfont( array $webfont ) { - wp_webfonts()->register_font( $webfont ); + wp_webfonts()->register_webfont( $webfont ); } } @@ -143,7 +143,7 @@ function wp_enqueue_webfonts( $webfonts ) { * @param string $webfont Font family to be enqueued. */ function wp_enqueue_webfont( $webfont ) { - wp_webfonts()->enqueue_font( $webfont ); + wp_webfonts()->enqueue_webfont( $webfont ); } } From bbb81977b0c13ab7d6889841790e9c6c55ffc081 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Mon, 28 Mar 2022 16:33:26 -0300 Subject: [PATCH 08/17] Add tests --- .../wordpress-6.0/class-wp-webfonts.php | 1 + phpunit/class-wp-webfonts-test.php | 306 ++++++++++++++++-- 2 files changed, 276 insertions(+), 31 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 6f46f905ce3d4..d72318ede691e 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -333,6 +333,7 @@ public function generate_and_enqueue_editor_styles() { return; } + wp_enqueue_style( 'wp-block-library' ); wp_add_inline_style( 'wp-block-library', $styles ); } diff --git a/phpunit/class-wp-webfonts-test.php b/phpunit/class-wp-webfonts-test.php index 382c1b1583e0d..e3a12c1b23374 100644 --- a/phpunit/class-wp-webfonts-test.php +++ b/phpunit/class-wp-webfonts-test.php @@ -26,16 +26,20 @@ function tearDown() { } /** + * Test wp_register_webfonts() bulk register webfonts. + * * @covers wp_register_webfonts * @covers WP_Webfonts::register_font - * @covers WP_Webfonts::get_fonts - * @covers WP_Webfonts::get_font_id + * @covers WP_Webfonts::get_registered_fonts + * @covers WP_Webfonts::get_enqueued_fonts */ - public function test_get_fonts() { + public function test_wp_register_webfonts() { + $font_family_name = 'Source Serif Pro'; + $fonts = array( array( 'provider' => 'local', - 'font-family' => 'Source Serif Pro', + 'font-family' => $font_family_name, 'font-style' => 'normal', 'font-weight' => '200 900', 'font-stretch' => 'normal', @@ -44,7 +48,7 @@ public function test_get_fonts() { ), array( 'provider' => 'local', - 'font-family' => 'Source Serif Pro', + 'font-family' => $font_family_name, 'font-style' => 'italic', 'font-weight' => '200 900', 'font-stretch' => 'normal', @@ -54,12 +58,170 @@ public function test_get_fonts() { ); $expected = array( - 'source-serif-pro-200-900-normal-local' => $fonts[0], - 'source-serif-pro-200-900-italic-local' => $fonts[1], + wp_webfonts()->get_font_slug( $font_family_name ) => $fonts, ); wp_register_webfonts( $fonts ); - $this->assertEquals( $expected, wp_webfonts()->get_fonts() ); + $this->assertEquals( $expected, wp_webfonts()->get_registered_webfonts() ); + $this->assertEquals( array(), wp_webfonts()->get_enqueued_webfonts() ); + } + + /** + * Test wp_register_webfont() register a single webfont. + * + * @covers wp_register_webfont + * @covers WP_Webfonts::register_font + * @covers WP_Webfonts::get_registered_fonts + * @covers WP_Webfonts::get_enqueued_fonts + */ + public function test_wp_register_webfont() { + $font_family_name = 'Source Serif Pro'; + + $font = array( + 'provider' => 'local', + 'font-family' => $font_family_name, + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ); + + $expected = array( + wp_webfonts()->get_font_slug( $font_family_name ) => array( $font ), + ); + + wp_register_webfont( $font ); + $this->assertEquals( $expected, wp_webfonts()->get_registered_webfonts() ); + $this->assertEquals( array(), wp_webfonts()->get_enqueued_webfonts() ); + } + + /** + * Test wp_register_webfont() does not enqueue the webfont on registration. + * + * @covers wp_register_webfont + * @covers WP_Webfonts::register_font + * @covers WP_Webfonts::get_registered_fonts + * @covers WP_Webfonts::get_enqueued_fonts + */ + public function test_wp_register_webfont_does_not_enqueue_on_registration() { + $font_family_name = 'Source Serif Pro'; + + $font = array( + 'provider' => 'local', + 'font-family' => $font_family_name, + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ); + + $expected = array( + wp_webfonts()->get_font_slug( $font_family_name ) => array( $font ), + ); + + wp_register_webfont( $font ); + $this->assertEquals( $expected, wp_webfonts()->get_registered_webfonts() ); + $this->assertEquals( array(), wp_webfonts()->get_enqueued_webfonts() ); + } + + /** + * Test wp_enqueue_webfonts() bulk enqueue webfonts. + * + * @covers wp_enqueue_webfonts + * @covers WP_Webfonts::enqueue_font + * @covers WP_Webfonts::get_enqueued_fonts + * @covers WP_Webfonts::get_registered_fonts + */ + public function test_wp_enqueue_webfonts() { + $source_serif_pro = array( + 'provider' => 'local', + 'font-family' => 'Source Serif Pro', + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ); + + wp_register_webfont( $source_serif_pro ); + + $roboto = array( + 'provider' => 'local', + 'font-family' => 'Roboto', + 'font-style' => 'normal', + 'font-weight' => '400', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/roboto/Roboto.ttf.woff2', + 'font-display' => 'fallback', + ); + + wp_register_webfont( $roboto ); + + $expected = array( + wp_webfonts()->get_font_slug( $source_serif_pro['font-family'] ) => array( $source_serif_pro ), + wp_webfonts()->get_font_slug( $roboto['font-family'] ) => array( $roboto ), + ); + + wp_enqueue_webfonts( + array( + $source_serif_pro['font-family'], + $roboto['font-family'], + ) + ); + + $this->assertEquals( $expected, wp_webfonts()->get_enqueued_webfonts() ); + $this->assertEquals( array(), wp_webfonts()->get_registered_webfonts() ); + } + + /** + * Test wp_enqueue_font() enqueues a registered webfont. + * + * @covers wp_enqueue_webfont + * @covers WP_Webfonts::enqueued_font + * @covers WP_Webfonts::get_enqueued_fonts + * @covers WP_Webfonts::get_registered_fonts + */ + public function test_wp_enqueue_webfont_enqueues_registered_webfont() { + $font_family_name = 'Source Serif Pro'; + + $font = array( + 'provider' => 'local', + 'font-family' => $font_family_name, + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ); + + $expected = array( + wp_webfonts()->get_font_slug( $font_family_name ) => array( $font ), + ); + + wp_register_webfont( $font ); + wp_enqueue_webfont( $font_family_name ); + + $this->assertEquals( array(), wp_webfonts()->get_registered_webfonts() ); + $this->assertEquals( $expected, wp_webfonts()->get_enqueued_webfonts() ); + } + + /** + * Test wp_enqueue_font() does not enqueue a webfont that was not registered. + * + * @covers wp_enqueue_webfont + * @covers WP_Webfonts::enqueued_font + * @covers WP_Webfonts::get_enqueued_fonts + * @covers WP_Webfonts::get_registered_fonts + */ + public function test_wp_enqueue_webfont_does_not_enqueue_unregistered_webfont() { + $font_family_name = 'Source Serif Pro'; + + wp_enqueue_webfont( $font_family_name ); + + $this->assertEquals( array(), wp_webfonts()->get_registered_webfonts() ); + $this->assertEquals( array(), wp_webfonts()->get_enqueued_webfonts() ); } /** @@ -79,11 +241,28 @@ public function test_get_providers() { } /** - * @covers WP_Webfonts::validate_font + * @covers WP:Webfonts::get_font_slug */ - public function test_validate_font() { + public function test_get_font_slug() { + // Test array without font family property. + $this->assertFalse( wp_webfonts()->get_font_slug( array() ) ); + + // Test $webfont['fontFamily'] returns the correct slug. + $this->assertEquals( 'source-serif-pro', wp_webfonts()->get_font_slug( array( 'fontFamily' => 'Source Serif Pro' ) ) ); + + // Test $webfont['font-family'] returns the correct slug. + $this->assertEquals( 'source-serif-pro', wp_webfonts()->get_font_slug( array( 'font-family' => 'Source Serif Pro' ) ) ); + + // Test a font family name returns the correct slug. + $this->assertEquals( 'source-serif-pro', wp_webfonts()->get_font_slug( 'Source Serif Pro' ) ); + } + + /** + * @covers WP_Webfonts::validate_webfont + */ + public function test_validate_webfont() { // Test empty array. - $this->assertFalse( wp_webfonts()->validate_font( array() ) ); + $this->assertFalse( wp_webfonts()->validate_webfont( array() ) ); $font = array( 'font-family' => 'Test Font 1', @@ -91,55 +270,120 @@ public function test_validate_font() { ); // Test missing provider fallback to local. - $this->assertEquals( 'local', wp_webfonts()->validate_font( $font )['provider'] ); + $this->assertEquals( 'local', wp_webfonts()->validate_webfont( $font )['provider'] ); // Test missing font-weight fallback to 400. - $this->assertEquals( '400', wp_webfonts()->validate_font( $font )['font-weight'] ); + $this->assertEquals( '400', wp_webfonts()->validate_webfont( $font )['font-weight'] ); // Test missing font-style fallback to normal. - $this->assertEquals( 'normal', wp_webfonts()->validate_font( $font )['font-style'] ); + $this->assertEquals( 'normal', wp_webfonts()->validate_webfont( $font )['font-style'] ); // Test missing font-display fallback to fallback. - $this->assertEquals( 'fallback', wp_webfonts()->validate_font( $font )['font-display'] ); + $this->assertEquals( 'fallback', wp_webfonts()->validate_webfont( $font )['font-display'] ); // Test local font with missing "src". - $this->assertFalse( wp_webfonts()->validate_font( array( 'font-family' => 'Test Font 2' ) ) ); + $this->assertFalse( wp_webfonts()->validate_webfont( array( 'font-family' => 'Test Font 2' ) ) ); // Test valid src URL, without a protocol. $font['src'] = '//example.com/SourceSerif4Variable-Roman.ttf.woff2'; - $this->assertEquals( wp_webfonts()->validate_font( $font )['src'], $font['src'] ); + $this->assertEquals( wp_webfonts()->validate_webfont( $font )['src'], $font['src'] ); // Test font-weight. $font_weights = array( 100, '100', '100 900', 'normal' ); foreach ( $font_weights as $value ) { $font['font-weight'] = $value; - $this->assertEquals( wp_webfonts()->validate_font( $font )['font-weight'], $value ); + $this->assertEquals( wp_webfonts()->validate_webfont( $font )['font-weight'], $value ); } // Test that invalid keys get removed from the font. $font['invalid-key'] = 'invalid'; - $this->assertArrayNotHasKey( 'invalid-key', wp_webfonts()->validate_font( $font ) ); + $this->assertArrayNotHasKey( 'invalid-key', wp_webfonts()->validate_webfont( $font ) ); } /** + * Test generate_and_enqueue_styles outputs only enqueued webfonts. + * * @covers WP_Webfonts::generate_styles */ public function test_generate_styles() { - $font = array( - 'provider' => 'local', - 'font-family' => 'Source Serif Pro', - 'font-style' => 'normal', - 'font-weight' => '200 900', - 'font-stretch' => 'normal', - 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', - 'font-display' => 'fallback', + wp_register_webfonts( + array( + array( + 'provider' => 'local', + 'font-family' => 'Roboto', + 'font-style' => 'normal', + 'font-weight' => '400', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/roboto/Roboto-Regular.ttf', + 'font-display' => 'fallback', + ), + array( + 'provider' => 'local', + 'font-family' => 'Source Serif Pro', + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ), + ) ); - wp_register_webfont( $font ); + wp_enqueue_webfont( 'Source Serif Pro' ); - $this->assertEquals( - '@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2\') format(\'woff2\');}', - wp_webfonts()->generate_styles() + wp_webfonts()->generate_and_enqueue_styles(); + + $expected = <<assertContains( + $expected, + get_echo( 'wp_print_styles' ) + ); + } + + /** + * Test generate_and_enqueue_editor_styles outputs registered and enqueued webfonts. + * Both are necessary so the editor correctly loads webfonts picked while in the editor. + * + * @covers WP_Webfonts::generate_and_enqueue_editor_styles + */ + public function test_generate_and_enqueue_editor_styles() { + wp_register_webfonts( + array( + array( + 'provider' => 'local', + 'font-family' => 'Roboto', + 'font-style' => 'normal', + 'font-weight' => '400', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/roboto/Roboto-Regular.ttf', + 'font-display' => 'fallback', + ), + array( + 'provider' => 'local', + 'font-family' => 'Source Serif Pro', + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ), + ) + ); + + wp_enqueue_webfont( 'Source Serif Pro' ); + + wp_webfonts()->generate_and_enqueue_editor_styles(); + + $expected = <<assertContains( + $expected, + get_echo( 'wp_print_styles' ) ); } } From e1ea79f5c4578e4de8fa710051204dcd6f07c71b Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Mon, 28 Mar 2022 16:46:03 -0300 Subject: [PATCH 09/17] Fix linter offenses --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index d72318ede691e..8d48afa3a191b 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -152,7 +152,7 @@ public function enqueue_webfont( $font_family_name ) { if ( isset( $this->enqueued_webfonts[ $slug ] ) ) { trigger_error( sprintf( - /* translators: %s unique slug to identify the webfont */ + /* translators: %s unique slug to identify the font family of the webfont */ __( 'The "%s" font family is already enqueued.', 'gutenberg' ), $slug ) @@ -162,7 +162,8 @@ public function enqueue_webfont( $font_family_name ) { } if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { - _doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is not registered.' ), $slug ), '6.0.0' ); + /* translators: %s unique slug to identify the font family of the webfont */ + _doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is not registered.', 'gutenberg' ), $slug ), '6.0.0' ); return false; } @@ -185,7 +186,7 @@ public static function get_font_slug( $to_convert ) { } elseif ( isset( $to_convert['fontFamily'] ) ) { $to_convert = $to_convert['fontFamily']; } else { - _doing_it_wrong( __FUNCTION__, __( 'Could not determine the font family name.' ), '6.0.0' ); + _doing_it_wrong( __FUNCTION__, __( 'Could not determine the font family name.', 'gutenberg' ), '6.0.0' ); return false; } } From bf7b3493010bb7e196521e0faec83477f26c8619 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Mon, 28 Mar 2022 17:57:16 -0300 Subject: [PATCH 10/17] Fire gutenberg_register_webfonts_from_theme_json on init --- lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php index a845b31b078bb..68d38a9042bff 100644 --- a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php +++ b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php @@ -147,4 +147,4 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { return $data; } -add_action( 'wp_loaded', 'gutenberg_register_webfonts_from_theme_json' ); +add_action( 'init', 'gutenberg_register_webfonts_from_theme_json' ); From 340df68716ad3a7c567c47b11a3c21e31c5de913 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Wed, 30 Mar 2022 17:22:57 -0300 Subject: [PATCH 11/17] Move webfonts logic from wordpress-6.0 to experimental The API, although looking really good, is not ready for Core. So we must move it from wordpress-6.0 to experimental, and only add it there when it's ready to enter WordPress. Relocate files to /lib/experimental. Per the Gutenberg PHP > File structure documentation https://github.com/WordPress/gutenberg/tree/trunk/lib#file-structure. --- .../class-wp-theme-json-resolver-6-0.php | 64 +++++++++++++++++++ ...class-wp-theme-json-resolver-gutenberg.php | 49 +------------- .../class-wp-webfonts-provider-local.php | 0 .../class-wp-webfonts-provider.php | 0 .../class-wp-webfonts.php | 0 .../register-webfonts-from-theme-json.php | 0 .../webfonts.php | 0 lib/load.php | 14 ++-- 8 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php rename lib/{compat/wordpress-6.0 => experimental}/class-wp-theme-json-resolver-gutenberg.php (66%) rename lib/{compat/wordpress-6.0 => experimental}/class-wp-webfonts-provider-local.php (100%) rename lib/{compat/wordpress-6.0 => experimental}/class-wp-webfonts-provider.php (100%) rename lib/{compat/wordpress-6.0 => experimental}/class-wp-webfonts.php (100%) rename lib/{compat/wordpress-6.0 => experimental}/register-webfonts-from-theme-json.php (100%) rename lib/{compat/wordpress-6.0 => experimental}/webfonts.php (100%) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php b/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php new file mode 100644 index 0000000000000..7b6e84c72a00f --- /dev/null +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php @@ -0,0 +1,64 @@ + $file ) { + $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); + if ( is_array( $decoded_file ) ) { + $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); + $variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data(); + if ( empty( $variation['title'] ) ) { + $variation['title'] = basename( $path, '.json' ); + } + $variations[] = $variation; + } + } + } + return $variations; + } +} diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php similarity index 66% rename from lib/compat/wordpress-6.0/class-wp-theme-json-resolver-gutenberg.php rename to lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 347554e6453fc..7714b6256d136 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -15,54 +15,7 @@ * * @access private */ -class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_5_9 { - - /** - * Given a theme.json structure modifies it in place - * to update certain values by its translated strings - * according to the language set by the user. - * - * @param array $theme_json The theme.json to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return array Returns the modified $theme_json_structure. - */ - protected static function translate( $theme_json, $domain = 'default' ) { - if ( null === static::$i18n_schema ) { - $i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' ); - static::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema; - } - - return wp_translate_settings_using_i18n_schema( static::$i18n_schema, $theme_json, $domain ); - } - - /** - * Returns the style variations defined by the theme. - * - * @return array - */ - public static function get_style_variations() { - $variations = array(); - $base_directory = get_stylesheet_directory() . '/styles'; - if ( is_dir( $base_directory ) ) { - $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); - $nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ); - ksort( $nested_html_files ); - foreach ( $nested_html_files as $path => $file ) { - $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); - if ( is_array( $decoded_file ) ) { - $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); - $variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data(); - if ( empty( $variation['title'] ) ) { - $variation['title'] = basename( $path, '.json' ); - } - $variations[] = $variation; - } - } - } - return $variations; - } - +class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_0 { /** * Returns the theme's data. * diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts-provider-local.php b/lib/experimental/class-wp-webfonts-provider-local.php similarity index 100% rename from lib/compat/wordpress-6.0/class-wp-webfonts-provider-local.php rename to lib/experimental/class-wp-webfonts-provider-local.php diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts-provider.php b/lib/experimental/class-wp-webfonts-provider.php similarity index 100% rename from lib/compat/wordpress-6.0/class-wp-webfonts-provider.php rename to lib/experimental/class-wp-webfonts-provider.php diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php similarity index 100% rename from lib/compat/wordpress-6.0/class-wp-webfonts.php rename to lib/experimental/class-wp-webfonts.php diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/experimental/register-webfonts-from-theme-json.php similarity index 100% rename from lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php rename to lib/experimental/register-webfonts-from-theme-json.php diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/experimental/webfonts.php similarity index 100% rename from lib/compat/wordpress-6.0/webfonts.php rename to lib/experimental/webfonts.php diff --git a/lib/load.php b/lib/load.php index 56d633964a96f..07cb509825dce 100644 --- a/lib/load.php +++ b/lib/load.php @@ -101,17 +101,17 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.0/class-wp-rest-block-patterns-controller.php'; require __DIR__ . '/compat/wordpress-6.0/class-wp-rest-block-pattern-categories-controller.php'; require __DIR__ . '/compat/wordpress-6.0/functions.php'; -require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php'; +require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php'; require __DIR__ . '/compat/wordpress-6.0/rest-api.php'; require __DIR__ . '/compat/wordpress-6.0/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.0/block-template.php'; -require __DIR__ . '/compat/wordpress-6.0/register-webfonts-from-theme-json.php'; -require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-gutenberg.php'; -require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts.php'; -require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider.php'; -require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider-local.php'; -require __DIR__ . '/compat/wordpress-6.0/webfonts.php'; require __DIR__ . '/compat/wordpress-6.0/edit-form-blocks.php'; +require __DIR__ . '/experimental/register-webfonts-from-theme-json.php'; +require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php'; +require __DIR__ . '/experimental/class-wp-webfonts.php'; +require __DIR__ . '/experimental/class-wp-webfonts-provider.php'; +require __DIR__ . '/experimental/class-wp-webfonts-provider-local.php'; +require __DIR__ . '/experimental/webfonts.php'; require __DIR__ . '/experimental/blocks.php'; require __DIR__ . '/blocks.php'; From 9fe1f8001e0e88a74485ac61d5a49f7836d56546 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Mar 2022 07:17:35 -0500 Subject: [PATCH 12/17] Update return types and comment for consistency and matching. --- lib/experimental/class-wp-webfonts.php | 16 ++++++++++------ lib/experimental/webfonts.php | 5 ++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/experimental/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php index 8d48afa3a191b..1a3b4e27f4478 100644 --- a/lib/experimental/class-wp-webfonts.php +++ b/lib/experimental/class-wp-webfonts.php @@ -58,7 +58,6 @@ class WP_Webfonts { * @since 6.0.0 */ public function init() { - // Register default providers. $this->register_provider( 'local', 'WP_Webfonts_Provider_Local' ); @@ -110,7 +109,7 @@ public function get_all_webfonts() { * * @since 6.0.0 * - * @return array + * @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID. */ public function get_providers() { return $this->providers; @@ -122,8 +121,9 @@ public function get_providers() { * @since 6.0.0 * * @param array $webfont The font argument. + * @return bool True if successfully registered, else false. */ - public function register_webfont( $webfont ) { + public function register_webfont( array $webfont ) { $webfont = $this->validate_webfont( $webfont ); // If not valid, bail out. @@ -139,12 +139,14 @@ public function register_webfont( $webfont ) { } $this->registered_webfonts[ $slug ][] = $webfont; + return true; } /** * Enqueue a font-family that has been already registered. * * @param string $font_family_name The font family name to be enqueued. + * @return bool True if successfully enqueued, else false. */ public function enqueue_webfont( $font_family_name ) { $slug = $this->get_font_slug( $font_family_name ); @@ -170,6 +172,7 @@ public function enqueue_webfont( $font_family_name ) { $this->enqueued_webfonts[ $slug ] = $this->registered_webfonts[ $slug ]; unset( $this->registered_webfonts[ $slug ] ); + return true; } /** @@ -177,7 +180,9 @@ public function enqueue_webfont( $font_family_name ) { * * @since 6.0.0 * - * @param array|string $to_convert The value to convert into a slug. Expected as the web font's array or a font-family as a string. + * @param array|string $to_convert The value to convert into a slug. Expected as the web font's array + * or a font-family as a string. + * @return string|false The font slug on success, or false if the font-family cannot be determined. */ public static function get_font_slug( $to_convert ) { if ( is_array( $to_convert ) ) { @@ -287,8 +292,7 @@ public function validate_webfont( $webfont ) { * * @param string $provider The provider name. * @param string $class The provider class name. - * - * @return bool Whether the provider was registered successfully. + * @return bool True if successfully registered, else false. */ public function register_provider( $provider, $class ) { if ( empty( $provider ) || empty( $class ) ) { diff --git a/lib/experimental/webfonts.php b/lib/experimental/webfonts.php index c700b89885c64..8d4fb91534e0c 100644 --- a/lib/experimental/webfonts.php +++ b/lib/experimental/webfonts.php @@ -171,7 +171,7 @@ function wp_enqueue_webfont( $webfont ) { * The class should be a child of `WP_Webfonts_Provider`. * See {@see WP_Webfonts_Provider}. * - * @return bool True when registered. False when provider does not exist. + * @return bool True if successfully registered, else false. */ function wp_register_webfont_provider( $name, $classname ) { return wp_webfonts()->register_provider( $name, $classname ); @@ -193,8 +193,7 @@ function wp_register_webfont_provider( $name, $classname ) { * * @since 6.0.0 * - * @return WP_Webfonts_Provider[] All registered providers, - * each keyed by their unique ID. + * @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID. */ function wp_get_webfont_providers() { return wp_webfonts()->get_providers(); From 6fba7fb542de70937cd77838ddd355746e87b5b7 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Mar 2022 09:19:53 -0500 Subject: [PATCH 13/17] Use trigger_log instead of error_log(). --- lib/experimental/class-wp-webfonts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php index 1a3b4e27f4478..02fa0862e9fdd 100644 --- a/lib/experimental/class-wp-webfonts.php +++ b/lib/experimental/class-wp-webfonts.php @@ -369,7 +369,7 @@ public function generate_styles( $font_families ) { $provider = $webfont['provider']; if ( ! isset( $providers[ $provider ] ) ) { /* translators: %s is the provider name. */ - error_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) ); + trigger_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) ); continue; } $webfonts_by_provider[ $provider ] = isset( $webfonts_by_provider[ $provider ] ) ? $webfonts_by_provider[ $provider ] : array(); @@ -385,7 +385,7 @@ public function generate_styles( $font_families ) { // Bail out if the provider class does not exist. if ( ! class_exists( $provider_class ) ) { /* translators: %s is the provider name. */ - error_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider_id ) ); + trigger_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider_id ) ); continue; } From 839282759275d9f712885d92472b98cfeff20146 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Mar 2022 09:25:39 -0500 Subject: [PATCH 14/17] Fix tests with _doing_it_wrong. Tests for methods that have a `_doing_it_wrong()` require an `expectedIncorrectUsage` annotation, proper usage of test fixture methods, and the usage of `__METHOD__` when invoking `_doing_it_wrong()`. This commit: - Fixes the test fixtures to use snake_case format and calling of the parent fixtures. - Refactors `get_font_slug()` tests into data providers (preferred in Core) and separates tests that will trigger `_doing_it_wrong()` to use the annotation. - Uses `__METHOD__` instead of `__FUNCTION__` in each `_doing_it_wrong()` instance. --- lib/experimental/class-wp-webfonts.php | 4 +- phpunit/class-wp-webfonts-test.php | 90 ++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/lib/experimental/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php index 02fa0862e9fdd..38788ef33e13a 100644 --- a/lib/experimental/class-wp-webfonts.php +++ b/lib/experimental/class-wp-webfonts.php @@ -165,7 +165,7 @@ public function enqueue_webfont( $font_family_name ) { if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { /* translators: %s unique slug to identify the font family of the webfont */ - _doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is not registered.', 'gutenberg' ), $slug ), '6.0.0' ); + _doing_it_wrong( __METHOD__, sprintf( __( 'The "%s" font family is not registered.', 'gutenberg' ), $slug ), '6.0.0' ); return false; } @@ -191,7 +191,7 @@ public static function get_font_slug( $to_convert ) { } elseif ( isset( $to_convert['fontFamily'] ) ) { $to_convert = $to_convert['fontFamily']; } else { - _doing_it_wrong( __FUNCTION__, __( 'Could not determine the font family name.', 'gutenberg' ), '6.0.0' ); + _doing_it_wrong( __METHOD__, __( 'Could not determine the font family name.', 'gutenberg' ), '6.0.0' ); return false; } } diff --git a/phpunit/class-wp-webfonts-test.php b/phpunit/class-wp-webfonts-test.php index e3a12c1b23374..49399879a74ba 100644 --- a/phpunit/class-wp-webfonts-test.php +++ b/phpunit/class-wp-webfonts-test.php @@ -12,17 +12,20 @@ class WP_Webfonts_Test extends WP_UnitTestCase { */ private $old_wp_webfonts; - function setUp() { + public function set_up() { + parent::set_up(); + global $wp_webfonts; $this->old_wp_webfonts = $wp_webfonts; $wp_webfonts = null; } - function tearDown() { + public function tear_down() { global $wp_webfonts; $wp_webfonts = $this->old_wp_webfonts; + parent::tear_down(); } /** @@ -214,14 +217,16 @@ public function test_wp_enqueue_webfont_enqueues_registered_webfont() { * @covers WP_Webfonts::enqueued_font * @covers WP_Webfonts::get_enqueued_fonts * @covers WP_Webfonts::get_registered_fonts + * + * @expectedIncorrectUsage WP_Webfonts::enqueue_webfont */ public function test_wp_enqueue_webfont_does_not_enqueue_unregistered_webfont() { $font_family_name = 'Source Serif Pro'; wp_enqueue_webfont( $font_family_name ); - $this->assertEquals( array(), wp_webfonts()->get_registered_webfonts() ); - $this->assertEquals( array(), wp_webfonts()->get_enqueued_webfonts() ); + $this->assertSame( array(), wp_webfonts()->get_registered_webfonts(), 'WP_Webfonts::get_registered_webfonts should return an empty array' ); + $this->assertSame( array(), wp_webfonts()->get_enqueued_webfonts(), 'WP_Webfonts::get_enqueued_webfonts should return an empty array' ); } /** @@ -241,20 +246,77 @@ public function test_get_providers() { } /** - * @covers WP:Webfonts::get_font_slug + * @dataProvider data_get_font_slug_when_cannot_determine_fontfamily + * @covers WP_Webfonts::get_font_slug + * + * @expectedIncorrectUsage WP_Webfonts::get_font_slug */ - public function test_get_font_slug() { - // Test array without font family property. - $this->assertFalse( wp_webfonts()->get_font_slug( array() ) ); + public function test_get_font_slug_when_cannot_determine_fontfamily( $to_convert ) { + $this->assertFalse( wp_webfonts()->get_font_slug( $to_convert ) ); + } - // Test $webfont['fontFamily'] returns the correct slug. - $this->assertEquals( 'source-serif-pro', wp_webfonts()->get_font_slug( array( 'fontFamily' => 'Source Serif Pro' ) ) ); + /** + * Data provider. + * + * @return array[] + */ + public function data_get_font_slug_when_cannot_determine_fontfamily() { + return array( + 'empty array' => array( array() ), + 'array without a font-family key' => array( + array( + 'provider' => 'local', + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-display' => 'fallback', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + ), + ), + "array with 'font family' key" => array( + array( 'font family' => 'Source Serif Pro' ), + ), + "array with 'Font-Family' key" => array( + array( 'Font-Family' => 'Source Serif Pro' ), + ), + "array with 'FontFamily' key" => array( + array( 'FontFamily' => 'Source Serif Pro' ), + ), + ); + } - // Test $webfont['font-family'] returns the correct slug. - $this->assertEquals( 'source-serif-pro', wp_webfonts()->get_font_slug( array( 'font-family' => 'Source Serif Pro' ) ) ); + /** + * @dataProvider data_get_font_slug + * @covers WP_Webfonts::get_font_slug + */ + public function test_get_font_slug( $to_convert, $expected ) { + $this->assertSame( $expected, wp_webfonts()->get_font_slug( $to_convert ) ); + } - // Test a font family name returns the correct slug. - $this->assertEquals( 'source-serif-pro', wp_webfonts()->get_font_slug( 'Source Serif Pro' ) ); + /** + * Data provider. + * + * @return array[] + */ + public function data_get_font_slug() { + return array( + "array using 'fontFamily' format" => array( + 'to_convert' => array( 'fontFamily' => 'Source Serif Pro' ), + 'expected' => 'source-serif-pro', + ), + "array using 'font-family' format" => array( + 'to_convert' => array( 'font-family' => 'Source Serif Pro' ), + 'expected' => 'source-serif-pro', + ), + 'string with font family name' => array( + 'to_convert' => 'Source Serif Pro', + 'expected' => 'source-serif-pro', + ), + 'empty string' => array( + 'to_convert' => '', + 'expected' => '', + ), + ); } /** From 6d6627220d285abcc9a07ca76cce1deb5188014d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Mar 2022 09:34:34 -0500 Subject: [PATCH 15/17] Add `get_webfonts_by_provider()` method to handle regrouping. The `WP_Webfonts::generate_styles()` should not be aware of how to reorganize the webfonts from grouped by font-family to grouped by provider. Why? This is a separate task from generating styles for the webfonts. This commit creates a private method called `get_webfonts_by_provider()` to encapsulate the know-how for this reorganizational work. It also includes a micro-optimization that eliminates an extra `foreach` loop and the building of an unnecessary `$webfonts` array of arrays. --- lib/experimental/class-wp-webfonts.php | 67 +++++++++++++++----------- phpunit/class-wp-webfonts-test.php | 2 +- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lib/experimental/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php index 38788ef33e13a..8ebbb027176b5 100644 --- a/lib/experimental/class-wp-webfonts.php +++ b/lib/experimental/class-wp-webfonts.php @@ -309,7 +309,8 @@ public function register_provider( $provider, $class ) { */ public function generate_and_enqueue_styles() { // Generate the styles. - $styles = $this->generate_styles( $this->get_enqueued_webfonts() ); + $webfonts = $this->get_webfonts_by_provider( $this->get_enqueued_webfonts() ); + $styles = $this->generate_styles( $webfonts ); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -331,7 +332,8 @@ public function generate_and_enqueue_styles() { */ public function generate_and_enqueue_editor_styles() { // Generate the styles. - $styles = $this->generate_styles( $this->get_all_webfonts() ); + $webfonts = $this->get_webfonts_by_provider( $this->get_all_webfonts() ); + $styles = $this->generate_styles( $webfonts ); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -347,35 +349,13 @@ public function generate_and_enqueue_editor_styles() { * * @since 6.0.0 * - * @param array[] $font_families Font families and each of their webfonts. + * @param array[] $webfonts_by_provider Webfonts organized by provider. * @return string $styles Generated styles. */ - public function generate_styles( $font_families ) { + private function generate_styles( array $webfonts_by_provider ) { $styles = ''; $providers = $this->get_providers(); - $webfonts = array(); - - // Grab only the font face declarations from $font_families. - foreach ( $font_families as $font_family ) { - foreach ( $font_family as $font_face ) { - $webfonts[] = $font_face; - } - } - - // Group webfonts by provider. - $webfonts_by_provider = array(); - foreach ( $webfonts as $slug => $webfont ) { - $provider = $webfont['provider']; - if ( ! isset( $providers[ $provider ] ) ) { - /* translators: %s is the provider name. */ - trigger_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) ); - continue; - } - $webfonts_by_provider[ $provider ] = isset( $webfonts_by_provider[ $provider ] ) ? $webfonts_by_provider[ $provider ] : array(); - $webfonts_by_provider[ $provider ][ $slug ] = $webfont; - } - /* * Loop through each of the providers to get the CSS for their respective webfonts * to incrementally generate the collective styles for all of them. @@ -385,7 +365,7 @@ public function generate_styles( $font_families ) { // Bail out if the provider class does not exist. if ( ! class_exists( $provider_class ) ) { /* translators: %s is the provider name. */ - trigger_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider_id ) ); + trigger_error( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider_id ) ); continue; } @@ -409,4 +389,37 @@ public function generate_styles( $font_families ) { return $styles; } + + + /** + * Reorganizes webfonts grouped by font-family into grouped by provider. + * + * @param array[] $font_families Font families and each of their webfonts. + * @return array[] Webfonts organized by providers. + */ + private function get_webfonts_by_provider( array $font_families ) { + $providers = $this->get_providers(); + $webfonts_by_provider = array(); + + foreach ( $font_families as $webfonts ) { + foreach ( $webfonts as $webfont ) { + $provider = $webfont['provider']; + + // Skip if the provider is not registered. + if ( ! isset( $providers[ $provider ] ) ) { + /* translators: %s is the provider name. */ + trigger_error( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) ); + continue; + } + + // Initialize a new provider collection. + if ( ! isset( $webfonts_by_provider[ $provider ] ) ) { + $webfonts_by_provider[ $provider ] = array(); + } + $webfonts_by_provider[ $provider ][] = $webfont; + } + } + + return $webfonts_by_provider; + } } diff --git a/phpunit/class-wp-webfonts-test.php b/phpunit/class-wp-webfonts-test.php index 49399879a74ba..1ab14f6cd4f3b 100644 --- a/phpunit/class-wp-webfonts-test.php +++ b/phpunit/class-wp-webfonts-test.php @@ -365,7 +365,7 @@ public function test_validate_webfont() { /** * Test generate_and_enqueue_styles outputs only enqueued webfonts. * - * @covers WP_Webfonts::generate_styles + * @covers WP_Webfonts::generate_and_enqueue_styles */ public function test_generate_styles() { wp_register_webfonts( From 90cc44482ff27727722b6071c0b4b08fa347876d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Mar 2022 11:03:24 -0500 Subject: [PATCH 16/17] Updates from code review. --- lib/experimental/class-wp-webfonts.php | 2 +- .../register-webfonts-from-theme-json.php | 8 +++----- lib/experimental/webfonts.php | 16 +++++++++------- phpunit/class-wp-webfonts-test.php | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/experimental/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php index 8ebbb027176b5..d4a73c77cc2d2 100644 --- a/lib/experimental/class-wp-webfonts.php +++ b/lib/experimental/class-wp-webfonts.php @@ -120,7 +120,7 @@ public function get_providers() { * * @since 6.0.0 * - * @param array $webfont The font argument. + * @param array $webfont Webfont to be registered. * @return bool True if successfully registered, else false. */ public function register_webfont( array $webfont ) { diff --git a/lib/experimental/register-webfonts-from-theme-json.php b/lib/experimental/register-webfonts-from-theme-json.php index 68d38a9042bff..3282a9eaebbc0 100644 --- a/lib/experimental/register-webfonts-from-theme-json.php +++ b/lib/experimental/register-webfonts-from-theme-json.php @@ -42,7 +42,7 @@ function gutenberg_register_webfonts_from_theme_json() { foreach ( $font_face['src'] as $src_key => $url ) { // Tweak the URL to be relative to the theme root. - if ( 0 !== strpos( $url, 'file:./' ) ) { + if ( ! str_starts_with( $url, 'file:./' ) ) { continue; } $font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) ); @@ -71,7 +71,6 @@ function gutenberg_register_webfonts_from_theme_json() { * Add missing fonts data to the global styles. * * @param array $data The global styles. - * * @return array The global styles with missing fonts data. */ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { @@ -85,10 +84,9 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { * Helper to get an array of the font-families. * * @param array $families_data The font-families data. - * * @return array The font-families array. */ - $get_families = function( $families_data ) { + $get_families = static function( $families_data ) { $families = array(); foreach ( $families_data as $family ) { $families[] = WP_Webfonts::get_font_slug( $family ); @@ -137,7 +135,7 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { } $data['settings']['typography']['fontFamilies'][] = array( - 'fontFamily' => false !== strpos( $family_name, ' ' ) ? "'{$family_name}'" : $family_name, + 'fontFamily' => str_contains( $family_name, ' ' ) ? "'{$family_name}'" : $family_name, 'name' => $family_name, 'slug' => $slug, 'fontFace' => $font_faces, diff --git a/lib/experimental/webfonts.php b/lib/experimental/webfonts.php index 8d4fb91534e0c..6d4c5f8dcc12b 100644 --- a/lib/experimental/webfonts.php +++ b/lib/experimental/webfonts.php @@ -60,11 +60,11 @@ function wp_webfonts() { * * @since 6.0.0 * - * @param array $webfonts Webfonts to be registered. + * @param array[] $webfonts Webfonts to be registered. * This contains an array of webfonts to be registered. * Each webfont is an array. */ - function wp_register_webfonts( array $webfonts = array() ) { + function wp_register_webfonts( array $webfonts ) { foreach ( $webfonts as $webfont ) { wp_register_webfont( $webfont ); } @@ -94,9 +94,10 @@ function wp_register_webfonts( array $webfonts = array() ) { * @since 6.0.0 * * @param array $webfont Webfont to be registered. + * @return bool True if successfully registered, else false. */ function wp_register_webfont( array $webfont ) { - wp_webfonts()->register_webfont( $webfont ); + return wp_webfonts()->register_webfont( $webfont ); } } @@ -119,7 +120,7 @@ function wp_register_webfont( array $webfont ) { * * @param string[] $webfonts Font families to be enqueued. */ - function wp_enqueue_webfonts( $webfonts ) { + function wp_enqueue_webfonts( array $webfonts ) { foreach ( $webfonts as $webfont ) { wp_enqueue_webfont( $webfont ); } @@ -140,10 +141,11 @@ function wp_enqueue_webfonts( $webfonts ) { * * @since 6.0.0 * - * @param string $webfont Font family to be enqueued. + * @param string $font_family_name The font family name to be enqueued. + * @return bool True if successfully enqueued, else false. */ - function wp_enqueue_webfont( $webfont ) { - wp_webfonts()->enqueue_webfont( $webfont ); + function wp_enqueue_webfont( $font_family_name ) { + return wp_webfonts()->enqueue_webfont( $font_family_name ); } } diff --git a/phpunit/class-wp-webfonts-test.php b/phpunit/class-wp-webfonts-test.php index 1ab14f6cd4f3b..4292747f475b2 100644 --- a/phpunit/class-wp-webfonts-test.php +++ b/phpunit/class-wp-webfonts-test.php @@ -367,7 +367,7 @@ public function test_validate_webfont() { * * @covers WP_Webfonts::generate_and_enqueue_styles */ - public function test_generate_styles() { + public function test_generate_and_enqueue_styles() { wp_register_webfonts( array( array( From cc63012ed9d00f8dced590d218d8cdc19721f88c Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 4 Apr 2022 08:07:39 -0500 Subject: [PATCH 17/17] Restore loading lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php. --- lib/load.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/load.php b/lib/load.php index 07cb509825dce..36397318b9d9d 100644 --- a/lib/load.php +++ b/lib/load.php @@ -101,6 +101,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.0/class-wp-rest-block-patterns-controller.php'; require __DIR__ . '/compat/wordpress-6.0/class-wp-rest-block-pattern-categories-controller.php'; require __DIR__ . '/compat/wordpress-6.0/functions.php'; +require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php'; require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php'; require __DIR__ . '/compat/wordpress-6.0/rest-api.php'; require __DIR__ . '/compat/wordpress-6.0/block-patterns.php';