| 1 |
<?php |
| 2 |
/** |
| 3 |
* Generate Dynamic Style |
| 4 |
* @package GutSliderBlocks |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. |
| 8 |
|
| 9 |
if( ! class_exists( 'GutSlider_Dynamic_Style' ) ) { |
| 10 |
|
| 11 |
class GutSlider_Dynamic_Style { |
| 12 |
|
| 13 |
/** |
| 14 |
* Constructor |
| 15 |
* return void |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_filter( 'render_block', [ $this, 'generate_dynamic_style' ], 10, 2 ); |
| 19 |
} |
| 20 |
|
| 21 |
function generate_dynamic_style($block_content, $block) { |
| 22 |
if (isset($block['blockName']) && str_contains($block['blockName'], 'gutsliders/')) { |
| 23 |
|
| 24 |
do_action( 'gutsliders_render_block', $block ); |
| 25 |
|
| 26 |
if (isset($block['attrs']['blockStyle'])) { |
| 27 |
|
| 28 |
$style = $block['attrs']['blockStyle']; |
| 29 |
|
| 30 |
$handle = isset( $block['attrs']['uniqueId'] ) ? $block['attrs']['uniqueId'] : 'slider-blocks'; |
| 31 |
|
| 32 |
if ( is_array( $style ) && !empty( $style ) ) { |
| 33 |
$style = implode(' ', $style); |
| 34 |
} |
| 35 |
// minify style to remove extra space |
| 36 |
$style = preg_replace( '/\s+/', ' ', $style ); |
| 37 |
|
| 38 |
// register style |
| 39 |
wp_register_style( $handle, false, [], GUTSLIDER_VERSION, 'all' ); // wp_register_style( $handle, $src, $deps, $ver, $media ); |
| 40 |
wp_enqueue_style( $handle ); |
| 41 |
wp_add_inline_style( $handle, $style ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
} |
| 46 |
return $block_content; |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
new GutSlider_Dynamic_Style(); // Initialize the class |