← All changes
|
modules/atomic-widgets/styles/styles-renderer.php
+20
-76
4.2.0-beta1
→
3.28.0-dev2
View file →
| @@ -2,15 +2,9 @@ | ||
| 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 | 9 | const DEFAULT_SELECTOR_PREFIX = '.elementor'; |
| 16 | 10 | |
| @@ -18,15 +12,15 @@ | ||
| 18 | 12 | * @var array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> |
| 19 | 13 | */ |
| 20 | 14 | private array $breakpoints; |
| 21 | 15 | |
| 22 | - private $on_font_enqueue; | |
| 16 | + private $on_prop_transform; | |
| 23 | 17 | |
| 24 | 18 | private string $selector_prefix; |
| 25 | 19 | |
| 26 | 20 | /** |
| 27 | 21 | * @param array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> $breakpoints |
| 28 | - * @param string $selector_prefix | |
| 22 | + * @param string $selector_prefix | |
| 29 | 23 | */ |
| 30 | 24 | private function __construct( array $breakpoints, string $selector_prefix = self::DEFAULT_SELECTOR_PREFIX ) { |
| 31 | 25 | $this->breakpoints = $breakpoints; |
| 32 | 26 | $this->selector_prefix = $selector_prefix; |
| @@ -42,9 +36,8 @@ | ||
| 42 | 36 | * Styles format: |
| 43 | 37 | * array<int, array{ |
| 44 | 38 | * id: string, |
| 45 | 39 | * type: string, |
| 46 | - * cssName: string | null, | |
| 47 | 40 | * variants: array<int, array{ |
| 48 | 41 | * props: array<string, mixed>, |
| 49 | 42 | * meta: array<string, mixed> |
| 50 | 43 | * }> |
| @@ -64,10 +57,10 @@ | ||
| 64 | 57 | |
| 65 | 58 | return implode( '', $css_style ); |
| 66 | 59 | } |
| 67 | 60 | |
| 68 | - public function on_font_enqueue( callable $callback ): self { | |
| 69 | - $this->on_font_enqueue = $callback; | |
| 61 | + public function on_prop_transform( callable $callback ): self { | |
| 62 | + $this->on_prop_transform = $callback; | |
| 70 | 63 | |
| 71 | 64 | return $this; |
| 72 | 65 | } |
| 73 | 66 | |
| @@ -102,13 +95,13 @@ | ||
| 102 | 95 | isset( $map[ $style_def['type'] ] ) && |
| 103 | 96 | $style_def['id'] |
| 104 | 97 | ) { |
| 105 | 98 | $type = $map[ $style_def['type'] ]; |
| 106 | - $name = $style_def['cssName'] ?? $style_def['id']; | |
| 99 | + $id = $style_def['id']; | |
| 107 | 100 | |
| 108 | 101 | $selector_parts = array_filter( [ |
| 109 | 102 | $this->selector_prefix, |
| 110 | - "{$type}{$name}", | |
| 103 | + "{$type}{$id}", | |
| 111 | 104 | ] ); |
| 112 | 105 | |
| 113 | 106 | return implode( ' ', $selector_parts ); |
| 114 | 107 | } |
| @@ -116,22 +109,18 @@ | ||
| 116 | 109 | return null; |
| 117 | 110 | } |
| 118 | 111 | |
| 119 | 112 | 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 ); | |
| 113 | + $css = $this->props_to_css_string( $variant['props'] ); | |
| 122 | 114 | |
| 123 | - if ( ! $css && ! $custom_css ) { | |
| 115 | + if ( ! $css ) { | |
| 124 | 116 | return ''; |
| 125 | 117 | } |
| 126 | 118 | |
| 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 | - } | |
| 119 | + $state = isset( $variant['meta']['state'] ) ? ':' . $variant['meta']['state'] : ''; | |
| 120 | + $selector = $base_selector . $state; | |
| 132 | 121 | |
| 133 | - $style_declaration = $selector . '{' . $css . $custom_css . '}'; | |
| 122 | + $style_declaration = $selector . '{' . $css . '}'; | |
| 134 | 123 | |
| 135 | 124 | if ( isset( $variant['meta']['breakpoint'] ) ) { |
| 136 | 125 | $style_declaration = $this->wrap_with_media_query( $variant['meta']['breakpoint'], $style_declaration ); |
| 137 | 126 | } |
| @@ -138,16 +127,17 @@ | ||
| 138 | 127 | |
| 139 | 128 | return $style_declaration; |
| 140 | 129 | } |
| 141 | 130 | |
| 142 | - | |
| 143 | 131 | private function props_to_css_string( array $props ): string { |
| 144 | 132 | $schema = Style_Schema::get(); |
| 145 | 133 | |
| 146 | - return Collection::make( Render_Props_Resolver::for_styles()->resolve( $schema, $props ) ) | |
| 134 | + return Collection::make( Props_Resolver::for_styles()->resolve( $schema, $props ) ) | |
| 147 | 135 | ->filter() |
| 148 | - ->map( function ( $value, $prop ) use ( $props, $schema ) { | |
| 149 | - $this->maybe_enqueue_font( $schema, $prop, $props[ $prop ] ?? null ); | |
| 136 | + ->map( function ( $value, $prop ) { | |
| 137 | + if ( $this->on_prop_transform ) { | |
| 138 | + call_user_func( $this->on_prop_transform, $prop, $value ); | |
| 139 | + } | |
| 150 | 140 | |
| 151 | 141 | return $prop . ':' . $value . ';'; |
| 152 | 142 | } ) |
| 153 | 143 | ->implode( '' ); |
| @@ -152,44 +142,8 @@ | ||
| 152 | 142 | } ) |
| 153 | 143 | ->implode( '' ); |
| 154 | 144 | } |
| 155 | 145 | |
| 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 | 146 | private function wrap_with_media_query( string $breakpoint_id, string $css ): string { |
| 193 | 147 | if ( ! isset( $this->breakpoints[ $breakpoint_id ] ) ) { |
| 194 | 148 | return $css; |
| 195 | 149 | } |
| @@ -198,24 +152,14 @@ | ||
| 198 | 152 | if ( isset( $breakpoint['is_enabled'] ) && ! $breakpoint['is_enabled'] ) { |
| 199 | 153 | return ''; |
| 200 | 154 | } |
| 201 | 155 | |
| 202 | - $query = $this->get_media_query( $this->breakpoints[ $breakpoint_id ] ); | |
| 156 | + $size = $this->get_breakpoint_size( $this->breakpoints[ $breakpoint_id ] ); | |
| 203 | 157 | |
| 204 | - return $query ? $query . '{' . $css . '}' : $css; | |
| 158 | + return $size ? '@media(' . $size . '){' . $css . '}' : $css; | |
| 205 | 159 | } |
| 206 | 160 | |
| 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 { | |
| 161 | + private function get_breakpoint_size( array $breakpoint ): ?string { | |
| 218 | 162 | $bound = 'min' === $breakpoint['direction'] ? 'min-width' : 'max-width'; |
| 219 | 163 | $width = $breakpoint['value'] . 'px'; |
| 220 | 164 | |
| 221 | 165 | return "{$bound}:{$width}"; |