PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.2
Elementor Website Builder – more than just a page builder v4.3.2
4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 All 455 releases
elementor / modules / mcp / abilities / appliers / v3 / v3-style-serializer.php

v3-style-serializer.php in Elementor Website Builder – more than just a page builder 4.3.2, at modules/mcp/abilities/appliers/v3/v3-style-serializer.php

108 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Mcp\Abilities\Appliers\V3;
4
5 use Elementor\Modules\Mcp\Abilities\Appliers\V3\Serializer\V3_Block_Accumulator;
6 use Elementor\Modules\Mcp\Abilities\Appliers\V3\Serializer\V3_Serializer_Registry;
7 use Elementor\Modules\Mcp\Abilities\Appliers\V3\Serializer\V3_Serializer_Registry_Factory;
8 use Elementor\Modules\Mcp\Abilities\Appliers\V3\Serializer\Block_Renderer;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Serializes a V3 widget's flat style settings back into a CSS string that
16 * V3_Style_Mapper can consume. Reverse of V3_Style_Mapper.
17 *
18 * The output is grouped by breakpoint / pseudo-state to match what the write path
19 * expects: base declarations, then `&:hover|focus|active { ... }`, then
20 * `@media(--breakpoint) { ... }` blocks with the same nesting inside.
21 */
22 class V3_Style_Serializer {
23
24 private V3_Serializer_Registry $registry;
25 private Block_Renderer $renderer;
26
27 public function __construct( ?V3_Serializer_Registry $registry = null, ?Block_Renderer $renderer = null ) {
28 $this->registry = $registry ?? V3_Serializer_Registry_Factory::create();
29 $this->renderer = $renderer ?? new Block_Renderer();
30 }
31
32 public function serialize( array $settings, string $widget_type, array $widget_config ): string {
33 $overrides = V3_Widget_Bridge_Registry::get_style_overrides( $widget_type );
34 $controls = $widget_config['controls'] ?? [];
35 $generic = V3_Style_Settings_Index::build( is_array( $controls ) ? $controls : [], $overrides );
36
37 $blocks = new V3_Block_Accumulator();
38
39 foreach ( $overrides as $match_key => $entry ) {
40 [ $property, $state ] = $this->split_match_key( (string) $match_key );
41 $this->dispatch_entry( $blocks, $settings, $entry, $property, $state );
42 }
43
44 foreach ( $generic as $match_key => $entry ) {
45 [ $property, $state ] = $this->split_match_key( (string) $match_key );
46 $this->dispatch_entry( $blocks, $settings, $entry, $property, $state );
47 }
48
49 $mapped_css = $this->renderer->render( $blocks );
50 $custom_css = $this->unwrap_custom_css( $settings['custom_css'] ?? null );
51
52 if ( '' === $mapped_css ) {
53 return $custom_css;
54 }
55
56 if ( '' === $custom_css ) {
57 return $mapped_css;
58 }
59
60 return $mapped_css . ' ' . $custom_css;
61 }
62
63 /**
64 * @param mixed $custom_css
65 */
66 private function unwrap_custom_css( $custom_css ): string {
67 if ( ! is_string( $custom_css ) ) {
68 return '';
69 }
70
71 $custom_css = trim( $custom_css );
72 if ( '' === $custom_css ) {
73 return '';
74 }
75
76 if ( preg_match( '/^\s*selector\s*\{\s*([\s\S]*?)\s*\}\s*$/i', $custom_css, $matches ) ) {
77 return trim( $matches[1] );
78 }
79
80 return $custom_css;
81 }
82
83 private function dispatch_entry( V3_Block_Accumulator $blocks, array $settings, array $entry, string $property, ?string $state ): void {
84 foreach ( $this->registry->all() as $serializer ) {
85 if ( ! $serializer->is_supported( $entry, $property, $state ) ) {
86 continue;
87 }
88
89 $serializer->emit( $blocks, $settings, $entry, $property, $state );
90
91 return;
92 }
93 }
94
95 /**
96 * @return array{0: string, 1: string|null}
97 */
98 private function split_match_key( string $match_key ): array {
99 if ( false === strpos( $match_key, '@' ) ) {
100 return [ $match_key, null ];
101 }
102
103 [ $property, $state ] = explode( '@', $match_key, 2 );
104
105 return [ $property, '' === $state ? null : $state ];
106 }
107 }
108