File "StylesheetGenerator.php"
Full Path: /home/jovision/public_html/wp/wp-content/plugins/host-webfonts-local/src/StylesheetGenerator.php
File size: 4.57 KB
MIME-type: text/x-php
Charset: utf-8
<?php
/* * * * * * * * * * * * * * * * * * * * *
*
* ██████╗ ███╗ ███╗ ██████╗ ███████╗
* ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
* ██║ ██║██╔████╔██║██║ ███╗█████╗
* ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
* ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
* ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
*
* @package : OMGF
* @author : Daan van den Bergh
* @copyright: © 2026 Daan van den Bergh
* @url : https://daan.dev
* * * * * * * * * * * * * * * * * * * */
namespace OMGF;
use OMGF\Helper as OMGF;
use OMGF\Admin\Settings;
class StylesheetGenerator {
/** @var int $timestamp */
private static $timestamp;
/** @var $fonts */
private $fonts;
/** @var string $plugin */
private $plugin;
/**
* OMGF_GenerateStylesheet constructor.
*/
public function __construct(
$fonts,
string $plugin
) {
$this->fonts = $fonts;
$this->plugin = $plugin;
self::set_timestamp();
}
/**
* Ensure the static timestamp is initialized for static contexts.
*
* This mirrors the constructor logic so static calls behave identically.
*
* @return void
*/
private static function set_timestamp() {
if ( self::$timestamp === null ) {
self::$timestamp = OMGF::get_option( Settings::OMGF_DB_CACHE_TIMESTAMP );
}
}
/**
* Generate a stylesheet based on the provided $fonts.
*
* @return string
*/
public function generate() {
$stylesheet = "/**\n * Auto Generated by $this->plugin\n * @author: Daan van den Bergh\n * @url: https://daan.dev\n */\n\n";
foreach ( $this->fonts as $font ) {
if ( empty( $font->variants ) ) {
continue; // @codeCoverageIgnore
}
foreach ( $font->variants as $variant ) {
/**
* Filter $variant to allow custom modifications of e.g., unicode-range, etc.
*
* @filter omgf_generate_stylesheet_font_variant
* @since v5.6.0
*/
$variant = apply_filters( 'omgf_generate_stylesheet_font_variant', $variant );
/**
* Filter font_family name.
*
* @since v4.5.1
*/
$font_family = apply_filters(
'omgf_generate_stylesheet_font_family',
rawurldecode( $variant->fontFamily )
);
$properties = [
'font-family' => "'$font_family'",
'font-style' => $variant->fontStyle,
'font-weight' => $variant->fontWeight,
'unicode-range' => $variant->range ?? '',
];
if ( ! empty( $variant->woff2 ) ) {
$properties['src'] = self::build_source_string( [ 'woff2' => $variant->woff2 ] );
}
if ( empty( $properties['src'] ) ) {
continue;
}
$stylesheet .= self::generate_font_face( $properties );
}
}
return apply_filters( 'omgf_generate_stylesheet_after', $stylesheet, $this->fonts );
}
/**
* @param array $sources e.g. { 'woff2' => '/path/to/font.woff2' } or when $type is 'local' { 0 => 'FontFamilyName' }
* @param string $type url | local
* @param bool $end_semi_colon
*
* @return string
*/
public static function build_source_string( $sources, $type = 'url', $end_semi_colon = true ) {
self::set_timestamp();
$source = '';
$n = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? "\n" : '';
$last_src = array_key_last( $sources );
foreach ( $sources as $format => $url ) {
$url = $type === 'url' ? $url . '?ver=' . self::$timestamp : $url;
$source .= "$type('$url')";
if ( $type !== 'local' ) {
$source .= ( ! is_numeric( $format ) ? " format('$format')" : '' );
}
if ( $format !== $last_src ) {
$source .= ",$n";
}
}
return $source . ( $end_semi_colon ? ';' : '' );
}
/**
* Generate a single @font-face CSS declaration.
*
* @since v5.10.0
*
* @param array $properties Key-value pairs of CSS properties (e.g. 'font-family' => 'Roboto')
*
* @return string
*/
public static function generate_font_face( $properties ) {
$n = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? "\n" : '';
$css = "@font-face{{$n}";
if ( ! isset( $properties['font-display'] ) ) {
$properties['font-display'] = OMGF::get_option( Settings::OMGF_OPTIMIZE_SETTING_DISPLAY_OPTION );
}
foreach ( $properties as $name => $value ) {
if ( empty( $value ) ) {
continue;
}
$value = rtrim( $value, '; ' );
$css .= "$name: $value;$n";
}
$css .= "}$n";
return apply_filters( 'omgf_stylesheet_generator_font_face', $css, $properties );
}
}