| 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_Widget_Base; |
| 8 |
use Elementor\Plugin; |
| 9 |
|
| 10 |
class Atomic_Styles { |
| 11 |
/** |
| 12 |
* @var array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> $breakpoints |
| 13 |
*/ |
| 14 |
private array $breakpoints = []; |
| 15 |
|
| 16 |
private Styles_Renderer $styles_renderer; |
| 17 |
|
| 18 |
public function register_hooks() { |
| 19 |
add_action( 'elementor/element/parse_css', fn( Post $post, Element_Base $element ) => $this->parse_element_style( $post, $element ), 10, 2 ); |
| 20 |
} |
| 21 |
|
| 22 |
private function get_breakpoints(): array { |
| 23 |
if ( count( $this->breakpoints ) === 0 ) { |
| 24 |
$this->breakpoints = $this->build_breakpoints(); |
| 25 |
} |
| 26 |
|
| 27 |
return $this->breakpoints; |
| 28 |
} |
| 29 |
|
| 30 |
private function build_breakpoints(): array { |
| 31 |
return Plugin::$instance->breakpoints->get_breakpoints_config(); |
| 32 |
} |
| 33 |
|
| 34 |
private function get_styles_renderer(): Styles_Renderer { |
| 35 |
if ( ! isset( $this->styles_renderer ) ) { |
| 36 |
$this->styles_renderer = $this->build_styles_renderer(); |
| 37 |
} |
| 38 |
|
| 39 |
return $this->styles_renderer; |
| 40 |
} |
| 41 |
|
| 42 |
private function build_styles_renderer(): Styles_Renderer { |
| 43 |
$breakpoints = $this->get_breakpoints(); |
| 44 |
|
| 45 |
return new Styles_Renderer( [ |
| 46 |
'breakpoints' => $breakpoints, |
| 47 |
] ); |
| 48 |
} |
| 49 |
|
| 50 |
private function parse_element_style( Post $post, Element_Base $element ) { |
| 51 |
if ( ! ( $element instanceof Atomic_Widget_Base ) || Post::class !== get_class( $post ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$styles = $element->get_raw_data()['styles']; |
| 56 |
|
| 57 |
if ( empty( $styles ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$this->styles_enqueue_fonts( $styles ); |
| 62 |
|
| 63 |
$post->get_stylesheet()->add_raw_css( $this->convert_styles_to_css( $styles ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param array<int, array{ |
| 68 |
* id: string, |
| 69 |
* type: string, |
| 70 |
* variants: array<int, array{ |
| 71 |
* props: array<string, mixed>, |
| 72 |
* meta: array<string, mixed> |
| 73 |
* }> |
| 74 |
* }> $styles |
| 75 |
*/ |
| 76 |
private function styles_enqueue_fonts( array $styles ): void { |
| 77 |
foreach ( $styles as $style ) { |
| 78 |
foreach ( $style['variants'] as $variant ) { |
| 79 |
if ( isset( $variant['props']['font-family'] ) ) { |
| 80 |
Plugin::$instance->frontend->enqueue_font( $variant['props']['font-family'] ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private function convert_styles_to_css( array $styles ): string { |
| 87 |
$styles_renderer = $this->get_styles_renderer(); |
| 88 |
|
| 89 |
return $styles_renderer->render( $styles ); |
| 90 |
} |
| 91 |
} |
| 92 |
|