| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | exit; |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | use ABlocks\Classes\AssetsGenerator; |
| 9 | +use ABlocks\Classes\FontStack; | |
| 9 | 10 | |
| 10 | 11 | class CssGenerator { |
| 11 | 12 | private $custom_css = ''; |
| 12 | 13 | private $parent_class; |
| @@ -12,12 +13,17 @@ | ||
| 12 | 13 | private $parent_class; |
| 13 | 14 | private $class_styles = []; |
| 14 | 15 | |
| 15 | 16 | public function __construct( $attributes = [] ) { |
| 17 | + if ( ! isset( $attributes['block_id'] ) ) { | |
| 18 | + return; | |
| 19 | + } | |
| 16 | 20 | $this->parent_class = '.ablocks-block-' . $attributes['block_id']; |
| 17 | 21 | if ( isset( $attributes['_custom_css'] ) ) { |
| 18 | 22 | $this->custom_css = $attributes['_custom_css']; |
| 19 | 23 | } |
| 24 | + | |
| 25 | + | |
| 20 | 26 | // Alert - don't touch here |
| 21 | 27 | if ( isset( $attributes['_margin'] ) ) { // check has advanced tab or not |
| 22 | 28 | $this->add_class_styles( |
| 23 | 29 | '{{WRAPPER}}', |
| @@ -22,9 +28,12 @@ | ||
| 22 | 28 | $this->add_class_styles( |
| 23 | 29 | '{{WRAPPER}}', |
| 24 | 30 | BlockGlobal::get_wrapper_css( $attributes ), |
| 25 | 31 | BlockGlobal::get_wrapper_css( $attributes, 'Tablet' ), |
| 26 | - BlockGlobal::get_wrapper_css( $attributes, 'Mobile' ) | |
| 32 | + BlockGlobal::get_wrapper_css( $attributes, 'Mobile' ), | |
| 33 | + $this->custom_device_map( function ( $device ) use ( $attributes ) { | |
| 34 | + return BlockGlobal::get_wrapper_css( $attributes, $device ); | |
| 35 | + } ) | |
| 27 | 36 | ); |
| 28 | 37 | $this->add_class_styles( |
| 29 | 38 | '{{WRAPPER}}:hover', |
| 30 | 39 | BlockGlobal::get_wrapper_hover_css( $attributes ), |
| @@ -46,30 +55,92 @@ | ||
| 46 | 55 | $this->add_class_styles( |
| 47 | 56 | '{{WRAPPER}} > .ablocks-block-container', |
| 48 | 57 | BlockGlobal::get_container_css( $attributes ), |
| 49 | 58 | BlockGlobal::get_container_css( $attributes, 'Tablet' ), |
| 50 | - BlockGlobal::get_container_css( $attributes, 'Mobile' ) | |
| 59 | + BlockGlobal::get_container_css( $attributes, 'Mobile' ), | |
| 60 | + $this->custom_device_map( function ( $device ) use ( $attributes ) { | |
| 61 | + return BlockGlobal::get_container_css( $attributes, $device ); | |
| 62 | + } ) | |
| 51 | 63 | ); |
| 52 | 64 | }//end if |
| 53 | 65 | } |
| 54 | 66 | |
| 55 | - public function add_class_styles( $class_name, $desktop_styles, $tablet_styles = [], $mobile_styles = [] ) { | |
| 67 | + public function add_class_styles( $class_name, $desktop_styles, $tablet_styles = [], $mobile_styles = [], $responsive_styles = [] ) { | |
| 56 | 68 | $this->class_styles[] = [ |
| 57 | 69 | 'class_name' => $class_name, |
| 58 | 70 | 'desktop_styles' => $desktop_styles, |
| 59 | 71 | 'tablet_styles' => $tablet_styles, |
| 60 | - 'mobile_styles' => $mobile_styles | |
| 72 | + 'mobile_styles' => $mobile_styles, | |
| 73 | + 'responsive_styles' => $responsive_styles, | |
| 61 | 74 | ]; |
| 62 | 75 | } |
| 63 | 76 | |
| 77 | + /** | |
| 78 | + * Custom-breakpoint style map from a per-device builder (excludes the | |
| 79 | + * built-in Tablet/Mobile so the standard path is untouched). | |
| 80 | + */ | |
| 81 | + public function custom_device_map( $builder ) { | |
| 82 | + $map = []; | |
| 83 | + | |
| 84 | + // Nothing to build when the site registers no custom breakpoints — and | |
| 85 | + // running the phantom probe anyway is not free: it hands every control a | |
| 86 | + // device suffix that has no stored keys, which is what made controls that | |
| 87 | + // read `$value[ 'x' . $device ]` unguarded spray "Undefined array key | |
| 88 | + // …___ablocksphantom___" notices on every page under WP_DEBUG. | |
| 89 | + $custom_devices = []; | |
| 90 | + foreach ( \ABlocks\Helper::get_responsive_devices() as $d ) { | |
| 91 | + if ( $d['width'] <= 0 || 'Tablet' === $d['id'] || 'Mobile' === $d['id'] ) { | |
| 92 | + continue; | |
| 93 | + } | |
| 94 | + $custom_devices[] = $d; | |
| 95 | + } | |
| 96 | + if ( empty( $custom_devices ) ) { | |
| 97 | + return $map; | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Subtract a "phantom" baseline (builder run against a value-less suffix) | |
| 101 | + // so controls that only default Tablet/Mobile don't leak phantom | |
| 102 | + // declarations into custom breakpoints. Leaves only real overrides. | |
| 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 | + | |
| 64 | 127 | public function generate_css() { |
| 65 | 128 | $css_output = ''; |
| 66 | 129 | |
| 67 | 130 | foreach ( $this->class_styles as $class_style ) { |
| 68 | - $desktop_css = AssetsGenerator::minify_css( $this->generate_css_for_media_query( 'desktop', $class_style['desktop_styles'] ) ); | |
| 69 | - $tablet_css = AssetsGenerator::minify_css( $this->generate_css_for_media_query( 'tablet', $class_style['tablet_styles'] ) ); | |
| 70 | - $mobile_css = AssetsGenerator::minify_css( $this->generate_css_for_media_query( 'mobile', $class_style['mobile_styles'] ) ); | |
| 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'] ); | |
| 71 | 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 | + | |
| 72 | 143 | $parent_selector = $this->get_parent_selector( $class_style['class_name'] ); |
| 73 | 144 | $css_blocks = []; |
| 74 | 145 | |
| 75 | 146 | $addToCssBlocks = function ( $mediaQuery, $max_width, $css ) use ( &$css_blocks, $parent_selector ) { |
| @@ -77,14 +148,55 @@ | ||
| 77 | 148 | $css_blocks[] = ( '' !== $mediaQuery ) ? "@media screen and (max-width: $max_width) {\n$parent_selector {\n$css\n}\n}" : "$parent_selector {\n$css\n}"; |
| 78 | 149 | } |
| 79 | 150 | }; |
| 80 | 151 | |
| 152 | + // Always add desktop CSS | |
| 81 | 153 | $addToCssBlocks( '', '', $desktop_css ); |
| 82 | - $addToCssBlocks( 'tablet', $this->get_breakpoint( 'tablet' ), $tablet_css ); | |
| 83 | - $addToCssBlocks( 'mobile', $this->get_breakpoint( 'mobile' ), $mobile_css ); | |
| 84 | 154 | |
| 155 | + if ( empty( $class_style['responsive_styles'] ) ) { | |
| 156 | + // Standard path (no custom breakpoints) — unchanged output. | |
| 157 | + if ( $tablet_raw !== $desktop_raw ) { | |
| 158 | + $addToCssBlocks( 'tablet', $this->get_breakpoint( 'tablet' ), $tablet_css ); | |
| 159 | + } | |
| 160 | + if ( $mobile_raw !== $tablet_raw ) { | |
| 161 | + $addToCssBlocks( 'mobile', $this->get_breakpoint( 'mobile' ), $mobile_css ); | |
| 162 | + } | |
| 163 | + } else { | |
| 164 | + // Custom breakpoints present: emit every breakpoint width-descending | |
| 165 | + // (narrowest last so it wins), each deduped against desktop. | |
| 166 | + $bp_defaults = \ABlocks\Helper::get_breakpoints(); | |
| 167 | + $bp_list = [ | |
| 168 | + [ 'width' => $bp_defaults['tablet'], 'min' => 0, 'max' => $bp_defaults['tablet'], 'styles' => $class_style['tablet_styles'] ], | |
| 169 | + [ 'width' => $bp_defaults['mobile'], 'min' => 0, 'max' => $bp_defaults['mobile'], 'styles' => $class_style['mobile_styles'] ], | |
| 170 | + ]; | |
| 171 | + foreach ( $class_style['responsive_styles'] as $bp ) { | |
| 172 | + $bp_list[] = [ | |
| 173 | + 'width' => (int) $bp['width'], | |
| 174 | + 'min' => isset( $bp['min'] ) ? (int) $bp['min'] : 0, | |
| 175 | + 'max' => isset( $bp['max'] ) ? (int) $bp['max'] : (int) $bp['width'], | |
| 176 | + 'styles' => $bp['styles'], | |
| 177 | + ]; | |
| 178 | + } | |
| 179 | + usort( $bp_list, function ( $a, $b ) { | |
| 180 | + return (int) $b['width'] - (int) $a['width']; | |
| 181 | + } ); | |
| 182 | + foreach ( $bp_list as $bp ) { | |
| 183 | + $bp_styles = $this->filter_responsive_styles( $desktop_styles, $bp['styles'] ); | |
| 184 | + $bp_raw = $this->generate_css_for_media_query( 'bp', $bp_styles ); | |
| 185 | + if ( $bp_raw === $desktop_raw || '' === trim( $bp_raw ) ) { | |
| 186 | + continue; | |
| 187 | + } | |
| 188 | + $bp_css = AssetsGenerator::minify_css( $bp_raw ); | |
| 189 | + $media = \ABlocks\Helper::breakpoint_media_condition( $bp['min'], $bp['max'] ); | |
| 190 | + if ( '' === $bp_css || '' === $media ) { | |
| 191 | + continue; | |
| 192 | + } | |
| 193 | + $css_blocks[] = "@media screen and $media {\n$parent_selector {\n$bp_css\n}\n}"; | |
| 194 | + } | |
| 195 | + } | |
| 196 | + | |
| 85 | 197 | $css_output .= implode( "\n\n", $css_blocks ) . "\n\n"; |
| 86 | - } | |
| 198 | + }//end foreach | |
| 87 | 199 | |
| 88 | 200 | $css_output .= $this->get_custom_css(); |
| 89 | 201 | |
| 90 | 202 | return preg_replace( '/\s+/', ' ', $css_output ); |
| @@ -97,8 +209,12 @@ | ||
| 97 | 209 | return ''; |
| 98 | 210 | } |
| 99 | 211 | $css_string = implode("\n", array_map( |
| 100 | 212 | function ( $property, $value ) { |
| 213 | + // See Helper::esc_css_value(): these values come from block | |
| 214 | + // attributes and land inside an inline <style> element. | |
| 215 | + $property = \ABlocks\Helper::esc_css_value( $property ); | |
| 216 | + $value = \ABlocks\Helper::esc_css_value( $value ); | |
| 101 | 217 | return "$property: $value;"; |
| 102 | 218 | }, |
| 103 | 219 | array_keys( $styles ), |
| 104 | 220 | $styles |
| @@ -107,13 +223,14 @@ | ||
| 107 | 223 | return $css_string . "\n"; |
| 108 | 224 | } |
| 109 | 225 | |
| 110 | 226 | public function get_breakpoint( $media_query ) { |
| 227 | + $bp = \ABlocks\Helper::get_breakpoints(); | |
| 111 | 228 | switch ( $media_query ) { |
| 112 | 229 | case 'tablet': |
| 113 | - return '800px'; | |
| 230 | + return $bp['tablet'] . 'px'; | |
| 114 | 231 | case 'mobile': |
| 115 | - return '480px'; | |
| 232 | + return $bp['mobile'] . 'px'; | |
| 116 | 233 | default: |
| 117 | 234 | return '1200px'; |
| 118 | 235 | } |
| 119 | 236 | } |
| @@ -119,7 +236,63 @@ | ||
| 119 | 236 | } |
| 120 | 237 | |
| 121 | 238 | public function get_parent_selector( $class_name ) { |
| 122 | 239 | return $this->parent_class ? str_replace( '{{WRAPPER}}', $this->parent_class, $class_name ) : $class_name; |
| 240 | + } | |
| 241 | + | |
| 242 | + private function remove_empty_css( $base_styles ) { | |
| 243 | + if ( empty( $base_styles ) || ! is_array( $base_styles ) || ( is_array( $base_styles ) && ! count( $base_styles ) ) ) { | |
| 244 | + return []; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $styles = []; | |
| 248 | + | |
| 249 | + foreach ( $base_styles as $prop => $value ) { | |
| 250 | + if ( 'font-family' === $prop ) { | |
| 251 | + // Safety net for styles that bypass the Typography control and set a | |
| 252 | + // bare family name directly. FontStack leaves finished values alone. | |
| 253 | + $value = FontStack::build( $value ); | |
| 254 | + } | |
| 255 | + // Trim value to avoid whitespace-only entries | |
| 256 | + $trimmed = trim( $value ); | |
| 257 | + // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number | |
| 258 | + if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%|s|ms|deg|fr|ch|ex)$/i', $trimmed ) ) { | |
| 259 | + continue; | |
| 260 | + } | |
| 261 | + | |
| 262 | + $styles[ $prop ] = $trimmed; | |
| 263 | + } | |
| 264 | + | |
| 265 | + return $styles; | |
| 266 | + } | |
| 267 | + | |
| 268 | + private function filter_responsive_styles( $base_styles, $responsive_styles ) { | |
| 269 | + if ( empty( $responsive_styles ) || ! is_array( $responsive_styles ) ) { | |
| 270 | + return []; | |
| 271 | + } | |
| 272 | + | |
| 273 | + $difference = []; | |
| 274 | + | |
| 275 | + foreach ( $responsive_styles as $prop => $value ) { | |
| 276 | + $trimmed = trim( $value ); | |
| 277 | + | |
| 278 | + // // Remove if empty, or if it matches only a unit (like 'px', '%', 'em') without any number | |
| 279 | + if ( $trimmed === '' || preg_match( '/^(px|em|rem|vh|vw|vmin|vmax|cm|mm|in|pt|pc|%|s|ms|deg|fr|ch|ex)$/i', $trimmed ) ) { | |
| 280 | + continue; | |
| 281 | + } | |
| 282 | + | |
| 283 | + // Only include if the value is different from base styles | |
| 284 | + if ( ! isset( $base_styles[ $prop ] ) || $base_styles[ $prop ] !== $trimmed ) { | |
| 285 | + $difference[ $prop ] = $trimmed; | |
| 286 | + } | |
| 287 | + } | |
| 288 | + | |
| 289 | + return $difference; | |
| 290 | + } | |
| 291 | + | |
| 292 | + | |
| 293 | + public function get_font_family_css( string $font_name, string $category = '' ): string { | |
| 294 | + // Kept for back-compat; the stack is built centrally now. | |
| 295 | + return FontStack::build( $font_name ); | |
| 123 | 296 | } |
| 124 | 297 | } |
| 125 | 298 | |