font handling fix

This commit is contained in:
rodolfomartinez 2026-02-02 11:04:00 -05:00
parent d5ecb31dad
commit fda7bd1d12
6 changed files with 727 additions and 22 deletions

View file

@ -144,6 +144,10 @@ class MLF_Font_Registry {
return new WP_Error('not_ours', 'Cannot delete fonts not imported by this plugin');
}
// Get font info before deletion for cleanup
$settings = json_decode($family->post_content, true);
$font_slug = $settings['slug'] ?? $family->post_name;
// Get font faces (children)
$faces = get_children([
'post_parent' => $family_id,
@ -154,11 +158,11 @@ class MLF_Font_Registry {
// Delete font face files and posts
foreach ($faces as $face) {
$settings = json_decode($face->post_content, true);
$face_settings = json_decode($face->post_content, true);
if (isset($settings['src'])) {
if (isset($face_settings['src'])) {
// Convert file:. URL to path
$src = $settings['src'];
$src = $face_settings['src'];
$src = str_replace('file:./', '', $src);
$file_path = trailingslashit($font_dir['path']) . basename($src);
@ -176,12 +180,83 @@ class MLF_Font_Registry {
// Delete family post
wp_delete_post($family_id, true);
// Clean up global styles references to this font
$this->remove_font_from_global_styles($font_slug);
// Clear all font-related caches
$this->clear_font_caches();
return true;
}
/**
* Remove references to a deleted font from global styles.
*
* This prevents block errors when a font is deleted but still referenced.
*
* @param string $font_slug The font slug to remove.
*/
private function remove_font_from_global_styles($font_slug) {
// Get the global styles post for the current theme
$global_styles = get_posts([
'post_type' => 'wp_global_styles',
'posts_per_page' => 1,
'post_status' => 'publish',
'tax_query' => [
[
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => get_stylesheet(),
],
],
]);
if (empty($global_styles)) {
return;
}
$global_styles_post = $global_styles[0];
$content = json_decode($global_styles_post->post_content, true);
if (empty($content)) {
return;
}
$modified = false;
// Helper function to recursively remove font references
$remove_font_refs = function (&$data) use ($font_slug, &$modified, &$remove_font_refs) {
if (!is_array($data)) {
return;
}
foreach ($data as $key => &$value) {
// Check for fontFamily references using the slug pattern
if ($key === 'fontFamily' && is_string($value)) {
// WordPress uses format like: var(--wp--preset--font-family--font-slug)
if (strpos($value, '--font-family--' . $font_slug) !== false) {
unset($data[$key]);
$modified = true;
}
}
// Check for typography.fontFamily in element/block styles
if (is_array($value)) {
$remove_font_refs($value);
}
}
};
$remove_font_refs($content);
if ($modified) {
wp_update_post([
'ID' => $global_styles_post->ID,
'post_content' => wp_json_encode($content),
]);
}
}
/**
* Clear all font-related caches.
*/
@ -320,6 +395,82 @@ class MLF_Font_Registry {
return $result;
}
/**
* Get all fonts imported by this plugin, including actual filenames.
*
* This method includes the actual filename stored in the database,
* which is needed to correctly reference variable vs static font files.
*
* @return array Array of font data with filenames.
*/
public function get_imported_fonts_with_src() {
$fonts = get_posts([
'post_type' => 'wp_font_family',
'posts_per_page' => 100,
'post_status' => 'publish',
'meta_key' => '_mlf_imported',
'meta_value' => '1',
]);
if (empty($fonts)) {
return [];
}
// Collect all font IDs for batch query
$font_ids = wp_list_pluck($fonts, 'ID');
// Single query to get ALL font faces for ALL fonts
$all_faces = get_posts([
'post_type' => 'wp_font_face',
'posts_per_page' => 1000,
'post_status' => 'publish',
'post_parent__in' => $font_ids,
]);
// Group faces by parent font ID
$faces_by_font = [];
foreach ($all_faces as $face) {
$parent_id = $face->post_parent;
if (!isset($faces_by_font[$parent_id])) {
$faces_by_font[$parent_id] = [];
}
$faces_by_font[$parent_id][] = $face;
}
$result = [];
foreach ($fonts as $font) {
$settings = json_decode($font->post_content, true);
// Get variants from pre-fetched data
$faces = $faces_by_font[$font->ID] ?? [];
$variants = [];
foreach ($faces as $face) {
$face_settings = json_decode($face->post_content, true);
// Extract filename from src (format: "file:./filename.woff2")
$src = $face_settings['src'] ?? '';
$filename = str_replace('file:./', '', $src);
$variants[] = [
'weight' => $face_settings['fontWeight'] ?? '400',
'style' => $face_settings['fontStyle'] ?? 'normal',
'filename' => $filename,
];
}
$result[] = [
'id' => $font->ID,
'name' => $settings['name'] ?? $font->post_title,
'slug' => $settings['slug'] ?? $font->post_name,
'variants' => $variants,
];
}
return $result;
}
/**
* Validate that a path is within the WordPress fonts directory.
*