PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.2.2
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.2.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 1.2.2, at src/fonts.php

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