includes/Assets/class-google-fonts.php
Google Fonts Local Hosting class.
Class Google_Fonts
Class Google_Fonts
class Google_FontsConstants
| Constant | Visibility | Value | Line |
|---|---|---|---|
FONTS_CACHE_DIR | private | \'/cache/wppo/fonts\' | 34 |
CHROME_UA | private | \'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_HOOK | public | \'wppo_google_fonts_download\' | 393 |
Properties
| Property | Visibility | Type | Default | Line |
|---|---|---|---|---|
$options | private | array | — | 50 |
$font_cache_dir | private | string | — | 58 |
$font_cache_url | private | string | — | 66 |
public __construct()
public function __construct(array $options)Constructor. Parameter Type Default Description $optionsarray— Plugin settings array.
publicstatic backoff_ttl()
public static function backoff_ttl(): intFailure-backoff TTL (seconds) for the Google Fonts fetch sentinels.
Return: int.
publicstatic get_font_display()
public static function get_font_display(): stringResolve the configured font-display value for self-hosted CSS.
Return: string — font-display value, or \’\’ when injection is disabled.
publicstatic order_font_sources()
public static function order_font_sources(string $css): stringReorder @font-face src lists so WOFF2 sources come first. Parameter Type Default Description $cssstring— Stylesheet CSS.
Return: string — CSS with woff2-first src ordering.
public maybe_subset_css()
public function maybe_subset_css(string $css): stringOptionally keep only requested unicode-range subsets. Parameter Type Default Description $cssstring— Stylesheet CSS.
Return: string — Possibly subset-filtered CSS.
public process_style_tag()
public function process_style_tag($tag, $handle, $href): stringProcess a stylesheet <link> tag to replace Google Fonts URL with local cache. Parameter Type Default Description $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.
public process_buffer()
public function process_buffer(string $buffer): stringProcess HTML buffer to intercept @import and inline <link> Google Fonts references. Parameter Type Default Description $bufferstring— The HTML buffer.
Return: string — The modified HTML buffer.
private is_google_fonts_url()
private function is_google_fonts_url(string $url): boolWhether a URL points at the Google Fonts CSS host (exact host match). Parameter Type Default Description $urlstring— Candidate URL.
Return: bool — True when the host is exactly fonts.googleapis.com.
public download_and_rewrite()
public function download_and_rewrite($url): stringDownload Google Fonts CSS, fetch font files, rewrite URLs, and cache locally. Parameter Type Default Description $urlstring— The Google Fonts CSS URL.
Return: string — Local CSS URL on success, empty string on failure/cache-miss.
private maybe_queue_download()
private function maybe_queue_download(string $key, string $url): voidQueue an out-of-band Google Fonts download job (deduped by CSS key). Parameter Type Default Description $keystring— CSS md5 key. $urlstring— Normalized Google Fonts CSS URL.
Return: void.
public process_queued_download()
public function process_queued_download(string $key, string $url): boolAction Scheduler / cron callback: fetch CSS + font files out-of-band. Parameter Type Default Description $keystring— CSS md5 key. $urlstring— Normalized Google Fonts CSS URL.
Return: bool — True when the local CSS file now exists.
publicstatic process_queued_download_static()
public static function process_queued_download_static(=array() $args, string=\'\' $url): voidStatic entry point for the Action Scheduler / WP-Cron hook. Parameter Type Default Description $args=array()— Args array with key/url, or the CSS key. $urlstring=\'\'— Normalized Google Fonts CSS URL (when $args is a key).
Return: void.
publicstatic handle_queued_download_action()
public static function handle_queued_download_action(=array() $args): voidAction Scheduler callback wrapper (single-arg hook signature). Parameter Type Default Description $args=array()— Job args with key/url.
Return: void.
private normalize_google_fonts_url()
private function normalize_google_fonts_url($url)Normalize a Google Fonts URL. Parameter Type Default Description $urlstring— The raw URL.
Return: string — Normalized URL or empty string if not a Google Fonts URL.
private download_font_file()
private function download_font_file($url, $dest)Download a single font file from Google\’s CDN. Parameter Type Default Description $urlstring— The font file URL. $deststring— Local destination path.
Return: bool — True on success, false on failure.
public generate_metric_fallback()
public function generate_metric_fallback(string $family): stringGenerate metric-matched fallback CSS for a font family (size-adjust etc). Parameter Type Default Description $familystring— Font family name.
Return: string — Fallback @font-face CSS or empty string.
public inject_metric_fallback()
public function inject_metric_fallback(string $buffer): stringInject metric-matched fallback style into buffer when enabled. Parameter Type Default Description $bufferstring— HTML buffer.
Return: string — Modified buffer.
publicstatic clear_font_cache()
public static function clear_font_cache()Clear the entire Google Fonts cache directory.
Return: void.
Hooks
Hooks referenced in includes/Assets/class-google-fonts.php: Hook Type Line Notes wppo_google_fonts_backofffilter 99 @param ×1 wppo_font_displayfilter 117 — wppo_font_metric_fallback_cssfilter 967 @param ×2