| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 |
|
| 5 |
use Elementor\Core\Files\CSS\Post; |
| 6 |
use Elementor\Element_Base; |
| 7 |
use Elementor\Modules\AtomicWidgets\Base\Atomic_Element_Base; |
| 8 |
use Elementor\Modules\AtomicWidgets\Base\Atomic_Widget_Base; |
| 9 |
use Elementor\Plugin; |
| 10 |
|
| 11 |
class Atomic_Widget_Styles { |
| 12 |
public function register_hooks() { |
| 13 |
add_action( 'elementor/element/parse_css', fn( Post $post, Element_Base $element ) => $this->parse_element_style( $post, $element ), 10, 2 ); |
| 14 |
} |
| 15 |
|
| 16 |
private function parse_element_style( Post $post, Element_Base $element ) { |
| 17 |
if ( ! ( $element instanceof Atomic_Widget_Base || $element instanceof Atomic_Element_Base ) |
| 18 |
|| Post::class !== get_class( $post ) ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$styles = $element->get_raw_data()['styles']; |
| 23 |
|
| 24 |
if ( empty( $styles ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$this->styles_enqueue_fonts( $styles ); |
| 29 |
|
| 30 |
$css = Styles_Renderer::make( [ |
| 31 |
'breakpoints' => Plugin::$instance->breakpoints->get_breakpoints_config(), |
| 32 |
] )->render( $styles ); |
| 33 |
|
| 34 |
$post->get_stylesheet()->add_raw_css( $css ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param array<int, array{ |
| 39 |
* id: string, |
| 40 |
* type: string, |
| 41 |
* variants: array<int, array{ |
| 42 |
* props: array<string, mixed>, |
| 43 |
* meta: array<string, mixed> |
| 44 |
* }> |
| 45 |
* }> $styles |
| 46 |
*/ |
| 47 |
private function styles_enqueue_fonts( array $styles ): void { |
| 48 |
foreach ( $styles as $style ) { |
| 49 |
foreach ( $style['variants'] as $variant ) { |
| 50 |
if ( isset( $variant['props']['font-family'] ) ) { |
| 51 |
Plugin::$instance->frontend->enqueue_font( $variant['props']['font-family'] ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|