PluginProbe
Elementor Website Builder – more than just a page builder / 3.26.0-dev5
Elementor Website Builder – more than just a page builder v3.26.0-dev5
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 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
← All changes | modules/atomic-widgets/styles/styles-renderer.php +22 -107 4.3.0-beta33.26.0-dev5 View file →
@@ -2,57 +2,39 @@
2 2
3 3 namespace Elementor\Modules\AtomicWidgets\Styles;
4 4
5 5 use Elementor\Core\Utils\Collection;
6 -use Elementor\Modules\AtomicWidgets\Module;
7 -use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Font_Enqueueable;
8 -use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
9 -use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
10 -use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver;
11 -use Elementor\Plugin;
12 -use Elementor\Utils;
6 +use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver;
13 7
14 8 class Styles_Renderer {
15 - const DEFAULT_SELECTOR_PREFIX = '.elementor';
16 -
17 9 /**
18 - * @var array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}>
10 + * @var array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> $breakpoints
19 11 */
20 12 private array $breakpoints;
21 13
22 - private $on_font_enqueue;
23 -
24 - private string $selector_prefix;
25 -
26 14 /**
27 - * @param array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> $breakpoints
28 - * @param string $selector_prefix
15 + * Styles_Renderer constructor.
16 + *
17 + * @param array{
18 + * breakpoints: array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}>
19 + * } $config
29 20 */
30 - private function __construct( array $breakpoints, string $selector_prefix = self::DEFAULT_SELECTOR_PREFIX ) {
31 - $this->breakpoints = $breakpoints;
32 - $this->selector_prefix = $selector_prefix;
21 + public function __construct( array $config ) {
22 + $this->breakpoints = $config['breakpoints'];
33 23 }
34 24
35 - public static function make( array $breakpoints, string $selector_prefix = self::DEFAULT_SELECTOR_PREFIX ): self {
36 - return new self( $breakpoints, $selector_prefix );
37 - }
38 -
39 25 /**
40 26 * Render the styles to a CSS string.
41 27 *
42 - * Styles format:
43 - * array<int, array{
28 + * @param array<int, array{
44 29 * id: string,
45 30 * type: string,
46 - * cssName: string | null,
47 31 * variants: array<int, array{
48 32 * props: array<string, mixed>,
49 33 * meta: array<string, mixed>
50 34 * }>
51 - * }>
35 + * }> $styles Array of style definitions.
52 36 *
53 - * @param array $styles Array of style definitions.
54 - *
55 37 * @return string Rendered CSS string.
56 38 */
57 39 public function render( array $styles ): string {
58 40 $css_style = [];
@@ -64,14 +46,8 @@
64 46
65 47 return implode( '', $css_style );
66 48 }
67 49
68 - public function on_font_enqueue( callable $callback ): self {
69 - $this->on_font_enqueue = $callback;
70 -
71 - return $this;
72 - }
73 -
74 50 private function style_definition_to_css_string( array $style ): string {
75 51 $base_selector = $this->get_base_selector( $style );
76 52
77 53 if ( ! $base_selector ) {
@@ -101,17 +77,9 @@
101 77 isset( $style_def['id'] ) &&
102 78 isset( $map[ $style_def['type'] ] ) &&
103 79 $style_def['id']
104 80 ) {
105 - $type = $map[ $style_def['type'] ];
106 - $name = $style_def['cssName'] ?? $style_def['id'];
107 -
108 - $selector_parts = array_filter( [
109 - $this->selector_prefix,
110 - "{$type}{$name}",
111 - ] );
112 -
113 - return implode( ' ', $selector_parts );
81 + return $map[ $style_def['type'] ] . $style_def['id'];
114 82 }
115 83
116 84 return null;
117 85 }
@@ -116,22 +84,18 @@
116 84 return null;
117 85 }
118 86
119 87 private function variant_to_css_string( string $base_selector, array $variant ): string {
120 - $css = $this->props_to_css_string( $variant['props'] ) ?? '';
121 - $custom_css = $this->custom_css_to_css_string( $variant['custom_css'] ?? null );
88 + $css = $this->props_to_css_string( $variant['props'] );
122 89
123 - if ( ! $css && ! $custom_css ) {
90 + if ( ! $css ) {
124 91 return '';
125 92 }
126 93
127 - if ( isset( $variant['meta']['state'] ) ) {
128 - $selector = Style_States::get_selector_with_state( $base_selector, $variant['meta']['state'] );
129 - } else {
130 - $selector = $base_selector;
131 - }
94 + $state = isset( $variant['meta']['state'] ) ? ':' . $variant['meta']['state'] : '';
95 + $selector = $base_selector . $state;
132 96
133 - $style_declaration = $selector . '{' . $css . $custom_css . '}';
97 + $style_declaration = $selector . '{' . $css . '}';
134 98
135 99 if ( isset( $variant['meta']['breakpoint'] ) ) {
136 100 $style_declaration = $this->wrap_with_media_query( $variant['meta']['breakpoint'], $style_declaration );
137 101 }
@@ -138,58 +102,19 @@
138 102
139 103 return $style_declaration;
140 104 }
141 105
142 -
143 106 private function props_to_css_string( array $props ): string {
144 107 $schema = Style_Schema::get();
145 108
146 - return Collection::make( Render_Props_Resolver::for_styles()->resolve( $schema, $props ) )
109 + return Collection::make( Props_Resolver::for_styles()->resolve( $schema, $props ) )
147 110 ->filter()
148 - ->map( function ( $value, $prop ) use ( $props, $schema ) {
149 - $this->maybe_enqueue_font( $schema, $prop, $props[ $prop ] ?? null );
150 -
111 + ->map( function ( $value, $prop ) {
151 112 return $prop . ':' . $value . ';';
152 113 } )
153 114 ->implode( '' );
154 115 }
155 116
156 - private function maybe_enqueue_font( array $schema, string $prop_key, $prop_value ): void {
157 - if ( ! $this->on_font_enqueue || ! is_array( $prop_value ) || empty( $prop_value['value'] ) ) {
158 - return;
159 - }
160 -
161 - $enqueueable = $this->resolve_font_enqueueable( $schema[ $prop_key ] ?? null, $prop_value );
162 -
163 - if ( ! $enqueueable ) {
164 - return;
165 - }
166 -
167 - $font = $enqueueable->get_enqueue_font_family( $prop_value['value'] );
168 -
169 - if ( $font ) {
170 - call_user_func( $this->on_font_enqueue, $font );
171 - }
172 - }
173 -
174 - private function resolve_font_enqueueable( ?Prop_Type $prop_type, array $prop_value ): ?Font_Enqueueable {
175 - if ( $prop_type instanceof Union_Prop_Type ) {
176 - $prop_type = $prop_type->get_prop_type( $prop_value['$$type'] ?? '' );
177 - }
178 -
179 - if ( $prop_type instanceof Font_Enqueueable ) {
180 - return $prop_type;
181 - }
182 -
183 - return null;
184 - }
185 -
186 - private function custom_css_to_css_string( ?array $custom_css ): string {
187 - return ! empty( $custom_css['raw'] )
188 - ? Utils::decode_string( $custom_css['raw'], '' ) . '\n'
189 - : '';
190 - }
191 -
192 117 private function wrap_with_media_query( string $breakpoint_id, string $css ): string {
193 118 if ( ! isset( $this->breakpoints[ $breakpoint_id ] ) ) {
194 119 return $css;
195 120 }
@@ -198,24 +123,14 @@
198 123 if ( isset( $breakpoint['is_enabled'] ) && ! $breakpoint['is_enabled'] ) {
199 124 return '';
200 125 }
201 126
202 - $query = $this->get_media_query( $this->breakpoints[ $breakpoint_id ] );
127 + $size = $this->get_breakpoint_size( $this->breakpoints[ $breakpoint_id ] );
203 128
204 - return $query ? $query . '{' . $css . '}' : $css;
129 + return $size ? '@media(' . $size . '){' . $css . '}' : $css;
205 130 }
206 131
207 - public static function get_media_query( $breakpoint ): ?string {
208 - if ( isset( $breakpoint['is_enabled'] ) && ! $breakpoint['is_enabled'] ) {
209 - return null;
210 - }
211 -
212 - $size = self::get_breakpoint_size( $breakpoint );
213 -
214 - return $size ? '@media(' . $size . ')' : null;
215 - }
216 -
217 - private static function get_breakpoint_size( array $breakpoint ): ?string {
132 + private function get_breakpoint_size( array $breakpoint ): ?string {
218 133 $bound = 'min' === $breakpoint['direction'] ? 'min-width' : 'max-width';
219 134 $width = $breakpoint['value'] . 'px';
220 135
221 136 return "{$bound}:{$width}";