PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 2.6.4
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v2.6.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 2.6.4, at inc/fonts.php

120 lines 3.3 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
8 if ( ! class_exists( 'blockspare_Google_Fonts' ) ) {
9 class blockspare_Google_Fonts {
10
11 function __construct() {
12 add_action( 'wp_head', array( $this, 'enqueue_frontend_block_fonts' ), 100 );
13 }
14
15 public function enqueue_frontend_block_fonts() {
16
17
18
19 if ( ! apply_filters( 'blockspare_enqueue_fonts', true ) ) {
20 return;
21 }
22
23 if ( is_single() || is_page() || is_404() ) {
24 global $post;
25 if ( is_object( $post ) && property_exists( $post, 'post_content' ) ) {
26 $this->_enqueue_frontend_block_fonts( $post->post_content );
27 }
28 } elseif ( is_archive() || is_home() || is_search() ) {
29 global $wp_query;
30 foreach ( $wp_query as $post ) {
31 if ( is_object( $post ) && property_exists( $post, 'post_content' ) ) {
32 $this->_enqueue_frontend_block_fonts( $post->post_content );
33 }
34 }
35 }
36 }
37
38 public function _enqueue_frontend_block_fonts( $content ) {
39 $blocks = parse_blocks( $content );
40 $google_fonts = $this->gather_google_fonts( $blocks );
41 $this->enqueue_google_fonts( $google_fonts );
42 }
43
44 public function is_web_font( $font_name ) {
45
46 return ! in_array( $font_name , [ 'Arial', 'Times New Roman', 'Helvetica', 'Georgia' ] );
47 }
48
49 public function is_blockspare_block( $block_name ) {
50 if(!empty($block_name)){
51 return strpos( $block_name, 'blockspare/' ) === 0;
52
53 }
54 return false;
55 }
56
57 public function gather_google_fonts( $blocks ) {
58 $google_fonts = array();
59 foreach ( $blocks as $block ) {
60
61 // Gather all "fontFamily" attribute values
62 if ( $this->is_blockspare_block( $block['blockName'] ) ) {
63 foreach ( $block['attrs'] as $attr_name => $font_name ) {
64 if ( preg_match( '/fontFamily$/i', $attr_name ) ) {
65 if ( ! $this->is_web_font( $font_name ) ) {
66 continue;
67 }
68 if ( ! in_array( $font_name, $google_fonts ) ) {
69 $google_fonts[] = $font_name;
70 }
71 }
72 }
73 }
74 if ( ! empty( $block['innerBlocks'] ) ) {
75 $google_fonts = array_unique( array_merge( $google_fonts, $this->gather_google_fonts( $block['innerBlocks'] ) ) );
76 }
77 }
78
79 return $google_fonts;
80 }
81
82 /**
83 * Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647
84 */
85 public function enqueue_google_fonts( $google_fonts ) {
86 if ( ! count( $google_fonts) ) {
87 return;
88 }
89
90 foreach ( $google_fonts as &$font ) {
91 $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
92 }
93
94 $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%s', implode( rawurlencode( '|' ), $google_fonts ) );
95
96 $subsets = [
97 'ru_RU' => 'cyrillic',
98 'bg_BG' => 'cyrillic',
99 'he_IL' => 'hebrew',
100 'el' => 'greek',
101 'vi' => 'vietnamese',
102 'uk' => 'cyrillic',
103 'cs_CZ' => 'latin-ext',
104 'ro_RO' => 'latin-ext',
105 'pl_PL' => 'latin-ext',
106 ];
107
108 $locale = get_locale();
109 if ( isset( $subsets[ $locale ] ) ) {
110 $fonts_url .= '&subset=' . $subsets[ $locale ];
111 }
112
113 wp_enqueue_style( 'blockspare-google-fonts', $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
114 }
115
116 }
117
118 new blockspare_Google_Fonts();
119 }
120