File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/lib/webfont-loader.php
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Gtm_Webfont_Loader {
private static $instance;
protected $fonts = [];
/*
GDPR flag for fetching font files from server side
*/
public $gdpr = false;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
protected function __construct() {
// Use priority=9 to support late wp_enqueue_style
add_action( 'wp_footer', [$this, 'get_style'], 9 );
// Allow override GDPR flag
$gdpr_option = (bool) get_option( 'gtm_typography_enable_local_fonts', false );
$this->gdpr = DEFINED( 'GTM_ALLOW_WEBFONT_GDPR' ) ? GTM_ALLOW_WEBFONT_GDPR : $gdpr_option;
// Add preconnect if can load fonts from google site directly
if ( ! $this->gdpr ) {
add_filter( 'wp_resource_hints', [$this, 'resource_hints'], 10, 2 );
}
}
public function resource_hints( $urls, $relation_type ) {
if ( 'preconnect' === $relation_type ) {
// Hint for google font css
$urls[] = [
'href' => 'https://fonts.googleapis.com',
'crossorigin',
];
// Hint for actual font files
$urls[] = [
'href' => 'https://fonts.gstatic.com',
'crossorigin',
];
}
return $urls;
}
public function add( $family, $weights = 400 ) {
if ( ! is_array( $weights ) ) {
$weights = [$weights];
}
$this->fonts[$family] = array_unique( array_merge( $this->fonts[$family] ?? [], $weights ) );
}
protected function the_css() {
foreach ( $this->fonts as $family => $weights ) {
// Gfont requires sorted value
natsort( $weights );
$url = gtm_generate_web_font_url( $family, $weights );
if ( $this->gdpr ) {
// Download and cached on server side
$downloader = new Gtm_Webfont_Downloader();
$contents = $downloader->get_styles( $url );
if ( $contents ) {
echo wp_strip_all_tags( $contents ); // phpcs:ignore WordPress.Security.EscapeOutput
}
} else {
// Add direct link to google font's website
$style_slug = gtm_generate_web_font_handle( $family, $weights );
wp_enqueue_style( $style_slug, $url );
}
}
}
public function get_style() {
echo '<style>';
$this->the_css();
echo '</style>';
}
}