Skip to content

Commit 14aa223

Browse files
authored
Merge pull request #136 from brainstormforce/inline-google-font-support
Support inline style of Google fonts
2 parents fa9b30f + d973899 commit 14aa223

5 files changed

Lines changed: 110 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
**Tags:** fonts, custom fonts, Google Fonts, performance, full site editing
55
**Requires at least:** 5.0
66
**Tested up to:** 6.7
7-
**Stable tag:** 2.1.10
7+
**Stable tag:** 2.1.11
88
**License:** GPLv2 or later
99
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -151,6 +151,9 @@ Yes, Custom Fonts is completely free to use, without any limitation.
151151

152152

153153
## Changelog ##
154+
### 2.1.11 ###
155+
- Added logic to generate and enqueue a local CSS file for downloaded Google Fonts, ensuring they load in both the editor and frontend to support inline font styles.
156+
154157
### 2.1.10 ###
155158
- Resolved issue where plugin was not properly deploying on wp.org
156159

classes/class-bsf-custom-fonts-render.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,105 @@ public function __construct() {
139139
add_filter( 'elementor/fonts/additional_fonts', array( $this, 'add_elementor_fonts' ) );
140140
// Astra filter before creating google fonts URL.
141141
add_filter( 'astra_google_fonts_selected', array( $this, 'remove_custom_font_google_url' ) );
142+
// Compatibility introduce for google local font should work as a inline.
143+
add_filter( 'wp_enqueue_scripts', array( $this, 'load_local_google_fonts' ) );
144+
145+
}
146+
147+
/**
148+
* Generate and enqueue a local CSS file for self-hosted Google Fonts.
149+
*
150+
* This function scans the `/bcf-fonts/` directory inside `wp-content/` for locally stored Google Fonts.
151+
* It dynamically creates an `@font-face` CSS file (`local-fonts.css`) that defines font-face rules
152+
* for all detected font files. The generated CSS is then enqueued for use on the site.
153+
*
154+
* @since 2.1.11
155+
*/
156+
public function load_local_google_fonts() {
157+
// We are using WP_Filesystem for managing google fonts files which is necessary for the proper functionality of the plugin.
158+
global $wp_filesystem;
159+
160+
// If the filesystem has not been instantiated yet, do it here.
161+
if ( ! function_exists( 'WP_Filesystem' ) ) {
162+
require_once ABSPATH . 'wp-admin/includes/file.php'; // PHPCS:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
163+
}
164+
165+
WP_Filesystem(); // Initialize the filesystem.
166+
167+
$upload_dir = WP_CONTENT_DIR . '/bcf-fonts';
168+
$css_file = WP_CONTENT_DIR . '/bcf-fonts/local-fonts.css';
169+
170+
if ( ! is_dir( $upload_dir ) ) {
171+
return;
172+
}
173+
174+
$font_face_css = '';
175+
176+
$font_folders = array_diff( scandir( $upload_dir ), array( '.', '..' ) );
177+
178+
foreach ( $font_folders as $folder ) {
179+
$font_folder_path = $upload_dir . '/' . $folder;
180+
181+
if ( is_dir( $font_folder_path ) ) {
182+
$font_files = glob( $font_folder_path . '/*.{woff2,woff,ttf,otf,eot,svg}', GLOB_BRACE );
183+
184+
$font_variations = array();
185+
186+
foreach ( $font_files as $font_file ) {
187+
$font_url = content_url( '/bcf-fonts/' . $folder . '/' . basename( $font_file ) );
188+
189+
preg_match( '/(.*)-([0-9a-z]+)-(\w+)\d*\.(woff2|woff|ttf|otf|eot|svg)/', basename( $font_file ), $matches );
190+
191+
if ( count( $matches ) ) {
192+
$font_name = trim( $matches[1] );
193+
$font_weight = $matches[2];
194+
$font_style = $matches[3];
195+
$file_format = $matches[4];
196+
197+
$font_variations[ "$font_name-$font_weight-$font_style" ][ $file_format ] = $font_url;
198+
}
199+
}
200+
201+
foreach ( $font_variations as $key => $formats ) {
202+
list($font_name, $font_weight, $font_style) = explode( '-', $key );
203+
204+
$src = array();
205+
if ( ! empty( $formats['woff2'] ) ) {
206+
$src[] = "url('{$formats['woff2']}') format('woff2')";
207+
}
208+
if ( ! empty( $formats['woff'] ) ) {
209+
$src[] = "url('{$formats['woff']}') format('woff')";
210+
}
211+
if ( ! empty( $formats['ttf'] ) ) {
212+
$src[] = "url('{$formats['ttf']}') format('truetype')";
213+
}
214+
if ( ! empty( $formats['otf'] ) ) {
215+
$src[] = "url('{$formats['otf']}') format('opentype')";
216+
}
217+
if ( ! empty( $formats['eot'] ) ) {
218+
$src[] = "url('{$formats['eot']}') format('embedded-opentype')";
219+
}
220+
if ( ! empty( $formats['svg'] ) ) {
221+
$src[] = "url('{$formats['svg']}') format('svg')";
222+
}
223+
224+
$font_face_css .= "
225+
@font-face {
226+
font-family: '{$font_name}';
227+
src: " . implode( ', ', $src ) . ";
228+
font-weight: {$font_weight};
229+
font-style: {$font_style};
230+
}
231+
";
232+
}
233+
}
234+
}
235+
236+
// Use WP_Filesystem to write to file instead of file_put_contents.
237+
$wp_filesystem->put_contents( $css_file, $font_face_css, FS_CHMOD_FILE );
238+
239+
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
240+
wp_enqueue_style( 'local-google-fonts', content_url( '/bcf-fonts/local-fonts.css' ), array(), null );
142241
}
143242

144243
/**

custom-fonts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Author: Brainstorm Force
77
* Author URI: http://www.brainstormforce.com
88
* Text Domain: custom-fonts
9-
* Version: 2.1.10
9+
* Version: 2.1.11
1010
*
1111
* @package Bsf_Custom_Fonts
1212
*/
@@ -25,7 +25,7 @@
2525
define( 'BSF_CUSTOM_FONTS_BASE', plugin_basename( BSF_CUSTOM_FONTS_FILE ) );
2626
define( 'BSF_CUSTOM_FONTS_DIR', plugin_dir_path( BSF_CUSTOM_FONTS_FILE ) );
2727
define( 'BSF_CUSTOM_FONTS_URI', plugins_url( '/', BSF_CUSTOM_FONTS_FILE ) );
28-
define( 'BSF_CUSTOM_FONTS_VER', '2.1.10' );
28+
define( 'BSF_CUSTOM_FONTS_VER', '2.1.11' );
2929
define( 'BSF_CUSTOM_FONTS_POST_TYPE', 'bsf_custom_fonts' );
3030
define( 'BSF_CUSTOM_FONTS_ADMIN_PAGE', 'bsf-custom-fonts' );
3131

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "custom-fonts",
3-
"version": "2.1.10",
3+
"version": "2.1.11",
44
"main": "Gruntfile.js",
55
"author": "Brainstorm Force",
66
"workspaces": [

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.me/BrainstormForce
44
Tags: fonts, custom fonts, Google Fonts, performance, full site editing
55
Requires at least: 5.0
66
Tested up to: 6.7
7-
Stable tag: 2.1.10
7+
Stable tag: 2.1.11
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -151,6 +151,9 @@ Yes, Custom Fonts is completely free to use, without any limitation.
151151

152152

153153
== Changelog ==
154+
= 2.1.11
155+
- Added logic to generate and enqueue a local CSS file for downloaded Google Fonts, ensuring they load in both the editor and frontend to support inline font styles.
156+
154157
= 2.1.10
155158
- Resolved issue where plugin was not properly deploying on wp.org
156159

0 commit comments

Comments
 (0)