font management fixes

This commit is contained in:
rodolfomartinez 2026-02-02 08:31:36 -05:00
parent 847ed92c23
commit d5ecb31dad
8 changed files with 728 additions and 99 deletions

View file

@ -25,11 +25,14 @@ class MLF_Font_Downloader {
private $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
/**
* All available font weights.
* Common font weights to request.
*
* Most fonts support at least 300-700. We also include 800-900 for fonts
* that support Black/Heavy weights.
*
* @var array
*/
private $all_weights = [100, 200, 300, 400, 500, 600, 700, 800, 900];
private $all_weights = [300, 400, 500, 600, 700, 800, 900];
/**
* Download a font from Google Fonts.
@ -151,6 +154,8 @@ class MLF_Font_Downloader {
/**
* Download static fonts (fallback when variable not available).
*
* Tries different weight combinations since fonts support varying weights.
*
* @param string $font_name Font family name.
* @param bool $include_italic Whether to include italic styles.
* @return array|WP_Error Download result or error.
@ -158,8 +163,28 @@ class MLF_Font_Downloader {
private function download_static_fonts($font_name, $include_italic) {
$styles = $include_italic ? ['normal', 'italic'] : ['normal'];
// Fetch CSS from Google
$css = $this->fetch_static_css($font_name, $this->all_weights, $styles);
// Try different weight combinations - fonts support varying weights
$weight_sets = [
[300, 400, 500, 600, 700, 800, 900], // Full range
[300, 400, 500, 600, 700, 800], // Without 900
[300, 400, 500, 600, 700], // Common range
[400, 500, 600, 700], // Without light
[400, 700], // Just regular and bold
];
$css = null;
foreach ($weight_sets as $weights) {
$css = $this->fetch_static_css($font_name, $weights, $styles);
if (!is_wp_error($css)) {
break;
}
// If error is not "font not found", stop trying
if ($css->get_error_code() !== 'font_not_found') {
return $css;
}
}
if (is_wp_error($css)) {
return $css;
@ -191,6 +216,9 @@ class MLF_Font_Downloader {
/**
* Fetch variable font CSS from Google Fonts API.
*
* Tries progressively smaller weight ranges until one works,
* since different fonts support different weight ranges.
*
* @param string $font_name Font family name.
* @param bool $italic Whether to fetch italic variant.
* @return string|WP_Error CSS content or error.
@ -198,15 +226,26 @@ class MLF_Font_Downloader {
private function fetch_variable_css($font_name, $italic = false) {
$family = str_replace(' ', '+', $font_name);
if ($italic) {
// Request italic variable font
$url = "https://fonts.googleapis.com/css2?family={$family}:ital,wght@1,100..900&display=swap";
} else {
// Request roman variable font
$url = "https://fonts.googleapis.com/css2?family={$family}:wght@100..900&display=swap";
// Try different weight ranges - fonts support varying ranges
$weight_ranges = ['300..900', '300..800', '300..700', '400..700'];
foreach ($weight_ranges as $range) {
if ($italic) {
$url = "https://fonts.googleapis.com/css2?family={$family}:ital,wght@1,{$range}&display=swap";
} else {
$url = "https://fonts.googleapis.com/css2?family={$family}:wght@{$range}&display=swap";
}
$result = $this->fetch_css($url);
// If successful or error is not "font not found", return
if (!is_wp_error($result) || $result->get_error_code() !== 'font_not_found') {
return $result;
}
}
return $this->fetch_css($url);
// All ranges failed
return new WP_Error('no_variable', 'Variable font not available');
}
/**