| | |
| | | // 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 |
| | |
| | | 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'; |
| | |
| | | */ |
| | | protected function registerGlobalHooks(): void |
| | | { |
| | | add_action('init', [$this, 'checkCSS']); |
| | | add_action('wp_loaded', [self::class, 'checkCSS']); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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)) { |
| | |
| | | } |
| | | } |
| | | |
| | | 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/'; |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | |
| | | $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"); |
| | |
| | | */ |
| | | public function get(string $name, array $options = []): string |
| | | { |
| | | if ($name === '') { |
| | | //No icon requested |
| | | return ''; |
| | | } |
| | | $style = $options['style'] ?? $this->style; |
| | | $name = $this->map[$name] ?? $name; |
| | | |
| | |
| | | |
| | | public function registerStyle(): void |
| | | { |
| | | $timestamp = CacheManager::getTimestamp('icons_' . $this->source); |
| | | $timestamp = Cache::lastModified('icons_' . $this->source); |
| | | $handle = 'jvb-icons-' . $this->source; |
| | | |
| | | wp_register_style( |
| | |
| | | |
| | | // 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'); |
| | | } |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | } |
| | | $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 |