PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 2.1.2
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v2.1.2
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 / src / fonts.php

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

116 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 return strpos( $block_name, 'blockspare/' ) === 0;
51 }
52
53 public function gather_google_fonts( $blocks ) {
54 $google_fonts = array();
55 foreach ( $blocks as $block ) {
56
57 // Gather all "fontFamily" attribute values
58 if ( $this->is_blockspare_block( $block['blockName'] ) ) {
59 foreach ( $block['attrs'] as $attr_name => $font_name ) {
60 if ( preg_match( '/fontFamily$/i', $attr_name ) ) {
61 if ( ! $this->is_web_font( $font_name ) ) {
62 continue;
63 }
64 if ( ! in_array( $font_name, $google_fonts ) ) {
65 $google_fonts[] = $font_name;
66 }
67 }
68 }
69 }
70 if ( ! empty( $block['innerBlocks'] ) ) {
71 $google_fonts = array_unique( array_merge( $google_fonts, $this->gather_google_fonts( $block['innerBlocks'] ) ) );
72 }
73 }
74
75 return $google_fonts;
76 }
77
78 /**
79 * Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647
80 */
81 public function enqueue_google_fonts( $google_fonts ) {
82 if ( ! count( $google_fonts) ) {
83 return;
84 }
85
86 foreach ( $google_fonts as &$font ) {
87 $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
88 }
89
90 $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%s', implode( rawurlencode( '|' ), $google_fonts ) );
91
92 $subsets = [
93 'ru_RU' => 'cyrillic',
94 'bg_BG' => 'cyrillic',
95 'he_IL' => 'hebrew',
96 'el' => 'greek',
97 'vi' => 'vietnamese',
98 'uk' => 'cyrillic',
99 'cs_CZ' => 'latin-ext',
100 'ro_RO' => 'latin-ext',
101 'pl_PL' => 'latin-ext',
102 ];
103
104 $locale = get_locale();
105 if ( isset( $subsets[ $locale ] ) ) {
106 $fonts_url .= '&subset=' . $subsets[ $locale ];
107 }
108
109 wp_enqueue_style( 'blockspare-google-fonts', $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
110 }
111
112 }
113
114 new blockspare_Google_Fonts();
115 }
116