PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-fonts.php

class-fonts.php in Block Animations, Motion & Scroll Effects – Ghost Kit 2.25.0, at classes/class-fonts.php

369 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Fonts for typography component
4 *
5 * @package ghostkit
6 */
7
8 // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
9
10 /**
11 * GhostKit_Fonts
12 */
13 class GhostKit_Fonts {
14 /**
15 * GhostKit_Fonts constructor.
16 */
17 public function __construct() {
18 add_filter( 'gkt_fonts_list', array( $this, 'add_google_fonts' ) );
19 add_filter( 'gkt_fonts_list', array( $this, 'add_default_site_fonts' ), 9 );
20
21 // enqueue fonts.
22 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_all_fonts_assets' ), 12 );
23 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_all_fonts_assets' ), 12 );
24 }
25
26 /**
27 * Enqueue frontend & editor assets
28 */
29 public function enqueue_all_fonts_assets() {
30 $fonts = $this->get_font_loader_list();
31
32 if ( ( is_admin() || ! empty( $fonts ) ) && isset( $fonts['google-fonts'] ) && ! empty( $fonts['google-fonts'] ) ) {
33 $families = array();
34
35 foreach ( $fonts['google-fonts'] as $font => $font_data ) {
36 $family = $font;
37
38 if ( isset( $font_data['widths'] ) && ! empty( $font_data['widths'] ) ) {
39 $weights = array();
40
41 foreach ( $font_data['widths'] as $weight ) {
42 $weights[] = $weight;
43 }
44
45 $family .= ':' . implode( ',', $weights );
46 }
47
48 $families[] = $family;
49 }
50
51 $query_args = array(
52 'family' => implode( '|', $families ),
53 'display' => 'swap',
54 );
55
56 wp_enqueue_style( 'ghostkit-fonts-google', add_query_arg( $query_args, 'https://fonts.googleapis.com/css' ), array(), '2.25.0' );
57 }
58 }
59
60 /**
61 * Create Font Loader List for webfont-loader.
62 *
63 * @return array - Font Loader List.
64 */
65 public function get_font_loader_list() {
66 $post_id = null;
67 $fonts_list = apply_filters( 'gkt_fonts_list', array() );
68 $default_typography = apply_filters( 'gkt_custom_typography', array() );
69 $fonts = array();
70 $unique_fonts = array();
71 $webfont_list = array();
72 $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
73 $is_admin_editor = is_admin() && $screen && $screen->is_block_editor;
74
75 if ( is_singular() ) {
76 $post_id = get_the_ID();
77 } elseif ( $is_admin_editor ) {
78 global $post;
79 $post_id = isset( $post->ID ) ? $post->ID : null;
80 }
81
82 $is_single = is_singular() && $post_id;
83 $is_admin_editor = $is_admin_editor && $post_id;
84 $additional_font_weights = array();
85
86 // Default Typography.
87 if ( ! empty( $default_typography ) ) {
88 // Global Typography.
89 $global_typography = get_option( 'ghostkit_typography', array() );
90 if ( isset( $global_typography['ghostkit_typography'] ) && $global_typography['ghostkit_typography'] ) {
91 $global_typography = json_decode( $global_typography['ghostkit_typography'], true );
92 }
93 if ( ! is_array( $global_typography ) ) {
94 $global_typography = array();
95 }
96
97 // Meta post Typography.
98 $meta_typography = array();
99 if ( $is_single || $is_admin_editor ) {
100 $meta_typography = get_post_meta( $post_id, 'ghostkit_typography', true );
101
102 if ( ! empty( $meta_typography ) ) {
103 $meta_typography = json_decode( $meta_typography, true );
104 }
105 if ( ! is_array( $meta_typography ) ) {
106 $meta_typography = array();
107 }
108 }
109
110 // Go through all defaults and prepare full fonts list.
111 foreach ( $default_typography as $key => $typography ) {
112 $result = array();
113
114 // Default data.
115 if (
116 isset( $typography['defaults']['font-family'] ) &&
117 ! empty( $typography['defaults']['font-family'] ) &&
118 isset( $typography['defaults']['font-family-category'] ) &&
119 ! empty( $typography['defaults']['font-family-category'] )
120 ) {
121 $weight = '';
122 if ( isset( $typography['defaults']['font-weight'] ) && ! empty( $typography['defaults']['font-weight'] ) ) {
123 $weight = $typography['defaults']['font-weight'];
124 }
125
126 // TODO: add additional_font_weights option support for typography React component.
127 if ( isset( $typography['additional_font_weights'] ) && ! empty( $typography['additional_font_weights'] ) ) {
128 $additional_font_weights[ $key ] = $typography['additional_font_weights'];
129 }
130
131 $result = array(
132 'family' => $typography['defaults']['font-family-category'],
133 'label' => $typography['defaults']['font-family'],
134 'weight' => $weight,
135 'typography' => $key,
136 );
137 }
138
139 // Global data.
140 if (
141 isset( $global_typography[ $key ]['fontFamily'] ) &&
142 ! empty( $global_typography[ $key ]['fontFamily'] ) &&
143 isset( $global_typography[ $key ]['fontFamilyCategory'] ) &&
144 ! empty( $global_typography[ $key ]['fontFamilyCategory'] )
145 ) {
146 $weight = '';
147 if ( isset( $global_typography[ $key ]['fontWeight'] ) && ! empty( $global_typography[ $key ]['fontWeight'] ) ) {
148 $weight = $global_typography[ $key ]['fontWeight'];
149 }
150
151 $result = array(
152 'family' => $global_typography[ $key ]['fontFamilyCategory'],
153 'label' => $global_typography[ $key ]['fontFamily'],
154 'weight' => $weight,
155 'typography' => $key,
156 );
157 }
158
159 // Meta data.
160 if (
161 isset( $meta_typography[ $key ]['fontFamily'] ) &&
162 ! empty( $meta_typography[ $key ]['fontFamily'] ) &&
163 isset( $meta_typography[ $key ]['fontFamilyCategory'] ) &&
164 ! empty( $meta_typography[ $key ]['fontFamilyCategory'] )
165 ) {
166 $weight = '';
167 if ( isset( $meta_typography[ $key ]['fontWeight'] ) && ! empty( $meta_typography[ $key ]['fontWeight'] ) ) {
168 $weight = $meta_typography[ $key ]['fontWeight'];
169 }
170
171 $result = array(
172 'family' => $meta_typography[ $key ]['fontFamilyCategory'],
173 'label' => $meta_typography[ $key ]['fontFamily'],
174 'weight' => $weight,
175 'typography' => $key,
176 );
177 }
178
179 // Save result.
180 if ( isset( $result['family'] ) && $result['family'] && isset( $result['label'] ) && $result['label'] ) {
181 $fonts[ $key ] = $result;
182 }
183 }
184 }
185
186 // clear array to unique.
187 $unique_fonts = array_map( 'unserialize', array_unique( array_map( 'serialize', $fonts ) ) );
188
189 foreach ( $unique_fonts as $font ) {
190 if ( isset( $font['family'] ) && ! empty( $font['family'] ) && array_key_exists( $font['family'], $fonts_list ) ) {
191 foreach ( $fonts_list[ $font['family'] ]['fonts'] as $find_font ) {
192 if ( $font['label'] === $find_font['name'] ) {
193 $weights = array();
194 $weight = ( isset( $font['weight'] ) && ! empty( $font['weight'] ) ) ? $font['weight'] : '';
195 $widths = ( isset( $find_font['widths'] ) && ! empty( $find_font['widths'] ) ) ? $find_font['widths'] : array();
196 $category = ( isset( $find_font['category'] ) && ! empty( $find_font['category'] ) ) ? $find_font['category'] : '';
197 $subsets = ( isset( $find_font['subsets'] ) && ! empty( $find_font['subsets'] ) ) ? $find_font['subsets'] : '';
198
199 if ( isset( $additional_font_weights ) && ! empty( $additional_font_weights ) ) {
200 foreach ( $additional_font_weights as $key => $additional_font_weight ) {
201 if ( $key === $font['typography'] ) {
202 $font_weights = $additional_font_weight;
203 }
204 }
205 }
206
207 if ( isset( $font_weights ) && ! empty( $font_weights ) && is_array( $font_weights ) ) {
208 $insert_weights = array();
209 if ( '' !== $weight ) {
210 $weight = str_replace( 'i', '', $weight );
211 $insert_weights[] = $weight;
212 $insert_weights[] = $weight . 'i';
213 }
214 $insert_weights = array_merge( $insert_weights, $font_weights );
215 foreach ( $insert_weights as $insert_weight ) {
216 if ( array_search( $insert_weight, $widths, true ) !== false ) {
217 $weights[] = $insert_weight;
218 }
219 }
220 } elseif ( '' !== $weight ) {
221 $weight = str_replace( 'i', '', $weight );
222
223 if ( '600' !== $weight &&
224 '700' !== $weight &&
225 '800' !== $weight &&
226 '900' !== $weight ) {
227
228 $insert_weights = array(
229 $weight,
230 $weight . 'i',
231 '700',
232 '700i',
233 );
234
235 foreach ( $insert_weights as $insert_weight ) {
236 if ( array_search( $insert_weight, $widths, true ) !== false ) {
237 $weights[] = $insert_weight;
238 }
239 }
240 } else {
241 $insert_weights = array(
242 $weight,
243 $weight . 'i',
244 );
245
246 foreach ( $insert_weights as $insert_weight ) {
247 if ( array_search( $insert_weight, $widths, true ) !== false ) {
248 $weights[] = $insert_weight;
249 }
250 }
251 }
252 } else {
253 $insert_weights = array(
254 '400',
255 '400i',
256 '700',
257 '700i',
258 );
259
260 foreach ( $insert_weights as $insert_weight ) {
261 if ( array_search( $insert_weight, $widths, true ) !== false ) {
262 $weights[] = $insert_weight;
263 }
264 }
265 }
266
267 if (
268 isset( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'] ) &&
269 ! empty( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'] ) &&
270 is_array( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'] )
271 ) {
272 $weights = array_values( array_unique( array_merge_recursive( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'], $weights ) ) );
273 }
274
275 $webfont_list[ $font['family'] ][ $font['label'] ] = array(
276 'widths' => $weights,
277 'category' => $category,
278 'subsets' => $subsets,
279 );
280 }
281 }
282 }
283 }
284
285 return $webfont_list;
286 }
287
288 /**
289 * Add Default fonts list.
290 *
291 * @param array $fonts - fonts list.
292 *
293 * @return array
294 */
295 public function add_default_site_fonts( $fonts ) {
296 $fonts['default'] = array(
297 'name' => __( 'Default Fonts Site', 'ghostkit' ),
298 'fonts' => array(
299 array(
300 'name' => 'Default Site Font',
301 'widths' => array(
302 '',
303 '400',
304 '700',
305 ),
306 'category' => 'sans-serif',
307 ),
308 ),
309 );
310 return $fonts;
311 }
312
313 /**
314 * Add Google fonts list.
315 *
316 * @param array $fonts - fonts list.
317 *
318 * @return array
319 */
320 public function add_google_fonts( $fonts ) {
321 $result = get_transient( 'ghostkit_google_fonts_list' );
322
323 // Get new fonts:
324 // https://developers.google.com/fonts/docs/developer_api?apix_params=%7B%22alt%22%3A%22json%22%2C%22fields%22%3A%22items(family%2Ckind%2Ccategory%2Cvariants%2Csubsets%2Cversion%2ClastModified)%22%2C%22prettyPrint%22%3Afalse%7D .
325
326 if ( ! $result ) {
327 $result = array();
328 $fonts_json = file_get_contents( ghostkit()->plugin_path . 'classes/google-fonts/webfonts.json' ); // phpcs:ignore
329 $fonts_object = json_decode( $fonts_json, true );
330
331 foreach ( $fonts_object['items'] as $font ) {
332 $weights = array();
333 foreach ( $font['variants'] as $variant ) {
334 $variant = str_replace( 'italic', 'i', $variant );
335
336 switch ( $variant ) {
337 case 'i':
338 $variant = '400i';
339 break;
340 case 'regular':
341 $variant = '400';
342 break;
343 }
344
345 $weights[] = $variant;
346 }
347 $result[] = array(
348 'name' => $font['family'],
349 'widths' => $weights,
350 'category' => $font['category'],
351 'subsets' => $font['subsets'],
352 );
353 }
354
355 if ( ! empty( $result ) ) {
356 set_transient( 'ghostkit_google_fonts_list', $result, DAY_IN_SECONDS );
357 }
358 }
359
360 $fonts['google-fonts'] = array(
361 'name' => 'Google Fonts',
362 'fonts' => $result,
363 );
364
365 return $fonts;
366 }
367 }
368 new GhostKit_Fonts();
369