PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 3.1.4
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v3.1.4
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / inc / fonts.php

fonts.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 3.1.4, at inc/fonts.php

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