PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.6.6
Gutenify – Visual Site Builder Blocks & Starter Templates v1.6.6
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / core / inc / blocks / class-dynamic-block-classname.php

class-dynamic-block-classname.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.6.6, at core/inc/blocks/class-dynamic-block-classname.php

168 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dynamic Block Classname Handler
4 *
5 * This file contains the `Dynamic_Block_Classname` class, which allows the dynamic addition
6 * of CSS classnames or inline styles to Gutenberg blocks during rendering.
7 *
8 * @package gutenify
9 * @subpackage Functions
10 * @since 1.0.0
11 */
12
13 namespace gutenify;
14
15 defined( 'ABSPATH' ) || exit; // Prevent direct access to the file.
16
17 /**
18 * Class Dynamic_Block_Classname.
19 *
20 * Handles dynamic addition of CSS classnames to Gutenberg blocks.
21 */
22 class Dynamic_Block_Classname {
23
24 /**
25 * Initializes the class by hooking into 'render_block'.
26 */
27 public static function init() {
28 add_filter( 'render_block', array( __CLASS__, 'render_block' ), 10, 3 );
29 }
30
31 /**
32 * Hook callback for modifying block output during rendering.
33 *
34 * @param string $block_content The original block HTML.
35 * @param array $block The block settings.
36 * @param object $instance The block instance (for reusable blocks).
37 * @return string Modified block HTML after applying custom filters.
38 */
39 public static function render_block( $block_content, $block, $instance ) {
40 // Skip processing for empty blocks or specific block types.
41 if ( empty( $block['blockName'] ) || in_array( $block['blockName'], array( 'structured-content/faq-item', 'structured-content/faq' ), true ) ) {
42 return $block_content;
43 }
44
45 // Generate a unique CSS class ID for this block.
46 $block_id = wp_unique_id( 'gtfy-' );
47
48 $html_processor = new \WP_HTML_Tag_Processor( $block_content );
49 if ( $html_processor->next_tag() ) {
50 $html_processor->add_class( $block_id );
51 }
52
53 // Enqueue dynamic styles if the block is valid.
54 if ( is_array( $block ) && ! empty( $block ) ) {
55 self::enqueue_dyanmic_style( $block, $block_id, $instance );
56 }
57
58 // Get the updated HTML content.
59 $updated_html = $html_processor->get_updated_html();
60
61 // Apply general and block-specific filters before returning.
62 $filter_prefix = 'gutenify_render_block';
63 $updated_html = apply_filters( $filter_prefix, $updated_html, $block, $instance, $block_id );
64 return apply_filters( $filter_prefix . '_' . $block['blockName'], $updated_html, $block, $instance, $block_id );
65 }
66
67 /**
68 * Enqueues dynamic styles for the block.
69 *
70 * @param array $block The block settings.
71 * @param string $block_id Unique ID for the block instance.
72 * @param object $instance The block instance.
73 */
74 public static function enqueue_dyanmic_style( $block, $block_id, $instance ) {
75 if ( empty( $block['blockName'] ) ) {
76 return false; // Exit early if the block has no name.
77 }
78
79 // Create a unique style handle based on the block name and block ID.
80 $handle = 'gutenify_' . str_replace( '/', '_', $block['blockName'] ) . '_' . $block_id;
81
82 // Register and enqueue an empty stylesheet for attaching inline styles.
83 wp_register_style( $handle, false );
84 wp_enqueue_style( $handle );
85
86 // Initialize CSS variable.
87 $css = '';
88
89 if ( ! in_array( $block['blockName'], array( 'gutenify/button' ), true ) ) {
90 $css = self::spacing_style( $block, $block_id, $instance );
91 }
92
93 $css .= self::position_style( $block, $block_id, $instance );
94
95 // Add the generated inline styles to the registered handle.
96 wp_add_inline_style( $handle, $css );
97 }
98
99 /**
100 * Generates dynamic spacing styles for the block.
101 *
102 * @param array $block The block settings.
103 * @param string $block_id Unique ID for the block instance.
104 * @param object $instance The block instance.
105 * @return string CSS styles.
106 */
107 public static function spacing_style( $block, $block_id, $instance ) {
108 // Initialize the CSS output variable.
109 $css = '';
110
111 // Get default margin from instance attributes.
112 $margin = ! empty( $instance->attributes['blockAdvanceOptions']['margin'] ) ? $instance->attributes['blockAdvanceOptions']['margin'] : array();
113
114 // Merge with block-level margin if provided.
115 if ( ! empty( $block['attrs']['blockAdvanceOptions']['margin'] ) ) {
116 $margin = wp_parse_args( $block['attrs']['blockAdvanceOptions']['margin'], $margin );
117 }
118
119 // Generate margin CSS.
120 $css .= Dynamic_Styles::get_spacing_with_media( '.' . $block_id, $margin, 'margin-' );
121
122 // Get default padding from instance attributes.
123 $padding = ! empty( $instance->attributes['blockAdvanceOptions']['padding'] ) ? $instance->attributes['blockAdvanceOptions']['padding'] : array();
124
125 // Merge with block-level padding if provided.
126 if ( ! empty( $block['attrs']['blockAdvanceOptions']['padding'] ) ) {
127 $padding = wp_parse_args( $block['attrs']['blockAdvanceOptions']['padding'], $padding );
128 }
129
130 // Generate padding CSS.
131 $css .= Dynamic_Styles::get_spacing_with_media( '.' . $block_id, $padding, 'padding-' );
132
133 // Return the combined spacing styles.
134 return $css;
135 }
136
137 /**
138 * Generates dynamic position styles (position, offsets, z-index, width,
139 * height, overflow) for the block, including any block-level custom
140 * breakpoints (e.g. AdvancedGroup's `customBlockBreakpoints` attribute).
141 *
142 * @param array $block The block settings.
143 * @param string $block_id Unique ID for the block instance.
144 * @param object $instance The block instance.
145 * @return string CSS styles.
146 */
147 public static function position_style( $block, $block_id, $instance ) {
148 $position = ! empty( $instance->attributes['blockAdvanceOptions']['position'] ) ? $instance->attributes['blockAdvanceOptions']['position'] : array();
149
150 if ( ! empty( $block['attrs']['blockAdvanceOptions']['position'] ) ) {
151 $position = wp_parse_args( $block['attrs']['blockAdvanceOptions']['position'], $position );
152 }
153
154 if ( empty( $position ) ) {
155 return '';
156 }
157
158 $custom_breakpoints = ! empty( $block['attrs']['customBlockBreakpoints'] ) && is_array( $block['attrs']['customBlockBreakpoints'] )
159 ? $block['attrs']['customBlockBreakpoints']
160 : array();
161
162 return Dynamic_Styles::get_position_with_media( '.' . $block_id, $position, $custom_breakpoints );
163 }
164 }
165
166 // Initialize the class.
167 Dynamic_Block_Classname::init();
168