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

@ -151,6 +151,72 @@ function mlf_get_capability() {
return apply_filters('mlf_manage_fonts_capability', 'edit_theme_options');
}
/**
* Add imported fonts to the theme.json typography settings.
*
* This makes fonts appear in the Site Editor typography dropdown.
*
* @param WP_Theme_JSON_Data $theme_json The theme.json data.
* @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();
if (empty($fonts)) {
return $theme_json;
}
$font_families = [];
foreach ($fonts as $font) {
$font_faces = [];
foreach ($font['variants'] as $variant) {
$weight = $variant['weight'];
$style = $variant['style'];
// Build filename based on our naming convention
$font_slug = sanitize_title($font['name']);
if (strpos($weight, ' ') !== false) {
// Variable font
$filename = sprintf('%s_%s_variable.woff2', $font_slug, $style);
} else {
$filename = sprintf('%s_%s_%s.woff2', $font_slug, $style, $weight);
}
$font_dir = wp_get_font_dir();
$font_url = trailingslashit($font_dir['url']) . $filename;
$font_faces[] = [
'fontFamily' => $font['name'],
'fontWeight' => $weight,
'fontStyle' => $style,
'src' => [$font_url],
];
}
$font_families[] = [
'name' => $font['name'],
'slug' => $font['slug'],
'fontFamily' => "'{$font['name']}', sans-serif",
'fontFace' => $font_faces,
];
}
$new_data = [
'version' => 2,
'settings' => [
'typography' => [
'fontFamilies' => $font_families,
],
],
];
return $theme_json->update_with($new_data);
}
add_filter('wp_theme_json_data_user', 'mlf_add_fonts_to_theme_json');
/**
* Register admin menu.
*/
@ -201,36 +267,46 @@ function mlf_enqueue_admin_assets($hook) {
return;
}
// Use filemtime for cache-busting during development
$css_version = MLF_VERSION . '.' . filemtime(MLF_PLUGIN_DIR . 'assets/admin.css');
$js_version = MLF_VERSION . '.' . filemtime(MLF_PLUGIN_DIR . 'assets/admin.js');
wp_enqueue_style(
'mlf-admin',
MLF_PLUGIN_URL . 'assets/admin.css',
[],
MLF_VERSION
$css_version
);
wp_enqueue_script(
'mlf-admin',
MLF_PLUGIN_URL . 'assets/admin.js',
['jquery'],
MLF_VERSION,
$js_version,
true
);
wp_localize_script('mlf-admin', 'mapleLocalFontsData', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'downloadNonce' => wp_create_nonce('mlf_download_font'),
'deleteNonce' => wp_create_nonce('mlf_delete_font'),
'searchNonce' => wp_create_nonce('mlf_search_fonts'),
'strings' => [
'ajaxUrl' => admin_url('admin-ajax.php'),
'downloadNonce' => wp_create_nonce('mlf_download_font'),
'deleteNonce' => wp_create_nonce('mlf_delete_font'),
'searchNonce' => wp_create_nonce('mlf_search_fonts'),
'checkUpdatesNonce' => wp_create_nonce('mlf_check_updates'),
'updateFontNonce' => wp_create_nonce('mlf_update_font'),
'strings' => [
'downloading' => __('Downloading...', 'maple-local-fonts'),
'deleting' => __('Deleting...', 'maple-local-fonts'),
'updating' => __('Updating...', 'maple-local-fonts'),
'checking' => __('Checking...', 'maple-local-fonts'),
'confirmDelete' => __('Are you sure you want to delete this font?', 'maple-local-fonts'),
'error' => __('An error occurred. Please try again.', 'maple-local-fonts'),
'searchPlaceholder' => __('Search Google Fonts...', 'maple-local-fonts'),
'searching' => __('Searching...', 'maple-local-fonts'),
'noResults' => __('No fonts found. Try a different search term.', 'maple-local-fonts'),
'selectFont' => __('Select a font from the search results above.', 'maple-local-fonts'),
'selectFont' => __('Please select a font first.', 'maple-local-fonts'),
'previewText' => __('Maple Fonts Preview', 'maple-local-fonts'),
'minChars' => __('Please enter at least 2 characters.', 'maple-local-fonts'),
'noUpdates' => __('All fonts are up to date.', 'maple-local-fonts'),
'updatesFound' => __('Updates available for %d font(s).', 'maple-local-fonts'),
],
]);
}