diff --git a/lib/experimental/class-wp-web-fonts.php b/lib/experimental/class-wp-web-fonts.php index 9c6b08892b54f..15df7ecf47e5c 100644 --- a/lib/experimental/class-wp-web-fonts.php +++ b/lib/experimental/class-wp-web-fonts.php @@ -100,7 +100,16 @@ public function __construct() { * * @since X.X.X * - * @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID. + * @return array $providers { + * An associative array of registered providers, keyed by their unique ID. + * + * @type string $provider_id => array { + * An associate array of provider's class name and fonts. + * + * @type string $class Fully qualified name of the provider's class. + * @type string[] $fonts An array of enqueued font handles for this provider. + * } + * } */ public function get_providers() { return $this->providers; diff --git a/lib/experimental/class-wp-webfonts.php b/lib/experimental/class-wp-webfonts.php index f03ee14da605e..fd00d39cfc5bf 100644 --- a/lib/experimental/class-wp-webfonts.php +++ b/lib/experimental/class-wp-webfonts.php @@ -37,7 +37,7 @@ class WP_Webfonts extends WP_Dependencies { */ public static function get_font_slug( $to_convert ) { $message = is_array( $to_convert ) - ? 'Use WP_Webfonts_Utils::get_font_family_from_variation() to get the font family from an array and then WP_Webfonts_Utils::convert_font_family_into_handle() to convert the font-family name into a handle' + ? 'Use WP_Webfonts_Utils::get l_font_family_from_variation() to get the font family from an array and then WP_Webfonts_Utils::convert_font_family_into_handle() to convert the font-family name into a handle' : 'Use WP_Webfonts_Utils::convert_font_family_into_handle() to convert the font-family name into a handle'; _deprecated_function( __METHOD__, 'X.X.X', $message ); @@ -100,16 +100,19 @@ public function get_all_webfonts() { * Registers a webfont. * * @since 6.0.0 - * @deprecated X.X.X Use wp_register_webfont_variation(). + * @deprecated X.X.X Use wp_register_webfonts(). * - * @param array $webfont Web font to register. - * @param string $font_family_handle Optional. Font family handle for the given variation. - * Default empty string. - * @param string $variation_handle Optional. Handle for the variation to register. + * @param array $webfont Web font to register. + * @param string $font_family_handle Optional. Font family handle for the given variation. + * Default empty string. + * @param string $variation_handle Optional. Handle for the variation to register. + * @param bool $silence_deprecation Optional. Silences the deprecation notice. For internal use. * @return string|false The font family slug if successfully registered, else false. */ - public function register_webfont( array $webfont, $font_family_handle = '', $variation_handle = '' ) { - _deprecated_function( __METHOD__, 'X.X.X', 'wp_register_webfont_variation()' ); + public function register_webfont( array $webfont, $font_family_handle = '', $variation_handle = '', $silence_deprecation = false ) { + if ( ! $silence_deprecation ) { + _deprecated_function( __METHOD__, 'X.X.X', 'wp_register_webfonts()' ); + } // When font family's handle is not passed, attempt to get it from the variation. if ( ! WP_Webfonts_Utils::is_defined( $font_family_handle ) ) { @@ -132,28 +135,15 @@ public function register_webfont( array $webfont, $font_family_handle = '', $var * Enqueue a font-family that has been already registered. * * @since 6.0.0 - * @deprecated X.X.X Use wp_webfonts()->enqueue() or wp_enqueue_webfont(). + * @deprecated X.X.X Use wp_enqueue_webfonts(). * * @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 ) { - _deprecated_function( __METHOD__, 'X.X.X', 'wp_webfonts()->enqueue() or wp_enqueue_webfont()' ); - - $slug = static::_get_font_slug( $font_family_name ); - - if ( isset( $this->enqueued[ $slug ] ) ) { - return true; - } - - if ( ! isset( $this->registered[ $slug ] ) ) { - /* translators: %s unique slug to identify the font family of the webfont */ - _doing_it_wrong( __METHOD__, sprintf( __( 'The "%s" font family is not registered.', 'gutenberg' ), $slug ), '6.0.0' ); - - return false; - } + _deprecated_function( __METHOD__, 'X.X.X', 'wp_enqueue_webfonts()' ); - $this->enqueue( $slug ); + wp_enqueue_webfonts( array( $font_family_name ) ); return true; } @@ -239,14 +229,29 @@ private static function _get_font_slug( $to_convert ) { * @return array[] */ private function _get_registered_webfonts() { - $registered = array(); + $font_families = array(); + $registered = array(); + + // Find the registered font families. foreach ( $this->registered as $handle => $obj ) { - // Skip the font-family. - if ( $obj->extra['is_font_family'] ) { + if ( ! $obj->extra['is_font_family'] ) { continue; } - $registered[ $handle ] = $obj->extra['font-properties']; + if ( ! isset( $registered[ $handle ] ) ) { + $registered[ $handle ] = array(); + } + + $font_families[ $handle ] = $obj->deps; + } + + // Build the return array structure. + foreach ( $font_families as $font_family_handle => $variations ) { + foreach ( $variations as $variation_handle ) { + $variation_obj = $this->registered[ $variation_handle ]; + + $registered[ $font_family_handle ][ $variation_handle ] = $variation_obj->extra['font-properties']; + } } return $registered; diff --git a/lib/experimental/register-webfonts-from-theme-json.php b/lib/experimental/register-webfonts-from-theme-json.php index d590d28cda0d9..4b49f6e0534fa 100644 --- a/lib/experimental/register-webfonts-from-theme-json.php +++ b/lib/experimental/register-webfonts-from-theme-json.php @@ -108,7 +108,7 @@ function gutenberg_register_webfonts_from_theme_json() { } wp_register_webfonts( $webfonts ); - wp_enqueue_webfont( $handles ); + wp_enqueue_webfonts( $handles ); } } diff --git a/lib/experimental/webfonts-deprecations.php b/lib/experimental/webfonts-deprecations.php index cd8bebddfc3e8..bd50b0f476d96 100644 --- a/lib/experimental/webfonts-deprecations.php +++ b/lib/experimental/webfonts-deprecations.php @@ -10,32 +10,29 @@ * @package WordPress */ -if ( ! function_exists( 'wp_enqueue_webfonts' ) ) { +if ( ! function_exists( 'wp_enqueue_webfont' ) ) { /** - * Enqueues a collection of font families. + * Enqueue a single font family that has been registered beforehand. * - * Example of how to enqueue Source Serif Pro and Roboto font families, both registered beforehand. + * Example of how to enqueue Source Serif Pro font: * * - * wp_enqueue_webfonts( - * 'Roboto', - * 'Sans Serif Pro' - * ); + * wp_enqueue_webfont( 'Source Serif Pro' ); * * * Font families should be enqueued from the `init` hook or later. * - * BACKPORT NOTE: Do not backport this function. - * * @since 6.0.0 - * @deprecated X.X.X Use wp_enqueue_webfont(). + * @deprecated X.X.X Use wp_enqueue_webfonts(). * - * @param string[] $font_families Font family handles (array of strings). + * @param string $font_family_name The font family name to be enqueued. + * @return bool True if successfully enqueued, else false. */ - function wp_enqueue_webfonts( array $font_families ) { - _deprecated_function( __FUNCTION__, 'X.X.X', 'wp_enqueue_webfont()' ); + function wp_enqueue_webfont( $font_family_name ) { + _deprecated_function( __FUNCTION__, 'X.X.X', 'wp_enqueue_webfonts()' ); - wp_enqueue_webfont( $font_families ); + wp_enqueue_webfonts( array( $font_family_name ) ); + return true; } } @@ -60,15 +57,15 @@ function wp_enqueue_webfonts( array $font_families ) { * * * @since 6.0.0 - * @deprecated X.X.X Use wp_enqueue_webfont(). + * @deprecated X.X.X Use wp_register_webfonts(). * * @param array $webfont Webfont to be registered. * @return string|false The font family slug if successfully registered, else false. */ function wp_register_webfont( array $webfont ) { - _deprecated_function( __FUNCTION__, 'X.X.X', 'wp_register_webfont_variation()' ); + _deprecated_function( __FUNCTION__, 'X.X.X', 'wp_register_webfonts()' ); - return wp_webfonts()->register_webfont( $webfont ); + return wp_webfonts()->register_webfont( $webfont, '', '', false ); } } @@ -88,11 +85,16 @@ function wp_register_webfont( array $webfont ) { * @since X.X.X * @deprecated X.X.X Use wp_webfonts()->get_providers(). * - * @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID. + * @return string[] All registered providers, each keyed by their unique ID. */ function wp_get_webfont_providers() { _deprecated_function( __FUNCTION__, 'X.X.X', 'wp_webfonts()->get_providers()' ); - return wp_webfonts()->get_providers(); + $providers = array(); + foreach ( wp_webfonts()->get_providers() as $id => $config ) { + $providers[ $id ] = $config['class']; + } + + return $providers; } } diff --git a/lib/experimental/webfonts.php b/lib/experimental/webfonts.php index f72bab3f1ff3c..c4ccc1640d9c8 100644 --- a/lib/experimental/webfonts.php +++ b/lib/experimental/webfonts.php @@ -84,16 +84,18 @@ function wp_register_webfonts( array $webfonts ) { } } -if ( ! function_exists( 'wp_enqueue_webfont' ) ) { +if ( ! function_exists( 'wp_enqueue_webfonts' ) ) { /** * Enqueues one or more font family and all of its variations. * * @since X.X.X * - * @param string|string[] $font_family Font family handle (string) or handles (array of strings). + * @param string[] $font_families Font family(ies) to enqueue. */ - function wp_enqueue_webfont( $font_family ) { - wp_webfonts()->enqueue( $font_family ); + function wp_enqueue_webfonts( array $font_families ) { + $handles = array_map( array( WP_Webfonts_Utils::class, 'convert_font_family_into_handle' ), $font_families ); + + wp_webfonts()->enqueue( $handles ); } } diff --git a/phpunit/webfonts/wpEnqueueWebfont-test.php b/phpunit/webfonts/wpEnqueueWebfont-test.php deleted file mode 100644 index fa371f1c2ec32..0000000000000 --- a/phpunit/webfonts/wpEnqueueWebfont-test.php +++ /dev/null @@ -1,69 +0,0 @@ -set_up_mock( 'enqueue' ); - $mock->expects( $this->once() ) - ->method( 'enqueue' ) - ->with( - $this->identicalTo( $font_family ) - ); - - wp_enqueue_webfont( $font_family ); - } - - /** - * Integration test for enqueuing a font family and all of its variations. - * - * @dataProvider data_enqueue - * - * @param string|string[] $font_family Font family to test. - * @param array $expected Expected queue. - */ - public function test_should_enqueue_after_registration( $font_family, array $expected ) { - foreach ( $this->get_data_registry() as $handle => $variations ) { - $this->setup_register( $handle, $variations ); - } - - wp_enqueue_webfont( $font_family ); - $this->assertEmpty( $this->get_queued_before_register(), '"queued_before_register" queue should be empty' ); - $this->assertSame( $expected, $this->get_enqueued_handles(), 'Queue should contain the given font family(ies)' ); - } - - /** - * Integration test for enqueuing before registering a font family and all of its variations. - * - * @dataProvider data_enqueue - * - * @param string|string[] $font_family Font family to test. - * @param array $not_used Not used. - * @param array $expected Expected "queued_before_register" queue. - */ - public function test_should_enqueue_before_registration( $font_family, array $not_used, array $expected ) { - wp_enqueue_webfont( $font_family ); - - $this->assertSame( $expected, $this->get_queued_before_register(), '"queued_before_register" queue should contain the given font family(ies)' ); - $this->assertEmpty( $this->get_enqueued_handles(), 'Queue should be empty' ); - } -} diff --git a/phpunit/webfonts/wpEnqueueWebfonts-test.php b/phpunit/webfonts/wpEnqueueWebfonts-test.php new file mode 100644 index 0000000000000..a0ee6af5c1dda --- /dev/null +++ b/phpunit/webfonts/wpEnqueueWebfonts-test.php @@ -0,0 +1,122 @@ +set_up_mock( 'enqueue' ); + $mock->expects( $this->once() ) + ->method( 'enqueue' ) + ->with( + $this->identicalTo( $expected_handles ) + ); + + wp_enqueue_webfonts( $font_families ); + } + + /** + * Integration test for enqueuing a font family and all of its variations. + * + * @dataProvider data_should_enqueue + * + * @param string[] $font_families Font families to test. + * @param string[] $expected_handles Expected handles passed to WP_Web_Fonts::enqueue(). + */ + public function test_should_enqueue_after_registration( $font_families, $expected_handles ) { + // Register the font-families. + foreach ( $this->get_data_registry() as $handle => $variations ) { + $this->setup_register( $handle, $variations ); + } + + wp_enqueue_webfonts( $font_families ); + + $this->assertEmpty( $this->get_queued_before_register(), '"queued_before_register" queue should be empty' ); + $this->assertSame( $expected_handles, $this->get_enqueued_handles(), 'Queue should contain the given font family(ies)' ); + } + + /** + * Integration test for enqueuing before registering a font family and all of its variations. + * + * @dataProvider data_should_enqueue + * + * @param string[] $font_families Font families to test. + * @param string[] $expected_handles Expected handles passed to WP_Web_Fonts::enqueue(). + */ + public function test_should_enqueue_before_registration( $font_families, $expected_handles ) { + wp_enqueue_webfonts( $font_families ); + + // Set up what "queued_before_register" queue should be. + $expected = array(); + foreach ( $expected_handles as $handle ) { + $expected[ $handle ] = null; + } + $this->assertSame( $expected, $this->get_queued_before_register(), '"queued_before_register" queue should contain the given font family(ies)' ); + $this->assertEmpty( $this->get_enqueued_handles(), 'Queue should be empty' ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_enqueue() { + return array( + '1: single word handle' => array( + 'font_families' => array( 'lato' ), + 'expected_handles' => array( 'lato' ), + ), + '1: multiple word handle' => array( + 'font_families' => array( 'source-serif-pro' ), + 'expected_handles' => array( 'source-serif-pro' ), + ), + '1: single word name' => array( + 'font_families' => array( 'Merriweather' ), + 'expected_handles' => array( 'merriweather' ), + ), + '1: multiple word name' => array( + 'font_families' => array( 'My Font' ), + 'expected_handles' => array( 'my-font' ), + ), + '>1: single word handle' => array( + 'font_families' => array( 'lato', 'merriweather' ), + 'expected_handles' => array( 'lato', 'merriweather' ), + ), + '>1: multiple word handle' => array( + 'font_families' => array( 'source-serif-pro', 'my-font' ), + 'expected_handles' => array( 'source-serif-pro', 'my-font' ), + ), + '>1: single word name' => array( + 'font_families' => array( 'Lato', 'Merriweather' ), + 'expected_handles' => array( 'lato', 'merriweather' ), + ), + '>1: multiple word name' => array( + 'font_families' => array( 'My Font', 'Source Serif Pro' ), + 'expected_handles' => array( 'my-font', 'source-serif-pro' ), + ), + '>1: mixture of word handles and names' => array( + 'font_families' => array( 'Source Serif Pro', 'Merriweather', 'my-font', 'Lato' ), + 'expected_handles' => array( 'source-serif-pro', 'merriweather', 'my-font', 'lato' ), + ), + ); + } +}