From 772462eeca3002a1d52508aeba485aab2b4742ad Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Tue, 03 Mar 2026 19:06:19 +0000
Subject: [PATCH] =MAJOR OVERHAUL. Likely should have made a new branch ages ago. Key changes: Registrar.php is the base for custom post types, taxonomies, and user roles. Replaces JVB_CONTENT, JVB_TAXONOMY, and JVB_USER constants, eliminates most of Features.php (except for JVB_SITE, JVB_MEMBERSHIP), and has built in sanitizing and validation via sub-classes. Also started a major overhaul of the Schema output. Created a shit ton of property traits and classes to help sanitize and ensure proper data for different schema types. Still a bunch to do, but better to be starting committing changes here on this other branch.
---
inc/managers/IconsManager.php | 88 +++++++++++++++++++++++--------------------
1 files changed, 47 insertions(+), 41 deletions(-)
diff --git a/inc/managers/IconsManager.php b/inc/managers/IconsManager.php
index d244340..3e12e60 100644
--- a/inc/managers/IconsManager.php
+++ b/inc/managers/IconsManager.php
@@ -1,6 +1,7 @@
<?php
namespace JVBase\managers;
+use JVBase\registrar\Registrar;
use JVBase\utility\Features;
if (!defined('ABSPATH')) {
@@ -18,7 +19,7 @@
// Instance-specific properties
protected string $source;
protected array $icons = []; // Icons for THIS source [style => [names]]
- protected CacheManager $cache;
+ protected Cache $cache;
protected string $style = 'regular';
protected array $styles = ['regular', 'bold', 'duotone', 'fill', 'light', 'thin'];
protected array $customIcons = []; // Custom icons for THIS source
@@ -42,7 +43,7 @@
private function __construct(string $source)
{
$this->source = $source;
- $this->cache = CacheManager::for('icons_' . $source, WEEK_IN_SECONDS);
+ $this->cache = Cache::for('icons_' . $source, WEEK_IN_SECONDS);
$this->style = (array_key_exists('icons', JVB_SITE) && in_array(JVB_SITE['icons'], $this->styles))
? JVB_SITE['icons']
: 'regular';
@@ -198,12 +199,7 @@
// Add icons from content/taxonomy/user configs (like old behavior)
$configIcons = $this->getIconsFromConfigs();
if (!empty($configIcons)) {
- foreach ($configIcons as $source => $icons) {
- if (!isset($defaults[$source])) {
- $defaults[$source] = [];
- }
- $defaults[$source] = array_merge($defaults[$source], $icons);
- }
+ $defaults['icons'] = array_merge($defaults['icons'], $configIcons);
}
// Allow filtering per source (extensibility)
@@ -221,28 +217,21 @@
}
/**
- * Get icons from JVB_CONTENT, JVB_TAXONOMY, JVB_USER configs
+ * Get icons from Registrar instances
+ *
*/
protected function getIconsFromConfigs(): array
{
$icons = [];
- $check = [JVB_CONTENT, JVB_TAXONOMY, JVB_USER];
+ $registered = Registrar::getRegistered();
- foreach ($check as $constant) {
- foreach ($constant as $key => $value) {
- if (isset($value['icon'])) {
- // Determine source based on context (you could add 'icon_source' to configs)
- $source = $value['icon_source'] ?? 'icons';
-
- if (!isset($icons[$source])) {
- $icons[$source] = [];
- }
- $icons[$source][] = $value['icon'];
- }
- }
+ foreach ($registered as $type) {
+ $registrar = Registrar::getInstance($type);
+ $icons[] = $registrar->getIcon();
}
- return $icons;
+
+ return array_unique(array_filter($icons));
}
/**
@@ -385,7 +374,7 @@
*/
protected function registerGlobalHooks(): void
{
- add_action('init', [$this, 'checkCSS']);
+ add_action('wp_loaded', [self::class, 'checkCSS']);
}
/**
@@ -407,10 +396,13 @@
public function enqueueIconStyles():void
{
- wp_enqueue_style('jvb-icons-'.$this->source);
+// if (file_exists(JVB_CHILD_URL . "assets/css/{$this->source}.css")){
+ wp_enqueue_style('jvb-icons-'.$this->source);
+// }
+
}
- public function checkCSS(): void
+ public static function checkCSS(): void
{
$needsUpdate = get_option(BASE.'icons_needs_update', []);
if (!empty($needsUpdate)) {
@@ -420,7 +412,7 @@
}
}
- protected static function regenerateAllCSS(array $sourcesToUpdate = []): void
+ public static function regenerateAllCSS(array $sourcesToUpdate = []): void
{
error_log('[IconsManager]:regenerateCSS');
$css_dir = JVB_CHILD_DIR.'/assets/css/';
@@ -429,28 +421,41 @@
wp_mkdir_p($css_dir);
}
+ // Load all icons from database option
+ $allIcons = get_option(BASE.'usedIcons', []);
+
// If no specific sources provided, regenerate all
if (empty($sourcesToUpdate)) {
- $sourcesToUpdate = array_fill_keys(array_keys(self::$instances), true);
+ $sourcesToUpdate = array_fill_keys(array_keys($allIcons), true);
}
- // Generate CSS only for sources that need it
- foreach (self::$instances as $source => $instance) {
- if (!isset($sourcesToUpdate[$source])) {
+ // Generate CSS for each source that needs it
+ foreach ($sourcesToUpdate as $source => $needsUpdate) {
+ if (!$needsUpdate || !isset($allIcons[$source])) {
continue;
}
+ // Get or create instance for this source
+ $instance = self::for($source);
+
+ // Temporarily set icons from database
+ $originalIcons = $instance->icons;
+ $instance->icons = $allIcons[$source];
+
$css = $instance->generateIconCSS();
$css_path = $css_dir . $source . '.css';
$instance->archiveCurrentVersion($css);
if (file_put_contents($css_path, $css) !== false) {
- CacheManager::updateTimestamp('icons_' . $source);
+ Cache::touch('icons_' . $source);
error_log("[IconsManager] Updated {$source}.css");
} else {
error_log("[IconsManager] Could not write {$source}.css");
}
+
+ // Restore original icons
+ $instance->icons = $originalIcons;
}
}
@@ -481,7 +486,7 @@
$instance->archiveCurrentVersion($css);
if (file_put_contents($css_path, $css) !== false) {
- CacheManager::updateTimestamp('icons_' . $source);
+ Cache::touch('icons_' . $source);
error_log("[IconsManager] Updated {$source}.css");
} else {
error_log("[IconsManager] Could not write {$source}.css");
@@ -516,6 +521,10 @@
*/
public function get(string $name, array $options = []): string
{
+ if ($name === '') {
+ //No icon requested
+ return '';
+ }
$style = $options['style'] ?? $this->style;
$name = $this->map[$name] ?? $name;
@@ -635,14 +644,12 @@
// Clean up SVG for CSS usage
$svg = preg_replace("/([\n\t]+)/", ' ', $svg);
$svg = preg_replace('/>\s*</', '><', $svg);
- $svg = trim($svg);
-
- return $svg;
+ return trim($svg);
}
public function registerStyle(): void
{
- $timestamp = CacheManager::getTimestamp('icons_' . $this->source);
+ $timestamp = Cache::lastModified('icons_' . $this->source);
$handle = 'jvb-icons-' . $this->source;
wp_register_style(
@@ -725,8 +732,7 @@
// Clear cache for all sources
foreach (self::$instances as $source => $instance) {
- $instance->cache->delete('icon_styles_css');
- CacheManager::updateTimestamp('icons_' . $source);
+ $instance->cache->forget('icon_styles_css');
}
}
@@ -780,7 +786,7 @@
if (file_put_contents($css_path, $entry['css']) !== false) {
$this->icons = $entry['iconList'];
$this->saveIcons();
- CacheManager::updateTimestamp('icons_' . $this->source);
+ Cache::touch('icons_' . $this->source);
return true;
}
@@ -801,7 +807,7 @@
}
$needsUpdate[$this->source] = true;
update_option(BASE.'icons_needs_update', $needsUpdate);
- CacheManager::updateTimestamp('icons_' . $this->source);
+ Cache::touch('icons_' . $this->source);
}
public function mergeVersions(array $timestamps): bool
--
Gitblit v1.10.0