| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly. |
| 4 |
if (! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
if (! function_exists('bs_ends_with')) { |
| 8 |
/** |
| 9 |
* This function is only available in PHP 8.0 and above. |
| 10 |
* |
| 11 |
* @param {string} $haystack |
| 12 |
* @param {string} $needle |
| 13 |
* @return {boolean} |
| 14 |
*/ |
| 15 |
function bs_ends_with($haystack, $needle) |
| 16 |
{ |
| 17 |
$needle_len = strlen($needle); |
| 18 |
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, -$needle_len)); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
if (! class_exists('blockspare_Google_Fonts')) { |
| 23 |
class blockspare_Google_Fonts |
| 24 |
{ |
| 25 |
|
| 26 |
public static $google_fonts = []; |
| 27 |
|
| 28 |
function __construct() |
| 29 |
{ |
| 30 |
|
| 31 |
add_filter('render_block', array($this, 'bs_gather_google_fonts'), 10, 2); |
| 32 |
} |
| 33 |
|
| 34 |
public function bs_gather_google_fonts($block_content, $block) |
| 35 |
{ |
| 36 |
if ($block_content === null) { |
| 37 |
return $block_content; |
| 38 |
} |
| 39 |
|
| 40 |
$block_name = isset($block['blockName']) ? $block['blockName'] : ''; |
| 41 |
if ($this->is_blockspare_block($block_name) && is_array($block['attrs'])) { |
| 42 |
// if (stripos($block_content, 'family') !== false) { |
| 43 |
foreach ($block['attrs'] as $attr_name => $font_name) { |
| 44 |
if (bs_ends_with(strtolower($attr_name), 'fontfamily')) { |
| 45 |
self::register_font($font_name); |
| 46 |
} |
| 47 |
} |
| 48 |
//} |
| 49 |
} |
| 50 |
|
| 51 |
return $block_content; |
| 52 |
} |
| 53 |
|
| 54 |
public static function enqueue_frontend_block_fonts() |
| 55 |
{ |
| 56 |
self::enqueue_google_fonts(array_unique(self::$google_fonts)); |
| 57 |
} |
| 58 |
|
| 59 |
public static function is_web_font($font_name) |
| 60 |
{ |
| 61 |
|
| 62 |
return ! in_array(strtolower($font_name), ['Arial', 'Times New Roman', 'Helvetica', 'Georgia']); |
| 63 |
} |
| 64 |
|
| 65 |
public static function is_blockspare_block($block_name) |
| 66 |
{ |
| 67 |
if (!empty($block_name)) { |
| 68 |
return strpos($block_name, 'blockspare/') === 0; |
| 69 |
} |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
public static function register_font($font_name) |
| 74 |
{ |
| 75 |
if (! self::is_web_font($font_name)) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if (! in_array($font_name, self::$google_fonts)) { |
| 80 |
// Allow themes to disable enqueuing fonts, helpful for custom fonts. |
| 81 |
if (apply_filters('blockspare_enqueue_font', true, $font_name)) { |
| 82 |
self::$google_fonts[] = $font_name; |
| 83 |
|
| 84 |
// Enqueue the fonts in the footer. |
| 85 |
add_filter('wp_footer', array('blockspare_Google_Fonts', 'enqueue_frontend_block_fonts')); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647 |
| 95 |
*/ |
| 96 |
public static function enqueue_google_fonts($google_fonts, $handle = 'blockspare-google-fonts') |
| 97 |
{ |
| 98 |
if (! count($google_fonts)) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
foreach ($google_fonts as &$font) { |
| 103 |
$font = str_replace(' ', '+', $font) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic'; |
| 104 |
} |
| 105 |
|
| 106 |
$fonts_url = sprintf('https://fonts.googleapis.com/css?family=%s', implode(rawurlencode('|'), $google_fonts)); |
| 107 |
|
| 108 |
$subsets = [ |
| 109 |
'ru_RU' => 'cyrillic', |
| 110 |
'bg_BG' => 'cyrillic', |
| 111 |
'he_IL' => 'hebrew', |
| 112 |
'el' => 'greek', |
| 113 |
'vi' => 'vietnamese', |
| 114 |
'uk' => 'cyrillic', |
| 115 |
'cs_CZ' => 'latin-ext', |
| 116 |
'ro_RO' => 'latin-ext', |
| 117 |
'pl_PL' => 'latin-ext', |
| 118 |
]; |
| 119 |
|
| 120 |
$locale = get_locale(); |
| 121 |
if (isset($subsets[$locale])) { |
| 122 |
$fonts_url .= '&subset=' . $subsets[$locale]; |
| 123 |
} |
| 124 |
|
| 125 |
wp_enqueue_style($handle, $fonts_url); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
new blockspare_Google_Fonts(); |
| 130 |
} |
| 131 |
|