PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 3.1.1
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v3.1.1
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
← All changes | inc/fonts.php +95 -102 trunk3.1.1 View file →
@@ -1,130 +1,123 @@
1 1 <?php
2 2
3 3 // Exit if accessed directly.
4 -if (! defined('ABSPATH')) {
5 - exit;
4 +if ( ! defined( 'ABSPATH' ) ) {
5 + exit;
6 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 - }
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 + }
20 19 }
21 20
22 -if (! class_exists('blockspare_Google_Fonts')) {
23 - class blockspare_Google_Fonts
24 - {
21 +if ( ! class_exists( 'blockspare_Google_Fonts' ) ) {
22 + class blockspare_Google_Fonts {
25 23
26 - public static $google_fonts = [];
24 + public static $google_fonts = [];
27 25
28 - function __construct()
29 - {
26 + function __construct() {
27 +
28 + add_filter( 'render_block', array( $this, 'bs_gather_google_fonts' ), 10, 2 );
29 + }
30 30
31 - add_filter('render_block', array($this, 'bs_gather_google_fonts'), 10, 2);
32 - }
31 + public function bs_gather_google_fonts($block_content, $block ) {
32 + if ( $block_content === null ) {
33 + return $block_content;
34 + }
33 35
34 - public function bs_gather_google_fonts($block_content, $block)
35 - {
36 - if ($block_content === null) {
37 - return $block_content;
38 - }
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 + }
39 46
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 - }
47 + return $block_content;
48 + }
50 49
51 - return $block_content;
52 - }
50 + public static function enqueue_frontend_block_fonts() {
51 + self::enqueue_google_fonts( array_unique( self::$google_fonts ) );
52 + }
53 53
54 - public static function enqueue_frontend_block_fonts()
55 - {
56 - self::enqueue_google_fonts(array_unique(self::$google_fonts));
57 - }
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 58
59 - public static function is_web_font($font_name)
60 - {
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 + }
61 66
62 - return ! in_array(strtolower($font_name), ['Arial', 'Times New Roman', 'Helvetica', 'Georgia']);
63 - }
67 + public static function register_font( $font_name ) {
68 + if ( ! self::is_web_font( $font_name ) ) {
69 + return;
70 + }
64 71
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 + 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;
72 76
73 - public static function register_font($font_name)
74 - {
75 - if (! self::is_web_font($font_name)) {
76 - return;
77 - }
77 + // Enqueue the fonts in the footer.
78 + add_filter( 'wp_footer', array( 'blockspare_Google_Fonts', 'enqueue_frontend_block_fonts' ) );
79 + }
80 + }
81 + }
78 82
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 83
84 - // Enqueue the fonts in the footer.
85 - add_filter('wp_footer', array('blockspare_Google_Fonts', 'enqueue_frontend_block_fonts'));
86 - }
87 - }
88 - }
84 +
89 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 + }
90 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 + }
91 97
98 + $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%s', implode( rawurlencode( '|' ), $google_fonts ) );
92 99
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 - }
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 + ];
101 111
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 - }
112 + $locale = get_locale();
113 + if ( isset( $subsets[ $locale ] ) ) {
114 + $fonts_url .= '&subset=' . $subsets[ $locale ];
115 + }
105 116
106 - $fonts_url = sprintf('https://fonts.googleapis.com/css?family=%s', implode(rawurlencode('|'), $google_fonts));
117 + wp_enqueue_style( $handle, $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
118 + }
107 119
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 - ];
120 + }
119 121
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();
122 + new blockspare_Google_Fonts();
130 123 }