-
Notifications
You must be signed in to change notification settings - Fork 69
/
wc-fonts.php
622 lines (559 loc) · 17.8 KB
/
wc-fonts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?php
/*
* Plugin Name: WordCamp.org Fonts
*/
class WordCamp_Fonts_Plugin {
protected $options;
/**
* Runs when file is loaded.
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_head', array( $this, 'wp_head_typekit' ), 102 ); // After safecss_style.
add_action( 'wp_head', array( $this, 'wp_head_google_web_fonts' ) );
add_action( 'wp_head', array( $this, 'wp_head_font_awesome' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_core_fonts' ) );
// Temporary workaround until we can use the core font library on WordCamp.org.
// See https://github.com/WordPress/gutenberg/pull/57697.
add_filter( 'wp_theme_json_data_theme', array( $this, 'inject_fonts_theme_json' ) );
}
/**
* Runs during init, loads the options array.
*/
public function init() {
$this->options = (array) get_option( 'wc-fonts-options', array() );
}
/**
* Provides the <head> output for Typekit settings.
*/
public function wp_head_typekit() {
if ( ! isset( $this->options['typekit-id'] ) || empty( $this->options['typekit-id'] ) ) {
return;
}
// phpcs:ignore -- Allow hardcoded script, and allow `sanitize_key` as an escaping function.
printf( '<script type="text/javascript" src="https://use.typekit.com/%s.js"></script>' . "\n", sanitize_key( $this->options['typekit-id'] ) );
printf( '<script type="text/javascript">try{Typekit.load();}catch(e){}</script>' );
}
/**
* Provides the <head> output for Google Web Fonts
*/
public function wp_head_google_web_fonts() {
if ( ! isset( $this->options['google-web-fonts'] ) || empty( $this->options['google-web-fonts'] ) ) {
return;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
printf( '<style>%s</style>', $this->options['google-web-fonts'] );
}
/**
* Provides the <head> output for Font Awesome
*/
public function wp_head_font_awesome() {
if ( ! empty( $this->options['font-awesome-url'] ) ) {
printf( "<style>@import url( '%s' );</style>", esc_url( $this->options['font-awesome-url'] ) );
}
if ( ! empty( $this->options['font-awesome-kit'] ) ) {
// phpcs:ignore -- Allow hardcoded script, and allow `sanitize_key` as an escaping function.
printf( '<script src="https://kit.fontawesome.com/%s.js"></script>', sanitize_key( $this->options['font-awesome-kit'] ) );
}
}
/**
* Allow sites to use Core fonts on the front-end
*/
public function enqueue_core_fonts() {
if ( isset( $this->options['dashicons'] ) && $this->options['dashicons'] ) {
wp_enqueue_style( 'dashicons' );
}
}
/**
* Runs during admin init, does Settings API
*/
public function admin_init() {
register_setting( 'wc-fonts-options', 'wc-fonts-options', array( $this, 'validate_options' ) );
add_settings_section( 'general', '', '__return_null', 'wc-fonts-options' );
add_settings_field(
'typekit-id',
__( 'Typekit ID', 'wordcamporg' ),
array( $this, 'field_typekit_id' ),
'wc-fonts-options',
'general'
);
add_settings_field(
'google-web-fonts',
__( 'Google Web Fonts', 'wordcamporg' ),
array( $this, 'field_google_web_fonts' ),
'wc-fonts-options',
'general'
);
add_settings_field(
'font-awesome-url',
__( 'Font Awesome', 'wordcamporg' ),
array( $this, 'field_font_awesome_url' ),
'wc-fonts-options',
'general'
);
add_settings_field(
'font-awesome-kit',
__( 'Font Awesome Kit', 'wordcamporg' ),
array( $this, 'field_font_awesome_kit' ),
'wc-fonts-options',
'general'
);
add_settings_field(
'dashicons',
esc_html__( 'Dashicons', 'wordcamporg' ),
array( $this, 'field_dashicons' ),
'wc-fonts-options',
'general'
);
}
/**
* Runs during admin_menu, adds a Fonts section to Appearance
*/
public function admin_menu() {
$fonts = add_theme_page(
esc_html__( 'Fonts', 'wordcamporg' ),
esc_html__( 'Fonts', 'wordcamporg' ),
'edit_theme_options',
'wc-fonts-options',
array( $this, 'render_admin_page' )
);
}
/**
* Uses the Settings API to render the Appearance > Fonts page
*/
public function render_admin_page() {
?>
<div class="wrap">
<h1>
<?php esc_html_e( 'Fonts', 'wordcamporg' ); ?>
</h1>
<?php settings_errors(); ?>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php
settings_fields( 'wc-fonts-options' );
do_settings_sections( 'wc-fonts-options' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Settings API field for the Typekit ID
*/
public function field_typekit_id() {
$value = isset( $this->options['typekit-id'] ) ? $this->options['typekit-id'] : '';
?>
<input type="text" name="wc-fonts-options[typekit-id]" value="<?php echo esc_attr( $value ); ?>" class="regular-text code" />
<p class="description">
<?php esc_html_e( 'Enter your Typekit Kit ID only. Do not add any URLs or JavaScript.', 'wordcamporg' ); ?>
</p>
<?php
}
/**
* Settings API field for the Google Web Fonts URLs
*/
public function field_google_web_fonts() {
$value = isset( $this->options['google-web-fonts'] ) ? $this->options['google-web-fonts'] : '';
?>
<textarea rows="5" name="wc-fonts-options[google-web-fonts]" class="large-text code"><?php
echo esc_textarea( $value );
?></textarea>
<p class="description">
<?php esc_html_e( 'Paste the Google Web Fonts @import URLs in this area, each one on a separate line.', 'wordcamporg' ); ?>
</p>
<?php
}
/**
* Settings API field for the Google Web Fonts URLs
*/
public function field_font_awesome_url() {
$value = isset( $this->options['font-awesome-url'] ) ? $this->options['font-awesome-url'] : '';
?>
<input type="text" name="wc-fonts-options[font-awesome-url]" value="<?php echo esc_url( $value ); ?>" class="large-text code" />
<p class="description">
<?php esc_html_e( 'For Font Awesome 4.7 and below. Enter the BootstrapCDN URL for the version you want.', 'wordcamporg' ); ?>
</p>
<?php
}
/**
* Settings API field for the Font Awesome kit ID
*/
public function field_font_awesome_kit() {
$value = isset( $this->options['font-awesome-kit'] ) ? $this->options['font-awesome-kit'] : '';
?>
<input type="text" name="wc-fonts-options[font-awesome-kit]" value="<?php echo esc_attr( $value ); ?>" class="regular-text code" />
<p class="description">
<?php esc_html_e( 'For Font Awesome 5+. Enter your Font Awesome Kit ID only. Do not add any URLs or JavaScript.', 'wordcamporg' ); ?>
</p>
<?php
}
/**
* Settings API field for the Dashicons checkbox
*/
public function field_dashicons() {
$value = isset( $this->options['dashicons'] ) ? $this->options['dashicons'] : '';
?>
<label>
<input type="checkbox" name="wc-fonts-options[dashicons]" <?php checked( $value ); ?> />
<?php esc_html_e( 'Enqueue Dashicons', 'wordcamporg' ); ?>
</label>
<?php
}
/**
* Triggered by the Settings API upon settings save.
*/
public function validate_options( $input ) {
$output = $this->options;
// Typekit.
if ( isset( $input['typekit-id'] ) ) {
$output['typekit-id'] = preg_replace( '/[^0-9a-zA-Z]+/', '', $input['typekit-id'] );
}
// Google Web Fonts.
if ( isset( $input['google-web-fonts'] ) ) {
$fonts = array();
$lines = explode( "\n", $input['google-web-fonts'] );
foreach ( $lines as $line ) {
$matches = array();
$url = preg_match( '#fonts\.googleapis\.com/css2?\?family=[^\)\'"]+#', $line, $matches );
if ( $matches ) {
$url = esc_url_raw( 'http://' . $matches[0] );
}
if ( ! $url ) {
continue;
}
$url = wp_parse_url( $url );
if ( 'fonts.googleapis.com' != $url['host'] ) {
continue;
}
if ( ! preg_match( '/^family=(.+)/i', $url['query'] ) ) {
continue;
}
$url = 'https://' . $url['host'] . $url['path'] . '?' . $url['query'];
$import = "@import url('" . esc_url_raw( $url ) . "');";
$fonts[] = $import;
}
$output['google-web-fonts'] = implode( "\n", $fonts );
}
// Font Awesome.
$output['font-awesome-url'] = '';
if ( isset( $input['font-awesome-url'] ) ) {
$url = wp_parse_url( $input['font-awesome-url'] );
if ( isset( $url['host'] ) && isset( $url['path'] ) ) {
$valid_hostname = in_array( $url['host'], [ 'maxcdn.bootstrapcdn.com', 'stackpath.bootstrapcdn.com' ] );
$valid_extension = '.css' === substr( $url['path'], strlen( $url['path'] ) - 4, 4 );
if ( $valid_hostname && $valid_extension ) {
$output['font-awesome-url'] = esc_url_raw( 'https://' . $url['host'] . $url['path'] );
}
}
}
// Font Awesome Kit.
if ( isset( $input['font-awesome-kit'] ) ) {
$output['font-awesome-kit'] = preg_replace( '/[^0-9a-zA-Z]+/', '', $input['font-awesome-kit'] );
}
// Dashicons.
$output['dashicons'] = isset( $input['dashicons'] ) && $input['dashicons'] ? true : false;
return $output;
}
/**
* Inject the local fonts for WordCamps.
*
* @todo Remove this when Gutenberg 17.6 is rolled out to WordCamp.org.
*
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
*
* @return WP_Theme_JSON_Data The updated theme settings.
*/
public function inject_fonts_theme_json( $theme_json ) {
$theme_data = $theme_json->get_data();
if ( ! isset( $theme_data['settings'] ) ) {
return $theme_json;
}
$fonts = _wp_array_get( $theme_data, array( 'settings', 'typography', 'fontFamilies', 'theme' ), array() );
// Add Krona One and Lora to WordCamp Europe sites.
// 1469: europe.wordcamp.org/2024.
// 1511: wceutest24.wordcamp.org/2024.
if ( in_array( get_current_blog_id(), array( 1469, 1511 ) ) ) {
$fonts[] = array(
'fontFace' => array(
array(
'fontFamily' => 'Krona One',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => site_url( '/wp-content/fonts/krona-one_normal_400.ttf' ),
),
),
'fontFamily' => "'Krona One', sans-serif",
'name' => 'Krona One',
'slug' => 'krona-one',
);
$fonts[] = array(
'fontFace' => array(
array(
'fontFamily' => 'Lora',
'fontStyle' => 'normal',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/lora_normal_700.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'normal',
'fontWeight' => 600,
'src' => site_url( '/wp-content/fonts/lora_normal_600.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'normal',
'fontWeight' => 500,
'src' => site_url( '/wp-content/fonts/lora_normal_500.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'normal',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/lora_normal_400.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'italic',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/lora_italic_700.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'italic',
'fontWeight' => 600,
'src' => site_url( '/wp-content/fonts/lora_italic_600.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'italic',
'fontWeight' => 500,
'src' => site_url( '/wp-content/fonts/lora_italic_500.ttf' ),
),
array(
'fontFamily' => 'Lora',
'fontStyle' => 'italic',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/lora_italic_400.ttf' ),
),
),
'fontFamily' => 'Lora',
'name' => 'Lora',
'slug' => 'lora',
);
}
// Add Roboto, Roboto Slab, and Sansita to WordCamp Asia sites.
// 1450: asia.wordcamp.org/2024.
if ( in_array( get_current_blog_id(), array( 1450 ) ) ) {
$fonts[] = array(
'fontFace' => array(
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'normal',
'fontWeight' => 900,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Black.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'normal',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Bold.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'normal',
'fontWeight' => 500,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Medium.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'normal',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Regular.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'normal',
'fontWeight' => 300,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Light.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'normal',
'fontWeight' => 100,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Thin.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'italic',
'fontWeight' => 900,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-BlackItalic.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'italic',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-BoldItalic.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'italic',
'fontWeight' => 500,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-MediumItalic.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'italic',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-Italic.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'italic',
'fontWeight' => 300,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-LightItalic.ttf' ),
),
array(
'fontFamily' => 'Roboto',
'fontStyle' => 'italic',
'fontWeight' => 100,
'src' => site_url( '/wp-content/fonts/Roboto/Roboto-ThinItalic.ttf' ),
),
),
'fontFamily' => "'Roboto', sans-serif",
'name' => 'Roboto',
'slug' => 'roboto',
);
$fonts[] = array(
'fontFace' => array(
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 900,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-Black.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 800,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-ExtraBold.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-Bold.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 600,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-SemiBold.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 500,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-Medium.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-Regular.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 300,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-Light.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 200,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-ExtraLight.ttf' ),
),
array(
'fontFamily' => 'Roboto Slab',
'fontStyle' => 'normal',
'fontWeight' => 100,
'src' => site_url( '/wp-content/fonts/Roboto_Slab/RobotoSlab-Thin.ttf' ),
),
),
'fontFamily' => "'Roboto Slab', serif",
'name' => 'Roboto Slab',
'slug' => 'roboto-slab',
);
$fonts[] = array(
'fontFace' => array(
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'normal',
'fontWeight' => 900,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-Black.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'normal',
'fontWeight' => 800,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-ExtraBold.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'normal',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-Bold.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'normal',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-Regular.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'italic',
'fontWeight' => 900,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-BlackItalic.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'italic',
'fontWeight' => 800,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-ExtraBoldItalic.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'italic',
'fontWeight' => 700,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-BoldItalic.ttf' ),
),
array(
'fontFamily' => 'Sansita',
'fontStyle' => 'italic',
'fontWeight' => 400,
'src' => site_url( '/wp-content/fonts/Sansita/Sansita-Italic.ttf' ),
),
),
'fontFamily' => "'Sansita', sans-serif",
'name' => 'Sansita',
'slug' => 'sansita',
);
}
// Return early if no fonts have been added.
if ( empty( $fonts ) ) {
return $theme_json;
}
// Build a new theme.json object.
$new_data = array(
'version' => 2,
);
_wp_array_set( $new_data, array( 'settings', 'typography', 'fontFamilies', 'theme' ), $fonts );
return $theme_json->update_with( $new_data );
}
}
new WordCamp_Fonts_Plugin();