PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
← All changes | includes/classes/css-generator-v2.php +306 -0 2.7.72.13.0 View file →
@@ -1,0 +1,306 @@
1 +<?php
2 +namespace ABlocks\Classes;
3 +
4 +if ( ! defined( 'ABSPATH' ) ) {
5 + exit;
6 +}
7 +
8 +use ABlocks\Classes\AssetsGenerator;
9 +use ABlocks\Classes\FontStack;
10 +
11 +class CssGeneratorV2 {
12 + private $custom_css = '';
13 + private $parent_class;
14 + private $class_styles = [];
15 +
16 + public function __construct( $attributes = [] ) {
17 + $this->parent_class = '.ablocks-block-' . $attributes['block_id'];
18 + if ( isset( $attributes['_custom_css'] ) ) {
19 + $this->custom_css = $attributes['_custom_css'];
20 + }
21 +
22 + // Alert - don't touch here
23 + if ( isset( $attributes['_margin'] ) ) { // check has advanced tab or not
24 + $this->add_class_styles(
25 + '{{WRAPPER}}',
26 + BlockGlobal::get_wrapper_css( $attributes ),
27 + BlockGlobal::get_wrapper_css( $attributes, 'Tablet' ),
28 + BlockGlobal::get_wrapper_css( $attributes, 'Mobile' ),
29 + $this->custom_device_map( function ( $device ) use ( $attributes ) {
30 + return BlockGlobal::get_wrapper_css( $attributes, $device );
31 + } )
32 + );
33 + $this->add_class_styles(
34 + '{{WRAPPER}}:hover',
35 + BlockGlobal::get_wrapper_hover_css( $attributes ),
36 + BlockGlobal::get_wrapper_hover_css( $attributes, 'Tablet' ),
37 + BlockGlobal::get_wrapper_hover_css( $attributes, 'Mobile' )
38 + );
39 + $this->add_class_styles(
40 + '{{WRAPPER}}.ablocks-hide-on-desktop,{{WRAPPER}}.ablocks-hide-on-tablet,{{WRAPPER}}.ablocks-hide-on-mobile',
41 + BlockGlobal::get_wrapper_device_responsive_css( $attributes ),
42 + BlockGlobal::get_wrapper_device_responsive_css( $attributes, 'Tablet' ),
43 + BlockGlobal::get_wrapper_device_responsive_css( $attributes, 'Mobile' )
44 + );
45 + $this->add_class_styles(
46 + '{{WRAPPER}}:hover > .ablocks-block-container',
47 + BlockGlobal::get_container_hover_css( $attributes ),
48 + BlockGlobal::get_container_hover_css( $attributes, 'Tablet' ),
49 + BlockGlobal::get_container_hover_css( $attributes, 'Mobile' )
50 + );
51 + $this->add_class_styles(
52 + '{{WRAPPER}} > .ablocks-block-container',
53 + BlockGlobal::get_container_css( $attributes ),
54 + BlockGlobal::get_container_css( $attributes, 'Tablet' ),
55 + BlockGlobal::get_container_css( $attributes, 'Mobile' ),
56 + $this->custom_device_map( function ( $device ) use ( $attributes ) {
57 + return BlockGlobal::get_container_css( $attributes, $device );
58 + } )
59 + );
60 + }//end if
61 + }
62 +
63 + public function add_class_styles( $class_name, $desktop_styles, $tablet_styles = [], $mobile_styles = [], $responsive_styles = [] ) {
64 + $this->class_styles[] = [
65 + 'class_name' => $class_name,
66 + 'desktop_styles' => $desktop_styles,
67 + 'tablet_styles' => $tablet_styles,
68 + 'mobile_styles' => $mobile_styles,
69 + // Optional map of custom breakpoints: [ suffix => [ width, styles ] ].
70 + 'responsive_styles' => $responsive_styles,
71 + ];
72 + }
73 +
74 + /**
75 + * Build the custom-breakpoint style map for a selector from a per-device
76 + * builder closure. Only user-registered custom breakpoints (not the built-in
77 + * Tablet/Mobile) are included, so the standard path is untouched.
78 + */
79 + public function custom_device_map( $builder ) {
80 + $map = [];
81 +
82 + // Nothing to build when the site registers no custom breakpoints — and
83 + // running the phantom probe anyway is not free: it hands every control a
84 + // device suffix that has no stored keys, which is what made controls that
85 + // read `$value[ 'x' . $device ]` unguarded spray "Undefined array key
86 + // …___ablocksphantom___" notices on every page under WP_DEBUG.
87 + $custom_devices = [];
88 + foreach ( \ABlocks\Helper::get_responsive_devices() as $d ) {
89 + if ( $d['width'] <= 0 || 'Tablet' === $d['id'] || 'Mobile' === $d['id'] ) {
90 + continue;
91 + }
92 + $custom_devices[] = $d;
93 + }
94 + if ( empty( $custom_devices ) ) {
95 + return $map;
96 + }
97 +
98 + // Phantom baseline: the builder run against a suffix that has no stored
99 + // values. Controls that hardcode only Tablet/Mobile defaults emit
100 + // "phantom" declarations (e.g. border-width:0) for any unknown suffix;
101 + // subtracting the phantom leaves only genuinely-overridden custom values,
102 + // with no per-control changes.
103 + $phantom = (array) call_user_func( $builder, '__ablocksphantom__' );
104 + foreach ( $custom_devices as $d ) {
105 + if ( $d['width'] <= 0 || 'Tablet' === $d['id'] || 'Mobile' === $d['id'] ) {
106 + continue;
107 + }
108 + $actual = (array) call_user_func( $builder, $d['suffix'] );
109 + $real = [];
110 + foreach ( $actual as $prop => $val ) {
111 + if ( ! array_key_exists( $prop, $phantom ) || $phantom[ $prop ] !== $val ) {
112 + $real[ $prop ] = $val;
113 + }
114 + }
115 + if ( ! empty( $real ) ) {
116 + $map[ $d['suffix'] ] = [
117 + 'width' => (int) $d['width'],
118 + 'min' => isset( $d['min'] ) ? (int) $d['min'] : 0,
119 + 'max' => isset( $d['max'] ) ? (int) $d['max'] : (int) $d['width'],
120 + 'styles' => $real,
121 + ];
122 + }
123 + }
124 + return $map;
125 + }
126 +
127 + public function generate_css() {
128 + $css_output = '';
129 +
130 + foreach ( $this->class_styles as $class_style ) {
131 + $desktop_styles = $this->remove_empty_css( $class_style['desktop_styles'] );
132 + $tablet_styles = $this->filter_responsive_styles( $desktop_styles, $class_style['tablet_styles'] );
133 + $mobile_styles = $this->filter_responsive_styles( array_merge( $desktop_styles, $tablet_styles ), $class_style['mobile_styles'] );
134 +
135 + $desktop_raw = $this->generate_css_for_media_query( 'desktop', $desktop_styles );
136 + $tablet_raw = $this->generate_css_for_media_query( 'tablet', $tablet_styles );
137 + $mobile_raw = $this->generate_css_for_media_query( 'mobile', $mobile_styles );
138 +
139 + $desktop_css = AssetsGenerator::minify_css( $desktop_raw );
140 + $tablet_css = AssetsGenerator::minify_css( $tablet_raw );
141 + $mobile_css = AssetsGenerator::minify_css( $mobile_raw );
142 +
143 + $parent_selector = $this->get_parent_selector( $class_style['class_name'] );
144 + $css_blocks = [];
145 +
146 + $addToCssBlocks = function ( $mediaQuery, $max_width, $css ) use ( &$css_blocks, $parent_selector ) {
147 + if ( ! empty( $css ) ) {
148 + $css_blocks[] = ( '' !== $mediaQuery )
149 + ? "@media screen and (max-width: $max_width) {\n$parent_selector {\n$css\n}\n}"
150 + : "$parent_selector {\n$css\n}";
151 + }
152 + };
153 +
154 + // Always add desktop CSS
155 + $addToCssBlocks( '', '', $desktop_css );
156 +
157 + if ( empty( $class_style['responsive_styles'] ) ) {
158 + // Standard path (no custom breakpoints) — unchanged output.
159 + // Only add tablet CSS if it differs from desktop
160 + if ( $tablet_raw !== $desktop_raw ) {
161 + $addToCssBlocks( 'tablet', $this->get_breakpoint( 'tablet' ), $tablet_css );
162 + }
163 +
164 + // Only add mobile CSS if it differs from desktop AND tablet
165 + if ( $mobile_raw !== $tablet_raw ) {
166 + $addToCssBlocks( 'mobile', $this->get_breakpoint( 'mobile' ), $mobile_css );
167 + }
168 + } else {
169 + // Custom breakpoints present: emit every breakpoint in
170 + // width-descending order (narrowest last so it wins the cascade),
171 + // each deduped against desktop.
172 + $bp_defaults = \ABlocks\Helper::get_breakpoints();
173 + $bp_list = [
174 + [ 'width' => $bp_defaults['tablet'], 'min' => 0, 'max' => $bp_defaults['tablet'], 'styles' => $class_style['tablet_styles'] ],
175 + [ 'width' => $bp_defaults['mobile'], 'min' => 0, 'max' => $bp_defaults['mobile'], 'styles' => $class_style['mobile_styles'] ],
176 + ];
177 + foreach ( $class_style['responsive_styles'] as $bp ) {
178 + $bp_list[] = [
179 + 'width' => (int) $bp['width'],
180 + 'min' => isset( $bp['min'] ) ? (int) $bp['min'] : 0,
181 + 'max' => isset( $bp['max'] ) ? (int) $bp['max'] : (int) $bp['width'],
182 + 'styles' => $bp['styles'],
183 + ];
184 + }
185 + usort( $bp_list, function ( $a, $b ) {
186 + return (int) $b['width'] - (int) $a['width'];
187 + } );
188 +
189 + foreach ( $bp_list as $bp ) {
190 + $bp_styles = $this->filter_responsive_styles( $desktop_styles, $bp['styles'] );
191 + $bp_raw = $this->generate_css_for_media_query( 'bp', $bp_styles );
192 + if ( $bp_raw === $desktop_raw || '' === trim( $bp_raw ) ) {
193 + continue;
194 + }
195 + $bp_css = AssetsGenerator::minify_css( $bp_raw );
196 + $media = \ABlocks\Helper::breakpoint_media_condition( $bp['min'], $bp['max'] );
197 + if ( '' === $bp_css || '' === $media ) {
198 + continue;
199 + }
200 + $css_blocks[] = "@media screen and $media {\n$parent_selector {\n$bp_css\n}\n}";
201 + }
202 + }
203 +
204 + $css_output .= implode( "\n\n", $css_blocks ) . "\n\n";
205 + }//end foreach
206 +
207 + $css_output .= $this->get_custom_css();
208 +
209 + return preg_replace( '/\s+/', ' ', $css_output );
210 + }
211 +
212 + public function get_custom_css() {
213 + return preg_replace( '/\bselector\b/', $this->parent_class, $this->custom_css );
214 + }
215 + public function generate_css_for_media_query( $media_query, $styles ) {
216 + if ( empty( $styles ) ) {
217 + return '';
218 + }
219 + $css_string = implode("\n", array_map(
220 + function ( $property, $value ) {
221 + // See Helper::esc_css_value(): these values come from block
222 + // attributes and land inside an inline <style> element.
223 + $property = \ABlocks\Helper::esc_css_value( $property );
224 + $value = \ABlocks\Helper::esc_css_value( $value );
225 + return "$property: $value;";
226 + },
227 + array_keys( $styles ),
228 + $styles
229 + ));
230 +
231 + return $css_string . "\n";
232 + }
233 +
234 + public function get_breakpoint( $media_query ) {
235 + $bp = \ABlocks\Helper::get_breakpoints();
236 + switch ( $media_query ) {
237 + case 'tablet':
238 + return $bp['tablet'] . 'px';
239 + case 'mobile':
240 + return $bp['mobile'] . 'px';
241 + default:
242 + return '1200px';
243 + }
244 + }
245 +
246 + public function get_parent_selector( $class_name ) {
247 + return $this->parent_class ? str_replace( '{{WRAPPER}}', $this->parent_class, $class_name ) : $class_name;
248 + }
249 + private function remove_empty_css( $base_styles ) {
250 + if ( empty( $base_styles ) || ! is_array( $base_styles ) || ( is_array( $base_styles ) && ! count( $base_styles ) ) ) {
251 + return [];
252 + }
253 +
254 + $styles = [];
255 +
256 + foreach ( $base_styles as $prop => $value ) {
257 + if ( 'font-family' === $prop ) {
258 + // Safety net for styles that bypass the Typography control and set a
259 + // bare family name directly. FontStack leaves finished values alone.
260 + $value = FontStack::build( $value );
261 + }
262 + // Trim value to avoid whitespace-only entries
263 + $trimmed = trim( $value );
264 + // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number
265 + if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%|s|ms|deg|fr|ch|ex)$/i', $trimmed ) ) {
266 + continue;
267 + }
268 +
269 + $styles[ $prop ] = $trimmed;
270 + }
271 +
272 + return $styles;
273 + }
274 + private function filter_responsive_styles( $base_styles, $responsive_styles ) {
275 + if ( empty( $responsive_styles ) || ! is_array( $responsive_styles ) ) {
276 + return [];
277 + }
278 +
279 + $difference = [];
280 +
281 + foreach ( $responsive_styles as $prop => $value ) {
282 + $trimmed = trim( $value );
283 +
284 + // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number
285 + if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%|s|ms|deg|fr|ch|ex)$/i', $trimmed ) ) {
286 + continue;
287 + }
288 +
289 + // Only include if the value is different from base styles
290 + if ( ! isset( $base_styles[ $prop ] ) || $base_styles[ $prop ] !== $trimmed ) {
291 + $difference[ $prop ] = $trimmed;
292 + }
293 + }
294 +
295 + return $difference;
296 + }
297 +
298 +
299 + public function get_font_family_css( string $font_name, string $category = '' ): string {
300 + // Kept for back-compat; the stack is built centrally now.
301 + return FontStack::build( $font_name );
302 + }
303 +
304 +
305 +}
306 +