| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fonts API BC Layer helpers. |
| 4 |
* |
| 5 |
* BACKPORT NOTE: Do not backport this file to Core. |
| 6 |
* This file is now part of the API's Backwards-Compatibility (BC) layer. |
| 7 |
* |
| 8 |
* @package Gutenberg |
| 9 |
* @subpackage Fonts API |
| 10 |
* @since 15.7.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Gutenberg_Fonts_API_BC_Layer |
| 15 |
* |
| 16 |
* BACKPORT NOTE: Do not backport this file to Core. |
| 17 |
*/ |
| 18 |
class Gutenberg_Fonts_API_BC_Layer { |
| 19 |
|
| 20 |
/** |
| 21 |
* Determines if the given fonts array is the deprecated array structure. |
| 22 |
* |
| 23 |
* @param array $fonts Array of fonts to check. |
| 24 |
* @return bool True when deprecated structure, else false. |
| 25 |
*/ |
| 26 |
public static function is_deprecated_structure( array $fonts ) { |
| 27 |
/* |
| 28 |
* Iterates over the array's first dimension keys, which should |
| 29 |
* be the variation's font-family. If a key is empty or a non-string, |
| 30 |
* then the fonts are using a deprecated structure. |
| 31 |
*/ |
| 32 |
foreach ( array_keys( $fonts ) as $font_family ) { |
| 33 |
if ( ! WP_Fonts_Utils::is_defined( $font_family ) ) { |
| 34 |
return true; |
| 35 |
} |
| 36 |
} |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Migrates deprecated fonts structure into new API data structure, |
| 42 |
* i.e. variations grouped by their font-family. |
| 43 |
* |
| 44 |
* @param array $fonts Array of fonts to migrate. |
| 45 |
* @param bool $silence_deprecation Optional. Silences the deprecation notice. For internal use. |
| 46 |
* Default false. |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public static function migrate_deprecated_structure( array $fonts, $silence_deprecation = false ) { |
| 50 |
if ( ! self::is_deprecated_structure( $fonts ) ) { |
| 51 |
return $fonts; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! $silence_deprecation ) { |
| 55 |
_deprecated_argument( |
| 56 |
__METHOD__, |
| 57 |
'14.9.1', |
| 58 |
'A deprecated fonts array structure passed to wp_register_fonts(). Variations must be grouped and keyed by their font family.' |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
$new_fonts = array(); |
| 63 |
foreach ( $fonts as $font ) { |
| 64 |
$font_family = WP_Fonts_Utils::get_font_family_from_variation( $font ); |
| 65 |
if ( ! $font_family ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! isset( $new_fonts[ $font_family ] ) ) { |
| 70 |
$new_fonts[ $font_family ] = array(); |
| 71 |
} |
| 72 |
|
| 73 |
$new_fonts[ $font_family ][] = $font; |
| 74 |
} |
| 75 |
|
| 76 |
return $new_fonts; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Handle the deprecated fonts structure. |
| 81 |
* |
| 82 |
* @param array $font Font for extracting font family. |
| 83 |
* @param string $message Deprecation message to throw. |
| 84 |
* @return string|null The font family slug if successfully registered. Else null. |
| 85 |
*/ |
| 86 |
protected static function extract_font_family_from_deprecated_fonts_structure( array $font, $message ) { |
| 87 |
_deprecated_argument( __METHOD__, '14.9.1', $message ); |
| 88 |
|
| 89 |
$font_family = WP_Fonts_Utils::get_font_family_from_variation( $font ); |
| 90 |
if ( ! $font_family ) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
return WP_Fonts_Utils::convert_font_family_into_handle( $font_family ); |
| 95 |
} |
| 96 |
} |
| 97 |
|