PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.4
Elementor Website Builder – more than just a page builder v3.27.4
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/atomic-widget-styles.php +31 -96 4.2.03.27.4 View file →
@@ -1,121 +1,56 @@
1 1 <?php
2 2
3 3 namespace Elementor\Modules\AtomicWidgets\Styles;
4 4
5 -use Elementor\Core\Base\Document;
6 -use Elementor\Modules\AtomicWidgets\Utils\Utils;
7 -use Elementor\Modules\GlobalClasses\Utils\Atomic_Elements_Utils;
8 -use Elementor\Utils as ElementorUtils;
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 9 use Elementor\Plugin;
10 10
11 11 class Atomic_Widget_Styles {
12 - const STYLES_KEY = 'local';
13 - const CONTEXT_FRONTEND = 'frontend';
14 - const CONTEXT_PREVIEW = 'preview';
15 -
16 12 public function register_hooks() {
17 - add_action( 'elementor/atomic-widgets/styles/register', function( Atomic_Styles_Manager $styles_manager, array $post_ids ) {
18 - $this->register_styles( $styles_manager, $post_ids );
19 - }, 30, 2 );
20 -
21 - add_action( 'elementor/document/after_save', fn( Document $document ) => $this->invalidate_cache(
22 - [ $document->get_main_post()->ID ],
23 - $this->get_context( ! Utils::is_post_published( $document ) )
24 - ), 20, 2 );
25 -
26 - add_action(
27 - 'elementor/core/files/clear_cache',
28 - fn() => $this->invalidate_cache(),
29 - );
30 -
31 - add_action(
32 - 'deleted_post',
33 - fn( $post_id ) => $this->invalidate_cache( [ $post_id ] )
34 - );
35 -
36 - add_action( 'update_option__elementor_pro_license_v2_data', fn() => Plugin::$instance->files_manager->clear_cache() );
37 - add_action( 'delete_option__elementor_pro_license_v2_data', fn() => Plugin::$instance->files_manager->clear_cache() );
13 + add_action( 'elementor/element/parse_css', fn( Post $post, Element_Base $element ) => $this->parse_element_style( $post, $element ), 10, 2 );
38 14 }
39 15
40 - private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) {
41 - $context = $this->get_context( Plugin::$instance->preview->is_editor_or_preview() );
42 -
43 - foreach ( $post_ids as $post_id ) {
44 - $get_styles = fn() => $this->parse_post_styles( $post_id );
45 -
46 - $styles_manager->register( [ self::STYLES_KEY, $post_id, $context ], $get_styles );
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;
47 20 }
48 - }
49 21
50 - private function parse_post_styles( $post_id ) {
51 - $post_styles = [];
22 + $styles = $element->get_raw_data()['styles'];
52 23
53 - Utils::traverse_post_elements( $post_id, function( $element_data ) use ( &$post_styles ) {
54 - $post_styles = array_merge( $post_styles, $this->parse_element_style( $element_data ) );
55 - } );
56 -
57 - return self::get_license_based_filtered_styles( $post_styles );
58 - }
59 -
60 - private function parse_element_style( array $element_data ) {
61 - $element_type = Atomic_Elements_Utils::get_element_type( $element_data );
62 - $element_instance = Atomic_Elements_Utils::get_element_instance( $element_type );
63 -
64 - if ( ! Utils::is_atomic( $element_instance ) ) {
65 - return [];
66 - }
67 -
68 - return $element_data['styles'] ?? [];
69 - }
70 -
71 - private function invalidate_cache( ?array $post_ids = null, ?string $context = null ) {
72 - if ( empty( $post_ids ) ) {
73 - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] );
74 -
24 + if ( empty( $styles ) ) {
75 25 return;
76 26 }
77 27
78 - $is_post_status_publish = self::CONTEXT_FRONTEND === $context;
28 + $this->styles_enqueue_fonts( $styles );
79 29
80 - // When a user publishes a post, we should invalidate the styles of the draft too
81 - foreach ( $post_ids as $post_id ) {
82 - do_action( 'elementor/atomic-widgets/styles/clear',
83 - empty( $context ) || $is_post_status_publish
84 - ? [ self::STYLES_KEY, $post_id ]
85 - : [ self::STYLES_KEY, $post_id, $context ]
86 - );
87 - }
88 - }
30 + $css = Styles_Renderer::make( [
31 + 'breakpoints' => Plugin::$instance->breakpoints->get_breakpoints_config(),
32 + ] )->render( $styles );
89 33
90 - private function get_context( bool $is_preview ) {
91 - return $is_preview ? self::CONTEXT_PREVIEW : self::CONTEXT_FRONTEND;
34 + $post->get_stylesheet()->add_raw_css( $css );
92 35 }
93 36
94 - public static function get_license_based_filtered_styles( $styles ) {
95 - if ( ElementorUtils::has_pro() && version_compare( ELEMENTOR_PRO_VERSION, '3.35', '<' ) ) {
96 - return $styles;
97 - }
98 -
99 - return apply_filters(
100 - 'elementor/atomic_widgets/editor_data/element_styles',
101 - self::remove_custom_css_from_styles( $styles ),
102 - $styles
103 - );
104 - }
105 -
106 - public static function remove_custom_css_from_styles( array $styles ) {
107 - if ( empty( $styles ) ) {
108 - return $styles;
109 - }
110 -
111 - foreach ( $styles as $style_id => $style ) {
112 - if ( isset( $style['variants'] ) && is_array( $style['variants'] ) ) {
113 - foreach ( $style['variants'] as $variant_index => $variant ) {
114 - unset( $styles[ $style_id ]['variants'][ $variant_index ]['custom_css'] );
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'] );
115 52 }
116 53 }
117 54 }
118 -
119 - return $styles;
120 55 }
121 56 }