PluginProbe
Gutenberg / 17.2.1
Gutenberg v17.2.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / experimental / fonts-api / bc-layer / class-gutenberg-fonts-api-bc-layer.php

class-gutenberg-fonts-api-bc-layer.php in Gutenberg 17.2.1, at lib/experimental/fonts-api/bc-layer/class-gutenberg-fonts-api-bc-layer.php

97 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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