Skip to content

Commit

Permalink
Improve deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Dec 15, 2022
1 parent 23dc407 commit 4ac7be0
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 122 deletions.
11 changes: 10 additions & 1 deletion lib/experimental/class-wp-web-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 33 additions & 28 deletions lib/experimental/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental/register-webfonts-from-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function gutenberg_register_webfonts_from_theme_json() {
}

wp_register_webfonts( $webfonts );
wp_enqueue_webfont( $handles );
wp_enqueue_webfonts( $handles );
}
}

Expand Down
40 changes: 21 additions & 19 deletions lib/experimental/webfonts-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
* <code>
* wp_enqueue_webfonts(
* 'Roboto',
* 'Sans Serif Pro'
* );
* wp_enqueue_webfont( 'Source Serif Pro' );
* </code>
*
* 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;
}
}

Expand All @@ -60,15 +57,15 @@ function wp_enqueue_webfonts( array $font_families ) {
* </code>
*
* @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 );
}
}

Expand All @@ -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;
}
}
10 changes: 6 additions & 4 deletions lib/experimental/webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down
69 changes: 0 additions & 69 deletions phpunit/webfonts/wpEnqueueWebfont-test.php

This file was deleted.

Loading

0 comments on commit 4ac7be0

Please sign in to comment.