← All changes
|
modules/atomic-widgets/styles/styles-renderer.php
+39
-8
4.1.3
→
4.3.0-beta3
View file →
| @@ -3,11 +3,14 @@ | ||
| 3 | 3 | namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Utils\Collection; |
| 6 | 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; | |
| 7 | 11 | use Elementor\Plugin; |
| 8 | 12 | use Elementor\Utils; |
| 9 | -use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver; | |
| 10 | 13 | |
| 11 | 14 | class Styles_Renderer { |
| 12 | 15 | const DEFAULT_SELECTOR_PREFIX = '.elementor'; |
| 13 | 16 | |
| @@ -15,9 +18,9 @@ | ||
| 15 | 18 | * @var array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> |
| 16 | 19 | */ |
| 17 | 20 | private array $breakpoints; |
| 18 | 21 | |
| 19 | - private $on_prop_transform; | |
| 22 | + private $on_font_enqueue; | |
| 20 | 23 | |
| 21 | 24 | private string $selector_prefix; |
| 22 | 25 | |
| 23 | 26 | /** |
| @@ -61,10 +64,10 @@ | ||
| 61 | 64 | |
| 62 | 65 | return implode( '', $css_style ); |
| 63 | 66 | } |
| 64 | 67 | |
| 65 | - public function on_prop_transform( callable $callback ): self { | |
| 66 | - $this->on_prop_transform = $callback; | |
| 68 | + public function on_font_enqueue( callable $callback ): self { | |
| 69 | + $this->on_font_enqueue = $callback; | |
| 67 | 70 | |
| 68 | 71 | return $this; |
| 69 | 72 | } |
| 70 | 73 | |
| @@ -141,16 +144,44 @@ | ||
| 141 | 144 | $schema = Style_Schema::get(); |
| 142 | 145 | |
| 143 | 146 | return Collection::make( Render_Props_Resolver::for_styles()->resolve( $schema, $props ) ) |
| 144 | 147 | ->filter() |
| 145 | - ->map( function ( $value, $prop ) { | |
| 146 | - if ( $this->on_prop_transform ) { | |
| 147 | - call_user_func( $this->on_prop_transform, $prop, $value ); | |
| 148 | - } | |
| 148 | + ->map( function ( $value, $prop ) use ( $props, $schema ) { | |
| 149 | + $this->maybe_enqueue_font( $schema, $prop, $props[ $prop ] ?? null ); | |
| 149 | 150 | |
| 150 | 151 | return $prop . ':' . $value . ';'; |
| 151 | 152 | } ) |
| 152 | 153 | ->implode( '' ); |
| 154 | + } | |
| 155 | + | |
| 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; | |
| 153 | 184 | } |
| 154 | 185 | |
| 155 | 186 | private function custom_css_to_css_string( ?array $custom_css ): string { |
| 156 | 187 | return ! empty( $custom_css['raw'] ) |