PluginProbe
Powerkit – Supercharge your WordPress Site / 2.5.5
Powerkit – Supercharge your WordPress Site v2.5.5
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / class-powerkit-custom-fonts.php

class-powerkit-custom-fonts.php in Powerkit – Supercharge your WordPress Site 2.5.5, at modules/class-powerkit-custom-fonts.php

652 lines 22.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Custom Fonts
4 *
5 * @package Powerkit
6 * @subpackage Modules
7 */
8
9 /**
10 * Init module
11 */
12 class Powerkit_Custom_Fonts extends Powerkit_Module {
13
14 /**
15 * Webfonts method
16 *
17 * @var string $load_method Webfonts method.
18 */
19 public $load_method = 'async';
20
21 /**
22 * Font base.
23 *
24 * This is used in case of Elementor's Font param
25 *
26 * @var string
27 */
28 private static $font_base = 'pk-custom-fonts';
29
30 /**
31 * Register module
32 */
33 public function register() {
34 $this->name = esc_html__( 'Custom Fonts', 'powerkit' );
35 $this->desc = esc_html__( 'Adds the ability to download custom fonts.', 'powerkit' );
36 $this->slug = 'custom_fonts';
37 $this->type = 'default';
38 $this->category = 'basic';
39 $this->priority = 1040;
40 $this->public = true;
41 $this->enabled = false;
42 $this->badge = esc_html__( 'Advanced', 'powerkit' );
43 $this->load_extensions = array(
44 'fonts',
45 );
46 $this->links = array(
47 array(
48 'name' => esc_html__( 'Go to settings', 'powerkit' ),
49 'url' => powerkit_get_page_url( 'fonts&tab=' . $this->slug, 'themes' ),
50 ),
51 array(
52 'name' => esc_html__( 'View documentation', 'powerkit' ),
53 'url' => powerkit_get_setting( 'documentation' ) . '/content-presentation/custom-fonts/',
54 'target' => '_blank',
55 ),
56 );
57 }
58
59 /**
60 * Initialize module
61 */
62 public function initialize() {
63 add_filter( 'powerkit_fonts_list', array( $this, 'custom_fonts' ), 10 );
64 add_filter( 'upload_mimes', array( $this, 'allow_mimes' ) );
65 add_filter( 'powerkit_fonts_register_settings', array( $this, 'register_settings' ), 10 );
66 add_action( 'customize_controls_print_styles', array( $this, 'frontend_enqueue' ), 100 );
67 add_action( 'wp_head', array( $this, 'frontend_enqueue' ), 100 );
68 add_action( 'admin_head', array( $this, 'editor_enqueue' ), 100 );
69 add_filter( 'init', array( $this, 'set_load_method' ) );
70 add_filter( 'elementor/fonts/groups', array( $this, 'elementor_group' ) );
71 add_filter( 'elementor/fonts/additional_fonts', array( $this, 'add_elementor_fonts' ) );
72 }
73
74 /**
75 * Set Webfonts method
76 */
77 public function set_load_method() {
78 $this->load_method = apply_filters( 'powerkit_webfonts_load_method', 'async' );
79 }
80
81 /**
82 * Add custom fonts
83 *
84 * @since 1.0.0
85 * @param array $fonts List fonts.
86 * @return array
87 */
88 public function custom_fonts( $fonts ) {
89
90 if ( is_customize_preview() ) {
91
92 $custom_fonts = get_option( 'powerkit_custom_fonts_list' );
93
94 if ( is_array( $custom_fonts ) && $custom_fonts ) {
95
96 $exclude = array();
97
98 $fonts['families']['custom_fonts'] = array(
99 'text' => esc_html__( 'Custom Fonts', 'powerkit' ),
100 'children' => array(),
101 );
102
103 foreach ( $custom_fonts as $key => $item ) {
104 if ( 'clone' === $key ) {
105 continue;
106 }
107 $id = sanitize_title( $item['name'] );
108
109 if ( $id ) {
110 if ( ! in_array( $id, $exclude, true ) ) {
111 $fonts['families']['custom_fonts']['children'][] = array(
112 'id' => $id,
113 'text' => $item['name'],
114 );
115
116 $exclude[] = $id;
117 }
118
119 $variant = str_replace( '400', 'regular', $item['weight'] );
120
121 if ( 'italic' === $item['style'] ) {
122 $variant .= 'italic';
123 }
124
125 if ( isset( $fonts['variants'][ $id ] ) && $fonts['variants'][ $id ] ) {
126 if ( ! in_array( $variant, $fonts['variants'][ $id ], true ) ) {
127 $fonts['variants'][ $id ][] = $variant;
128 }
129 } else {
130 $fonts['variants'][ $id ][] = $variant;
131 }
132 }
133 }
134 }
135 }
136
137 return $fonts;
138 }
139
140
141 /**
142 * Add Custom Font group to elementor font list.
143 *
144 * Group name "Custom" is added as the first element in the array.
145 *
146 * @param Array $font_groups default font groups in elementor.
147 * @return Array Modified font groups with newly added font group.
148 */
149 public function elementor_group( $font_groups ) {
150 $new_group[ self::$font_base ] = esc_html__( 'Custom Fonts', 'powerkit' );
151
152 $font_groups = $new_group + $font_groups;
153
154 return $font_groups;
155 }
156
157 /**
158 * Add Custom Fonts to the Elementor Page builder's font param.
159 *
160 * @param Array $fonts Custom Font's array.
161 */
162 public function add_elementor_fonts( $fonts ) {
163
164 $fonts_list = get_option( 'powerkit_custom_fonts_list' );
165
166 if ( is_array( $fonts_list ) && $fonts_list ) {
167 foreach ( $fonts_list as $item ) {
168 $id = sanitize_title( $item['name'] );
169
170 if ( $id ) {
171 $fonts[ $id ] = self::$font_base;
172 }
173 }
174 }
175
176 return $fonts;
177 }
178
179 /**
180 * Register new mime types and file extensions.
181 *
182 * @since 1.0.0
183 * @param array $mimes Current array of mime types..
184 */
185 public function allow_mimes( $mimes ) {
186 $mimes['woff'] = 'application/x-font-woff';
187 $mimes['woff2'] = 'application/x-font-woff2';
188
189 return $mimes;
190 }
191
192 /**
193 * Register settings
194 *
195 * @since 1.0.0
196 * @param array $settings List settings.
197 * @return array
198 */
199 public function register_settings( $settings ) {
200
201 $settings[] = array(
202 'id' => $this->slug,
203 'name' => esc_html__( 'Custom Fonts', 'powerkit' ),
204 'function' => array( $this, 'build_setting_page' ),
205 );
206
207 return $settings;
208 }
209
210 /**
211 * Build admin page
212 *
213 * @since 1.0.0
214 */
215 public function build_setting_page() {
216
217 powerkit_uuid_hash();
218
219 $this->save_options_page();
220
221 $powerkit_custom_fonts_id = get_option( 'powerkit_custom_fonts_counter', 1 );
222
223 $params = array(
224 'name' => '',
225 'weight' => '400',
226 'style' => 'normal',
227 'file_woff' => '',
228 'file_woff2' => '',
229 );
230
231 $action = 'new';
232
233 // Check wpnonce.
234 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
235 return;
236 }
237
238 // Check custom action.
239 if ( isset( $_REQUEST['action'] ) ) { // Input var ok; sanitization ok.
240 $action = sanitize_title( $_REQUEST['action'] ); // Input var ok; sanitization ok.
241
242 if ( isset( $_REQUEST['powerkit_custom_fonts_id'] ) && 'edit' === $action ) { // Input var ok.
243 $powerkit_custom_fonts_id = sanitize_key( $_REQUEST['powerkit_custom_fonts_id'] ); // Input var ok.
244 }
245 }
246
247 // Edit font.
248 if ( 'edit' === $action ) {
249 $font_list = get_option( 'powerkit_custom_fonts_list' );
250
251 if ( isset( $font_list[ $powerkit_custom_fonts_id ] ) ) {
252 $params = array_merge( (array) $params, (array) $font_list[ $powerkit_custom_fonts_id ] );
253 }
254 }
255
256 // Delete font.
257 if ( 'delete' === $action ) {
258 $this->delete_font();
259 }
260
261 // Settings page link.
262 $powerkit_custom_fonts_link = powerkit_get_page_url( 'fonts&tab=' . $this->slug, 'themes' );
263 ?>
264 <form method="post" action="<?php echo esc_url( $powerkit_custom_fonts_link ); ?>">
265 <div class="col-container">
266 <div id="col-left">
267 <div class="col-wrap">
268 <div class="form-wrap">
269 <div class="form-field">
270 <h2><?php esc_html_e( 'Instructions', 'powerkit' ); ?></h2>
271 <ol>
272 <li>
273 <?php
274 $link_webfont = sprintf( '<a href="%1$s" target="_blank">%2$s</a>', esc_url( 'https://www.fontsquirrel.com/tools/webfont-generator' ), esc_html__( 'Font Squirrel', 'powerkit' ) );
275
276 // translators: args - format woff, format woff, format woff2, link webfont-generator.
277 echo sprintf( esc_html__( 'Prepare your webfont files in %1$s and %2$s formats. You may convert your %3$s fonts at %4$s.', 'powerkit' ), '<code>woff</code>', '<code>woff2</code>', '<code>ttf</code>', wp_kses( $link_webfont, 'post' ) );
278 ?>
279 </li>
280 <li><?php esc_html_e( 'Upload your font files.', 'powerkit' ); ?></li>
281 <li><?php esc_html_e( 'Navigate to Appearance &rarr; Customise &rarr; Typography and select your font in typography controls under Custom Fonts.', 'powerkit' ); ?></li>
282 </ol>
283 </div>
284
285 <?php if ( 'edit' === $action ) : ?>
286 <h3>
287 <?php esc_html_e( 'Edit Font', 'powerkit' ); ?>
288
289 <small><a href="<?php echo esc_url( $powerkit_custom_fonts_link ); ?>"><?php esc_html_e( ' cancel ', 'powerkit' ); ?></a></small>
290 </h3>
291 <?php else : ?>
292 <h3><?php esc_html_e( 'Add New Font', 'powerkit' ); ?></h3>
293 <?php endif; ?>
294
295 <!-- Name -->
296 <div class="form-field form-required">
297 <label for="powerkit_custom_fonts_name"><?php esc_html_e( 'Name', 'powerkit' ); ?></label>
298
299 <input id="powerkit_custom_fonts_name" name="powerkit_custom_fonts_name" type="text" aria-required="true" value="<?php echo esc_attr( stripslashes( $params['name'] ) ); ?>" />
300 </div>
301
302 <!-- Font File [WOFF] -->
303 <div class="form-field">
304 <label><?php esc_html_e( 'Font .woff', 'powerkit' ); ?></label>
305
306 <div class="upload-font-container" data-type="application/x-font-woff">
307
308 <?php $file_woff = $params['file_woff'] ? basename( get_attached_file( $params['file_woff'] ) ) : null; ?>
309
310 <p><input class="filename" type="text" placeholder="<?php esc_html_e( 'Choose file..', 'powerkit' ); ?>" readonly value="<?php echo esc_html( $file_woff ); ?>"></p>
311
312 <!-- Add & remove file links -->
313 <div class="submitbox">
314 <a class="upload-font-link button <?php echo esc_attr( $params['file_woff'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Add File', 'powerkit' ); ?></a>
315 <a class="delete-font-link submitdelete <?php echo esc_attr( ! $params['file_woff'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove', 'powerkit' ); ?></a>
316 </div>
317
318 <input class="uploaded-font-id" id="powerkit_custom_fonts_file_woff" name="powerkit_custom_fonts_file_woff" type="hidden" value="<?php echo esc_attr( stripslashes( $params['file_woff'] ) ); ?>" />
319 </div>
320 </div>
321
322 <!-- Font File [WOFF2] -->
323 <div class="form-field">
324 <label><?php esc_html_e( 'Font .woff2', 'powerkit' ); ?></label>
325
326 <div class="upload-font-container" data-type="application/x-font-woff2">
327
328 <?php $file_woff2 = $params['file_woff2'] ? basename( get_attached_file( $params['file_woff2'] ) ) : null; ?>
329
330 <p><input class="filename" type="text" placeholder="<?php esc_html_e( 'Choose file..', 'powerkit' ); ?>" readonly value="<?php echo esc_html( $file_woff2 ); ?>"></p>
331
332 <!-- Add & remove file links -->
333 <div class="submitbox">
334 <a class="upload-font-link button <?php echo esc_attr( $params['file_woff2'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Add File', 'powerkit' ); ?></a>
335 <a class="delete-font-link submitdelete <?php echo esc_attr( ! $params['file_woff2'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove', 'powerkit' ); ?></a>
336 </div>
337
338 <input class="uploaded-font-id" id="powerkit_custom_fonts_file_woff2" name="powerkit_custom_fonts_file_woff2" type="hidden" value="<?php echo esc_attr( stripslashes( $params['file_woff2'] ) ); ?>" />
339 </div>
340 </div>
341
342 <!-- Font Weight -->
343 <div class="form-field">
344 <label for="powerkit_custom_fonts_weight"><?php esc_html_e( 'Weight', 'powerkit' ); ?></label>
345
346 <select class="regular-text" id="powerkit_custom_fonts_weight" name="powerkit_custom_fonts_weight">
347 <option value="100" <?php selected( '100', $params['weight'] ); ?>><?php esc_html_e( '100', 'powerkit' ); ?></option>
348 <option value="200" <?php selected( '200', $params['weight'] ); ?>><?php esc_html_e( '200', 'powerkit' ); ?></option>
349 <option value="300" <?php selected( '300', $params['weight'] ); ?>><?php esc_html_e( '300', 'powerkit' ); ?></option>
350 <option value="400" <?php selected( '400', $params['weight'] ); ?>><?php esc_html_e( '400 (regular)', 'powerkit' ); ?></option>
351 <option value="500" <?php selected( '500', $params['weight'] ); ?>><?php esc_html_e( '500', 'powerkit' ); ?></option>
352 <option value="600" <?php selected( '600', $params['weight'] ); ?>><?php esc_html_e( '600', 'powerkit' ); ?></option>
353 <option value="700" <?php selected( '700', $params['weight'] ); ?>><?php esc_html_e( '700', 'powerkit' ); ?></option>
354 <option value="800" <?php selected( '800', $params['weight'] ); ?>><?php esc_html_e( '800', 'powerkit' ); ?></option>
355 <option value="900" <?php selected( '900', $params['weight'] ); ?>><?php esc_html_e( '900', 'powerkit' ); ?></option>
356 </select>
357 </div>
358
359 <!-- Font Style -->
360 <div class="form-field">
361 <label for="powerkit_custom_fonts_style"><?php esc_html_e( 'Style', 'powerkit' ); ?></label>
362
363 <select class="regular-text" id="powerkit_custom_fonts_style" name="powerkit_custom_fonts_style">
364 <option value="normal" <?php selected( 'normal', $params['style'] ); ?>><?php esc_html_e( 'normal', 'powerkit' ); ?></option>
365 <option value="italic" <?php selected( 'italic', $params['style'] ); ?>><?php esc_html_e( 'italic', 'powerkit' ); ?></option>
366 </select>
367 </div>
368
369 <input name="powerkit_custom_fonts_id" type="hidden" value="<?php echo esc_html( $powerkit_custom_fonts_id ); ?>" />
370
371 <?php wp_nonce_field(); ?>
372
373 <?php if ( 'edit' === $action ) : ?>
374 <p class="submit"><input class="button button-primary" name="edit_settings" type="submit" value="<?php esc_html_e( 'Save Change', 'powerkit' ); ?>" /></p>
375 <?php else : ?>
376 <p class="submit"><input class="button button-primary" name="save_settings" type="submit" value="<?php esc_html_e( 'Add Font', 'powerkit' ); ?>" /></p>
377 <?php endif; ?>
378 </div>
379 </div>
380 </div>
381 <div id="col-right">
382 <div class="col-wrap wrap">
383 <table class="wrap wp-list-table widefat fixed striped">
384 <thead>
385 <tr>
386 <td scope="col" class="manage-column"><?php esc_html_e( 'Name', 'powerkit' ); ?></td>
387 <td scope="col" class="manage-column"><?php esc_html_e( 'Font Family', 'powerkit' ); ?></td>
388 <td scope="col" class="manage-column"><?php esc_html_e( 'Font Weight', 'powerkit' ); ?></td>
389 <td scope="col" class="manage-column"><?php esc_html_e( 'Font Style', 'powerkit' ); ?></td>
390 </tr>
391 </thead>
392 <tbody id="the-list">
393 <?php
394 $families = get_option( 'powerkit_custom_fonts_list', array() );
395
396 // Sort families.
397 if ( is_array( $families ) && $families ) {
398 $families_keys = array_keys( $families );
399
400 foreach ( $families as $key => $row ) {
401 $families_value[ $key ] = $row['name'];
402 $families_name[ $key ] = $row['weight'];
403 $families_order[ $key ] = $row['style'];
404 }
405 array_multisort( $families_value, SORT_DESC, $families_order, SORT_DESC, $families_name, SORT_ASC, $families, $families_keys );
406
407 $families = array_combine( $families_keys, $families );
408 }
409
410 // Loop families.
411 if ( $families ) {
412 foreach ( $families as $key => $family ) {
413
414 $edit_link = add_query_arg(
415 array(
416 '_wpnonce' => wp_create_nonce(),
417 'powerkit_custom_fonts_id' => $key,
418 'action' => 'edit',
419 ), $powerkit_custom_fonts_link
420 );
421
422 $delete_link = add_query_arg(
423 array(
424 '_wpnonce' => wp_create_nonce(),
425 'powerkit_custom_fonts_id' => $key,
426 'action' => 'delete',
427 ), $powerkit_custom_fonts_link
428 );
429 ?>
430 <tr>
431 <td scope="col" class="manage-column">
432 <a class="row-title" href="<?php echo esc_url( $edit_link ); ?>"><?php echo esc_html( $family['name'] ); ?></a>
433
434 <div class="row-actions">
435 <span class="edit">
436 <a href="<?php echo esc_url( $edit_link ); ?>" role="button"><?php esc_html_e( 'Edit', 'powerkit' ); ?></a> |
437 </span>
438 <span class="delete">
439 <a href="<?php echo esc_url( $delete_link ); ?>" class="powerkit-fonts-delete" role="button"><?php esc_html_e( 'Delete', 'powerkit' ); ?></a> |
440 </span>
441 <span class="view">
442 <a href="#" class="powerkit-fonts-view-code" role="button"><?php esc_html_e( 'View Code', 'powerkit' ); ?></a>
443 </span>
444 </div>
445 </td>
446 <td scope="col" class="manage-column"><?php echo esc_html( $family['slug'] ); ?></td>
447 <td scope="col" class="manage-column"><?php echo esc_html( $family['weight'] ); ?></td>
448 <td scope="col" class="manage-column"><?php echo esc_html( $family['style'] ); ?></td>
449 </tr>
450 <tr class="template-code hidden">
451 <td scope="col" class="manage-column" colspan="4">
452 <?php
453 // Example code.
454 $code = sprintf(
455 'h1 {
456 font-family: "%s";
457 font-weight: %s;
458 font-style: %s;
459 }',
460 $family['slug'],
461 $family['weight'],
462 $family['style']
463 );
464
465 $code = str_replace( "\t\t", '', $code );
466 ?>
467 <div id="template" class="template-box hidden">
468 <textarea readonly="readonly"><?php echo wp_kses( $code, 'post' ); ?></textarea>
469 </div>
470 </td>
471 </tr>
472 <tr class="hidden"></tr>
473 <?php
474 }
475 } else {
476 ?>
477 <tr>
478 <td scope="col" colspan="4"><?php esc_html_e( 'No fonts found. Add new fonts with the form to the left.', 'powerkit' ); ?></td>
479 </tr>
480 <?php
481 }
482 ?>
483 </tbody>
484 </table>
485 </div>
486 </div>
487 </div>
488 </form>
489 <?php
490 }
491
492 /**
493 * Delete font
494 *
495 * @since 1.0.0
496 */
497 protected function delete_font() {
498 if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok.
499 return;
500 }
501
502 if ( ! isset( $_GET['powerkit_custom_fonts_id'] ) ) { // Input var ok.
503 return;
504 }
505
506 // ID Font.
507 $id = sanitize_key( $_GET['powerkit_custom_fonts_id'] ); // Input var ok.
508
509 // Custom List.
510 $font_list = (array) get_option( 'powerkit_custom_fonts_list', array() );
511
512 // If exist font.
513 if ( isset( $font_list[ $id ] ) ) {
514
515 // Attachments delete.
516 if ( isset( $font_list[ $id ]['file_woff'] ) ) {
517 wp_delete_attachment( $font_list[ $id ]['file_woff'], true );
518 }
519 if ( isset( $font_list[ $id ]['file_woff2'] ) ) {
520 wp_delete_attachment( $font_list[ $id ]['file_woff2'], true );
521 }
522
523 // Delete font.
524 unset( $font_list[ $id ] );
525
526 // Save list.
527 update_option( 'powerkit_custom_fonts_list', $font_list );
528
529 // Output message.
530 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Font successfully deleted.', 'powerkit' ) );
531 }
532 }
533
534 /**
535 * Settings save
536 *
537 * @since 1.0.0
538 */
539 protected function save_options_page() {
540 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
541 return;
542 }
543
544 if ( ! isset( $_POST['powerkit_custom_fonts_id'] ) ) { // Input var ok.
545 return;
546 }
547
548 // Update id counter.
549 if ( isset( $_POST['save_settings'] ) ) { // Input var ok.
550 update_option( 'powerkit_custom_fonts_counter', intval( get_option( 'powerkit_custom_fonts_counter', 1 ) ) + 1 );
551 }
552
553 // ID Font.
554 $id = sanitize_key( $_POST['powerkit_custom_fonts_id'] ); // Input var ok.
555
556 // Custom List.
557 $font_list = (array) get_option( 'powerkit_custom_fonts_list', array() );
558
559 // Reset current settings.
560 $font_list[ $id ] = array();
561
562 // Set new settings.
563 if ( isset( $_POST['powerkit_custom_fonts_name'] ) ) { // Input var ok.
564 $font_list[ $id ]['slug'] = sanitize_title( $_POST['powerkit_custom_fonts_name'] ); // Input var ok; sanitization ok.
565 }
566 if ( isset( $_POST['powerkit_custom_fonts_name'] ) ) { // Input var ok.
567 $font_list[ $id ]['name'] = sanitize_text_field( $_POST['powerkit_custom_fonts_name'] ); // Input var ok; sanitization ok.
568 }
569 if ( isset( $_POST['powerkit_custom_fonts_file_woff'] ) ) { // Input var ok.
570 $font_list[ $id ]['file_woff'] = sanitize_text_field( $_POST['powerkit_custom_fonts_file_woff'] ); // Input var ok; sanitization ok.
571 }
572 if ( isset( $_POST['powerkit_custom_fonts_file_woff2'] ) ) { // Input var ok.
573 $font_list[ $id ]['file_woff2'] = sanitize_text_field( $_POST['powerkit_custom_fonts_file_woff2'] ); // Input var ok; sanitization ok.
574 }
575 if ( isset( $_POST['powerkit_custom_fonts_weight'] ) ) { // Input var ok.
576 $font_list[ $id ]['weight'] = sanitize_text_field( $_POST['powerkit_custom_fonts_weight'] ); // Input var ok; sanitization ok.
577 }
578 if ( isset( $_POST['powerkit_custom_fonts_style'] ) ) { // Input var ok.
579 $font_list[ $id ]['style'] = sanitize_text_field( $_POST['powerkit_custom_fonts_style'] ); // Input var ok; sanitization ok.
580 }
581
582 // Save list.
583 update_option( 'powerkit_custom_fonts_list', $font_list );
584
585 // Output message.
586 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Settings saved.', 'powerkit' ) );
587 }
588
589 /**
590 * Register fonts in the editor.
591 */
592 public function editor_enqueue() {
593 global $pagenow;
594
595 if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
596 $this->frontend_enqueue();
597 }
598 }
599
600 /**
601 * Frontend enqueue
602 *
603 * @since 1.0.0
604 */
605 public function frontend_enqueue() {
606
607 $custom_fonts = get_option( 'powerkit_custom_fonts_list' );
608
609 if ( is_array( $custom_fonts ) && $custom_fonts ) {
610
611 $exclude = array();
612
613 $font_face = null;
614
615 foreach ( $custom_fonts as $key => $item ) {
616
617 $id = sanitize_title( $item['name'] );
618
619 if ( $id ) {
620 $src = array();
621 $files = array();
622
623 $files['woff'] = wp_get_attachment_url( $item['file_woff'] );
624 $files['woff2'] = wp_get_attachment_url( $item['file_woff2'] );
625
626 foreach ( $files as $key_file => $file_url ) {
627 if ( $file_url ) {
628 $src[] = sprintf( 'url("%1$s") format("%2$s")', $file_url, $key_file );
629 }
630 }
631
632 if ( $src ) {
633 $src_line = implode( ',', $src );
634
635 $display = 'async' === $this->load_method ? 'swap' : 'auto';
636
637 $font_face .= sprintf( '@font-face { font-family: "%1$s"; src: %2$s; font-display: %3$s; font-weight: %4$s; font-style: %5$s;}', $id, $src_line, $display, $item['weight'], $item['style'] );
638 }
639 }
640 }
641
642 if ( $font_face ) {
643 ?>
644 <style><?php print( $font_face ); // XSS. ?></style>
645 <?php
646 }
647 }
648 }
649 }
650
651 new Powerkit_Custom_Fonts();
652