Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/2733c0050934a6d610a00841a24262ee to your computer and use it in GitHub Desktop.
Save wpmudev-sls/2733c0050934a6d610a00841a24262ee to your computer and use it in GitHub Desktop.
[Hummingbird Pro] - Fix font display issue for google fonts
<?php
/**
* Plugin Name: [Hummingbird Pro] - Fix font display issue for google fonts
* Description: [Hummingbird Pro] - Fix font display issue for google fonts - 1155234930966378
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_action( 'after_setup_theme', 'wpmudev_hmbp_fix_font_display_issue_for_google_font_func', 100 );
function wpmudev_hmbp_fix_font_display_issue_for_google_font_func() {
if( defined('WPHB_VERSION') && class_exists( 'Hummingbird\WP_Hummingbird' ) && class_exists('Hummingbird\Core\Settings') ){
if( ! Hummingbird\Core\Settings::get_setting( 'enabled', 'minify' ) || ( is_admin() && ! wp_doing_ajax() ) ){
return;
}
class WPMUDEV_Fix_Font_Display_Issue{
public function __construct(){
add_filter( 'print_styles_array', array( $this, 'add_font_display_swap' ), 1 );
// convert custom css font for Kirki Customizer Framework plugin
if( class_exists('Kirki_Modules_Webfonts_Embed') ){
add_action( 'kirki_dynamic_css', array( $this, 'enable_http_response_filter'), 0 );
add_action( 'kirki_dynamic_css', array( $this, 'disable_http_response_filter'), 999 );
}
// support Revoslider
if( class_exists('RevSliderFront') ){
add_filter( 'revslider_printCleanFontImport', array( $this, 'revslider_add_font_display') );
}
// Support Kallyas theme
if( class_exists('Zn_Framework') ){
add_filter( 'zn_dynamic_css', array( $this, 'add_font_display_swap_for_custom_css' ), 9999 );
}
}
public function add_font_display_swap( $handles ){
global $wp_styles;
if( $wp_styles->queue ){
foreach( $handles as $handle ){
if( isset( $wp_styles->registered[ $handle ] ) ){
$src = $wp_styles->registered[ $handle ]->src;
if( is_string( $src ) && strpos( $src, '//fonts.googleapis.com/css?family=') !== false && false === strpos( $src, 'display=') ){
$wp_styles->registered[ $handle ]->src = str_replace( '//fonts.googleapis.com/css?family=', '//fonts.googleapis.com/css?display=swap&family=', $src );
}
}
}
}
return $handles;
}
// add font display wrap for revoslider
public function revslider_add_font_display( $ret ){
if( false !== strpos( $ret, '//fonts.googleapis.com/css?family=') ){
$ret = str_replace( '//fonts.googleapis.com/css?family=', '//fonts.googleapis.com/css?display=swap&family=', $ret );
}elseif( $ret ){
$ret = str_replace( 'font-style: normal;', 'font-style: normal;font-display: swap;', $ret );
}
return $ret;
}
public function add_font_display_swap_for_custom_css( $css ){
$css = str_replace( '@font-face {', '@font-face {font-display: swap;', $css );
return $css;
}
public function enable_http_response_filter(){
add_filter( 'http_response', array( $this, 'add_font_display_swap_for_http_resonse' ), 10, 3 );
}
public function add_font_display_swap_for_http_resonse( $response, $parsed_args, $url ){
if( false !== strpos( $url, 'https://fonts.googleapis.com/css') ){
if( ! empty( $response['body'] ) ){
$response['body'] = $this->add_font_display_swap_for_custom_css( $response['body'] );
}
}
return $response;
}
public function disable_http_response_filter(){
remove_filter( 'http_response', array( $this, 'add_font_display_swap_for_http_resonse' ), 10, 3 );
}
}
$run = new WPMUDEV_Fix_Font_Display_Issue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment