PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.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
ablocks / includes / classes / css-generator.php

css-generator.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.0, at includes/classes/css-generator.php

205 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 CssGenerator {
12 private $custom_css = '';
13 private $parent_class;
14 private $class_styles = [];
15
16 public function __construct( $attributes = [] ) {
17 if ( ! isset( $attributes['block_id'] ) ) {
18 return;
19 }
20 $this->parent_class = '.ablocks-block-' . $attributes['block_id'];
21 if ( isset( $attributes['_custom_css'] ) ) {
22 $this->custom_css = $attributes['_custom_css'];
23 }
24
25
26 // Alert - don't touch here
27 if ( isset( $attributes['_margin'] ) ) { // check has advanced tab or not
28 $this->add_class_styles(
29 '{{WRAPPER}}',
30 BlockGlobal::get_wrapper_css( $attributes ),
31 BlockGlobal::get_wrapper_css( $attributes, 'Tablet' ),
32 BlockGlobal::get_wrapper_css( $attributes, 'Mobile' )
33 );
34 $this->add_class_styles(
35 '{{WRAPPER}}:hover',
36 BlockGlobal::get_wrapper_hover_css( $attributes ),
37 BlockGlobal::get_wrapper_hover_css( $attributes, 'Tablet' ),
38 BlockGlobal::get_wrapper_hover_css( $attributes, 'Mobile' )
39 );
40 $this->add_class_styles(
41 '{{WRAPPER}}.ablocks-hide-on-desktop,{{WRAPPER}}.ablocks-hide-on-tablet,{{WRAPPER}}.ablocks-hide-on-mobile',
42 BlockGlobal::get_wrapper_device_responsive_css( $attributes ),
43 BlockGlobal::get_wrapper_device_responsive_css( $attributes, 'Tablet' ),
44 BlockGlobal::get_wrapper_device_responsive_css( $attributes, 'Mobile' )
45 );
46 $this->add_class_styles(
47 '{{WRAPPER}}:hover > .ablocks-block-container',
48 BlockGlobal::get_container_hover_css( $attributes ),
49 BlockGlobal::get_container_hover_css( $attributes, 'Tablet' ),
50 BlockGlobal::get_container_hover_css( $attributes, 'Mobile' )
51 );
52 $this->add_class_styles(
53 '{{WRAPPER}} > .ablocks-block-container',
54 BlockGlobal::get_container_css( $attributes ),
55 BlockGlobal::get_container_css( $attributes, 'Tablet' ),
56 BlockGlobal::get_container_css( $attributes, 'Mobile' )
57 );
58 }//end if
59 }
60
61 public function add_class_styles( $class_name, $desktop_styles, $tablet_styles = [], $mobile_styles = [] ) {
62 $this->class_styles[] = [
63 'class_name' => $class_name,
64 'desktop_styles' => $desktop_styles,
65 'tablet_styles' => $tablet_styles,
66 'mobile_styles' => $mobile_styles
67 ];
68 }
69
70 public function generate_css() {
71 $css_output = '';
72
73 foreach ( $this->class_styles as $class_style ) {
74 $desktop_styles = $this->remove_empty_css( $class_style['desktop_styles'] );
75 $tablet_styles = $this->filter_responsive_styles( $desktop_styles, $class_style['tablet_styles'] );
76 $mobile_styles = $this->filter_responsive_styles( array_merge( $desktop_styles, $tablet_styles ), $class_style['mobile_styles'] );
77
78 $desktop_raw = $this->generate_css_for_media_query( 'desktop', $desktop_styles );
79 $tablet_raw = $this->generate_css_for_media_query( 'tablet', $tablet_styles );
80 $mobile_raw = $this->generate_css_for_media_query( 'mobile', $mobile_styles );
81
82 $desktop_css = AssetsGenerator::minify_css( $desktop_raw );
83 $tablet_css = AssetsGenerator::minify_css( $tablet_raw );
84 $mobile_css = AssetsGenerator::minify_css( $mobile_raw );
85
86 $parent_selector = $this->get_parent_selector( $class_style['class_name'] );
87 $css_blocks = [];
88
89 $addToCssBlocks = function ( $mediaQuery, $max_width, $css ) use ( &$css_blocks, $parent_selector ) {
90 if ( ! empty( $css ) ) {
91 $css_blocks[] = ( '' !== $mediaQuery ) ? "@media screen and (max-width: $max_width) {\n$parent_selector {\n$css\n}\n}" : "$parent_selector {\n$css\n}";
92 }
93 };
94
95 // Always add desktop CSS
96 $addToCssBlocks( '', '', $desktop_css );
97
98 // Only add tablet CSS if it differs from desktop
99 if ( $tablet_raw !== $desktop_raw ) {
100 $addToCssBlocks( 'tablet', $this->get_breakpoint( 'tablet' ), $tablet_css );
101 }
102
103 // Only add mobile CSS if it differs from desktop AND tablet
104 if ( $mobile_raw !== $tablet_raw ) {
105 $addToCssBlocks( 'mobile', $this->get_breakpoint( 'mobile' ), $mobile_css );
106 }
107
108 $css_output .= implode( "\n\n", $css_blocks ) . "\n\n";
109 }//end foreach
110
111 $css_output .= $this->get_custom_css();
112
113 return preg_replace( '/\s+/', ' ', $css_output );
114 }
115 public function get_custom_css() {
116 return preg_replace( '/\bselector\b/', $this->parent_class, $this->custom_css );
117 }
118 public function generate_css_for_media_query( $media_query, $styles ) {
119 if ( empty( $styles ) ) {
120 return '';
121 }
122 $css_string = implode("\n", array_map(
123 function ( $property, $value ) {
124 return "$property: $value;";
125 },
126 array_keys( $styles ),
127 $styles
128 ));
129
130 return $css_string . "\n";
131 }
132
133 public function get_breakpoint( $media_query ) {
134 switch ( $media_query ) {
135 case 'tablet':
136 return '800px';
137 case 'mobile':
138 return '480px';
139 default:
140 return '1200px';
141 }
142 }
143
144 public function get_parent_selector( $class_name ) {
145 return $this->parent_class ? str_replace( '{{WRAPPER}}', $this->parent_class, $class_name ) : $class_name;
146 }
147
148 private function remove_empty_css( $base_styles ) {
149 if ( empty( $base_styles ) || ! is_array( $base_styles ) || ( is_array( $base_styles ) && ! count( $base_styles ) ) ) {
150 return [];
151 }
152
153 $styles = [];
154
155 foreach ( $base_styles as $prop => $value ) {
156 if ( 'font-family' === $prop ) {
157 // Safety net for styles that bypass the Typography control and set a
158 // bare family name directly. FontStack leaves finished values alone.
159 $value = FontStack::build( $value );
160 }
161 // Trim value to avoid whitespace-only entries
162 $trimmed = trim( $value );
163 // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number
164 if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%|s|ms|deg|fr|ch|ex)$/i', $trimmed ) ) {
165 continue;
166 }
167
168 $styles[ $prop ] = $trimmed;
169 }
170
171 return $styles;
172 }
173
174 private function filter_responsive_styles( $base_styles, $responsive_styles ) {
175 if ( empty( $responsive_styles ) || ! is_array( $responsive_styles ) ) {
176 return [];
177 }
178
179 $difference = [];
180
181 foreach ( $responsive_styles as $prop => $value ) {
182 $trimmed = trim( $value );
183
184 // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number
185 if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%|s|ms|deg|fr|ch|ex)$/i', $trimmed ) ) {
186 continue;
187 }
188
189 // Only include if the value is different from base styles
190 if ( ! isset( $base_styles[ $prop ] ) || $base_styles[ $prop ] !== $trimmed ) {
191 $difference[ $prop ] = $trimmed;
192 }
193 }
194
195 return $difference;
196 }
197
198
199 public function get_font_family_css( string $font_name, string $category = '' ): string {
200 // Kept for back-compat; the stack is built centrally now.
201 return FontStack::build( $font_name );
202 }
203 }
204
205