11<?php
22/**
3- * CSS File Generator: Generates and caches CSS files for Stackable's global design system .
3+ * CSS File Generator: Base class for generating and caching CSS files .
44 *
55 * @since 3.19.0
66 */
1010 exit ;
1111}
1212
13- if ( ! class_exists ( 'Stackable_CSS_File_Generator ' ) ) {
13+ if ( ! class_exists ( 'Stackable_Base_CSS_File_Generator ' ) ) {
1414
1515 /**
16- * Stackable CSS File Generator.
16+ * Base CSS File Generator - Abstract class for generating and caching CSS files .
1717 */
18- class Stackable_CSS_File_Generator {
18+ abstract class Stackable_Base_CSS_File_Generator {
1919
2020 /**
21- * CSS file handle for the global design system .
21+ * CSS file handle.
2222 */
23- const CSS_HANDLE = ' ugb-global-design-system ' ;
23+ abstract protected static function get_css_handle () ;
2424
2525 /**
2626 * CSS file name prefix.
2727 */
28- const CSS_FILE_PREFIX = ' stackable-global- ' ;
28+ abstract protected static function get_css_file_prefix () ;
2929
3030 /**
31- * Initialize
31+ * Option name for caching the CSS file name.
3232 */
33- function __construct () {
34- // Add hooks to regenerate CSS when global settings change
35- add_action ( 'update_option_stackable_global_colors ' , array ( $ this , 'invalidate_current_css_file ' ) );
36- add_action ( 'update_option_stackable_global_typography ' , array ( $ this , 'invalidate_current_css_file ' ) );
37- add_action ( 'update_option_stackable_global_spacing_and_borders ' , array ( $ this , 'invalidate_current_css_file ' ) );
38- add_action ( 'update_option_stackable_global_color_schemes ' , array ( $ this , 'invalidate_current_css_file ' ) );
39- add_action ( 'update_option_stackable_global_block_styles ' , array ( $ this , 'invalidate_current_css_file ' ) );
40- add_action ( 'update_option_stackable_global_buttons_and_icons ' , array ( $ this , 'invalidate_current_css_file ' ) );
41- add_action ( 'update_option_stackable_global_preset_controls ' , array ( $ this , 'invalidate_current_css_file ' ) );
42- }
33+ abstract protected static function get_cache_option_name ();
4334
4435 /**
4536 * Get the CSS file path in the uploads directory.
@@ -67,33 +58,25 @@ public static function get_css_file_url() {
6758 * @return string
6859 */
6960 public static function get_css_file_name () {
70- $ css_content = self ::generate_css_content ();
61+ $ css_content = static ::generate_css_content ();
7162 $ content_hash = md5 ( $ css_content );
72- return self :: CSS_FILE_PREFIX . $ content_hash . '.css ' ;
63+ return static :: get_css_file_prefix () . $ content_hash . '.css ' ;
7364 }
7465
7566 /**
76- * Generate the CSS content from all global design system filters .
67+ * Generate the CSS content. Must be implemented by child classes .
7768 *
7869 * @return string
7970 */
80- public static function generate_css_content () {
81- // Apply all the filters that contribute to the global design system
82- $ css_content = apply_filters ( 'stackable_inline_styles_nodep ' , '' );
83-
84- // Add any additional global styles that might be needed
85- $ css_content = apply_filters ( 'stackable_global_css_file_content ' , $ css_content );
86-
87- return trim ( $ css_content );
88- }
71+ abstract protected static function generate_css_content ();
8972
9073 /**
9174 * Ensure the CSS directory exists.
9275 *
9376 * @return bool
9477 */
9578 public static function ensure_css_directory () {
96- $ css_dir = self ::get_css_file_path ();
79+ $ css_dir = static ::get_css_file_path ();
9780
9881 if ( ! file_exists ( $ css_dir ) ) {
9982 return wp_mkdir_p ( $ css_dir );
@@ -108,26 +91,26 @@ public static function ensure_css_directory() {
10891 * @return bool
10992 */
11093 public static function generate_css_file () {
111- $ css_content = self ::generate_css_content ();
94+ $ css_content = static ::generate_css_content ();
11295
11396 if ( empty ( $ css_content ) ) {
11497 return false ;
11598 }
11699
117100 // Ensure directory exists
118- if ( ! self ::ensure_css_directory () ) {
101+ if ( ! static ::ensure_css_directory () ) {
119102 return false ;
120103 }
121104
122- $ file_name = self ::get_css_file_name ();
123- $ file_path = self ::get_css_file_path () . $ file_name ;
105+ $ file_name = static ::get_css_file_name ();
106+ $ file_path = static ::get_css_file_path () . $ file_name ;
124107
125108 // Write the CSS file
126109 $ result = file_put_contents ( $ file_path , $ css_content );
127110
128111 if ( $ result !== false ) {
129112 // Update the cached file name in options
130- update_option ( ' stackable_global_css_file_name ' , $ file_name );
113+ update_option ( static :: get_cache_option_name () , $ file_name );
131114 return true ;
132115 }
133116
@@ -140,7 +123,7 @@ public static function generate_css_file() {
140123 * @return string|false
141124 */
142125 public static function get_cached_css_file_name () {
143- return get_option ( ' stackable_global_css_file_name ' );
126+ return get_option ( static :: get_cache_option_name () );
144127 }
145128
146129 /**
@@ -149,54 +132,186 @@ public static function get_cached_css_file_name() {
149132 * @return bool
150133 */
151134 public static function css_file_exists () {
152- $ file_name = self ::get_cached_css_file_name ();
135+ $ file_name = static ::get_cached_css_file_name ();
153136
154137 if ( ! $ file_name ) {
155138 return false ;
156139 }
157140
158- $ file_path = self ::get_css_file_path () . $ file_name ;
141+ $ file_path = static ::get_css_file_path () . $ file_name ;
159142 return file_exists ( $ file_path );
160143 }
161144
162145 /**
163- * Enqueue the global CSS file.
146+ * Enqueue the CSS file.
147+ *
148+ * @return bool
164149 */
165- public static function enqueue_global_css_file () {
150+ public static function enqueue_css_file () {
166151 // Check if we have a valid CSS file
167- if ( ! self ::css_file_exists () ) {
152+ if ( ! static ::css_file_exists () ) {
168153 // Generate the CSS file if it doesn't exist
169- self ::generate_css_file ();
154+ static ::generate_css_file ();
170155 }
171156
172- $ file_name = self ::get_cached_css_file_name ();
157+ $ file_name = static ::get_cached_css_file_name ();
173158
174159 if ( $ file_name ) {
175- $ file_url = self ::get_css_file_url () . $ file_name ;
160+ $ file_url = static ::get_css_file_url () . $ file_name ;
176161
177162 wp_enqueue_style (
178- self :: CSS_HANDLE ,
163+ static :: get_css_handle () ,
179164 $ file_url ,
180165 array (),
181166 STACKABLE_VERSION
182167 );
168+
169+ return true ;
183170 }
171+
172+ // Fallback to inline styles if file generation failed
173+ return static ::enqueue_inline_css ();
184174 }
185175
186176 /**
187- * Regenerate the CSS file when global settings change.
177+ * Enqueue CSS as inline styles as a fallback when file generation fails.
178+ *
179+ * @return bool
188180 */
189- public function invalidate_current_css_file () {
181+ public static function enqueue_inline_css () {
182+ $ css_content = static ::generate_css_content ();
183+
184+ if ( ! empty ( $ css_content ) ) {
185+ // Register a dummy style handle for inline CSS
186+ wp_register_style ( static ::get_css_handle () . '-inline ' , false );
187+ wp_add_inline_style ( static ::get_css_handle () . '-inline ' , $ css_content );
188+ wp_enqueue_style ( static ::get_css_handle () . '-inline ' );
189+
190+ return true ;
191+ }
192+
193+ return false ;
194+ }
195+
196+ /**
197+ * Get the CSS content as a string (useful for debugging or custom enqueuing).
198+ *
199+ * @return string
200+ */
201+ public static function get_css_content () {
202+ return static ::generate_css_content ();
203+ }
204+
205+ /**
206+ * Invalidate the current CSS file.
207+ */
208+ public static function invalidate_css_file () {
190209 // Delete the old CSS file if it exists
191- $ old_file_name = $ this -> get_cached_css_file_name ();
210+ $ old_file_name = static :: get_cached_css_file_name ();
192211 if ( $ old_file_name ) {
193- $ old_file_path = $ this -> get_css_file_path () . $ old_file_name ;
212+ $ old_file_path = static :: get_css_file_path () . $ old_file_name ;
194213 if ( file_exists ( $ old_file_path ) ) {
195214 unlink ( $ old_file_path );
196215 }
197216 }
198217 }
218+
219+ /**
220+ * Clear all cached CSS files for this generator.
221+ */
222+ public static function clear_css_cache () {
223+ $ css_dir = static ::get_css_file_path ();
224+
225+ if ( file_exists ( $ css_dir ) ) {
226+ $ files = glob ( $ css_dir . static ::get_css_file_prefix () . '*.css ' );
227+ foreach ( $ files as $ file ) {
228+ unlink ( $ file );
229+ }
230+ }
231+
232+ delete_option ( static ::get_cache_option_name () );
233+ }
234+ }
235+ }
236+
237+ if ( ! class_exists ( 'Stackable_Global_Design_System_CSS_Generator ' ) ) {
238+
239+ /**
240+ * Global Design System CSS Generator - Extends the base class for global design system.
241+ */
242+ class Stackable_Global_Design_System_CSS_Generator extends Stackable_Base_CSS_File_Generator {
243+
244+ /**
245+ * CSS file handle for the global design system.
246+ */
247+ protected static function get_css_handle () {
248+ return 'ugb-global-design-system ' ;
249+ }
250+
251+ /**
252+ * CSS file name prefix.
253+ */
254+ protected static function get_css_file_prefix () {
255+ return 'stackable-global- ' ;
256+ }
257+
258+ /**
259+ * Option name for caching the CSS file name.
260+ */
261+ protected static function get_cache_option_name () {
262+ return 'stackable_global_css_file_name ' ;
263+ }
264+
265+ /**
266+ * Generate the CSS content from all global design system filters.
267+ *
268+ * @return string
269+ */
270+ protected static function generate_css_content () {
271+ // Apply all the filters that contribute to the global design system
272+ $ css_content = apply_filters ( 'stackable_inline_styles_nodep ' , '' );
273+
274+ // Add any additional global styles that might be needed
275+ $ css_content = apply_filters ( 'stackable_global_css_file_content ' , $ css_content );
276+
277+ return trim ( $ css_content );
278+ }
279+
280+ /**
281+ * Initialize
282+ */
283+ function __construct () {
284+ // Add hooks to regenerate CSS when global settings change
285+ add_action ( 'update_option_stackable_global_colors ' , array ( $ this , 'invalidate_current_css_file ' ) );
286+ add_action ( 'update_option_stackable_global_typography ' , array ( $ this , 'invalidate_current_css_file ' ) );
287+ add_action ( 'update_option_stackable_global_spacing_and_borders ' , array ( $ this , 'invalidate_current_css_file ' ) );
288+ add_action ( 'update_option_stackable_global_color_schemes ' , array ( $ this , 'invalidate_current_css_file ' ) );
289+ add_action ( 'update_option_stackable_global_block_styles ' , array ( $ this , 'invalidate_current_css_file ' ) );
290+ add_action ( 'update_option_stackable_global_buttons_and_icons ' , array ( $ this , 'invalidate_current_css_file ' ) );
291+ add_action ( 'update_option_stackable_global_preset_controls ' , array ( $ this , 'invalidate_current_css_file ' ) );
292+ }
293+
294+ /**
295+ * Regenerate the CSS file when global settings change.
296+ */
297+ public function invalidate_current_css_file () {
298+ static ::invalidate_css_file ();
299+ }
300+
301+ /**
302+ * Enqueue the global CSS file (alias for backward compatibility).
303+ */
304+ public static function enqueue_global_css_file () {
305+ return static ::enqueue_css_file ();
306+ }
307+
308+ /**
309+ * Enqueue the global CSS as inline styles (useful for debugging or when file generation is disabled).
310+ */
311+ public static function enqueue_global_css_inline () {
312+ return static ::enqueue_inline_css ();
313+ }
199314 }
200315
201- new Stackable_CSS_File_Generator ();
316+ new Stackable_Global_Design_System_CSS_Generator ();
202317}
0 commit comments