From c44c49a836063c446658169b3888999305579593 Mon Sep 17 00:00:00 2001 From: rodolfomartinez Date: Mon, 2 Feb 2026 11:37:40 -0500 Subject: [PATCH] debugging fixes --- .../maple-fonts-wp/maple-local-fonts.php | 120 +++++++++++------- 1 file changed, 75 insertions(+), 45 deletions(-) diff --git a/native/wordpress/maple-fonts-wp/maple-local-fonts.php b/native/wordpress/maple-fonts-wp/maple-local-fonts.php index ebb087e..c56d4e8 100644 --- a/native/wordpress/maple-fonts-wp/maple-local-fonts.php +++ b/native/wordpress/maple-fonts-wp/maple-local-fonts.php @@ -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);