← All changes
|
modules/mcp/abilities/utils/style-variants-merger.php
+43
-14
4.3.2
→
4.3.3
View file →
| @@ -74,10 +74,18 @@ | ||
| 74 | 74 | ]; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | public static function build_variants( array $breakpoint_blocks, Css_Converter $converter ): array { |
| 78 | - $variants = []; | |
| 78 | + return self::build_variants_with_fallback_css( $breakpoint_blocks, $converter )['variants']; | |
| 79 | + } | |
| 79 | 80 | |
| 81 | + /** | |
| 82 | + * @return array{variants: array[], fallback_css: array<int, string>} fallback_css holds, per variant index, the declarations the converter could not convert natively. | |
| 83 | + */ | |
| 84 | + public static function build_variants_with_fallback_css( array $breakpoint_blocks, Css_Converter $converter ): array { | |
| 85 | + $variants = []; | |
| 86 | + $fallback_css = []; | |
| 87 | + | |
| 80 | 88 | foreach ( $breakpoint_blocks as $entry ) { |
| 81 | 89 | $bp = $entry['breakpoint']; |
| 82 | 90 | $blocks = $entry['blocks']; |
| 83 | 91 | $base_block_css = ''; |
| @@ -98,23 +106,32 @@ | ||
| 98 | 106 | $base_custom_css_parts[] = '&' . $selector . ' { ' . trim( $css ) . ' }'; |
| 99 | 107 | continue; |
| 100 | 108 | } |
| 101 | 109 | |
| 102 | - $variant = self::make_variant( $bp, $state, $css, $converter ); | |
| 103 | - if ( null !== $variant ) { | |
| 104 | - $variants[] = $variant; | |
| 105 | - } | |
| 110 | + self::add_variant( $variants, $fallback_css, self::make_variant( $bp, $state, $css, $converter ) ); | |
| 106 | 111 | } |
| 107 | 112 | |
| 108 | - $base_variant = self::make_variant( $bp, null, $base_block_css, $converter, $base_custom_css_parts ); | |
| 109 | - if ( null !== $base_variant ) { | |
| 110 | - $variants[] = $base_variant; | |
| 111 | - } | |
| 113 | + self::add_variant( $variants, $fallback_css, self::make_variant( $bp, null, $base_block_css, $converter, $base_custom_css_parts ) ); | |
| 112 | 114 | } |
| 113 | 115 | |
| 114 | - return $variants; | |
| 116 | + return [ | |
| 117 | + 'variants' => $variants, | |
| 118 | + 'fallback_css' => $fallback_css, | |
| 119 | + ]; | |
| 115 | 120 | } |
| 116 | 121 | |
| 122 | + private static function add_variant( array &$variants, array &$fallback_css, array $built ): void { | |
| 123 | + if ( null === $built['variant'] ) { | |
| 124 | + return; | |
| 125 | + } | |
| 126 | + | |
| 127 | + if ( '' !== $built['fallback_css'] ) { | |
| 128 | + $fallback_css[ count( $variants ) ] = $built['fallback_css']; | |
| 129 | + } | |
| 130 | + | |
| 131 | + $variants[] = $built['variant']; | |
| 132 | + } | |
| 133 | + | |
| 117 | 134 | public static function apply_mode( array $existing, array $new_variants, string $mode, array $affected_breakpoints ): array { |
| 118 | 135 | if ( 'replace' === $mode ) { |
| 119 | 136 | $kept = array_values( |
| 120 | 137 | array_filter( $existing, fn( $v ) => ! in_array( $v['meta']['breakpoint'] ?? null, $affected_breakpoints, true ) ) |
| @@ -199,14 +216,18 @@ | ||
| 199 | 216 | ? [ 'raw' => base64_encode( $merged ) ] // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Storing merged custom_css as base64. |
| 200 | 217 | : null; |
| 201 | 218 | } |
| 202 | 219 | |
| 203 | - private static function make_variant( string $breakpoint, ?string $state, string $css, Css_Converter $converter, array $extra_custom_css_parts = [] ): ?array { | |
| 220 | + /** | |
| 221 | + * @return array{variant: array|null, fallback_css: string} | |
| 222 | + */ | |
| 223 | + private static function make_variant( string $breakpoint, ?string $state, string $css, Css_Converter $converter, array $extra_custom_css_parts = [] ): array { | |
| 204 | 224 | $null_props = self::extract_null_props( $css ); |
| 205 | 225 | $stripped_css = self::strip_null_declarations( $css ); |
| 206 | 226 | $result = $converter->convert( $stripped_css ); |
| 207 | 227 | $props = $result['props'] ?? []; |
| 208 | - $custom_str = $result['customCss'] ?? ''; | |
| 228 | + $fallback_css = $result['customCss'] ?? ''; | |
| 229 | + $custom_str = $fallback_css; | |
| 209 | 230 | |
| 210 | 231 | if ( ! empty( $extra_custom_css_parts ) ) { |
| 211 | 232 | $extra = implode( ' ', $extra_custom_css_parts ); |
| 212 | 233 | $custom_str = '' !== $custom_str ? $custom_str . ' ' . $extra : $extra; |
| @@ -212,12 +233,15 @@ | ||
| 212 | 233 | $custom_str = '' !== $custom_str ? $custom_str . ' ' . $extra : $extra; |
| 213 | 234 | } |
| 214 | 235 | |
| 215 | 236 | if ( empty( $props ) && '' === $custom_str && empty( $null_props ) ) { |
| 216 | - return null; | |
| 237 | + return [ | |
| 238 | + 'variant' => null, | |
| 239 | + 'fallback_css' => '', | |
| 240 | + ]; | |
| 217 | 241 | } |
| 218 | 242 | |
| 219 | - return [ | |
| 243 | + $variant = [ | |
| 220 | 244 | 'meta' => [ |
| 221 | 245 | 'breakpoint' => $breakpoint, |
| 222 | 246 | 'state' => $state, |
| 223 | 247 | ], |
| @@ -225,8 +249,13 @@ | ||
| 225 | 249 | 'custom_css' => '' !== $custom_str |
| 226 | 250 | ? [ 'raw' => base64_encode( $custom_str ) ] // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Global class custom_css.raw is stored as base64. |
| 227 | 251 | : null, |
| 228 | 252 | 'null_props' => $null_props, |
| 253 | + ]; | |
| 254 | + | |
| 255 | + return [ | |
| 256 | + 'variant' => $variant, | |
| 257 | + 'fallback_css' => trim( $fallback_css ), | |
| 229 | 258 | ]; |
| 230 | 259 | } |
| 231 | 260 | |
| 232 | 261 | private static function extract_null_props( string $css ): array { |