PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.8.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.8.0
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 1.2.0 1.2.1 All 78 releases
ablocks / includes / classes / css-generator-v2.php

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

228 lines 7.0 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
10 class CssGeneratorV2 {
11 private $font_category;
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 $this->font_category = include ABLOCKS_BLOCKS_DIR_PATH . 'fonts.php';
22
23 // Alert - don't touch here
24 if ( isset( $attributes['_margin'] ) ) { // check has advanced tab or not
25 $this->add_class_styles(
26 '{{WRAPPER}}',
27 BlockGlobal::get_wrapper_css( $attributes ),
28 BlockGlobal::get_wrapper_css( $attributes, 'Tablet' ),
29 BlockGlobal::get_wrapper_css( $attributes, 'Mobile' )
30 );
31 $this->add_class_styles(
32 '{{WRAPPER}}:hover',
33 BlockGlobal::get_wrapper_hover_css( $attributes ),
34 BlockGlobal::get_wrapper_hover_css( $attributes, 'Tablet' ),
35 BlockGlobal::get_wrapper_hover_css( $attributes, 'Mobile' )
36 );
37 $this->add_class_styles(
38 '{{WRAPPER}}.ablocks-hide-on-desktop,{{WRAPPER}}.ablocks-hide-on-tablet,{{WRAPPER}}.ablocks-hide-on-mobile',
39 BlockGlobal::get_wrapper_device_responsive_css( $attributes ),
40 BlockGlobal::get_wrapper_device_responsive_css( $attributes, 'Tablet' ),
41 BlockGlobal::get_wrapper_device_responsive_css( $attributes, 'Mobile' )
42 );
43 $this->add_class_styles(
44 '{{WRAPPER}}:hover > .ablocks-block-container',
45 BlockGlobal::get_container_hover_css( $attributes ),
46 BlockGlobal::get_container_hover_css( $attributes, 'Tablet' ),
47 BlockGlobal::get_container_hover_css( $attributes, 'Mobile' )
48 );
49 $this->add_class_styles(
50 '{{WRAPPER}} > .ablocks-block-container',
51 BlockGlobal::get_container_css( $attributes ),
52 BlockGlobal::get_container_css( $attributes, 'Tablet' ),
53 BlockGlobal::get_container_css( $attributes, 'Mobile' )
54 );
55 }//end if
56 }
57
58 public function add_class_styles( $class_name, $desktop_styles, $tablet_styles = [], $mobile_styles = [] ) {
59 $this->class_styles[] = [
60 'class_name' => $class_name,
61 'desktop_styles' => $desktop_styles,
62 'tablet_styles' => $tablet_styles,
63 'mobile_styles' => $mobile_styles
64 ];
65 }
66
67 public function generate_css() {
68 $css_output = '';
69
70 foreach ( $this->class_styles as $class_style ) {
71 $desktop_styles = $this->remove_empty_css( $class_style['desktop_styles'] );
72 $tablet_styles = $this->filter_responsive_styles( $desktop_styles, $class_style['tablet_styles'] );
73 $mobile_styles = $this->filter_responsive_styles( array_merge( $desktop_styles, $tablet_styles ), $class_style['mobile_styles'] );
74
75 $desktop_raw = $this->generate_css_for_media_query( 'desktop', $desktop_styles );
76 $tablet_raw = $this->generate_css_for_media_query( 'tablet', $tablet_styles );
77 $mobile_raw = $this->generate_css_for_media_query( 'mobile', $mobile_styles );
78
79 $desktop_css = AssetsGenerator::minify_css( $desktop_raw );
80 $tablet_css = AssetsGenerator::minify_css( $tablet_raw );
81 $mobile_css = AssetsGenerator::minify_css( $mobile_raw );
82
83 $parent_selector = $this->get_parent_selector( $class_style['class_name'] );
84 $css_blocks = [];
85
86 $addToCssBlocks = function ( $mediaQuery, $max_width, $css ) use ( &$css_blocks, $parent_selector ) {
87 if ( ! empty( $css ) ) {
88 $css_blocks[] = ( '' !== $mediaQuery )
89 ? "@media screen and (max-width: $max_width) {\n$parent_selector {\n$css\n}\n}"
90 : "$parent_selector {\n$css\n}";
91 }
92 };
93
94 // Always add desktop CSS
95 $addToCssBlocks( '', '', $desktop_css );
96
97 // Only add tablet CSS if it differs from desktop
98 if ( $tablet_raw !== $desktop_raw ) {
99 $addToCssBlocks( 'tablet', $this->get_breakpoint( 'tablet' ), $tablet_css );
100 }
101
102 // Only add mobile CSS if it differs from desktop AND tablet
103 if ( $mobile_raw !== $tablet_raw ) {
104 $addToCssBlocks( 'mobile', $this->get_breakpoint( 'mobile' ), $mobile_css );
105 }
106
107 $css_output .= implode( "\n\n", $css_blocks ) . "\n\n";
108 }//end foreach
109
110 $css_output .= $this->get_custom_css();
111
112 return preg_replace( '/\s+/', ' ', $css_output );
113 }
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 private function remove_empty_css( $base_styles ) {
148 if ( empty( $base_styles ) || ! is_array( $base_styles ) || ( is_array( $base_styles ) && ! count( $base_styles ) ) ) {
149 return [];
150 }
151
152 $styles = [];
153
154 foreach ( $base_styles as $prop => $value ) {
155 if ( 'font-family' === $prop && isset( $this->font_category[ $value ] ) ) {
156 $value = $this->get_font_family_css( $value, $this->font_category[ $value ] );
157 }
158 // Trim value to avoid whitespace-only entries
159 $trimmed = trim( $value );
160 // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number
161 if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%)$/i', $trimmed ) ) {
162 continue;
163 }
164
165 $styles[ $prop ] = $trimmed;
166 }
167
168 return $styles;
169 }
170 private function filter_responsive_styles( $base_styles, $responsive_styles ) {
171 if ( empty( $responsive_styles ) || ! is_array( $responsive_styles ) ) {
172 return [];
173 }
174
175 $difference = [];
176
177 foreach ( $responsive_styles as $prop => $value ) {
178 $trimmed = trim( $value );
179
180 // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number
181 if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%)$/i', $trimmed ) ) {
182 continue;
183 }
184
185 // Only include if the value is different from base styles
186 if ( ! isset( $base_styles[ $prop ] ) || $base_styles[ $prop ] !== $trimmed ) {
187 $difference[ $prop ] = $trimmed;
188 }
189 }
190
191 return $difference;
192 }
193
194
195 public function get_font_family_css( string $font_name, string $category ): string {
196 // Quote font name if it contains spaces
197 if ( strpos( $font_name, ' ' ) !== false ) {
198 $font_name = '"' . $font_name . '"';
199 }
200
201 // Map category to fallback
202 switch ( $category ) {
203 case 'serif':
204 $fallback = 'serif';
205 break;
206 case 'sans-serif':
207 $fallback = 'sans-serif';
208 break;
209 case 'monospace':
210 $fallback = 'monospace';
211 break;
212 case 'display':
213 $fallback = 'sans-serif';
214 break;
215 case 'handwriting':
216 $fallback = 'cursive';
217 break;
218 default:
219 $fallback = 'sans-serif';
220 }
221
222 return $font_name . ', ' . $fallback;
223 }
224
225
226 }
227
228