| 1 |
<?php |
| 2 |
/** |
| 3 |
* Load Google Fonts |
| 4 |
* @package GutSliderBlocks |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. |
| 8 |
|
| 9 |
if( ! class_exists( 'GutSlider_Load_Fonts' ) ) { |
| 10 |
|
| 11 |
class GutSlider_Load_Fonts { |
| 12 |
|
| 13 |
private static $all_fonts = []; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor |
| 17 |
* return void |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'wp_enqueue_scripts', [ $this, 'fonts_loader' ], 10); |
| 21 |
add_action( 'admin_enqueue_scripts', [ $this, 'fonts_loader' ], 10 ); |
| 22 |
add_action('gutsliders_render_block', [ $this, 'font_generator' ]); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Font generator |
| 27 |
* return void |
| 28 |
*/ |
| 29 |
public function font_generator( $block ) { |
| 30 |
if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) { |
| 31 |
$attributes = $block['attrs']; |
| 32 |
|
| 33 |
foreach ( $attributes as $key => $value ) { |
| 34 |
if ( ! empty( $value ) && strpos( $key, 'gutsliders_' ) === 0 && strpos( $key, 'FontFamily' ) !== false ) { |
| 35 |
self::$all_fonts[] = $value; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
$this->fonts_loader(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Load fonts |
| 45 |
* return void |
| 46 |
*/ |
| 47 |
public function fonts_loader() { |
| 48 |
if ( is_array( self::$all_fonts ) && count( self::$all_fonts ) > 0 ) { |
| 49 |
|
| 50 |
$fonts = array_filter( array_unique( self::$all_fonts ) ); |
| 51 |
|
| 52 |
if ( ! empty( $fonts ) ) { |
| 53 |
|
| 54 |
$system = array( |
| 55 |
'Arial', |
| 56 |
'Tahoma', |
| 57 |
'Verdana', |
| 58 |
'Helvetica', |
| 59 |
'Times New Roman', |
| 60 |
'Trebuchet MS', |
| 61 |
'Georgia', |
| 62 |
); |
| 63 |
|
| 64 |
$gfonts = ''; |
| 65 |
$gfonts_attr = ':100,200,300,400,500,600,700,800,900'; |
| 66 |
|
| 67 |
foreach ($fonts as $font) { |
| 68 |
if ( ! in_array( $font, $system, true) && ! empty( $font ) ) { |
| 69 |
$gfonts .= str_replace( ' ', '+', trim( $font ) ) . $gfonts_attr . '|'; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if (!empty($gfonts)) { |
| 74 |
$font_array = explode('|', $gfonts); |
| 75 |
|
| 76 |
foreach ($font_array as $font) { |
| 77 |
if (empty($font)) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
$query_args = ['family' => $font]; |
| 82 |
$font_handle = 'gutsliders-font-' . sanitize_title($font); |
| 83 |
|
| 84 |
wp_register_style( |
| 85 |
$font_handle, |
| 86 |
add_query_arg($query_args, '//fonts.googleapis.com/css'), |
| 87 |
[], |
| 88 |
GUTSLIDER_VERSION, |
| 89 |
'all' |
| 90 |
); |
| 91 |
|
| 92 |
wp_enqueue_style($font_handle); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Reset. |
| 97 |
$gfonts = ''; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
new GutSlider_Load_Fonts(); // Initialize the class |