PluginProbe
Powerkit – Supercharge your WordPress Site / 2.9.0
Powerkit – Supercharge your WordPress Site v2.9.0
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.9.0, at modules/class-powerkit-custom-fonts.php

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