| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers\V3\Serializer; |
| 4 |
|
| 5 |
use Elementor\Modules\Mcp\Abilities\Appliers\V3\Mapper\Responsive_Key_Resolver; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Renders a {@see V3_Block_Accumulator} back into a CSS string that |
| 13 |
* {@see \Elementor\Modules\Mcp\Abilities\Appliers\V3\V3_Style_Mapper} can consume. |
| 14 |
* |
| 15 |
* Layout: base declarations, then `&:hover|focus|active { ... }`, then |
| 16 |
* `@media(--breakpoint) { ... }` blocks with the same nesting inside. |
| 17 |
*/ |
| 18 |
class Block_Renderer { |
| 19 |
|
| 20 |
public function render( V3_Block_Accumulator $blocks ): string { |
| 21 |
$grouped = $blocks->all(); |
| 22 |
$parts = []; |
| 23 |
|
| 24 |
$default = $grouped[ Responsive_Key_Resolver::BASE_BREAKPOINT ] ?? []; |
| 25 |
unset( $grouped[ Responsive_Key_Resolver::BASE_BREAKPOINT ] ); |
| 26 |
|
| 27 |
$rendered_default = $this->render_state_group( $default ); |
| 28 |
if ( '' !== $rendered_default ) { |
| 29 |
$parts[] = $rendered_default; |
| 30 |
} |
| 31 |
|
| 32 |
foreach ( $grouped as $breakpoint => $state_group ) { |
| 33 |
$rendered = $this->render_state_group( $state_group ); |
| 34 |
if ( '' === $rendered ) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
$parts[] = sprintf( '@media(--%s) { %s }', $breakpoint, $rendered ); |
| 38 |
} |
| 39 |
|
| 40 |
return implode( ' ', $parts ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @param array<string, array<string, string>> $state_group |
| 45 |
*/ |
| 46 |
private function render_state_group( array $state_group ): string { |
| 47 |
$parts = []; |
| 48 |
|
| 49 |
$base = $state_group[''] ?? []; |
| 50 |
unset( $state_group[''] ); |
| 51 |
if ( ! empty( $base ) ) { |
| 52 |
$parts[] = $this->render_declarations( $base ); |
| 53 |
} |
| 54 |
|
| 55 |
foreach ( $state_group as $state => $declarations ) { |
| 56 |
if ( empty( $declarations ) ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
$parts[] = sprintf( '&:%s { %s }', $state, $this->render_declarations( $declarations ) ); |
| 60 |
} |
| 61 |
|
| 62 |
return implode( ' ', $parts ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param array<string, string> $declarations |
| 67 |
*/ |
| 68 |
private function render_declarations( array $declarations ): string { |
| 69 |
$parts = []; |
| 70 |
foreach ( $declarations as $property => $value ) { |
| 71 |
$parts[] = sprintf( '%s: %s;', $property, $value ); |
| 72 |
} |
| 73 |
|
| 74 |
return implode( ' ', $parts ); |
| 75 |
} |
| 76 |
} |
| 77 |
|