debugging fixes
This commit is contained in:
parent
fda7bd1d12
commit
c44c49a836
1 changed files with 75 additions and 45 deletions
|
|
@ -282,57 +282,87 @@ function mlf_get_capability() {
|
|||
* @return WP_Theme_JSON_Data Modified theme.json data.
|
||||
*/
|
||||
function mlf_add_fonts_to_theme_json($theme_json) {
|
||||
$registry = new MLF_Font_Registry();
|
||||
$fonts = $registry->get_imported_fonts_with_src();
|
||||
// Wrap in try-catch to prevent breaking Site Editor if something goes wrong
|
||||
try {
|
||||
$registry = new MLF_Font_Registry();
|
||||
$fonts = $registry->get_imported_fonts_with_src();
|
||||
|
||||
if (empty($fonts)) {
|
||||
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],
|
||||
];
|
||||
if (empty($fonts) || !is_array($fonts)) {
|
||||
return $theme_json;
|
||||
}
|
||||
|
||||
$font_families[] = [
|
||||
'name' => $font['name'],
|
||||
'slug' => $font['slug'],
|
||||
'fontFamily' => "'{$font['name']}', sans-serif",
|
||||
'fontFace' => $font_faces,
|
||||
];
|
||||
}
|
||||
// Build our font families
|
||||
$font_families = [];
|
||||
$font_dir = wp_get_font_dir();
|
||||
|
||||
// Use update_with to merge - WordPress handles the merging logic
|
||||
$new_data = [
|
||||
'version' => 2,
|
||||
'settings' => [
|
||||
'typography' => [
|
||||
'fontFamilies' => $font_families,
|
||||
if (empty($font_dir['url'])) {
|
||||
return $theme_json;
|
||||
}
|
||||
|
||||
$font_base_url = trailingslashit($font_dir['url']);
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue