debugging fixes

This commit is contained in:
rodolfomartinez 2026-02-02 11:37:40 -05:00
parent fda7bd1d12
commit c44c49a836

View file

@ -282,57 +282,87 @@ function mlf_get_capability() {
* @return WP_Theme_JSON_Data Modified theme.json data. * @return WP_Theme_JSON_Data Modified theme.json data.
*/ */
function mlf_add_fonts_to_theme_json($theme_json) { function mlf_add_fonts_to_theme_json($theme_json) {
$registry = new MLF_Font_Registry(); // Wrap in try-catch to prevent breaking Site Editor if something goes wrong
$fonts = $registry->get_imported_fonts_with_src(); try {
$registry = new MLF_Font_Registry();
$fonts = $registry->get_imported_fonts_with_src();
if (empty($fonts)) { if (empty($fonts) || !is_array($fonts)) {
return $theme_json; return $theme_json;
}
// Build our font families
$font_families = [];
$font_dir = wp_get_font_dir();
$font_base_url = trailingslashit($font_dir['url']);
foreach ($fonts as $font) {
$font_faces = [];
foreach ($font['variants'] as $variant) {
$weight = $variant['weight'];
$style = $variant['style'];
$filename = $variant['filename'];
// Use direct file URL
$font_url = $font_base_url . $filename;
$font_faces[] = [
'fontFamily' => $font['name'],
'fontWeight' => $weight,
'fontStyle' => $style,
'fontDisplay' => 'swap',
'src' => [$font_url],
];
} }
$font_families[] = [ // Build our font families
'name' => $font['name'], $font_families = [];
'slug' => $font['slug'], $font_dir = wp_get_font_dir();
'fontFamily' => "'{$font['name']}', sans-serif",
'fontFace' => $font_faces,
];
}
// Use update_with to merge - WordPress handles the merging logic if (empty($font_dir['url'])) {
$new_data = [ return $theme_json;
'version' => 2, }
'settings' => [
'typography' => [ $font_base_url = trailingslashit($font_dir['url']);
'fontFamilies' => $font_families,
foreach ($fonts as $font) {
// Validate required font properties
if (empty($font['name']) || empty($font['slug']) || empty($font['variants'])) {
continue;
}
$font_faces = [];
foreach ($font['variants'] as $variant) {
// Validate variant data
if (empty($variant['filename'])) {
continue;
}
$weight = !empty($variant['weight']) ? $variant['weight'] : '400';
$style = !empty($variant['style']) ? $variant['style'] : 'normal';
$filename = $variant['filename'];
// Use direct file URL
$font_url = $font_base_url . $filename;
$font_faces[] = [
'fontFamily' => $font['name'],
'fontWeight' => $weight,
'fontStyle' => $style,
'fontDisplay' => 'swap',
'src' => [$font_url],
];
}
// Only add font if it has valid faces
if (!empty($font_faces)) {
$font_families[] = [
'name' => $font['name'],
'slug' => $font['slug'],
'fontFamily' => "'{$font['name']}', sans-serif",
'fontFace' => $font_faces,
];
}
}
// Only update if we have valid fonts
if (empty($font_families)) {
return $theme_json;
}
// Use update_with to merge - WordPress handles the merging logic
$new_data = [
'version' => 2,
'settings' => [
'typography' => [
'fontFamilies' => $font_families,
],
], ],
], ];
];
return $theme_json->update_with($new_data); return $theme_json->update_with($new_data);
} catch (Exception $e) {
// Log error but don't break the Site Editor
error_log('MLF theme.json filter error: ' . $e->getMessage());
return $theme_json;
}
} }
add_filter('wp_theme_json_data_user', 'mlf_add_fonts_to_theme_json', 10); add_filter('wp_theme_json_data_user', 'mlf_add_fonts_to_theme_json', 10);