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