PluginProbe
SlingBlocks – Gutenberg Blocks by FunnelKit (Formerly WooFunnels) / 1.3.0
SlingBlocks – Gutenberg Blocks by FunnelKit (Formerly WooFunnels) v1.3.0
1.8.1 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.6.0 1.7.0 1.8.0
slingblocks / font / fonts.php

fonts.php in SlingBlocks – Gutenberg Blocks by FunnelKit (Formerly WooFunnels) 1.3.0, at font/fonts.php

147 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Loads the Google Fonts used in Woofunnels blocks in the frontend.
4 */
5
6 // Exit if accessed directly.
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10 if ( ! class_exists( 'BWF_Google_Fonts' ) ) {
11 class BWF_Google_Fonts {
12
13 public static $google_fonts = [];
14
15 function __construct() {
16 add_filter( 'render_block', array( $this, 'gather_google_fonts' ), 10, 2 );
17 add_filter( 'wp_footer', array( $this, 'enqueue_frontend_block_fonts' ) );
18 }
19
20 public function gather_google_fonts( $block_content, $block ) {
21
22 /** return if admin panel */
23 if ( is_admin() ) {
24 return $block_content;
25 }
26
27 if ( $this->is_woofunnel_block( $block['blockName'] ) && is_array( $block['attrs'] ) ) {
28 foreach ( $block['attrs'] as $attr_name => $font_name ) {
29 if ( preg_match( '/font$/i', $attr_name ) ) {
30 if ( isset( $font_name['desktop'] ) && isset( $font_name['desktop']['family'] ) && ! empty( $font_name['desktop']['family'] ) ) {
31 self::register_font( $font_name['desktop']['family'] );
32 }
33 if ( isset( $font_name['tablet'] ) && isset( $font_name['tablet']['family'] ) && ! empty( $font_name['tablet']['family'] ) ) {
34 self::register_font( $font_name['tablet']['family'] );
35 }
36 if ( isset( $font_name['mobile'] ) && isset( $font_name['mobile']['family'] ) && ! empty( $font_name['mobile']['family'] ) ) {
37 self::register_font( $font_name['mobile']['family'] );
38 }
39 }
40 }
41 }
42
43 return $block_content;
44 }
45
46 public function enqueue_frontend_block_fonts() {
47 self::enqueue_google_fonts( array_unique( self::$google_fonts ) );
48 }
49
50 public static function is_web_font( $font_name ) {
51 return ! in_array( strtolower( $font_name ), [ 'serif', 'sans-serif', 'monospace', 'serif-alt' ] );
52 }
53
54 public static function is_system_font( $font_name ) {
55 $standard_font = file_exists( dirname( __FILE__ ) . '/standard-fonts.php' ) ? include dirname( __FILE__ ) . '/standard-fonts.php' : null;
56 if ( $standard_font ) {
57 foreach ( $standard_font as $key => $value ) {
58 if ( strtolower( $font_name ) === strtolower( $value['value'] ) ) {
59 return true;
60 }
61 }
62 }
63
64 return false;
65 }
66
67 public function is_woofunnel_block( $block_name ) {
68 return ! is_null( $block_name ) && ( strpos( $block_name, 'bwfblocks/' ) === 0 || strpos( $block_name, 'sling-block/' ) === 0 );
69 }
70
71 public static function register_font( $font_name ) {
72 if ( ! self::is_web_font( $font_name ) ) {
73 return;
74 }
75 if ( self::is_system_font( $font_name ) ) {
76 return;
77 }
78
79 if ( ! in_array( $font_name, self::$google_fonts ) ) {
80 // Allow themes to disable enqueuing fonts, helpful for custom fonts.
81 if ( apply_filters( 'bwfblock_enqueue_font', true, $font_name ) ) {
82 self::$google_fonts[] = $font_name;
83 }
84 }
85 }
86
87 /**
88 * Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647
89 */
90 public static function enqueue_google_fonts( $google_fonts, $handle = 'bwfblock-google-fonts' ) {
91 global $post;
92 if ( ! $post instanceof WP_Post ) {
93 return;
94 }
95 $default_font = get_post_meta( $post->ID, 'bwfblock_default_font', true );
96
97 if ( ! count( $google_fonts ) && empty( $default_font ) ) {
98 return;
99 }
100
101 // Add default in block font set
102 if ( ! empty( $default_font ) ) {
103 $google_fonts[] = $default_font;
104 $google_fonts = array_unique( $google_fonts );
105 }
106
107 foreach ( $google_fonts as &$font ) {
108
109 if ( ! empty( $font ) ) {
110 $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
111 }
112 }
113
114 $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%s', implode( rawurlencode( '|' ), $google_fonts ) );
115
116 $subsets = [
117 'ru_RU' => 'cyrillic',
118 'bg_BG' => 'cyrillic',
119 'he_IL' => 'hebrew',
120 'el' => 'greek',
121 'vi' => 'vietnamese',
122 'uk' => 'cyrillic',
123 'cs_CZ' => 'latin-ext',
124 'ro_RO' => 'latin-ext',
125 'pl_PL' => 'latin-ext',
126 ];
127
128 $locale = get_locale();
129 if ( isset( $subsets[ $locale ] ) ) {
130 $fonts_url .= '&subset=' . $subsets[ $locale ];
131 }
132
133 wp_enqueue_style( $handle, $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
134
135 if ( ! empty( $default_font ) ) {
136 $default_font_selector = apply_filters( 'bwfblock_default_font_selector', 'body, body *' );
137 $default_font_css = "$default_font_selector{font-family:$default_font;}";
138
139 wp_add_inline_style( $handle, $default_font_css );
140 }
141 }
142
143 }
144
145 new BWF_Google_Fonts();
146 }
147