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-wp-webfonts.php

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

239 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Webfonts API class: backwards-compatibility (BC) layer for all
4 * deprecated publicly exposed methods and functionality.
5 *
6 * This class/file will NOT be backported to Core. Rather for sites
7 * using the previous API, it exists to prevent breakages, giving
8 * developers time to upgrade their code.
9 *
10 * @package Gutenberg
11 * @subpackage Fonts API's BC Layer
12 * @since X.X.X
13 */
14
15 if ( class_exists( 'WP_Webfonts' ) ) {
16 return;
17 }
18
19 /**
20 * Class WP_Webfonts
21 *
22 * @deprecated GB 15.1 Use WP_Fonts instead.
23 */
24 class WP_Webfonts {
25 /**
26 * Instance of WP_Fonts.
27 *
28 * @var WP_Fonts
29 */
30 private $wp_fonts;
31
32 /**
33 * Instantiate an instance of WP_Webfonts.
34 *
35 * @param null|WP_Fonts $wp_fonts Optional. Instance of WP_Fonts.
36 * Default uses wp_fonts().
37 */
38 public function __construct( $wp_fonts = null ) {
39 $this->wp_fonts = ! empty( $wp_fonts ) ? $wp_fonts : wp_fonts();
40 }
41
42 /**
43 * Gets the font slug.
44 *
45 * @since X.X.X
46 * @deprecated Use WP_Fonts_Utils::convert_font_family_into_handle() or WP_Fonts_Utils::get_font_family_from_variation().
47 *
48 * @param array|string $to_convert The value to convert into a slug. Expected as the web font's array
49 * or a font-family as a string.
50 * @return string|false The font slug on success, or false if the font-family cannot be determined.
51 */
52 public static function get_font_slug( $to_convert ) {
53 $message = is_array( $to_convert )
54 ? 'Use WP_Fonts_Utils::get_font_family_from_variation() to get the font family from an array and then WP_Fonts_Utils::convert_font_family_into_handle() to convert the font-family name into a handle'
55 : 'Use WP_Fonts_Utils::convert_font_family_into_handle() to convert the font-family name into a handle';
56 _deprecated_function( __METHOD__, 'GB 14.9.1', $message );
57
58 if ( empty( $to_convert ) ) {
59 return false;
60 }
61
62 $font_family_name = is_array( $to_convert )
63 ? WP_Fonts_Utils::get_font_family_from_variation( $to_convert )
64 : $to_convert;
65
66 $slug = false;
67 if ( ! empty( $font_family_name ) ) {
68 $slug = WP_Fonts_Utils::convert_font_family_into_handle( $font_family_name );
69 }
70
71 return $slug;
72 }
73
74 /**
75 * Initializes the API.
76 *
77 * @since 6.0.0
78 * @deprecated GB 14.9.1 Use wp_fonts().
79 */
80 public static function init() {
81 _deprecated_function( __METHOD__, 'GB 14.9.1', 'wp_fonts()' );
82 }
83
84 /**
85 * Get the list of all registered font family handles.
86 *
87 * @since X.X.X
88 * @deprecated GB 15.8.0 Use wp_fonts()->get_registered_font_families().
89 *
90 * @return string[]
91 */
92 public function get_registered_font_families() {
93 _deprecated_function( __METHOD__, 'GB 15.8.0', 'wp_fonts()->get_registered_font_families()' );
94
95 return $this->wp_fonts->get_registered_font_families();
96 }
97
98 /**
99 * Gets the list of registered fonts.
100 *
101 * @since 6.0.0
102 * @deprecated 14.9.1 Use wp_fonts()->get_registered().
103 *
104 * @return array[]
105 */
106 public function get_registered_webfonts() {
107 _deprecated_function( __METHOD__, '14.9.1', 'wp_fonts()->get_registered()' );
108
109 return $this->_get_registered_webfonts();
110 }
111
112 /**
113 * Gets the list of enqueued fonts.
114 *
115 * @since 6.0.0
116 * @deprecated GB 14.9.1 Use wp_fonts()->get_enqueued().
117 *
118 * @return array[]
119 */
120 public function get_enqueued_webfonts() {
121 _deprecated_function( __METHOD__, 'GB 14.9.1', 'wp_fonts()->get_enqueued()' );
122
123 return $this->wp_fonts->queue;
124 }
125
126 /**
127 * Gets the list of all fonts.
128 *
129 * @since 6.0.0
130 * @deprecated GB 14.9.1 Use wp_fonts()->get_registered().
131 *
132 * @return array[]
133 */
134 public function get_all_webfonts() {
135 _deprecated_function( __METHOD__, 'GB 14.9.1', 'wp_fonts()->get_registered()' );
136
137 return $this->_get_registered_webfonts();
138 }
139
140 /**
141 * Registers a webfont.
142 *
143 * @since 6.0.0
144 * @deprecated GB 14.9.1 Use wp_register_fonts().
145 *
146 * @param array $webfont Web font to register.
147 * @param string $font_family_handle Optional. Font family handle for the given variation.
148 * Default empty string.
149 * @param string $variation_handle Optional. Handle for the variation to register.
150 * @param bool $silence_deprecation Optional. Silences the deprecation notice. For internal use.
151 * @return string|false The font family slug if successfully registered, else false.
152 */
153 public function register_webfont( array $webfont, $font_family_handle = '', $variation_handle = '', $silence_deprecation = false ) {
154 if ( ! $silence_deprecation ) {
155 _deprecated_function( __METHOD__, 'GB 14.9.1', 'wp_register_fonts()' );
156 }
157
158 // Bail out if no variation passed as there's not to register.
159 if ( empty( $webfont ) ) {
160 return false;
161 }
162
163 // Restructure definition: keyed by font-family and array of variations.
164 $font = array( $webfont );
165 if ( WP_Fonts_Utils::is_defined( $font_family_handle ) ) {
166 $font = array( $font_family_handle => $font );
167 } else {
168 $font = Gutenberg_Fonts_API_BC_Layer::migrate_deprecated_structure( $font, true );
169 $font_family_handle = array_key_first( $font );
170 }
171
172 if ( empty( $font ) || empty( $font_family_handle ) ) {
173 return false;
174 }
175
176 // If the variation handle was passed, add it as variation key.
177 if ( WP_Fonts_Utils::is_defined( $variation_handle ) ) {
178 $font[ $font_family_handle ] = array( $variation_handle => $font[ $font_family_handle ][0] );
179 }
180
181 // Register with the Fonts API.
182 $handle = wp_register_fonts( $font );
183 if ( empty( $handle ) ) {
184 return false;
185 }
186 return array_pop( $handle );
187 }
188
189 /**
190 * Enqueue a font-family that has been already registered.
191 *
192 * @since 6.0.0
193 * @deprecated GB 14.9.1 Use wp_enqueue_fonts().
194 *
195 * @param string $font_family_name The font family name to be enqueued.
196 * @return bool True if successfully enqueued, else false.
197 */
198 public function enqueue_webfont( $font_family_name ) {
199 _deprecated_function( __METHOD__, 'GB 14.9.1', 'wp_enqueue_fonts()' );
200
201 wp_enqueue_fonts( array( $font_family_name ) );
202 return true;
203 }
204
205 /**
206 * Gets the registered webfonts in the original web font property structure keyed by each handle.
207 *
208 * @return array[]
209 */
210 private function _get_registered_webfonts() {
211 $font_families = array();
212 $registered = array();
213
214 // Find the registered font families.
215 foreach ( $this->wp_fonts->registered as $handle => $obj ) {
216 if ( ! $obj->extra['is_font_family'] ) {
217 continue;
218 }
219
220 if ( ! isset( $registered[ $handle ] ) ) {
221 $registered[ $handle ] = array();
222 }
223
224 $font_families[ $handle ] = $obj->deps;
225 }
226
227 // Build the return array structure.
228 foreach ( $font_families as $font_family_handle => $variations ) {
229 foreach ( $variations as $variation_handle ) {
230 $variation_obj = $this->wp_fonts->registered[ $variation_handle ];
231
232 $registered[ $font_family_handle ][ $variation_handle ] = $variation_obj->extra['font-properties'];
233 }
234 }
235
236 return $registered;
237 }
238 }
239