class-google-fonts.php

Google Fonts Local Hosting class.

Source includes/Assets/class-google-fonts.php5 min readPart of Performance Optimisation

includes/Assets/class-google-fonts.php

Google Fonts Local Hosting class.

Namespace: PerformanceOptimise\\Inc · Lines: 1033

Class Google_Fonts

Class Google_Fonts

Source: includes/Assets/class-google-fonts.php, line 26

class Google_Fonts

Tags: @since 2.0.0

Constants

ConstantVisibilityValueLine
FONTS_CACHE_DIRprivate\'/cache/wppo/fonts\'34
CHROME_UAprivate\'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\'42
AS_HOOKpublic\'wppo_google_fonts_download\'393

Properties

PropertyVisibilityTypeDefaultLine
$optionsprivatearray—50
$font_cache_dirprivatestring—58
$font_cache_urlprivatestring—66

public __construct()

public function __construct(array $options)

Constructor.

ParameterTypeDefaultDescription
$optionsarray—Plugin settings array.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 74

publicstatic backoff_ttl()

public static function backoff_ttl(): int

Failure-backoff TTL (seconds) for the Google Fonts fetch sentinels.

Return: int.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 92

publicstatic get_font_display()

public static function get_font_display(): string

Resolve the configured font-display value for self-hosted CSS.

Return: string — font-display value, or \’\’ when injection is disabled.

Tags: @since 2.2.0

Source: includes/Assets/class-google-fonts.php, line 113

publicstatic order_font_sources()

public static function order_font_sources(string $css): string

Reorder @font-face src lists so WOFF2 sources come first.

ParameterTypeDefaultDescription
$cssstring—Stylesheet CSS.

Return: string — CSS with woff2-first src ordering.

Tags: @since 2.2.0

Source: includes/Assets/class-google-fonts.php, line 152

public maybe_subset_css()

public function maybe_subset_css(string $css): string

Optionally keep only requested unicode-range subsets.

ParameterTypeDefaultDescription
$cssstring—Stylesheet CSS.

Return: string — Possibly subset-filtered CSS.

Tags: @since 2.2.0

Source: includes/Assets/class-google-fonts.php, line 204

public process_style_tag()

public function process_style_tag($tag, $handle, $href): string

Process a stylesheet <link> tag to replace Google Fonts URL with local cache.

ParameterTypeDefaultDescription
$tagmixed—The link tag HTML.
$handlemixed—The stylesheet handle.
$hrefmixed—The stylesheet URL (guarded with is_string: style_loader_tag can pass non-strings).

Return: string — Modified link tag with local URL or original tag.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 262

public process_buffer()

public function process_buffer(string $buffer): string

Process HTML buffer to intercept @import and inline <link> Google Fonts references.

ParameterTypeDefaultDescription
$bufferstring—The HTML buffer.

Return: string — The modified HTML buffer.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 315

private is_google_fonts_url()

private function is_google_fonts_url(string $url): bool

Whether a URL points at the Google Fonts CSS host (exact host match).

ParameterTypeDefaultDescription
$urlstring—Candidate URL.

Return: bool — True when the host is exactly fonts.googleapis.com.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 381

public download_and_rewrite()

public function download_and_rewrite($url): string

Download Google Fonts CSS, fetch font files, rewrite URLs, and cache locally.

ParameterTypeDefaultDescription
$urlstring—The Google Fonts CSS URL.

Return: string — Local CSS URL on success, empty string on failure/cache-miss.

Tags: @since 2.0.0 Failure sentinel transient (wppo_gf_fail_*). Out-of-band download via Action Scheduler.

Source: includes/Assets/class-google-fonts.php, line 410

private maybe_queue_download()

private function maybe_queue_download(string $key, string $url): void

Queue an out-of-band Google Fonts download job (deduped by CSS key).

ParameterTypeDefaultDescription
$keystring—CSS md5 key.
$urlstring—Normalized Google Fonts CSS URL.

Return: void.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 448

public process_queued_download()

public function process_queued_download(string $key, string $url): bool

Action Scheduler / cron callback: fetch CSS + font files out-of-band.

ParameterTypeDefaultDescription
$keystring—CSS md5 key.
$urlstring—Normalized Google Fonts CSS URL.

Return: bool — True when the local CSS file now exists.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 498

publicstatic process_queued_download_static()

public static function process_queued_download_static(=array() $args, string=\'\' $url): void

Static entry point for the Action Scheduler / WP-Cron hook.

ParameterTypeDefaultDescription
$args=array()—Args array with key/url, or the CSS key.
$urlstring=\'\'—Normalized Google Fonts CSS URL (when $args is a key).

Return: void.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 714

publicstatic handle_queued_download_action()

public static function handle_queued_download_action(=array() $args): void

Action Scheduler callback wrapper (single-arg hook signature).

ParameterTypeDefaultDescription
$args=array()—Job args with key/url.

Return: void.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 741

private normalize_google_fonts_url()

private function normalize_google_fonts_url($url)

Normalize a Google Fonts URL.

ParameterTypeDefaultDescription
$urlstring—The raw URL.

Return: string — Normalized URL or empty string if not a Google Fonts URL.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 760

private download_font_file()

private function download_font_file($url, $dest)

Download a single font file from Google\’s CDN.

ParameterTypeDefaultDescription
$urlstring—The font file URL.
$deststring—Local destination path.

Return: bool — True on success, false on failure.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 798

public generate_metric_fallback()

public function generate_metric_fallback(string $family): string

Generate metric-matched fallback CSS for a font family (size-adjust etc).

ParameterTypeDefaultDescription
$familystring—Font family name.

Return: string — Fallback @font-face CSS or empty string.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 889

public inject_metric_fallback()

public function inject_metric_fallback(string $buffer): string

Inject metric-matched fallback style into buffer when enabled.

ParameterTypeDefaultDescription
$bufferstring—HTML buffer.

Return: string — Modified buffer.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 977

publicstatic clear_font_cache()

public static function clear_font_cache()

Clear the entire Google Fonts cache directory.

Return: void.

Tags: @since 2.0.0

Source: includes/Assets/class-google-fonts.php, line 1018

Hooks

Hooks referenced in includes/Assets/class-google-fonts.php:

HookTypeLineNotes
wppo_google_fonts_backofffilter99@param ×1
wppo_font_displayfilter117—
wppo_font_metric_fallback_cssfilter967@param ×2