Skip to content

Commit d29b88e

Browse files
change block style inheritance to css file generation
1 parent 5e2cf3d commit d29b88e

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/css-file-generator.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,82 @@ public static function enqueue_global_css_inline() {
314314
}
315315

316316
new Stackable_Global_Design_System_CSS_Generator();
317+
}
318+
319+
if ( ! class_exists( 'Stackable_Block_Style_Inheritance_CSS_Generator' ) ) {
320+
321+
/**
322+
* Block Style Inheritance CSS Generator - Extends the base class for block theme style inheritance.
323+
*/
324+
class Stackable_Block_Style_Inheritance_CSS_Generator extends Stackable_Base_CSS_File_Generator {
325+
326+
/**
327+
* CSS file handle for block style inheritance.
328+
*/
329+
protected static function get_css_handle() {
330+
return 'ugb-block-style-inheritance';
331+
}
332+
333+
/**
334+
* CSS file name prefix.
335+
*/
336+
protected static function get_css_file_prefix() {
337+
return 'stackable-block-inheritance-';
338+
}
339+
340+
/**
341+
* Option name for caching the CSS file name.
342+
*/
343+
protected static function get_cache_option_name() {
344+
return 'stackable_block_inheritance_css_file_name';
345+
}
346+
347+
/**
348+
* Generate the CSS content from block style inheritance filters.
349+
*
350+
* @return string
351+
*/
352+
protected static function generate_css_content() {
353+
// Apply the filter that contributes to block style inheritance
354+
$css_content = apply_filters( 'stackable_block_style_inheritance_inline_styles_nodep', '' );
355+
356+
// Add any additional block inheritance styles that might be needed
357+
$css_content = apply_filters( 'stackable_block_inheritance_css_file_content', $css_content );
358+
359+
return trim( $css_content );
360+
}
361+
362+
/**
363+
* Initialize
364+
*/
365+
function __construct() {
366+
// Add hooks to regenerate CSS when theme.json or block settings change
367+
add_action( 'switch_theme', array( $this, 'invalidate_current_css_file' ) );
368+
add_action( 'update_option_theme_mods_' . get_stylesheet(), array( $this, 'invalidate_current_css_file' ) );
369+
add_action( 'customize_save_after', array( $this, 'invalidate_current_css_file' ) );
370+
}
371+
372+
/**
373+
* Regenerate the CSS file when block style inheritance settings change.
374+
*/
375+
public function invalidate_current_css_file() {
376+
static::invalidate_css_file();
377+
}
378+
379+
/**
380+
* Enqueue the block inheritance CSS file (alias for backward compatibility).
381+
*/
382+
public static function enqueue_block_inheritance_css_file() {
383+
return static::enqueue_css_file();
384+
}
385+
386+
/**
387+
* Enqueue the block inheritance CSS as inline styles (useful for debugging or when file generation is disabled).
388+
*/
389+
public static function enqueue_block_inheritance_css_inline() {
390+
return static::enqueue_inline_css();
391+
}
392+
}
393+
394+
new Stackable_Block_Style_Inheritance_CSS_Generator();
317395
}

src/init.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ public function load_frontend_scripts_conditionally( $block_content, $block ) {
240240

241241
// Register inline frontend styles for theme.json block style inheritance
242242
public function enqueue_inline_block_style_inheritance() {
243-
$block_style_inline_css = apply_filters( 'stackable_block_style_inheritance_inline_styles_nodep', '' );
244-
if ( ! empty( $block_style_inline_css ) ) {
245-
wp_register_style( 'ugb-block-style-inheritance-nodep', false );
246-
wp_add_inline_style( 'ugb-block-style-inheritance-nodep', $block_style_inline_css );
247-
wp_enqueue_style( 'ugb-block-style-inheritance-nodep' );
243+
// Use the new CSS generator class with built-in fallback to inline styles
244+
if ( ! is_admin() && get_option( 'stackable_use_css_files', 'yes' ) === 'yes' ) {
245+
Stackable_Block_Style_Inheritance_CSS_Generator::enqueue_block_inheritance_css_file();
246+
} else {
247+
Stackable_Block_Style_Inheritance_CSS_Generator::enqueue_block_inheritance_css_inline();
248248
}
249249
}
250250

0 commit comments

Comments
 (0)